mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-24 03:54:20 +00:00
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
from typing import *
|
|
|
|
from sqlalchemy import *
|
|
from sqlalchemy.orm import *
|
|
from sqlalchemy.ext.declarative import declared_attr
|
|
|
|
from .fiorygi import Fiorygi
|
|
from royalnet.utils import asyncify
|
|
|
|
if TYPE_CHECKING:
|
|
from royalnet.commands import CommandData
|
|
|
|
|
|
class FiorygiTransaction:
|
|
__tablename__ = "fiorygitransactions"
|
|
|
|
@declared_attr
|
|
def id(self):
|
|
return Column(Integer, primary_key=True)
|
|
|
|
@declared_attr
|
|
def change(self):
|
|
return Column(Integer, nullable=False)
|
|
|
|
@declared_attr
|
|
def user_id(self):
|
|
return Column(Integer, ForeignKey("fiorygi.user_id"), nullable=False)
|
|
|
|
@declared_attr
|
|
def wallet(self):
|
|
return relationship("Fiorygi", backref=backref("transactions"))
|
|
|
|
@property
|
|
def user(self):
|
|
return self.wallet.user
|
|
|
|
@declared_attr
|
|
def reason(self):
|
|
return Column(String, nullable=False, default="")
|
|
|
|
def __repr__(self):
|
|
return f"<{self.__class__.__name__}: {self.change:+} to {self.user.username} for {self.reason}>"
|
|
|
|
@classmethod
|
|
async def spawn_fiorygi(cls, data: "CommandData", user, qty: int, reason: str):
|
|
if user.fiorygi is None:
|
|
data.session.add(data._interface.alchemy.get(Fiorygi)(
|
|
user_id=user.uid,
|
|
fiorygi=0
|
|
))
|
|
await data.session_commit()
|
|
|
|
transaction = data._interface.alchemy.get(FiorygiTransaction)(
|
|
user_id=user.uid,
|
|
change=qty,
|
|
reason=reason
|
|
)
|
|
data.session.add(transaction)
|
|
|
|
user.fiorygi.fiorygi += qty
|
|
await data.session_commit()
|
|
|
|
if len(user.telegram) > 0:
|
|
user_str = user.telegram[0].mention()
|
|
else:
|
|
user_str = user.username
|
|
|
|
if qty > 0:
|
|
msg = f"💰 [b]{user_str}[/b] ha ottenuto [b]{qty}[/b] fioryg{'i' if qty != 1 else ''} per [i]{reason}[/i]!"
|
|
elif qty == 0:
|
|
msg = f"❓ [b]{user_str}[/b] ha ottenuto [b]{qty}[/b] fioryg{'i' if qty != 1 else ''}...? " \
|
|
f"Per [i]{reason}[/i]...? Cosa?"
|
|
else:
|
|
msg = f"💸 [b]{user_str}[/b] ha perso [b]{-qty}[/b] fioryg{'i' if qty != -1 else ''} per [i]{reason}[/i]."
|
|
|
|
await data._interface.call_herald_event("telegram", "telegram_message",
|
|
chat_id=data._interface.config["Telegram"]["main_group_id"],
|
|
text=msg)
|