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

Add /dig and /bury commands

This commit is contained in:
Steffo 2021-04-25 23:23:24 +02:00
parent 827dd0a836
commit 4a5391cb14
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 159 additions and 1 deletions

View file

@ -63,6 +63,8 @@ register_telegram(commands.fiorygi_give, ["give"], r"(?P<target>\S+)\s+(?P<amoun
register_telegram(commands.fiorygi_magick, ["magick"], r"(?P<target>\S+)\s+(?P<amount>[0-9]+)\s+(?P<reason>.+)")
register_telegram(commands.fiorygi_transactions_self, ["transactions"])
register_telegram(commands.fiorygi_transactions_other, ["transactions"], r"(?P<target>\S+)")
register_telegram(commands.fiorygi_dig, ["dig"], r"(?P<slug>[a-z0-9-]+)")
register_telegram(commands.fiorygi_bury, ["bury"], r"(?P<slug>[a-z0-9-]+)\s+(?P<value>[0-9]+)(?:\s+(?P<message>.+))?")
pda.implementations["telethon.1"].register_conversation(r)

View file

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

View file

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

View file

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