1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00
royalnet/rpgpack/utils/getactivebattle.py

37 lines
1.1 KiB
Python
Raw 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
async def get_active_battle(data: rc.CommandData) -> Optional[DndActiveBattle]:
interface = data._interface
alchemy = interface.alchemy
idata = get_interface_data(data)
DndAcBaT = alchemy.get(DndActiveBattle)
active_battles: List[DndActiveBattle] = await ru.asyncify(
data.session
.query(DndAcBaT)
.filter_by(interface_name=interface.name)
.all
)
for active_battle in active_battles:
if interface.name == "telegram":
# interface_data is chat id
chat_id = pickle.loads(active_battle.interface_data)
if chat_id == idata:
return active_battle
elif interface.name == "discord":
# 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