1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add treasures

This commit is contained in:
Steffo 2020-05-21 15:16:03 +02:00
parent 8f4ec1d1b9
commit cb37a65a81
Signed by: steffo
GPG key ID: 896A80F55F7C97F0
5 changed files with 105 additions and 0 deletions

View file

@ -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__

View file

@ -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!")

View file

@ -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!")

View file

@ -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__

View file

@ -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)