From 7e704dd6b923e8ce3768426c5f31c0708ff414b4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 15 Aug 2022 01:14:51 +0000 Subject: [PATCH] Royalnetify a bit the code --- royalpack/__main__.py | 2 +- royalpack/commands/roll.py | 41 +++++++++----------------------------- 2 files changed, 10 insertions(+), 33 deletions(-) diff --git a/royalpack/__main__.py b/royalpack/__main__.py index 592dce5e..e7e85641 100644 --- a/royalpack/__main__.py +++ b/royalpack/__main__.py @@ -111,7 +111,7 @@ register_telegram(tg_router, commands.fiorygi_bury, ["bury"], register_telegram(tg_router, commands.version, ["version"]) register_telegram(tg_router, commands.cv, ["cv", "civù"]) register_telegram(tg_router, commands.rocoinflip, ["rocoinflip"], r'"(?P[^"]+)"\s+"(?P[^"]+)"') -register_telegram(tg_router, commands.roll, ["roll"], r"(?P.+)") +register_telegram(tg_router, commands.roll, ["roll"], r"(?P[0-9]*)d(?P[0-9]+)(?P[+-][0-9]+)") register_telegram(tg_router, commands.diobot, ["diobot", "phrase"]) register_telegram(tg_router, commands.loginprogress, ["loginprogress"]) diff --git a/royalpack/commands/roll.py b/royalpack/commands/roll.py index d4265b82..47e788cb 100644 --- a/royalpack/commands/roll.py +++ b/royalpack/commands/roll.py @@ -10,38 +10,16 @@ import royalpack.bolts as rb @rb.capture_errors @engi.TeleportingConversation -async def roll(*, _msg: engi.Message, message: str, **__): +async def roll(*, _msg: engi.Message, qty: int, die: int, mod: int, **__): """ - Tira un dado + Tira un dado nel formato di D&D: `1d20+1`, ad esempio. """ - if not re.match("[0-9]*d[0-9]+(\+[0-9])?(\-[0-9])?", message): - await _msg.reply(text="❗️ Errore: la stringa deve rispettare il pattern [quantity]d[die][modifier]") - - - # quantità di dadi - quantity, die = message.split('d') - if(message[0] =='d'): - quantity = 1 - - # modificatore - if '+' in message or '-' in message: - if '+' in message: - die, modifier = die.split('+') - else: - die, modifier = die.split('-') - modifier = '-'+ modifier - else: - modifier = 0 - - # rendiamo tutto un int, se non lo è già - quantity = int(quantity) - die = int(die) - modifier = int(modifier) - # modificatore supersegreto della fortuna. Ooooh! Questo è Top Secret! + # Steffo: hol up r = random.Random(x=hash(datetime.date.today())) luck = r.randrange(-100, 100)/100*die/3 + random.seed(datetime.datetime.now()) # rolliamo i dadi richiesti @@ -56,15 +34,14 @@ async def roll(*, _msg: engi.Message, message: str, **__): roll.append(result) # formuliamo una risposta da mostrare all'utente - answer = "🎲 " + message + " = " - answer += str(roll) - if modifier: + answer = f"🎲 {qty}d{die}{mod:+} = {roll}" + if mod: answer +=" " - if modifier > 0: + if mod > 0: answer += "+" - answer +=str(modifier) + answer +=str(mod) - answer += " = "+str(sum(roll)+modifier) + answer += " = "+str(sum(roll)+mod) await _msg.reply(text=answer)