From cb86e0923d58ce1f233b6e32dcf08d2e1824b59c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 21 Aug 2022 01:44:38 +0200 Subject: [PATCH] `roll`: Improve and fix everything --- royalpack/__main__.py | 2 +- royalpack/commands/__init__.py | 1 + royalpack/commands/roll.py | 82 ++++++++++++++++++++++++---------- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/royalpack/__main__.py b/royalpack/__main__.py index e7e85641..9cc8cb3d 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[0-9]*)d(?P[0-9]+)(?P[+-][0-9]+)") +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/__init__.py b/royalpack/commands/__init__.py index d82d34d2..9c511de6 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -21,3 +21,4 @@ from .smecds import * from .spell import * from .version import * from .whoami import * +from .roll import * diff --git a/royalpack/commands/roll.py b/royalpack/commands/roll.py index c578aa05..a650377d 100644 --- a/royalpack/commands/roll.py +++ b/royalpack/commands/roll.py @@ -1,3 +1,4 @@ +import typing as t import datetime import random @@ -8,41 +9,76 @@ import royalpack.bolts as rb @rb.capture_errors @engi.TeleportingConversation -async def roll(*, _msg: engi.Message, qty: int, die: int, mod: int, **__): +async def roll(*, _msg: engi.Message, qty: int, die: int, mod: t.Optional[int], **__): """ Tira un dado nel formato di D&D: `1d20+1`, ad esempio. + + Non perfettamente casuale, **non usare per competizioni o altre cose serie**! """ - # 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 + # modificatore supersegreto della fortuna. ooooh! questo è top secret! + # "blame cate for this" --steffo - random.seed(datetime.datetime.now()) + # usa il giorno attuale come seed + seed = hash(datetime.date.today()) + lrand = random.Random(x=seed) + # la variazione massima del risultato deve essere di un sesto del tiro + luck = lrand.triangular(low=-0.17, high=0.17, mode=0.0) - # rolliamo i dadi richiesti - roll = [] - for i in range(quantity): - extracted = random.randint(1, die) + # veniamo al dunque - result = int(round(extracted+luck, 0)) + # resetta il seed + drand = random.Random() + + # tiriamo i dadi richiesti! + rolls = [] + for i in range(qty): + # trova il risultato base del tiro + result = drand.randint(1, die) + + # tira il valore di fortuna per vedere l'effetto sul dado + result += (drand.random() * luck) * die + + # arrotonda il risultato + result = round(result) + + # limita il risultato tra massimo e minimo per non destare sospetti result = min(result, die) result = max(result, 1) - roll.append(result) + rolls.append(result) + + # decidi un'emoji da mostrare + if luck > 0: + emoji = "🔆" + else: + emoji = "🔅" # formuliamo una risposta da mostrare all'utente - answer = f"🎲 {qty}d{die}{mod:+} = {roll}" - if mod: - answer +=" " - if mod > 0: - answer += "+" - answer +=str(mod) - - answer += " = "+str(sum(roll)+mod) - await _msg.reply(text=answer) + # usando una lista di stringhe che uniremo per inviare il messaggio + # perchè? performance! + answer = [ + f"{emoji} {qty}d{die}", + ] + + if mod and mod != 0: + answer.append(f"{mod:+}") + + answer.append(" = ") + + total = 0 + for index, result in enumerate(rolls): + if index != 0: + answer.append("+") + answer.append(f"{result}") + total += result + + answer.append(f" = \uE01B{total}\uE00B") + + await _msg.reply(text="".join(answer)) - -__all__ = ("roll",) \ No newline at end of file +__all__ = ( + "roll", +) \ No newline at end of file