144 lines
6.1 KiB
Python
144 lines
6.1 KiB
Python
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_value(source_nick, 2)
|
|
last = datetime.strptime(db.get_user_value(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_values(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]:
|
|
if db.get_user_values(parts[1]) == -1:
|
|
return await self.bot.privmsg(channel, 'Those who haven\'t played yet shall not be patted.')
|
|
|
|
headpat = bool(random.choices([True, False], weights=[0.10, 0.90])[0])
|
|
source_data = db.get_user_values(source_nick)[0]
|
|
target_data = db.get_user_values(parts[1])[0]
|
|
|
|
last = datetime.strptime(source_data[6], df)
|
|
curr = datetime.strptime(datetime.now().strftime(df), df)
|
|
diff = round((curr - last).total_seconds() / 60.0)
|
|
|
|
if diff >= 30:
|
|
if source_data[2] >= 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_values(source_nick, {
|
|
'pats': (source_data[2] - 1),
|
|
'pattedTimes': (source_data[4] + 1),
|
|
'pattedLast': curr
|
|
})
|
|
db.set_user_values(parts[1], {
|
|
'patted': (target_data[3] + 1)
|
|
})
|
|
else:
|
|
await self.bot.privmsg(channel, 'You don\'t have enough pats left to pat someone.')
|
|
else:
|
|
rem = remaining_time(target_data[6], 30 * 60000)
|
|
await self.bot.privmsg(channel, f'You have to wait {rem} before you can pat {parts[1]} '
|
|
f'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_value(source_nick, 2)
|
|
patted = db.get_user_value(source_nick, 3)
|
|
patted_t = db.get_user_value(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_value(parts[1], 2)
|
|
patted = db.get_user_value(parts[1], 3)
|
|
patted_t = db.get_user_value(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.')
|