making dice roller a Module

This commit is contained in:
Brian S. Stephan 2010-07-25 21:41:05 -05:00
parent e8d9228b61
commit a6eb6b36d6
1 changed files with 79 additions and 74 deletions

View File

@ -184,6 +184,83 @@ class Countdown(Module):
connection.privmsg(replypath, relstr)
except NoOptionError: pass
class Dice(Module):
"""Rolls dice, for RPGs mostly
"""
def __init__(self, config, server):
super(Dice, self).__init__(config, server)
def register_handlers(self, server):
server.add_global_handler('pubmsg', self.on_pubmsg)
server.add_global_handler('privmsg', self.on_privmsg)
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
overallroll = what
rolls = re.split(';\s*', overallroll)
for roll in rolls:
pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
regex = re.compile(pattern)
matches = regex.search(roll)
if matches is not None:
# set variables including defaults for unspecified stuff
faces = int(matches.group(4))
comment = matches.group(7)
if matches.group(1) is None:
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:
modifier = int(matches.group(6))
result = ''
if comment is not None:
result += comment + ': '
for t in range(times):
ressubstr = ""
rolls = []
for d in range(dice):
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 += ', '
connection.privmsg(replypath, result)
#####
# sub_join_channel
# join a channel when told to by an admin
@ -304,77 +381,6 @@ def sub_change_nick(connection, event, nick, userhost, replypath, what, admin_un
config.set('IRC', 'nick', newnick)
connection.privmsg(replypath, 'changed nickname')
#####
# sub_dice
# roll dice, primarily for roleplaying games
#####
def sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked):
overallroll = what
rolls = re.split(';\s*', overallroll)
for roll in rolls:
pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
regex = re.compile(pattern)
matches = regex.search(roll)
if matches is not None:
# set variables including defaults for unspecified stuff
faces = int(matches.group(4))
comment = matches.group(7)
if matches.group(1) is None:
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:
modifier = int(matches.group(6))
result = ''
if comment is not None:
result += comment + ': '
for t in range(times):
ressubstr = ""
rolls = []
for d in range(dice):
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 += ', '
connection.privmsg(replypath, result)
#####
# on_connect
# handler for when the bot has connected to IRC
@ -423,7 +429,6 @@ def on_privmsg(connection, event):
# standard commands
sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked)
#####
# on_pubmsg
@ -464,7 +469,6 @@ def on_pubmsg(connection, event):
# standard commands
sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked)
#####
# init
@ -501,8 +505,9 @@ server.add_global_handler("welcome", on_connect)
server.add_global_handler('privmsg', on_privmsg)
server.add_global_handler('pubmsg', on_pubmsg)
gt = GoogleTranslate(config, server)
count = Countdown(config, server)
dice = Dice(config, server)
gt = GoogleTranslate(config, server)
# loop forever
irc.process_forever()