2017-05-15 22:26:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-05-30 11:51:45 +00:00
|
|
|
import irc3
|
|
|
|
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-05-16 06:54:47 +00:00
|
|
|
from irc3.plugins.command import command
|
2017-05-16 10:06:29 +00:00
|
|
|
from irc3.utils import IrcString
|
2017-06-30 13:03:01 +00:00
|
|
|
from psycopg2 import Error
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
from . import DatabasePlugin
|
|
|
|
from ..utils import parse_int
|
|
|
|
|
|
|
|
|
2017-06-30 13:03:01 +00:00
|
|
|
@irc3.plugin
|
2017-05-15 22:26:10 +00:00
|
|
|
class McManiac(DatabasePlugin):
|
2017-05-18 00:01:44 +00:00
|
|
|
requires = ['irc3.plugins.command',
|
2017-07-07 00:11:20 +00:00
|
|
|
'bot.plugins.storage']
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
@command(options_first=True)
|
2017-08-22 13:57:57 +00:00
|
|
|
def mcmaniac(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-28 10:46:35 +00:00
|
|
|
"""Get a random McManiaC or by index.
|
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
%%mcmaniac [<index>]
|
|
|
|
"""
|
2017-05-28 10:46:35 +00:00
|
|
|
index = args.get('<index>')
|
2017-06-30 13:03:01 +00:00
|
|
|
|
2017-05-28 10:46:35 +00:00
|
|
|
if index:
|
|
|
|
index = parse_int(index)
|
|
|
|
if not index:
|
|
|
|
return
|
2017-07-07 00:11:20 +00:00
|
|
|
index, order = index
|
2017-05-28 10:46:35 +00:00
|
|
|
order = 'id {order}'.format(order=order)
|
2017-06-30 13:03:01 +00:00
|
|
|
offset = 'offset %s'
|
2017-05-16 05:45:10 +00:00
|
|
|
else:
|
2017-05-28 10:46:35 +00:00
|
|
|
order = 'random()'
|
2017-06-30 13:03:01 +00:00
|
|
|
offset = ''
|
|
|
|
|
|
|
|
# Fetch result from database
|
|
|
|
self.cur.execute('''
|
|
|
|
select
|
|
|
|
item,
|
|
|
|
rank() over (order by id),
|
|
|
|
count(*) over (rows between unbounded preceding
|
|
|
|
and unbounded following) as total
|
|
|
|
from
|
|
|
|
mcmaniacs
|
|
|
|
order by
|
|
|
|
{order}
|
|
|
|
limit
|
|
|
|
1
|
|
|
|
{offset}
|
|
|
|
'''.format(order=order, offset=offset), [index])
|
2017-05-28 10:46:35 +00:00
|
|
|
result = self.cur.fetchone()
|
2017-06-30 13:03:01 +00:00
|
|
|
|
2017-05-28 10:46:35 +00:00
|
|
|
if result:
|
2017-06-30 13:03:01 +00:00
|
|
|
return '[{rank}/{total}] {item}'.format(**result)
|
2017-05-28 10:46:35 +00:00
|
|
|
|
2017-07-06 12:40:48 +00:00
|
|
|
# TODO: fix regex ("McFooiaC McBariaC" adds "Mc\S+iaC")
|
2017-06-30 13:03:01 +00:00
|
|
|
@irc3.event(r'^:(?P<mask>\S+) PRIVMSG \S+ :.*(?P<item>Mc\S+iaC).*')
|
2017-05-30 14:13:20 +00:00
|
|
|
def save(self, mask: str, item: str):
|
|
|
|
if IrcString(mask).nick != self.bot.nick:
|
2017-06-30 13:03:01 +00:00
|
|
|
try:
|
|
|
|
# Insert into database
|
|
|
|
self.cur.execute('''
|
2017-08-22 14:15:52 +00:00
|
|
|
INSERT INTO
|
2017-06-30 13:03:01 +00:00
|
|
|
mcmaniacs (item)
|
2017-08-22 14:15:52 +00:00
|
|
|
VALUES
|
2017-06-30 13:03:01 +00:00
|
|
|
(%s)
|
|
|
|
''', [item])
|
|
|
|
self.con.commit()
|
|
|
|
except Error:
|
|
|
|
# Rollback transaction on error
|
|
|
|
self.con.rollback()
|