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

53 lines
1.6 KiB
Python
Raw Normal View History

from typing import *
import royalnet.commands as rc
import royalnet.utils as ru
from ..tables import DndBattleUnit
from .getactivebattle import get_active_battle
from .getactivechar import get_active_character
from ..types.faction import Faction
from sqlalchemy import and_
2020-09-16 00:37:31 +00:00
async def get_targets(target: Optional[str], *, data: rc.CommandData, session) -> List[DndBattleUnit]:
DndBattleUnitT = data.alchemy.get(DndBattleUnit)
active_battle = await get_active_battle(data)
if active_battle is None:
raise rc.CommandError("No battle is active in this chat.")
battle = active_battle.battle
# Get the active character
if not target or target.upper() == "SELF":
active_character = await get_active_character(data)
if active_character is None:
return []
char = active_character.character
2020-09-16 00:37:31 +00:00
return await ru.asyncify(session.query(DndBattleUnitT).filter_by(
linked_character=char,
battle=battle
).all)
# Get all
if target.upper() == "ALL":
2020-09-16 00:37:31 +00:00
return await ru.asyncify(session.query(DndBattleUnitT).filter_by(
battle=battle
).all)
# Get by faction
try:
faction = Faction.get(target)
except ValueError:
pass
else:
2020-09-16 00:37:31 +00:00
return await ru.asyncify(session.query(DndBattleUnitT).filter_by(
faction=faction,
battle=battle
).all)
# Get by ilike
2020-09-16 00:37:31 +00:00
return await ru.asyncify(session.query(DndBattleUnitT).filter(and_(
DndBattleUnitT.name.ilike(target),
DndBattleUnitT.battle == battle
)).all)