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

94 lines
3.6 KiB
Python
Raw Normal View History

2020-04-30 19:50:09 +00:00
from typing import *
2020-04-08 20:00:57 +00:00
import datetime
import re
import dateparser
import typing
2020-08-20 01:20:53 +00:00
import royalnet.utils as ru
2020-04-30 19:50:09 +00:00
import royalnet.commands as rc
2020-08-20 01:20:53 +00:00
import royalnet.serf.telegram as rst
2020-05-10 22:46:12 +00:00
2020-07-07 00:13:19 +00:00
from ..tables import MMEvent
from ..utils import MMTask
2020-07-02 19:40:33 +00:00
class MatchmakingCommand(rc.Command):
name: str = "matchmaking"
description: str = "Cerca persone per una partita a qualcosa!"
syntax: str = ""
aliases = ["mm", "lfg"]
2020-08-20 01:20:53 +00:00
def __init__(self, serf, config):
super().__init__(serf, config)
2020-07-02 19:40:33 +00:00
# Find all active MMEvents and run the tasks for them
session = self.alchemy.Session()
# Create a new MMEvent and run it
2020-08-20 01:20:53 +00:00
if isinstance(self.serf, rst.TelegramSerf):
2020-07-02 19:40:33 +00:00
MMEventT = self.alchemy.get(MMEvent)
active_mmevents = (
session
.query(MMEventT)
.filter(
2020-08-20 01:20:53 +00:00
MMEventT.interface == self.serf.interface_name,
2020-07-02 19:40:33 +00:00
MMEventT.interrupted == False
2020-04-30 19:50:09 +00:00
)
2020-07-02 19:40:33 +00:00
.all()
)
for mmevent in active_mmevents:
task = MMTask(mmevent.mmid, command=self)
task.start()
@staticmethod
def _parse_args(args) -> Tuple[Optional[datetime.datetime], str, str]:
"""Parse command arguments, either using the standard syntax or the Proto syntax."""
try:
timestring, title, description = args.match(r"(?:\[\s*([^]]+)\s*]\s*)?([^\n]+)\s*\n?\s*(.+)?\s*", re.DOTALL)
except rc.InvalidInputError:
timestring, title, description = args.match(r"(?:\s*(.+?)\s*\n\s*)?([^\n]+)\s*\n?\s*(.+)?\s*", re.DOTALL)
if timestring is not None:
try:
dt: typing.Optional[datetime.datetime] = dateparser.parse(timestring, settings={
"PREFER_DATES_FROM": "future"
})
except OverflowError:
dt = None
if dt is None:
raise rc.InvalidInputError("La data che hai specificato non è valida.")
if dt <= datetime.datetime.now():
raise rc.InvalidInputError("La data che hai specificato è nel passato.")
if dt - datetime.datetime.now() >= datetime.timedelta(days=366):
raise rc.InvalidInputError("Hai specificato una data tra più di un anno!\n"
"Se volevi scrivere un'orario, ricordati che le ore sono separate da "
"due punti (:) e non da punto semplice!")
2020-04-30 19:50:09 +00:00
else:
2020-07-02 19:40:33 +00:00
dt = None
return dt, title, description
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
"""Handle a matchmaking command call."""
# Parse the arguments, either with the standard syntax or with the Proto syntax
dt, title, description = self._parse_args(args)
# Add the MMEvent to the database
2020-08-20 01:20:53 +00:00
async with data.session_acm() as session:
author = await data.find_author(session=session, required=True)
2020-08-20 01:20:53 +00:00
mmevent: MMEvent = self.alchemy.get(MMEvent)(creator=author,
datetime=dt,
title=title,
description=description,
interface=self.serf.interface_name)
session.add(mmevent)
await ru.asyncify(session.commit)
2020-07-02 19:40:33 +00:00
# Create and run a task for the newly created MMEvent
task = MMTask(mmevent.mmid, command=self)
task.start()
2020-07-02 19:40:33 +00:00
await data.reply(f"🚩 Matchmaking creato!")