From 4a5391cb14292961a2d32e0074bc912a082cf4a8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 25 Apr 2021 23:23:24 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20/dig=20and=20/bury=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalpack/__main__.py | 2 + royalpack/commands/fiorygi.py | 124 ++++++++++++++++++ .../113c2444dad4_make_find_time_nullable.py | 32 +++++ royalpack/database/base.py | 2 +- 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 royalpack/database/alembic/versions/113c2444dad4_make_find_time_nullable.py diff --git a/royalpack/__main__.py b/royalpack/__main__.py index 7a7a334a..591cd143 100644 --- a/royalpack/__main__.py +++ b/royalpack/__main__.py @@ -63,6 +63,8 @@ register_telegram(commands.fiorygi_give, ["give"], r"(?P\S+)\s+(?P\S+)\s+(?P[0-9]+)\s+(?P.+)") register_telegram(commands.fiorygi_transactions_self, ["transactions"]) register_telegram(commands.fiorygi_transactions_other, ["transactions"], r"(?P\S+)") +register_telegram(commands.fiorygi_dig, ["dig"], r"(?P[a-z0-9-]+)") +register_telegram(commands.fiorygi_bury, ["bury"], r"(?P[a-z0-9-]+)\s+(?P[0-9]+)(?:\s+(?P.+))?") pda.implementations["telethon.1"].register_conversation(r) diff --git a/royalpack/commands/fiorygi.py b/royalpack/commands/fiorygi.py index 82f51bc9..954373b7 100644 --- a/royalpack/commands/fiorygi.py +++ b/royalpack/commands/fiorygi.py @@ -1,8 +1,10 @@ +import royalnet.royaltyping as t import royalnet.engineer as engi import royalpack.database as db import royalpack.bolts as rb import sqlalchemy.sql as ss import functools +import arrow @engi.use_database(db.lazy_session_class) @@ -197,6 +199,126 @@ async def fiorygi_magick( await _msg.reply(text=f"๐Ÿฆ Hai modificato il portafoglio di {_target} di \uE01Bฦ’ {amount}\uE00B.") +@engi.use_database(db.lazy_session_class) +@rb.use_ryglogin(allow_anonymous=False) +@engi.TeleportingConversation +async def fiorygi_dig( + *, + _user: db.User, + _session: db.SessionType, + _msg: engi.Message, + slug: str, + **__ +): + """ + Cerca un tesoro con un dato nome. + """ + + treasure: t.Optional[db.Treasure] = _session.execute( + ss.select( + db.Treasure + ).where( + db.Treasure.slug == slug + ) + ).scalar() + + if treasure is None: + await _msg.reply(text=f"๐Ÿ Non hai trovato nulla.") + return + + if treasure.finder is not None: + await _msg.reply(text=f"๐Ÿ Quel tesoro รจ giร  stato trovato da {treasure.finder}.") + return + + treasure.finder = _user + treasure.find_time = arrow.now() + + trans = db.Transaction( + minus=None, + plus=_user, + amount=treasure.value, + reason=f"""๐Ÿ– Ritrovamento del tesoro `{treasure.slug}`""", + ) + _session.add(trans) + _user.fiorygi += treasure.value + + _session.commit() + + if treasure.message: + await _msg.reply(text=f'๐Ÿ– Hai trovato un tesoro nascosto dal valore di \uE01Bฦ’ {treasure.value}\uE00B ' + f'con il seguente messaggio:\n\n"{treasure.message}"') + else: + await _msg.reply(text=f"๐Ÿ– Hai trovato un tesoro nascosto dal valore di \uE01Bฦ’ {treasure.value}\uE00B!") + + +@engi.use_database(db.lazy_session_class) +@rb.use_ryglogin(allow_anonymous=False) +@engi.TeleportingConversation +async def fiorygi_bury( + *, + _user: db.User, + _session: db.SessionType, + _msg: engi.Message, + slug: str, + value: int, + message: t.Optional[str], + **__ +): + """ + Nascondi un tesoro con un dato nome. + """ + + treasure: t.Optional[db.Treasure] = _session.execute( + ss.select( + db.Treasure + ).where( + db.Treasure.slug == slug + ) + ).scalar() + + if treasure is not None: + if treasure.finder is not None: + await _msg.reply(text=f"๐Ÿ Un tesoro con quel nome รจ giร  stato trovato da {treasure.finder}.") + else: + await _msg.reply(text=f"๐Ÿ– Un tesoro con quel nome esiste giร , " + f"\uE01Bma non รจ ancora stato trovato da nessuno\uE00B!") + return + + if value <= 0: + await _msg.reply(text=f"โš ๏ธ Il valore del tesoro deve essere intera positiva.") + return + + if _user.fiorygi < value: + await _msg.reply(text=f"โš ๏ธ Non hai abbastanza fiorygi da nascondere.") + return + + treasure = db.Treasure( + slug=slug, + creator=_user, + creation_time=arrow.now(), + value=value, + message=message, + ) + _session.add(treasure) + + trans = db.Transaction( + minus=_user, + plus=None, + amount=treasure.value, + reason=f"""๐Ÿ– Occultamento di un tesoro""", + ) + _session.add(trans) + _user.fiorygi -= value + + _session.commit() + + if treasure.message: + await _msg.reply(text=f'๐Ÿ– Hai nascosto un tesoro dal valore di \uE01Bฦ’ {treasure.value}\uE00B ' + f'con il seguente messaggio:\n"{treasure.message}"') + else: + await _msg.reply(text=f'๐Ÿ– Hai nascosto un tesoro dal valore di \uE01Bฦ’ {treasure.value}\uE00B.') + + __all__ = ( "fiorygi_balance_self", "fiorygi_balance_other", @@ -204,4 +326,6 @@ __all__ = ( "fiorygi_magick", "fiorygi_transactions_self", "fiorygi_transactions_other", + "fiorygi_dig", + "fiorygi_bury", ) diff --git a/royalpack/database/alembic/versions/113c2444dad4_make_find_time_nullable.py b/royalpack/database/alembic/versions/113c2444dad4_make_find_time_nullable.py new file mode 100644 index 00000000..5dda1576 --- /dev/null +++ b/royalpack/database/alembic/versions/113c2444dad4_make_find_time_nullable.py @@ -0,0 +1,32 @@ +"""Make find_time nullable + +Revision ID: 113c2444dad4 +Revises: e6b0d97063a1 +Create Date: 2021-04-25 23:17:50.846179 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '113c2444dad4' +down_revision = 'e6b0d97063a1' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('fiorygi_treasures', 'find_time', + existing_type=postgresql.TIMESTAMP(), + nullable=True) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('fiorygi_treasures', 'find_time', + existing_type=postgresql.TIMESTAMP(), + nullable=False) + # ### end Alembic commands ### diff --git a/royalpack/database/base.py b/royalpack/database/base.py index b95b8947..de864aba 100644 --- a/royalpack/database/base.py +++ b/royalpack/database/base.py @@ -263,7 +263,7 @@ class Treasure(Base, ra.ColRepr, ra.Updatable): finder_fk = s.Column(s.String, s.ForeignKey("users.sub")) finder = so.relationship("User", foreign_keys=(finder_fk,)) - find_time = s.Column(su.ArrowType, nullable=False) + find_time = s.Column(su.ArrowType) value = s.Column(s.Integer, nullable=False) message = s.Column(s.Text)