comparison orpg/dieroller/utils.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children c54768cffbd4
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 #!/usr/bin/env python
2 # Copyright (C) 2000-2001 The OpenRPG Project
3 #
4 # openrpg-dev@lists.sourceforge.net
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 # --
20 #
21 # File: dieroller/utils.py
22 # Author: OpenRPG Team
23 # Maintainer:
24 # Version:
25 # $Id: utils.py,v 1.22 2007/05/05 05:30:10 digitalxero Exp $
26 #
27 # Description: Classes to help manage the die roller
28 #
29
30 __version__ = "$Id: utils.py,v 1.22 2007/05/05 05:30:10 digitalxero Exp $"
31
32 from die import *
33 from wod import *
34 from d20 import *
35 from hero import *
36 from shadowrun import *
37 from sr4 import *
38 from hackmaster import *
39 from wodex import *
40 from srex import *
41 from gurps import *
42 from runequest import *
43 from savage import *
44 from trinity import *
45
46 import re
47
48 rollers = ['std','wod','d20','hero','shadowrun', 'sr4','hackmaster','srex','wodex', 'gurps', 'runequest', 'sw', 'trinity']
49
50 class roller_manager:
51 def __init__(self,roller_class="d20"):
52 try:
53 self.setRoller(roller_class)
54 except:
55 self.roller_class = "std"
56
57 def setRoller(self,roller_class):
58 try:
59 rollers.index(roller_class)
60 self.roller_class = roller_class
61 except:
62 raise Exception, "Invalid die roller!"
63
64 def getRoller(self):
65 return self.roller_class
66
67 def listRollers(self):
68 return rollers
69
70 def stdDieToDClass(self,match):
71 s = match.group(0)
72 (num,sides) = s.split('d')
73
74 if sides.strip().upper() == 'F':
75 sides = "'f'"
76
77 try:
78 if int(num) > 100 or int(sides) > 10000:
79 return None
80 except:
81 pass
82
83 return "(" + num.strip() + "**" + self.roller_class + "(" + sides.strip() + "))"
84
85 # Use this to convert ndm-style (3d6) dice to d_base format
86 def convertTheDieString(self,s):
87 reg = re.compile("\d+\s*[a-zA-Z]+\s*[\dFf]+")
88 (result, num_matches) = reg.subn(self.stdDieToDClass, s)
89 if num_matches == 0 or result is None:
90 try:
91 s2 = self.roller_class + "(0)." + s
92 test = eval(s2)
93 return s2
94 except:
95 pass
96 return result
97
98
99 def proccessRoll(self,s):
100 return str(eval(self.convertTheDieString(s)))