Refactors roll

En flink ook. Nu met regex en meer pythonic one-liners die niet of
nauwelijks leesbaar zijn. Eindeloos plezier.
This commit is contained in:
2022-12-06 01:39:28 +01:00
parent ba5958fb77
commit 3760d8cb02
+48 -49
View File
@@ -9,6 +9,7 @@ import os
import random
import caldav
import warnings
import re
class Shitpost(commands.Cog):
"""Schijtpaal commando's. Alleen bruikbaar in de daarvoor toegewezen schijtpaal kanalen."""
@@ -466,61 +467,59 @@ class Shitpost(commands.Cog):
else:
yield from ctx.channel.send(ctx.author.mention + ' Het is nu ' + datetime.now().strftime('%H:%M'))
def get_dice_roll(self, nrOfDice, dieType, modifier=None):
if nrOfDice > 99 or dieType > 9999 or dieType < 2 or (modifier and abs(modifier) > 9999):
return None
# Do the rolling
random.seed(datetime.now())
total = 0
rolls = []
for x in range(0, nrOfDice):
roll = random.randint(1, dieType)
total += roll
rolls.append(roll)
total += modifier if modifier else 0
#Tally results
modifierText = "%+d" % modifier if modifier else "" # print modifier with sign
returnText = f"Rolling {nrOfDice}d{dieType}{modifierText}...\n"
rolls = [f"[**{roll}**]" if dieType >= 10 and (roll == 1 or roll == dieType) else f"[{roll}]" for roll in rolls]
returnText += "+".join(rolls)
returnText += modifierText
if nrOfDice > 1 or modifier: # only show calculation if it makes sense:
returnText += " = **" + str(total) + "**"
return returnText
def get_dice_parameters(self, dice):
match = re.match(r"(\d+)d(\d+)(\s*([+-])\s*(\d+))?\s*", dice.lower())
if not match:
return None
numDice = int(match.group(1))
die = int(match.group(2))
modifier = None
modified = match.group(3)
if modified:
modifierType = match.group(4)
modifier = int(match.group(5))
if "-" in modifierType:
modifier = -modifier
return (numDice, die, modifier)
@commands.command(pass_context=True)
@asyncio.coroutine
def roll(self, ctx, dice="1d20", modifier1="", modifier2=""):
def roll(self, ctx, *, dice):
"""Roll een dobbelsteen. Of een paar. Bijvoorbeeld:
!roll 4d20
!roll 1d6 + 5
"""
diceSplit = dice.lower().split('d')
if len(diceSplit) != 2:
return
modifier1 = modifier1.replace("+", "") #this is both possible: "1d20 + 1" and "1d20 +1"
modifier2 = modifier2.replace("+", "")
if modifier1 == "-":
modifier1 = ""; #this is also possible: "1d20 - 1"
modifier2 = "-" + modifier2
try: #check if they are numbers:
nrOfDice = 1 if (diceSplit[0]=="") else int(diceSplit[0]) #allow input like: "!roll d20"
dieType = int(diceSplit[1])
mod1 = 0 if (modifier1=="") else int(modifier1)
mod2 = 0 if (modifier2=="") else int(modifier2)
except ValueError:
return
#validate values:
if nrOfDice < 1 or nrOfDice > 99 or dieType < 2 or dieType > 100:
return
if mod1 < -999 or mod1 > 999 or mod2 < -999 or mod2 > 999:
return
#roll:
random.seed(datetime.now())
total = 0
returnText = "Rolling " + str(nrOfDice) + "d" + str(dieType) + "...\n"
for x in range(0, nrOfDice):
if x != 0:
returnText += "+ "
roll = random.randint(1, dieType)
total += roll
if dieType >= 10 and (roll == 1 or roll == dieType):
returnText += "[**" + str(roll) + "**] " #make significant numbers bold
else:
returnText += "[" + str(roll) + "] "
total += mod1 + mod2
if mod1 != 0:
if mod1 > 0:
returnText += "+"
returnText += str(mod1) + " "
if mod2 != 0:
if mod2 > 0:
returnText += "+"
returnText += str(mod2) + " "
if nrOfDice > 1 or mod1 != 0 or mod2 != 0: #only show calculation if it makes sense:
returnText += " = __**" + str(total) + "**__"
yield from ctx.channel.send(returnText)
(nrOfDice, dieType, modifier) = self.get_dice_parameters(dice)
message = self.get_dice_roll(nrOfDice, dieType, modifier)
if message:
yield from ctx.channel.send(message)
else:
yield from ctx.channel.send("Die roll vind ik echt een beetje te mal..")
def get_weerbericht(self, wData, verbose=None):
if not wData: