1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/rpgpack/commands/dndactivebattle.py
2020-09-16 02:37:31 +02:00

66 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from royalnet.commands import *
from royalnet.utils import asyncify
from ..tables import DndCharacter, DndActiveBattle, DndBattle
from ..utils import get_active_battle, get_interface_data
import pickle
class DndactivebattleCommand(Command):
name: str = "dndactivebattle"
description: str = "Set a D&D battle as active."
aliases = ["dab", "dndab", "activebattle", "dactivebattle"]
syntax = "{name|id}"
async def run(self, args: CommandArgs, data: CommandData) -> None:
BattleT = self.alchemy.get(DndBattle)
ABattleT = self.alchemy.get(DndActiveBattle)
async with data.session_acm() as session:
identifier = args.joined()
active_battle = await get_active_battle(session=session, data=data)
# Display the active character
if identifier == "":
if active_battle is None:
await data.reply(" No battles have ever been activated in this chat.")
else:
await data.reply(active_battle.battle.create_message())
return
# Find the battle
try:
identifier = int(identifier)
except ValueError:
# Find the battle by name
battles = await asyncify(session.query(BattleT).filter_by(name=identifier).all)
if len(battles) >= 2:
char_string = "\n".join([f"[c]{battle.id}[/c]" for battle in battles])
raise CommandError(f"Multiple battles share the name [b]{identifier}[/b], "
f"please activate one of them by using their id:\n{char_string}")
elif len(battles) == 1:
battle = battles[0]
else:
battle = None
else:
# Find the battle by id
battle = await asyncify(session.query(BattleT)
.filter_by(id=identifier)
.one_or_none)
if battle is None:
raise CommandError("No such battle found.")
# Check if the player already has an active character
if active_battle is None:
# Create a new active battle
active_battle = ABattleT(
battle=battle,
interface_name=data.command.serf.__class__.__name__,
interface_data=pickle.dumps(get_interface_data(data)))
session.add(active_battle)
else:
# Change the active character
active_battle.battle = battle
await asyncify(session.commit)
await data.reply(f"⚔️ [b]{battle}[/b]! Roll initiative!")