1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Matchmaking improvements

This commit is contained in:
Steffo 2019-10-27 23:19:12 +01:00
parent a079c8a730
commit 155e024e67
3 changed files with 35 additions and 29 deletions

View file

@ -28,8 +28,13 @@ class Command:
@property @property
def alchemy(self): def alchemy(self):
"""A shortcut to ``self.interface.alchemy``""" """A shortcut to ``self.interface.alchemy``."""
return self.interface.alchemy return self.interface.alchemy
@property
def loop(self):
"""A shortcut to ``self.interface.loop``."""
return self.interface.loop
async def run(self, args: CommandArgs, data: CommandData) -> None: async def run(self, args: CommandArgs, data: CommandData) -> None:
raise NotImplementedError() raise NotImplementedError()

View file

@ -5,13 +5,13 @@ import typing
from telegram import Bot as PTBBot from telegram import Bot as PTBBot
from telegram import Message as PTBMessage from telegram import Message as PTBMessage
from telegram.error import BadRequest, Unauthorized from telegram.error import BadRequest, Unauthorized
from telegram import InlineKeyboardMarkup as IKM from telegram import InlineKeyboardMarkup as InKeMa
from telegram import InlineKeyboardButton as IKB from telegram import InlineKeyboardButton as InKeBu
from royalnet.commands import * from royalnet.commands import *
from royalnet.bots import TelegramBot from royalnet.bots import TelegramBot
from royalnet.utils import telegram_escape, asyncify, sleep_until from royalnet.utils import telegram_escape, asyncify, sleep_until
from ..tables import MMEvent, MMResponse, User, Telegram from ..tables import MMEvent, MMResponse, User
from ..utils import MMChoice, MMInterfaceData, MMInterfaceDataTelegram from ..utils import MMChoice, MMInterfaceDataTelegram
class MatchmakingCommand(Command): class MatchmakingCommand(Command):
@ -30,10 +30,11 @@ class MatchmakingCommand(Command):
# Find all relevant MMEvents and run them # Find all relevant MMEvents and run them
session = self.alchemy.Session() session = self.alchemy.Session()
mmevents = ( mmevents = (
session.query(self.alchemy.MMEvent) session
.filter(self.alchemy.MMEvent.interface == self.interface.name, .query(self.alchemy.MMEvent)
self.alchemy.MMEvent.datetime > datetime.datetime.now()) .filter(self.alchemy.MMEvent.interface == self.interface.name,
.all() self.alchemy.MMEvent.datetime > datetime.datetime.now())
.all()
) )
for mmevent in mmevents: for mmevent in mmevents:
self.interface.loop.create_task(self._run_mmevent(mmevent.mmid)) self.interface.loop.create_task(self._run_mmevent(mmevent.mmid))
@ -55,11 +56,11 @@ class MatchmakingCommand(Command):
except OverflowError: except OverflowError:
dt = None dt = None
if dt is None: if dt is None:
await data.reply("⚠️ La data che hai specificato non è valida.") raise CommandError("La data che hai specificato non è valida.")
return
if dt <= datetime.datetime.now(): if dt <= datetime.datetime.now():
await data.reply("⚠️ La data che hai specificato è nel passato.") raise CommandError("La data che hai specificato è nel passato.")
return if dt >= datetime.datetime.now() + datetime.timedelta(days=90):
raise CommandError("La data che hai specificato è a più di 90 giorni di distanza da oggi.")
mmevent: MMEvent = self.interface.alchemy.MMEvent(creator=author, mmevent: MMEvent = self.interface.alchemy.MMEvent(creator=author,
datetime=dt, datetime=dt,
title=title, title=title,
@ -67,7 +68,7 @@ class MatchmakingCommand(Command):
interface=self.interface.name) interface=self.interface.name)
data.session.add(mmevent) data.session.add(mmevent)
await data.session_commit() await data.session_commit()
await self._run_mmevent(mmevent.mmid) self.interface.loop(self._run_mmevent(mmevent.mmid))
await data.reply(f"✅ Evento [b]{mmevent.title}[/b] creato!") await data.reply(f"✅ Evento [b]{mmevent.title}[/b] creato!")
_mm_chat_id = -1001287169422 _mm_chat_id = -1001287169422
@ -85,16 +86,17 @@ class MatchmakingCommand(Command):
return text return text
def _gen_telegram_keyboard(self, mmevent: MMEvent): def _gen_telegram_keyboard(self, mmevent: MMEvent):
return IKM([ return InKeMa([
[IKB(f"{MMChoice.YES.value} Ci sarò!", callback_data=f"mm{mmevent.mmid}_YES")], [InKeBu(f"{MMChoice.YES.value} Ci sarò!", callback_data=f"mm{mmevent.mmid}_YES")],
[IKB(f"{MMChoice.MAYBE.value} (Forse.)", callback_data=f"mm{mmevent.mmid}_MAYBE")], [InKeBu(f"{MMChoice.MAYBE.value} (Forse.)", callback_data=f"mm{mmevent.mmid}_MAYBE")],
[IKB(f"{MMChoice.LATE_SHORT.value} Arrivo dopo 5-10 min.", callback_data=f"mm{mmevent.mmid}_LATE_SHORT")], [InKeBu(f"{MMChoice.LATE_SHORT.value} Arrivo dopo 5-10 min.",
[IKB(f"{MMChoice.LATE_MEDIUM.value} Arrivo dopo 15-35 min.", callback_data=f"mm{mmevent.mmid}_LATE_SHORT")],
callback_data=f"mm{mmevent.mmid}_LATE_MEDIUM")], [InKeBu(f"{MMChoice.LATE_MEDIUM.value} Arrivo dopo 15-35 min.",
[IKB(f"{MMChoice.LATE_LONG.value} Arrivo dopo 40+ min.", callback_data=f"mm{mmevent.mmid}_LATE_LONG")], callback_data=f"mm{mmevent.mmid}_LATE_MEDIUM")],
[IKB(f"{MMChoice.NO_TIME.value} Non posso a quell'ora...", callback_data=f"mm{mmevent.mmid}_NO_TIME")], [InKeBu(f"{MMChoice.LATE_LONG.value} Arrivo dopo 40+ min.", callback_data=f"mm{mmevent.mmid}_LATE_LONG")],
[IKB(f"{MMChoice.NO_INTEREST.value} Non mi interessa.", callback_data=f"mm{mmevent.mmid}_NO_INTEREST")], [InKeBu(f"{MMChoice.NO_TIME.value} Non posso a quell'ora...", callback_data=f"mm{mmevent.mmid}_NO_TIME")],
[IKB(f"{MMChoice.NO_TECH.value} Ho un problema!", callback_data=f"mm{mmevent.mmid}_NO_TECH")], [InKeBu(f"{MMChoice.NO_INTEREST.value} Non mi interessa.", callback_data=f"mm{mmevent.mmid}_NO_INTEREST")],
[InKeBu(f"{MMChoice.NO_TECH.value} Ho un problema!", callback_data=f"mm{mmevent.mmid}_NO_TECH")],
]) ])
async def _update_telegram_mm_message(self, client: PTBBot, mmevent: MMEvent): async def _update_telegram_mm_message(self, client: PTBBot, mmevent: MMEvent):
@ -165,7 +167,8 @@ class MatchmakingCommand(Command):
self._gen_mm_message(mmevent)), self._gen_mm_message(mmevent)),
parse_mode="HTML", parse_mode="HTML",
disable_webpage_preview=True, disable_webpage_preview=True,
reply_markup=self._gen_telegram_keyboard(mmevent)) reply_markup=self._gen_telegram_keyboard(
mmevent))
# Store message data in the interface data object # Store message data in the interface data object
mmevent.interface_data = MMInterfaceDataTelegram(chat_id=self._mm_chat_id, mmevent.interface_data = MMInterfaceDataTelegram(chat_id=self._mm_chat_id,
message_id=message.message_id) message_id=message.message_id)
@ -236,8 +239,6 @@ class MatchmakingCommand(Command):
if self.interface.name == "telegram": if self.interface.name == "telegram":
await self.interface.bot.safe_api_call(client.delete_message, await self.interface.bot.safe_api_call(client.delete_message,
chat_id=mmevent.interface_data.chat_id, chat_id=mmevent.interface_data.chat_id,
message_id=mmevent.interface_data.message_id, message_id=mmevent.interface_data.message_id)
parse_mode="HTML",
disable_webpage_preview=True)
# The end! # The end!
await asyncify(session.close) await asyncify(session.close)

View file

@ -1,4 +1,4 @@
semantic = "5.0a74" semantic = "5.0a75"
if __name__ == "__main__": if __name__ == "__main__":
print(semantic) print(semantic)