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

91 lines
2.6 KiB
Python
Raw Normal View History

2020-04-26 21:13:13 +00:00
from typing import *
import datetime
2020-04-26 21:13:13 +00:00
2020-01-31 00:47:44 +00:00
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declared_attr
2020-08-20 01:20:53 +00:00
import royalnet.utils as ru
2020-01-31 00:47:44 +00:00
2020-04-26 21:13:13 +00:00
from .fiorygi import Fiorygi
if TYPE_CHECKING:
from royalnet.commands import CommandData
2020-01-31 00:47:44 +00:00
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
2020-03-18 16:35:08 +00:00
def wallet(self):
2020-01-31 00:47:44 +00:00
return relationship("Fiorygi", backref=backref("transactions"))
2020-03-18 16:35:08 +00:00
@property
def user(self):
return self.wallet.user
2020-01-31 00:47:44 +00:00
@declared_attr
def reason(self):
return Column(String, nullable=False, default="")
@declared_attr
def timestamp(self):
return Column(DateTime)
2020-01-31 00:47:44 +00:00
def __repr__(self):
return f"<{self.__class__.__name__}: {self.change:+} to {self.user.username} for {self.reason}>"
2020-04-26 21:13:13 +00:00
@classmethod
2020-08-23 22:13:38 +00:00
async def spawn_fiorygi(cls, user, qty: int, reason: str, *, data: "CommandData", session):
FiorygiT = data.alchemy.get(cls)
FiorygiTransactionT = data.alchemy.get(FiorygiTransaction)
2020-04-26 21:13:13 +00:00
if user.fiorygi is None:
2020-08-23 22:13:38 +00:00
session.add(
FiorygiT(
2020-08-20 01:20:53 +00:00
user_id=user.uid,
fiorygi=0
2020-08-23 22:13:38 +00:00
)
)
await ru.asyncify(session.commit)
2020-08-20 01:20:53 +00:00
2020-08-23 22:13:38 +00:00
session.add(
FiorygiTransactionT(
2020-04-26 21:13:13 +00:00
user_id=user.uid,
2020-08-20 01:20:53 +00:00
change=qty,
reason=reason,
timestamp=datetime.datetime.now()
)
2020-08-23 22:13:38 +00:00
)
user.fiorygi.fiorygi += qty
await ru.asyncify(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 mantenuto i suoi fiorygi attuali per [i]{reason}[/i].\nWait, 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.command.serf.call_herald_event(
"telegram", "telegram_message",
chat_id=data.command.config["Telegram"]["main_group_id"],
text=msg)