1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/rpgpack/utils/getactivebattle.py
2020-09-16 02:37:31 +02:00

35 lines
1.2 KiB
Python

from typing import *
from ..tables import DndActiveBattle
from ..utils import get_interface_data
import royalnet.utils as ru
import royalnet.commands as rc
import pickle
async def get_active_battle(*, data: rc.CommandData, session) -> Optional[DndActiveBattle]:
alchemy = data.alchemy
idata = get_interface_data(data)
DndAcBaT = alchemy.get(DndActiveBattle)
active_battles: List[DndActiveBattle] = await ru.asyncify(
session
.query(DndAcBaT)
.filter_by(interface_name=data.command.serf.__class__.__name__)
.all
)
for active_battle in active_battles:
if data.command.serf.__class__.__name__ == "TelegramSerf":
# interface_data is chat id
chat_id = pickle.loads(active_battle.interface_data)
if chat_id == idata:
return active_battle
elif data.command.serf.__class__.__name__ == "DiscordSerf":
# interface_data is channel id
chat_id = pickle.loads(active_battle.interface_data)
if chat_id == idata:
return active_battle
else:
raise rc.UnsupportedError("This interface isn't supported yet.")
return None