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

36 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-02-22 01:16:54 +00:00
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
2020-09-16 00:37:31 +00:00
async def get_active_battle(*, data: rc.CommandData, session) -> Optional[DndActiveBattle]:
alchemy = data.alchemy
2020-02-22 01:16:54 +00:00
idata = get_interface_data(data)
DndAcBaT = alchemy.get(DndActiveBattle)
active_battles: List[DndActiveBattle] = await ru.asyncify(
2020-09-16 00:37:31 +00:00
session
2020-02-22 01:16:54 +00:00
.query(DndAcBaT)
2020-09-16 00:37:31 +00:00
.filter_by(interface_name=data.command.serf.__class__.__name__)
2020-02-22 01:16:54 +00:00
.all
)
for active_battle in active_battles:
2020-09-16 00:37:31 +00:00
if data.command.serf.__class__.__name__ == "TelegramSerf":
2020-02-22 01:16:54 +00:00
# interface_data is chat id
chat_id = pickle.loads(active_battle.interface_data)
if chat_id == idata:
return active_battle
2020-09-16 00:37:31 +00:00
elif data.command.serf.__class__.__name__ == "DiscordSerf":
2020-02-22 01:16:54 +00:00
# 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