First set of updates from the major "backendification" rewrite #1
| @ -1,6 +1,5 @@ | ||||
| """Roll dice when asked, intended for RPGs.""" | ||||
| import logging | ||||
| import math | ||||
| import re | ||||
| import random | ||||
| 
 | ||||
| @ -15,18 +14,17 @@ logger = logging.getLogger(__name__) | ||||
| 
 | ||||
| 
 | ||||
| class Dice(Plugin): | ||||
| 
 | ||||
|     """Roll simple or complex dice strings.""" | ||||
| 
 | ||||
|     def __init__(self): | ||||
|         """Set up the plugin.""" | ||||
|         super(Dice, self).__init__() | ||||
|         self.roller = DiceRoller() | ||||
| 
 | ||||
|     def start(self): | ||||
|         """Set up the handlers.""" | ||||
| 
 | ||||
|         self.roller = DiceRoller() | ||||
| 
 | ||||
|         self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!roll\s+(.*)$', | ||||
|                                                          self.handle_roll, -20) | ||||
|         self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!ctech\s+(.*)$', | ||||
|                                                          self.handle_ctech, -20) | ||||
|         self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!random\s+(.*)$', | ||||
|                                                          self.handle_random, -20) | ||||
| 
 | ||||
| @ -34,9 +32,7 @@ class Dice(Plugin): | ||||
| 
 | ||||
|     def stop(self): | ||||
|         """Tear down handlers.""" | ||||
| 
 | ||||
|         self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_roll) | ||||
|         self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_ctech) | ||||
|         self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_random) | ||||
| 
 | ||||
|         super(Dice, self).stop() | ||||
| @ -70,109 +66,6 @@ class Dice(Plugin): | ||||
|             reply = "{0:s}: {1:s}".format(nick, self.roller.do_roll(dicestr)) | ||||
|         return self.bot.reply(event, re.sub(r'(\d+)(.*?\s+)(\(.*?\))', r'\1\214\3', reply)) | ||||
| 
 | ||||
|     def handle_ctech(self, connection, event, match): | ||||
|         """Handle cthulhutech dice rolls.""" | ||||
| 
 | ||||
|         nick = NickMask(event.source).nick | ||||
|         rollitrs = re.split(';\s*', match.group(1)) | ||||
|         reply = "" | ||||
|         for count, roll in enumerate(rollitrs): | ||||
|             pattern = '^(\d+)d(?:(\+|\-)(\d+))?(?:\s+(.*))?' | ||||
|             regex = re.compile(pattern) | ||||
|             matches = regex.search(roll) | ||||
|             if matches is not None: | ||||
|                 dice = int(matches.group(1)) | ||||
|                 modifier = 0 | ||||
| 
 | ||||
|                 if matches.group(2) is not None and matches.group(3) is not None: | ||||
|                     if str(matches.group(2)) == '-': | ||||
|                         modifier = -1 * int(matches.group(3)) | ||||
|                     else: | ||||
|                         modifier = int(matches.group(3)) | ||||
| 
 | ||||
|                 result = roll + ': ' | ||||
| 
 | ||||
|                 rolls = [] | ||||
|                 for d in range(dice): | ||||
|                     rolls.append(random.randint(1, 10)) | ||||
|                 rolls.sort() | ||||
|                 rolls.reverse() | ||||
| 
 | ||||
|                 # highest single die method | ||||
|                 method1 = rolls[0] | ||||
| 
 | ||||
|                 # highest set method | ||||
|                 method2 = 0 | ||||
|                 rolling_sum = 0 | ||||
|                 for i, r in enumerate(rolls): | ||||
|                     # if next roll is same as current, sum and continue, else see if sum is best so far | ||||
|                     if i+1 < len(rolls) and rolls[i+1] == r: | ||||
|                         if rolling_sum == 0: | ||||
|                             rolling_sum = r | ||||
|                         rolling_sum += r | ||||
|                     else: | ||||
|                         if rolling_sum > method2: | ||||
|                             method2 = rolling_sum | ||||
|                         rolling_sum = 0 | ||||
|                 # check for set in progress (e.g. lots of 1s) | ||||
|                 if rolling_sum > method2: | ||||
|                     method2 = rolling_sum | ||||
| 
 | ||||
|                 # straight method | ||||
|                 method3 = 0 | ||||
|                 rolling_sum = 0 | ||||
|                 count = 0 | ||||
|                 for i, r in enumerate(rolls): | ||||
|                     # if next roll is one less as current, sum and continue, else check len and see if sum is best so far | ||||
|                     if i+1 < len(rolls) and rolls[i+1] == r-1: | ||||
|                         if rolling_sum == 0: | ||||
|                             rolling_sum = r | ||||
|                             count += 1 | ||||
|                         rolling_sum += r-1 | ||||
|                         count += 1 | ||||
|                     else: | ||||
|                         if count >= 3 and rolling_sum > method3: | ||||
|                             method3 = rolling_sum | ||||
|                         rolling_sum = 0 | ||||
|                 # check for straight in progress (e.g. straight ending in 1) | ||||
|                 if count >= 3 and rolling_sum > method3: | ||||
|                     method3 = rolling_sum | ||||
| 
 | ||||
|                 # get best roll | ||||
|                 best = max([method1, method2, method3]) | ||||
| 
 | ||||
|                 # check for critical failure | ||||
|                 botch = False | ||||
|                 ones = 0 | ||||
|                 for r in rolls: | ||||
|                     if r == 1: | ||||
|                         ones += 1 | ||||
|                 if ones >= math.ceil(float(len(rolls))/2): | ||||
|                     botch = True | ||||
| 
 | ||||
|                 if botch: | ||||
|                     result += 'BOTCH' | ||||
|                 else: | ||||
|                     result += str(best + modifier) | ||||
|                 rollres = '' | ||||
|                 for i,r in enumerate(rolls): | ||||
|                     rollres += str(r) | ||||
|                     if i is not len(rolls)-1: | ||||
|                         rollres += ',' | ||||
|                 result += ' [' + rollres | ||||
|                 if modifier != 0: | ||||
|                     if modifier > 0: | ||||
|                         result += ' +' + str(modifier) | ||||
|                     else: | ||||
|                         result += ' -' + str(modifier * -1) | ||||
|                 result += ']' | ||||
| 
 | ||||
|                 reply += result | ||||
|                 if count is not len(rollitrs)-1: | ||||
|                     reply += "; " | ||||
|         if reply is not "": | ||||
|             msg = "{0:s}: {1:s}".format(nick, reply) | ||||
|             return self.bot.reply(event, msg) | ||||
| 
 | ||||
| 
 | ||||
| class DiceRoller(object): | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user