2020-03-04 00:00:55 +00:00
|
|
|
from typing import *
|
|
|
|
import royalnet
|
|
|
|
import royalnet.commands as rc
|
|
|
|
import royalnet.utils as ru
|
2020-03-04 18:29:33 +00:00
|
|
|
from ..types import Faction
|
2020-03-04 00:00:55 +00:00
|
|
|
from ..tables import DndBattleUnit
|
|
|
|
from ..utils import get_active_battle
|
|
|
|
|
|
|
|
|
|
|
|
class DndaddunitCommand(rc.Command):
|
|
|
|
name: str = "dndaddunit"
|
|
|
|
|
|
|
|
description: str = "Add an Unit to a Battle."
|
|
|
|
|
2020-03-04 18:29:33 +00:00
|
|
|
aliases = ["dau", "dndau", "addunit", "daddunit"]
|
2020-03-04 00:00:55 +00:00
|
|
|
|
|
|
|
syntax: str = "{faction} {name} {initiative} {health} {armorclass}"
|
|
|
|
|
|
|
|
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
|
|
|
|
faction = Faction[args[0].upper()]
|
|
|
|
name = args[1]
|
|
|
|
initiative = int(args[2])
|
|
|
|
health = args[3]
|
|
|
|
armor_class = int(args[4])
|
|
|
|
|
|
|
|
DndBattleUnitT = self.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.")
|
|
|
|
|
2020-03-04 18:29:33 +00:00
|
|
|
units_with_same_name = await ru.asyncify(data.session.query(DndBattleUnitT).filter_by(
|
|
|
|
name=name,
|
|
|
|
battle=active_battle.battle
|
|
|
|
).all)
|
|
|
|
|
|
|
|
if len(units_with_same_name) != 0:
|
|
|
|
raise rc.InvalidInputError("A unit with the same name already exists.")
|
|
|
|
|
2020-03-04 00:00:55 +00:00
|
|
|
dbu = DndBattleUnitT(
|
2020-03-04 18:29:33 +00:00
|
|
|
linked_character_id=None,
|
2020-03-04 00:00:55 +00:00
|
|
|
initiative=initiative,
|
|
|
|
faction=faction,
|
|
|
|
name=name,
|
|
|
|
health_string=health,
|
|
|
|
armor_class=armor_class,
|
|
|
|
battle=active_battle.battle
|
|
|
|
)
|
|
|
|
|
|
|
|
data.session.add(dbu)
|
|
|
|
await data.session_commit()
|
|
|
|
|
2020-03-04 18:29:33 +00:00
|
|
|
await data.reply(f"{dbu}\n"
|
|
|
|
f"joins the battle!")
|
|
|
|
|
|
|
|
if dbu.health.hidden:
|
|
|
|
await data.delete_invoking()
|