comparison orpg/dieroller/utils.py @ 71:449a8900f9ac ornery-dev

Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
author sirebral
date Thu, 20 Aug 2009 03:00:39 -0500
parents c54768cffbd4
children bb22f0f1a7ec
comparison
equal deleted inserted replaced
70:52a5fa913008 71:449a8900f9ac
40 from srex import * 40 from srex import *
41 from gurps import * 41 from gurps import *
42 from runequest import * 42 from runequest import *
43 from savage import * 43 from savage import *
44 from trinity import * 44 from trinity import *
45 from orpg.orpgCore import component
46
47 import re 45 import re
48 46
49 rollers = ['std','wod','d20','hero','shadowrun', 'sr4','hackmaster','srex','wodex', 'gurps', 'runequest', 'sw', 'trinity'] 47 rollers = ['std','wod','d20','hero','shadowrun', 'sr4','hackmaster','srex','wodex', 'gurps', 'runequest', 'sw', 'trinity']
50 48
51 class roller_manager: 49 class roller_manager:
50
52 def __init__(self,roller_class="d20"): 51 def __init__(self,roller_class="d20"):
53 try: self.setRoller(roller_class) 52 try: self.setRoller(roller_class)
54 except: self.roller_class = "std" 53 except: self.roller_class = "std"
55 54
55
56 def setRoller(self,roller_class): 56 def setRoller(self,roller_class):
57 try: 57 try:
58 rollers.index(roller_class) 58 rollers.index(roller_class)
59 self.roller_class = roller_class 59 self.roller_class = roller_class
60 except: raise Exception, "Invalid die roller!" 60 except: raise Exception, "Invalid die roller!"
61 61
62
62 def getRoller(self): 63 def getRoller(self):
63 return self.roller_class 64 return self.roller_class
64 65
66
65 def listRollers(self): 67 def listRollers(self):
66 return rollers 68 return rollers
67 69
70
68 def stdDieToDClass(self,match): 71 def stdDieToDClass(self,match):
69 s = match.group(0) 72 s = match.group(0)
70 (num,sides) = s.split('d') 73 (num,sides) = s.split('d')
71 74
72 if sides.strip().upper() == 'F': sides = "'f'" 75 if sides.strip().upper() == 'F': sides = "'f'"
75 return None 78 return None
76 except: pass 79 except: pass
77 return "(" + num.strip() + "**" + self.roller_class + "(" + sides.strip() + "))" 80 return "(" + num.strip() + "**" + self.roller_class + "(" + sides.strip() + "))"
78 81
79 # Use this to convert ndm-style (3d6) dice to d_base format 82 # Use this to convert ndm-style (3d6) dice to d_base format
83
80 def convertTheDieString(self,s): 84 def convertTheDieString(self,s):
81 reg = re.compile("\d+\s*[a-zA-Z]+\s*[\dFf]+") 85 reg = re.compile("\d+\s*[a-zA-Z]+\s*[\dFf]+")
82 (result, num_matches) = reg.subn(self.stdDieToDClass, s) 86 (result, num_matches) = reg.subn(self.stdDieToDClass, s)
83 if num_matches == 0 or result is None: 87 if num_matches == 0 or result is None:
84 try: 88 try:
86 test = eval(s2) 90 test = eval(s2)
87 return s2 91 return s2
88 except: pass 92 except: pass
89 return result 93 return result
90 94
95
91 def proccessRoll(self,s): 96 def proccessRoll(self,s):
92 return str(eval(self.convertTheDieString(s))) 97 return str(eval(self.convertTheDieString(s)))
93 #component.add('dieroller', roller_manager()) 98
99 DiceManager = roller_manager