2020-03-16 22:21:24 +00:00
|
|
|
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)
|
2020-03-16 22:21:24 +00:00
|
|
|
|
|
|
|
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(
|
2020-03-16 22:21:24 +00:00
|
|
|
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(
|
2020-03-16 22:21:24 +00:00
|
|
|
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(
|
2020-03-16 22:21:24 +00:00
|
|
|
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_(
|
2020-03-16 22:21:24 +00:00
|
|
|
DndBattleUnitT.name.ilike(target),
|
|
|
|
DndBattleUnitT.battle == battle
|
|
|
|
)).all)
|