demand a roll prefix on the Dice module
This commit is contained in:
parent
b9ef46606d
commit
5b44b66c4d
121
modules/Dice.py
121
modules/Dice.py
@ -28,73 +28,76 @@ class Dice(Module):
|
|||||||
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
overallroll = what
|
overallroll = what
|
||||||
|
|
||||||
rolls = re.split(';\s*', overallroll)
|
if what.split(' ')[0] == 'roll':
|
||||||
reply = ""
|
rolls = re.split(';\s*', overallroll)
|
||||||
for roll in rolls:
|
reply = ""
|
||||||
pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
|
for roll in rolls:
|
||||||
regex = re.compile(pattern)
|
pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
|
||||||
matches = regex.search(roll)
|
regex = re.compile(pattern)
|
||||||
|
matches = regex.search(roll)
|
||||||
|
|
||||||
if matches is not None:
|
if matches is not None:
|
||||||
# set variables including defaults for unspecified stuff
|
# set variables including defaults for unspecified stuff
|
||||||
faces = int(matches.group(4))
|
faces = int(matches.group(4))
|
||||||
comment = matches.group(7)
|
comment = matches.group(7)
|
||||||
|
|
||||||
if matches.group(1) is None:
|
if matches.group(1) is None:
|
||||||
times = 1
|
times = 1
|
||||||
else:
|
|
||||||
times = int(matches.group(1))
|
|
||||||
|
|
||||||
if matches.group(3) is None:
|
|
||||||
dice = 1
|
|
||||||
else:
|
|
||||||
dice = int(matches.group(3))
|
|
||||||
|
|
||||||
if matches.group(2) is None:
|
|
||||||
top = dice
|
|
||||||
else:
|
|
||||||
top = int(matches.group(2))
|
|
||||||
|
|
||||||
if matches.group(5) is None or matches.group(6) is None:
|
|
||||||
modifier = 0
|
|
||||||
else:
|
|
||||||
if str(matches.group(5)) == '-':
|
|
||||||
modifier = -1 * int(matches.group(6))
|
|
||||||
else:
|
else:
|
||||||
modifier = int(matches.group(6))
|
times = int(matches.group(1))
|
||||||
|
|
||||||
result = ''
|
if matches.group(3) is None:
|
||||||
if comment is not None:
|
dice = 1
|
||||||
result += comment + ': '
|
else:
|
||||||
|
dice = int(matches.group(3))
|
||||||
|
|
||||||
for t in range(times):
|
if matches.group(2) is None:
|
||||||
ressubstr = ""
|
top = dice
|
||||||
rolls = []
|
else:
|
||||||
for d in range(dice):
|
top = int(matches.group(2))
|
||||||
rolls.append(str(random.randint(1, faces)))
|
|
||||||
rolls.sort()
|
if matches.group(5) is None or matches.group(6) is None:
|
||||||
rolls.reverse()
|
modifier = 0
|
||||||
ressubstr = ','.join(rolls[0:top])
|
else:
|
||||||
sum = 0
|
if str(matches.group(5)) == '-':
|
||||||
for r in rolls[0:top]:
|
modifier = -1 * int(matches.group(6))
|
||||||
sum += int(r)
|
|
||||||
sumplus = sum + modifier
|
|
||||||
result += str(sumplus) + ' [' + ressubstr
|
|
||||||
if modifier != 0:
|
|
||||||
if modifier > 0:
|
|
||||||
result += ' + ' + str(modifier)
|
|
||||||
else:
|
else:
|
||||||
result += ' - ' + str(-1 * modifier)
|
modifier = int(matches.group(6))
|
||||||
result += ']'
|
|
||||||
|
|
||||||
if t != times-1:
|
result = nick + ': ' + roll + ': '
|
||||||
result += ', '
|
if comment is not None:
|
||||||
|
result += comment + ': '
|
||||||
|
|
||||||
reply += result
|
for t in range(times):
|
||||||
if roll is not rolls[-1]:
|
ressubstr = ""
|
||||||
reply += "; "
|
rolls = []
|
||||||
if reply is not "":
|
for d in range(dice):
|
||||||
return self.reply(connection, replypath, reply)
|
rolls.append(str(random.randint(1, faces)))
|
||||||
|
rolls.sort()
|
||||||
|
rolls.reverse()
|
||||||
|
ressubstr = ','.join(rolls[0:top])
|
||||||
|
sum = 0
|
||||||
|
for r in rolls[0:top]:
|
||||||
|
sum += int(r)
|
||||||
|
sumplus = sum + modifier
|
||||||
|
result += str(sumplus) + ' [' + ressubstr
|
||||||
|
if modifier != 0:
|
||||||
|
if modifier > 0:
|
||||||
|
result += ' + ' + str(modifier)
|
||||||
|
else:
|
||||||
|
result += ' - ' + str(-1 * modifier)
|
||||||
|
result += ']'
|
||||||
|
|
||||||
|
if t != times-1:
|
||||||
|
result += ', '
|
||||||
|
|
||||||
|
reply += result
|
||||||
|
if roll is not rolls[-1]:
|
||||||
|
reply += "; "
|
||||||
|
if reply is not "":
|
||||||
|
return self.reply(connection, replypath, reply)
|
||||||
|
else:
|
||||||
|
print(what.split(' ')[0])
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user