2017-05-16 00:26:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-05-30 13:51:45 +02:00
|
|
|
import irc3
|
2017-08-22 15:57:57 +02:00
|
|
|
from docopt import Dict
|
2017-05-16 08:54:47 +02:00
|
|
|
from irc3.plugins.command import command
|
2017-05-16 12:06:29 +02:00
|
|
|
from irc3.utils import IrcString
|
2017-05-16 00:26:10 +02:00
|
|
|
|
|
|
|
from . import DatabasePlugin
|
2017-08-22 17:43:48 +02:00
|
|
|
from .utils import parse_int
|
2017-05-16 00:26:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class McManiac(DatabasePlugin):
|
|
|
|
@command(options_first=True)
|
2017-08-22 15:57:57 +02:00
|
|
|
def mcmaniac(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-28 12:46:35 +02:00
|
|
|
"""Get a random McManiaC or by index.
|
|
|
|
|
2017-05-16 00:26:10 +02:00
|
|
|
%%mcmaniac [<index>]
|
|
|
|
"""
|
2017-05-28 12:46:35 +02:00
|
|
|
index = args.get('<index>')
|
2017-06-30 15:03:01 +02:00
|
|
|
|
2017-05-28 12:46:35 +02:00
|
|
|
if index:
|
|
|
|
index = parse_int(index)
|
|
|
|
if not index:
|
|
|
|
return
|
2017-07-07 02:11:20 +02:00
|
|
|
index, order = index
|
2017-05-28 12:46:35 +02:00
|
|
|
order = 'id {order}'.format(order=order)
|
2017-08-22 20:05:32 +02:00
|
|
|
offset = 'OFFSET %s'
|
2017-05-16 07:45:10 +02:00
|
|
|
else:
|
2017-05-28 12:46:35 +02:00
|
|
|
order = 'random()'
|
2017-06-30 15:03:01 +02:00
|
|
|
offset = ''
|
|
|
|
|
|
|
|
# Fetch result from database
|
|
|
|
self.cur.execute('''
|
2017-08-22 20:05:32 +02:00
|
|
|
SELECT
|
2017-06-30 15:03:01 +02:00
|
|
|
item,
|
2017-08-22 20:05:32 +02:00
|
|
|
rank() OVER (ORDER BY id),
|
|
|
|
count(*) OVER (ROWS BETWEEN UNBOUNDED PRECEDING
|
|
|
|
AND UNBOUNDED FOLLOWING) AS total
|
|
|
|
FROM
|
2017-06-30 15:03:01 +02:00
|
|
|
mcmaniacs
|
2017-08-22 20:05:32 +02:00
|
|
|
ORDER BY
|
2017-06-30 15:03:01 +02:00
|
|
|
{order}
|
2017-08-22 20:05:32 +02:00
|
|
|
LIMIT
|
2017-06-30 15:03:01 +02:00
|
|
|
1
|
|
|
|
{offset}
|
|
|
|
'''.format(order=order, offset=offset), [index])
|
2017-05-28 12:46:35 +02:00
|
|
|
result = self.cur.fetchone()
|
2017-06-30 15:03:01 +02:00
|
|
|
|
2017-05-28 12:46:35 +02:00
|
|
|
if result:
|
2017-06-30 15:03:01 +02:00
|
|
|
return '[{rank}/{total}] {item}'.format(**result)
|
2017-05-28 12:46:35 +02:00
|
|
|
|
2017-07-06 14:40:48 +02:00
|
|
|
# TODO: fix regex ("McFooiaC McBariaC" adds "Mc\S+iaC")
|
2017-06-30 15:03:01 +02:00
|
|
|
@irc3.event(r'^:(?P<mask>\S+) PRIVMSG \S+ :.*(?P<item>Mc\S+iaC).*')
|
2017-05-30 16:13:20 +02:00
|
|
|
def save(self, mask: str, item: str):
|
|
|
|
if IrcString(mask).nick != self.bot.nick:
|
2017-08-22 20:05:32 +02:00
|
|
|
self.cur.execute('''
|
|
|
|
INSERT INTO
|
|
|
|
mcmaniacs (item)
|
|
|
|
VALUES
|
|
|
|
(%s)
|
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
''', [item])
|
|
|
|
self.con.commit()
|