diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index 8125a946..fca178ba 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -57,6 +57,8 @@ from .royalpack import RoyalpackCommand from .givefiorygi import GivefiorygiCommand from .help import HelpCommand from .pug import PugCommand +from .createtreasure import CreatetreasureCommand +from .treasure import TreasureCommand # Enter the commands of your Pack here! available_commands = [ @@ -118,6 +120,8 @@ available_commands = [ GivefiorygiCommand, HelpCommand, PugCommand, + CreatetreasureCommand, + TreasureCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/createtreasure.py b/royalpack/commands/createtreasure.py new file mode 100644 index 00000000..d49a75f8 --- /dev/null +++ b/royalpack/commands/createtreasure.py @@ -0,0 +1,43 @@ +from typing import * +import royalnet +import royalnet.commands as rc +import royalnet.utils as ru +from ..tables import Treasure + + +class CreatetreasureCommand(rc.Command): + name: str = "createtreasure" + + description: str = "Crea un nuovo tesoro di Fiorygi." + + syntax: str = "{codice} {valore}" + + async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None: + author = await data.get_author(error_if_none=True) + if "banker" not in author.roles: + raise rc.UserError("Non hai permessi sufficienti per eseguire questo comando.") + + code = args[0] + try: + value = int(args[1]) + except ValueError: + raise rc.InvalidInputError("Il valore deve essere maggiore o uguale a 0.") + if value < 0: + raise rc.InvalidInputError("Il valore deve essere maggiore o uguale a 0.") + + TreasureT = self.alchemy.get(Treasure) + + treasure = await ru.asyncify(data.session.query(TreasureT).get, code) + if treasure is not None: + raise rc.UserError("Esiste già un Treasure con quel codice.") + + treasure = TreasureT( + code=code, + value=value, + redeemed_by=None + ) + data.session.add(treasure) + await data.session_commit() + + await data.delete_invoking() + await data.reply("✅ Tesoro creato!") diff --git a/royalpack/commands/treasure.py b/royalpack/commands/treasure.py new file mode 100644 index 00000000..70d58482 --- /dev/null +++ b/royalpack/commands/treasure.py @@ -0,0 +1,33 @@ +from typing import * +import royalnet +import royalnet.commands as rc +import royalnet.utils as ru +from ..tables import Treasure, FiorygiTransaction + + +class TreasureCommand(rc.Command): + name: str = "treasure" + + description: str = "Riscatta un Treasure che hai trovato da qualche parte." + + syntax: str = "{code}" + + async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None: + author = await data.get_author(error_if_none=True) + code = args[0] + + TreasureT = self.alchemy.get(Treasure) + + treasure = await ru.asyncify(data.session.query(TreasureT).get, code) + if treasure is None: + raise rc.UserError("Non esiste nessun Treasure con quel codice.") + if treasure.redeemed_by is not None: + raise rc.UserError(f"Quel tesoro è già stato riscattato da {treasure.redeemed_by}.") + + treasure.redeemed_by = author + await data.session_commit() + await FiorygiTransaction.spawn_fiorygi(data, + author, + treasure.value, + f'aver trovato il tesoro "{treasure.code}"') + await data.reply("🤑 Tesoro riscattato!") diff --git a/royalpack/tables/__init__.py b/royalpack/tables/__init__.py index 6e8a6937..6eac5697 100644 --- a/royalpack/tables/__init__.py +++ b/royalpack/tables/__init__.py @@ -17,6 +17,7 @@ from .brawlhalladuos import BrawlhallaDuo from .mmevents import MMEvent from .mmresponse import MMResponse from .cvstats import Cvstats +from .treasure import Treasure # Enter the tables of your Pack here! available_tables = [ @@ -38,6 +39,7 @@ available_tables = [ MMEvent, MMResponse, Cvstats, + Treasure, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/tables/treasure.py b/royalpack/tables/treasure.py new file mode 100644 index 00000000..daf1018a --- /dev/null +++ b/royalpack/tables/treasure.py @@ -0,0 +1,23 @@ +from sqlalchemy import * +from sqlalchemy.orm import * +from sqlalchemy.ext.declarative import declared_attr + + +class Treasure: + __tablename__ = "treasures" + + @declared_attr + def code(self): + return Column(String, primary_key=True) + + @declared_attr + def redeemed_by_id(self): + return Column(Integer, ForeignKey("users.uid")) + + @declared_attr + def redeemed_by(self): + return relationship("User") + + @declared_attr + def value(self): + return Column(Integer, nullable=False)