diff --git a/pat.py b/pat.py new file mode 100644 index 0000000..7f1eea3 --- /dev/null +++ b/pat.py @@ -0,0 +1,142 @@ +import random +import time +from datetime import datetime + +from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select +from src.channel_manager import ChannelManager +from src.db import Database +from src.plugin_base import PluginBase + +meta = MetaData() +pat_table = Table( + 'Pat', + meta, + Column('id', Integer, primary_key=True, autoincrement=True), + Column('name', String, unique=True, nullable=False), + Column('pats', Integer, default=0), + Column('patted', Integer, default=0), + Column('pattedTimes', Integer, default=0), + Column('patLast', String, default='1999/01/01 00:00:00'), + Column('pattedLast', String, default='1999/01/01 00:00:00'), +) +db = Database(pat_table, meta) +df = '%Y/%m/%d %H:%M:%S' + + +def remaining_time(date: str, timeout: int): + diff = (int(time.mktime(datetime.strptime(datetime.now().strftime(df), df).timetuple()) * 1000) - + int(time.mktime(datetime.strptime(date, df).timetuple()) * 1000)) + h = int((timeout - diff) / (60 * 60 * 1000) % 24) + m = int((timeout - diff) / (60 * 1000) % 60) + s = int((timeout - diff) / 1000 % 60) + hms = '' + + if h == 0 and m == 0 and s == 0: + return 0 + + if h != 0: + hms += f'{h}h ' + if m != 0: + hms += f'{m}m ' + if s != 0: + hms += f'{s}s ' + + return f'{hms[:-1]}' + + +def insert_user(user: str): + with db.engine.connect() as conn: + stmt = select(pat_table).where(pat_table.c.name == user) + cnt = len(conn.execute(stmt).fetchall()) + + if cnt == 0: + conn.execute(( + insert(pat_table). + values({'name': user}) + )) + conn.commit() + + +class Plugin(PluginBase): + async def handle_message(self, source_nick, channel, message): + parts = message.split() + db.create_table(pat_table.name) + self.channel_manager = ChannelManager() + + if parts[0].lower() == '!pat': + insert_user(source_nick) + + if len(parts) == 1: + pats = db.get_user(source_nick, 2) + last = datetime.strptime(db.get_user(source_nick, 5), df) + now = datetime.strptime(datetime.now().strftime(df), df) + diff = round((now - last).total_seconds() / 60.0) + + if diff >= 15: + await self.bot.action(channel, f'gives 1 pat to {source_nick}.') + + db.set_user(source_nick, { + 'pats': (pats + 1), + 'patLast': now.strftime(df) + }) + else: + rem = remaining_time(last.strftime(df), 15 * 60000) + await self.bot.privmsg(channel, f'You have to wait {rem} before you can claim more pats.') + elif len(parts) == 2: # Pat someone + if self.bot.config['Connection']['Nick'] != parts[1]: + if source_nick != parts[1]: + pats = db.get_user(parts[1], 2) + patted = db.get_user(source_nick, 3) + patted_t = db.get_user(parts[1], 4) + last = datetime.strptime(db.get_user(parts[1], 6), df) + now = datetime.strptime(datetime.now().strftime(df), df) + diff = round((now - last).total_seconds() / 60.0) + headpat = bool(random.choices([True, False], weights=[0.10, 0.90])[0]) + + if diff >= 30: + if pats >= 1: + if headpat: + await self.bot.action(channel, f'pats {parts[1]} on the head.') + else: + await self.bot.action(channel, f'pats {parts[1]}.') + + db.set_user(parts[1], { + 'pats': (pats - 1), + 'pattedTimes': (patted_t + 1), + 'pattedLast': now.strftime(df) + }) + db.set_user(source_nick, { + 'patted': (patted + 1) + }) + elif pats == -1: + await self.bot.privmsg(channel, 'This user doesn\'t exist (yet).') + else: + await self.bot.privmsg(channel, 'You don\'t have enough pats left to pat someone.') + else: + rem = remaining_time(last.strftime(df), 30 * 60000) + await self.bot.privmsg(channel, f'You have to wait {rem} before you can pat someone again.') + else: + await self.bot.privmsg(channel, 'You cannot pat yourself.') + else: + await self.bot.privmsg(channel, 'I\'m not able to pat myself.') + elif parts[0].lower() == '!pats': + if len(parts) == 1: + pats = db.get_user(source_nick, 2) + patted = db.get_user(source_nick, 3) + patted_t = db.get_user(source_nick, 4) + + if pats >= 0: + await self.bot.privmsg(channel, f'{source_nick} has patted others {patted_t} time(s), ' + f'has been patted {patted} time(s), and has {pats} pat(s) left.') + else: + await self.bot.privmsg(channel, 'You haven\'t played this game yet, there\'s nothing to show.') + elif len(parts) == 2: + pats = db.get_user(parts[1], 2) + patted = db.get_user(parts[1], 3) + patted_t = db.get_user(parts[1], 4) + + if pats >= 0: + await self.bot.privmsg(channel, f'{parts[1]} has patted others {patted_t} time(s), ' + f'has been patted {patted} time(s), and has {pats} pat(s) left.') + else: + await self.bot.privmsg(channel, 'This user doesn\'t exist.')