Skip to content

Commit

Permalink
Calculate Action Total (top 2 dice)
Browse files Browse the repository at this point in the history
  • Loading branch information
freiheit committed Oct 30, 2023
1 parent 4dd22a1 commit cf67f32
Showing 1 changed file with 65 additions and 11 deletions.
76 changes: 65 additions & 11 deletions mvkdicebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,71 @@ async def on_ready():


@bot.hybrid_command()
async def roll(ctx, *, dice: str):
"""Rolls a pool of dice in NdN format. Example: '?roll 1d20 2d10 d8 2d6'"""
logger.debug(f"roll: {dice}")
try:
rolls, limit = map(int, dice.split("d"))
except Exception:
await ctx.send("Format has to be in NdN!")
return

result = ", ".join(str(random.randint(1, limit)) for r in range(rolls))
await ctx.send(result)
async def roll(ctx, *, dicestr: str):
"""Rolls a pool of dice in NdN format.
Example: '?roll 1d20 2d10 d8 2d6'
Ignores anything extra it doesn't understand.
"""
logger.debug(f"roll: {dicestr}")

# types of dice we're looking for:
dicecounts = { 20:0, 12:0,
10: 0,
8: 0,
6: 0,
4: 0,
}

dicerolls = {}
flatdicerolls = []

patternNdN = re.compile(r'([0-9]*)d([0-9]+)')

for (count, size) in re.findall(patternNdN, dicestr):
logger.debug(f"roll: count={count} size={size}")
size=int(size)
if len(count) >= 1:
count=int(count)
elif len(count) < 1 or int(count) < 1:
count=1
if size in dicecounts:
dicecounts[size] += count
else:
await ctx.send(f"Invalid dice size d{size}")

flatdicerolls = []

for size in dicecounts:
if dicecounts[size] > 0:
# logger.debug(f"rolling: d{size}={dicecounts[size]}")
dicerolls[size] = []
for i in range(0, dicecounts[size]):
result = (random.randint(1,size))
dicerolls[size].append(result)
flatdicerolls.append(result)

flatdicerolls.sort(reverse=True)

if len(dicerolls) > 0:
answer = "Dice: "
for size in dicerolls:
answer += f"**{len(dicerolls[size])}d{size}**{ str(dicerolls[size])} "
answer += "\n"
action_dice = flatdicerolls[:2]
action_total = sum(action_dice)
answer += f"Action Total: {str(action_total)} {str(action_dice)}\n"
await ctx.send(answer)
else:
await ctx.send(f"No valid NdNs found in '{dicestr}'")

# try:
# rolls, limit = map(int, dicestr.split("d"))
# except Exception:
# await ctx.send("Format has to be in NdN!")
# return

# result = ", ".join(str(random.randint(1, limit)) for r in range(rolls))
# await ctx.send(result)


class ImproperlyConfigured(Exception):
Expand Down

0 comments on commit cf67f32

Please sign in to comment.