From 479657d6eee928d42bc04ec6839454eacc49b200 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 4 Mar 2020 01:00:55 +0100 Subject: [PATCH] Some progress on the initiative tracker --- rpgpack/commands/__init__.py | 2 ++ rpgpack/commands/dndaddunit.py | 45 +++++++++++++++++++++++++++ rpgpack/tables/dndactivebattle.py | 6 +++- rpgpack/tables/dndactivecharacters.py | 8 +++-- rpgpack/tables/dndbattle.py | 3 ++ rpgpack/tables/dndbattleunit.py | 20 ++++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 rpgpack/commands/dndaddunit.py diff --git a/rpgpack/commands/__init__.py b/rpgpack/commands/__init__.py index 29b717e3..3cd5ed96 100644 --- a/rpgpack/commands/__init__.py +++ b/rpgpack/commands/__init__.py @@ -12,6 +12,7 @@ from .testhealth import TesthealthCommand from .testfaction import TestfactionCommand from .dndnewbattle import DndnewbattleCommand from .dndactivebattle import DndactivebattleCommand +from .dndaddunit import DndaddunitCommand # Enter the commands of your Pack here! available_commands = [ @@ -28,6 +29,7 @@ available_commands = [ TestfactionCommand, DndnewbattleCommand, DndactivebattleCommand, + DndaddunitCommand ] # Don't change this, it should automatically generate __all__ diff --git a/rpgpack/commands/dndaddunit.py b/rpgpack/commands/dndaddunit.py new file mode 100644 index 00000000..36d7a2a9 --- /dev/null +++ b/rpgpack/commands/dndaddunit.py @@ -0,0 +1,45 @@ +from typing import * +import royalnet +import royalnet.commands as rc +import royalnet.utils as ru +from ..types import Faction, Health +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." + + aliases = ["dau", "dndau", "daddunit", ""] + + 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.") + + dbu = DndBattleUnitT( + 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() + + await data.reply(f"✅ [b]{dbu.name}[/b] joined the battle!\n" + f"{dbu}") diff --git a/rpgpack/tables/dndactivebattle.py b/rpgpack/tables/dndactivebattle.py index 620fe9a1..5f381017 100644 --- a/rpgpack/tables/dndactivebattle.py +++ b/rpgpack/tables/dndactivebattle.py @@ -6,9 +6,13 @@ from sqlalchemy.ext.declarative import * class DndActiveBattle: __tablename__ = "dndactivebattle" + @declared_attr + def active_battle_id(self): + return Column(Integer, primary_key=True) + @declared_attr def battle_id(self): - return Column(Integer, ForeignKey("dndbattle.id"), primary_key=True) + return Column(Integer, ForeignKey("dndbattle.id")) @declared_attr def battle(self): diff --git a/rpgpack/tables/dndactivecharacters.py b/rpgpack/tables/dndactivecharacters.py index 1b47ebd1..11a55ca4 100644 --- a/rpgpack/tables/dndactivecharacters.py +++ b/rpgpack/tables/dndactivecharacters.py @@ -6,13 +6,17 @@ from sqlalchemy.ext.declarative import * class DndActiveCharacter: __tablename__ = "dndactivecharacters" + @declared_attr + def active_character_id(self): + return Column(Integer, primary_key=True) + @declared_attr def character_id(self): - return Column(Integer, ForeignKey("dndcharacters.character_id"), primary_key=True) + return Column(Integer, ForeignKey("dndcharacters.character_id")) @declared_attr def user_id(self): - return Column(Integer, ForeignKey("users.uid"), primary_key=True) + return Column(Integer, ForeignKey("users.uid")) @declared_attr def character(self): diff --git a/rpgpack/tables/dndbattle.py b/rpgpack/tables/dndbattle.py index 62666ccb..cabb64bf 100644 --- a/rpgpack/tables/dndbattle.py +++ b/rpgpack/tables/dndbattle.py @@ -28,4 +28,7 @@ class DndBattle: string = [] string.append(f"⚔️ [b]{self.name}[/b]\n") string.append(f"{self.description}\n") + string.append("\n") + for unit in sorted(self.units, key=lambda u: -u.initiative): + string.append(f"{unit}\n") return "".join(string) diff --git a/rpgpack/tables/dndbattleunit.py b/rpgpack/tables/dndbattleunit.py index 64b98720..cd997f61 100644 --- a/rpgpack/tables/dndbattleunit.py +++ b/rpgpack/tables/dndbattleunit.py @@ -54,3 +54,23 @@ class DndBattleUnit: @declared_attr def status(self): return Column(String) + + def __str__(self): + string = [ + f"{self.faction.value}", + f"[b]{self.name}[/b]", + f"[{self.initiative}]", + ] + + if self.health: + string.append(f"{self.health}") + if self.armor_class: + string.append(f"🛡 {self.armor_class}") + if self.extra: + string.append(f"💠 {self.extra}") + if self.status: + string.append(f"💫 {self.status}") + return " ".join(string) + + def __repr__(self): + return f""