comparison orpg/dieroller/base.py @ 184:dcae32e219f1 beta

Traipse Beta 'OpenRPG' {100117-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Beta) New Features: Added Bookmarks Added 'boot' command to remote admin Added confirmation window for sent nodes Minor changes to allow for portability to an OpenSUSE linux OS Miniatures Layer pop up box allows users to turn off Mini labels, from FlexiRPG Zoom Mouse plugin added Images added to Plugin UI Switching to Element Tree Map efficiency, from FlexiRPG Added Status Bar to Update Manager New TrueDebug Class in orpg_log (See documentation for usage) Portable Mercurial Tip of the Day added, from Core and community New Reference Syntax added for custom PC sheets New Child Reference for gametree New Parent Reference for gametree New Gametree Recursion method, mapping, context sensitivity, and effeciency.. New Features node with bonus nodes and Node Referencing help added Dieroller structure from Core New DieRoller portability for odd Dice Added 7th Sea die roller; ie [7k3] = [7d10.takeHighest(3).open(10)] New 'Mythos' System die roller added Added new vs. die roller method for WoD; ie [3v3] = [3d10.vs(3)]. Included for Mythos roller also New Warhammer FRPG Die Roller (Special thanks to Puu-san for the support) New EZ_Tree Reference system. Push a button, Traipse the tree, get a reference (Beta!) Fixes: Fix to Text based Server Fix to Remote Admin Commands Fix to Pretty Print, from Core Fix to Splitter Nodes not being created Fix to massive amounts of images loading, from Core Fix to Map from gametree not showing to all clients Fix to gametree about menus Fix to Password Manager check on startup Fix to PC Sheets from tool nodes. They now use the tabber_panel Fix to Whiteboard ID to prevent random line or text deleting. Fixes to Server, Remote Server, and Server GUI Fix to Update Manager; cleaner clode for saved repositories Fixes made to Settings Panel and now reactive settings when Ok is pressed Fixes to Alternity roller's attack roll. Uses a simple Tuple instead of a Splice Fix to Use panel of Forms and Tabbers. Now longer enters design mode Fix made Image Fetching. New fetching image and new failed image Modified ID's to prevent non updated clients from ruining the fix. default_manifest.xml renamed to default_upmana.xml
author sirebral
date Sun, 17 Jan 2010 21:37:34 -0600
parents 0d9b746b5751
children 13054be69834
comparison
equal deleted inserted replaced
183:0d9b746b5751 184:dcae32e219f1
20 # 20 #
21 # File: die.py 21 # File: die.py
22 # Author: Andrew Bennett 22 # Author: Andrew Bennett
23 # Maintainer: 23 # Maintainer:
24 # Version: 24 # Version:
25 # $Id: die.py,v 1.13 2007/03/13 17:53:42 digitalxero Exp $ 25 # $Id: die.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
26 # 26 #
27 # Description: This class is used to make working with dice easier 27 # Description: This class is used to make working with dice easier
28 # 28 #
29 29
30 __version__ = "$Id: die.py,v 1.13 2007/03/13 17:53:42 digitalxero Exp $" 30 __version__ = "$Id: die.py,v Traipse 'Ornery-Orc' prof.ebral Exp Exp $"
31
32 31
33 import random 32 import random
34 import UserList 33 import UserList
35 import copy 34 import copy
36 #import string
37 35
38 class die_base(UserList.UserList): 36 class die_base(UserList.UserList):
39 name = None 37 name = None
40 38
41 def __init__(self,source = []): 39 def __init__(self,source = []):
42 if isinstance(source, (int, float, basestring)): 40 if isinstance(source, (int, float, basestring)):
43 s = [] 41 s = []
44 s.append(di(source)) 42 s.append(di(source))
45 else: 43 else: s = source
46 s = source
47 UserList.UserList.__init__(self,s) 44 UserList.UserList.__init__(self,s)
48
49 45
50 def sum(self): 46 def sum(self):
51 s = 0 47 s = 0
52 for a in self.data: 48 for a in self.data:
53 s += int(a) 49 s += int(a)
56 def __lshift__(self,other): 52 def __lshift__(self,other):
57 if type(other) == type(3) or type(other) == type(3.0): 53 if type(other) == type(3) or type(other) == type(3.0):
58 o = other 54 o = other
59 elif hasattr(other,"sum"): 55 elif hasattr(other,"sum"):
60 o = other.sum() 56 o = other.sum()
61 else: 57 else: return None
62 return None
63
64 result = [] 58 result = []
65 for die in self: 59 for die in self:
66 if die < o: 60 if die < o: result.append(die)
67 result.append(die)
68 return self.__class__(result) 61 return self.__class__(result)
69 62
70 def __rshift__(self,other): 63 def __rshift__(self,other):
71 64 if type(other) == type(3) or type(other) == type(3.0): o = other
72 if type(other) == type(3) or type(other) == type(3.0): 65 elif hasattr(other,"sum"): o = other.sum()
73 o = other 66 else: return None
74 elif hasattr(other,"sum"):
75 o = other.sum()
76 else:
77 return None
78
79 result = [] 67 result = []
80 for die in self: 68 for die in self:
81 if die > o: 69 if die > o: result.append(die)
82 result.append(die)
83 return self.__class__(result) 70 return self.__class__(result)
84 71
85 def __rlshift__(self,other): 72 def __rlshift__(self,other):
86 return self.__rshift__(other) 73 return self.__rshift__(other)
87 74
88 def __rrshift__(self,other): 75 def __rrshift__(self,other):
89 return self.__lshift__(other) 76 return self.__lshift__(other)
90
91 77
92 def __str__(self): 78 def __str__(self):
93 if len(self.data) > 0: 79 if len(self.data) > 0:
94 myStr = "[" + str(self.data[0]) 80 myStr = "[" + str(self.data[0])
95 for a in self.data[1:]: 81 for a in self.data[1:]:
96 myStr += "," 82 myStr += ","
97 myStr += str(a) 83 myStr += str(a)
98 myStr += "] = (" + str(self.sum()) + ")" 84 myStr += "] = (" + str(self.sum()) + ")"
99 else: 85 else: myStr = "[] = (0)"
100 myStr = "[] = (0)"
101 return myStr 86 return myStr
102 87
103 def __lt__(self,other): 88 def __lt__(self,other):
104 if type(other) == type(3) or type(other) == type(3.0): 89 if type(other) == type(3) or type(other) == type(3.0):
105 return (self.sum() < other) 90 return (self.sum() < other)
106 elif hasattr(other,"sum"): 91 elif hasattr(other,"sum"): return (self.sum() < other.sum())
107 return (self.sum() < other.sum()) 92 else: return UserList.UserList.__lt__(self,other)
108 else:
109 return UserList.UserList.__lt__(self,other)
110 93
111 def __le__(self,other): 94 def __le__(self,other):
112 if type(other) == type(3) or type(other) == type(3.0): 95 if type(other) == type(3) or type(other) == type(3.0):
113 return (self.sum() <= other) 96 return (self.sum() <= other)
114 elif hasattr(other,"sum"): 97 elif hasattr(other,"sum"): return (self.sum() <= other.sum())
115 return (self.sum() <= other.sum()) 98 else: return UserList.UserList.__le__(self,other)
116 else:
117 return UserList.UserList.__le__(self,other)
118 99
119 def __eq__(self,other): 100 def __eq__(self,other):
120 if type(other) == type(3) or type(other) == type(3.0): 101 if type(other) == type(3) or type(other) == type(3.0):
121 return (self.sum() == other) 102 return (self.sum() == other)
122 elif hasattr(other,"sum"): 103 elif hasattr(other,"sum"): return (self.sum() == other.sum())
123 return (self.sum() == other.sum()) 104 else: return UserList.UserList.__eq__(self,other)
124 else:
125 return UserList.UserList.__eq__(self,other)
126 105
127 def __ne__(self,other): 106 def __ne__(self,other):
128 if type(other) == type(3) or type(other) == type(3.0): 107 if type(other) == type(3) or type(other) == type(3.0): return (self.sum() != other)
129 return (self.sum() != other) 108 elif hasattr(other,"sum"): return (self.sum() != other.sum())
130 elif hasattr(other,"sum"): 109 else: return UserList.UserList.__ne__(self,other)
131 return (self.sum() != other.sum())
132 else:
133 return UserList.UserList.__ne__(self,other)
134 110
135 def __gt__(self,other): 111 def __gt__(self,other):
136 if type(other) == type(3) or type(other) == type(3.0): 112 if type(other) == type(3) or type(other) == type(3.0): return (self.sum() > other)
137 return (self.sum() > other) 113 elif hasattr(other,"sum"): return (self.sum() > other.sum())
138 elif hasattr(other,"sum"): 114 else: return UserList.UserList.__gt__(self,other)
139 return (self.sum() > other.sum())
140 else:
141 return UserList.UserList.__gt__(self,other)
142 115
143 def __ge__(self,other): 116 def __ge__(self,other):
144 if type(other) == type(3) or type(other) == type(3.0): 117 if type(other) == type(3) or type(other) == type(3.0): return (self.sum() >= other)
145 return (self.sum() >= other) 118 elif hasattr(other,"sum"): return (self.sum() >= other.sum())
146 elif hasattr(other,"sum"): 119 else: return UserList.UserList.__ge__(self,other)
147 return (self.sum() >= other.sum())
148 else:
149 return UserList.UserList.__ge__(self,other)
150 120
151 def __cmp__(self,other): 121 def __cmp__(self,other):
152 # this function included for backwards compatibility 122 # this function included for backwards compatibility
153 # As of 2.1, lists implement the "rich comparison" 123 # As of 2.1, lists implement the "rich comparison"
154 # methods overloaded above. 124 # methods overloaded above.
155 if type(other) == type(3) or type(other) == type(3.0): 125 if type(other) == type(3) or type(other) == type(3.0): return cmp(self.sum(), other)
156 return cmp(self.sum(), other) 126 elif hasattr(other,"sum"): return cmp(self.sum(), other.sum())
157 elif hasattr(other,"sum"): 127 else: return UserList.UserList.__cmp__(self,other)
158 return cmp(self.sum(), other.sum())
159 else:
160 return UserList.UserList.__cmp__(self,other)
161 128
162 129
163 def __rcmp__(self,other): 130 def __rcmp__(self,other):
164 return self.__cmp__(other) 131 return self.__cmp__(other)
165 132
166 def __add__(self,other): 133 def __add__(self,other):
167 mycopy = copy.deepcopy(self) 134 mycopy = copy.deepcopy(self)
168 if type(other) == type(3) or type(other) == type(3.0): 135 if type(other) == type(3) or type(other) == type(3.0):
169 #if other < 0:
170 # return self.__sub__(-other)
171 #other = [di(other,other)]
172 other = [static_di(other)] 136 other = [static_di(other)]
173 #return self.sum() + other 137 elif type(other) == type("test"): return self
174
175 elif type(other) == type("test"):
176 return self
177 mycopy.extend(other) 138 mycopy.extend(other)
178 #result = UserList.UserList.__add__(mycopy,other)
179 return mycopy 139 return mycopy
180 140
181 def __iadd__(self,other): 141 def __iadd__(self,other):
182 return self.__add__(other) 142 return self.__add__(other)
183 143
195 155
196 def __sub__(self,other): 156 def __sub__(self,other):
197 mycopy = copy.deepcopy(self) 157 mycopy = copy.deepcopy(self)
198 if type(other) == type(3) or type(other) == type(3.0): 158 if type(other) == type(3) or type(other) == type(3.0):
199 neg_die = static_di(-other) 159 neg_die = static_di(-other)
200 #neg_die.set_value(-other)
201 other = [neg_die] 160 other = [neg_die]
202 #return self.sum() - other 161 else: other = -other
203 else:
204 other = -other
205 mycopy.extend(other) 162 mycopy.extend(other)
206 return mycopy 163 return mycopy
207 164
208 def __rsub__(self,other): 165 def __rsub__(self,other):
209 mycopy = -copy.deepcopy(self) 166 mycopy = -copy.deepcopy(self)
210 #print type(other)
211 if type(other) == type(3) or type(other) == type(3.0): 167 if type(other) == type(3) or type(other) == type(3.0):
212 new_die = di(0) 168 new_die = di(0)
213 new_die.set_value(other) 169 new_die.set_value(other)
214 other = new_die 170 other = new_die
215 mycopy.insert(0,other) 171 mycopy.insert(0,other)
217 173
218 def __isub__(self,other): 174 def __isub__(self,other):
219 return self.__sub__(other) 175 return self.__sub__(other)
220 176
221 def __mul__(self,other): 177 def __mul__(self,other):
222 if type(other) == type(3) or type(other) == type(3.0): 178 if type(other) == type(3) or type(other) == type(3.0): return self.sum() * other
223 return self.sum() * other 179 elif hasattr(other,"sum"): return other.sum() * self.sum()
224 elif hasattr(other,"sum"): 180 else: return UserList.UserList.__mul__(self,other)
225 return other.sum() * self.sum()
226 else:
227 return UserList.UserList.__mul__(self,other)
228 181
229 def __rmul__(self,other): 182 def __rmul__(self,other):
230 return self.__mul__(other) 183 return self.__mul__(other)
231 184
232 def __div__(self,other): 185 def __div__(self,other):
233 if type(other) == type(3) or type(other) == type(3.0): 186 if type(other) == type(3) or type(other) == type(3.0): return float(self.sum()) / other
234 return float(self.sum()) / other 187 elif hasattr(other,"sum"): return float(self.sum()) / other.sum()
235 elif hasattr(other,"sum"): 188 else: return UserList.UserList.__div__(self,other)
236 return float(self.sum()) / other.sum()
237 else:
238 return UserList.UserList.__div__(self,other)
239 189
240 def __rdiv__(self,other): 190 def __rdiv__(self,other):
241 if type(other) == type(3) or type(other) == type(3.0): 191 if type(other) == type(3) or type(other) == type(3.0): return other / float(self.sum())
242 return other / float(self.sum()) 192 elif hasattr(other,"sum"): return other.sum() / float(self.sum())
243 elif hasattr(other,"sum"): 193 else: return UserList.UserList.__rdiv__(self,other)
244 return other.sum() / float(self.sum())
245 else:
246 return UserList.UserList.__rdiv__(self,other)
247 194
248 def __mod__(self,other): 195 def __mod__(self,other):
249 if type(other) == type(3) or type(other) == type(3.0): 196 if type(other) == type(3) or type(other) == type(3.0): return self.sum()%other
250 return self.sum()%other 197 elif hasattr(other,"sum"): return self.sum() % other.sum()
251 elif hasattr(other,"sum"): 198 else: return UserList.UserList.__mod__(self,other)
252 return self.sum() % other.sum()
253 else:
254 return UserList.UserList.__mod__(self,other)
255 199
256 def __rmod__(self,other): 200 def __rmod__(self,other):
257 if type(other) == type(3) or type(other) == type(3.0): 201 if type(other) == type(3) or type(other) == type(3.0): return other % self.sum()
258 return other % self.sum() 202 elif hasattr(other,"sum"): return other.sum() % self.sum()
259 elif hasattr(other,"sum"): 203 else: return UserList.UserList.__rmod__(self,other)
260 return other.sum() % self.sum()
261 else:
262 return UserList.UserList.__rmod__(self,other)
263 204
264 def __neg__(self): 205 def __neg__(self):
265 for i in range(len(self.data)): 206 for i in range(len(self.data)): self.data[i] = -self.data[i]
266 self.data[i] = -self.data[i]
267 return self 207 return self
268 208
269 def __pos__(self): 209 def __pos__(self):
270 for i in range(len(self.data)): 210 for i in range(len(self.data)): self.data[i] = +self.data[i]
271 self.data[i] = +self.data[i]
272 return self 211 return self
273 212
274 def __abs__(self): 213 def __abs__(self):
275 for i in range(len(self.data)): 214 for i in range(len(self.data)): self.data[i] = abs(self.data[i])
276 self.data[i] = abs(self.data[i]) 215 return self
277 return self
278 #return abs(self.sum())
279 216
280 def __pow__(self,other): 217 def __pow__(self,other):
281 if type(other) == type(3) or type(other) == type(3.0): 218 if type(other) == type(3) or type(other) == type(3.0): return self.sum() ** other
282 return self.sum() ** other 219 elif hasattr(other,"sum"): return self.sum() ** other.sum()
283 elif hasattr(other,"sum"): 220 else: return UserList.UserList.__pow__(self,other)
284 return self.sum() ** other.sum()
285 else:
286 return UserList.UserList.__pow__(self,other)
287
288 221
289 def __rpow__(self,other): 222 def __rpow__(self,other):
290 # We're overloading exponentiation of ints to create "other" number of dice 223 # We're overloading exponentiation of ints to create "other" number of dice
291
292 if other >= 1: 224 if other >= 1:
293 result = self.__class__(self[0].sides) 225 result = self.__class__(self[0].sides)
294 for t in range(other-1): 226 for t in range(other-1): result+=self.__class__(self[0].sides)
295 result+=self.__class__(self[0].sides) 227 else: result = None
296 else:
297 result = None
298
299 return result 228 return result
300 229
301 ### di class to handle actual dice 230 ### di class to handle actual dice
302 231
303 class di: 232 class di:
307 self.value = None 236 self.value = None
308 self.target = None 237 self.target = None
309 self.roll(min) 238 self.roll(min)
310 239
311 def __str__(self): 240 def __str__(self):
312 if len(self.history) > 1: 241 if len(self.history) > 1: return str(self.history)
313 return str(self.history) 242 else: return str(self.value)
314 else:
315 return str(self.value)
316 243
317 def __neg__(self): 244 def __neg__(self):
318 self.value = -self.value 245 self.value = -self.value
319 for i in range(len(self.history)): 246 for i in range(len(self.history)): self.history[i] = -self.history[i]
320 self.history[i] = -self.history[i]
321 return self 247 return self
322 248
323 def __pos__(self): 249 def __pos__(self):
324 self.value = +self.value 250 self.value = +self.value
325 for i in range(len(self.history)): 251 for i in range(len(self.history)): self.history[i] = +self.history[i]
326 self.history[i] = +self.history[i]
327 return self 252 return self
328 253
329 def __abs__(self): 254 def __abs__(self):
330 self.value = abs(self.value) 255 self.value = abs(self.value)
331 for i in range(len(self.history)): 256 for i in range(len(self.history)):
332 self.history[i] = abs(self.history[i]) 257 self.history[i] = abs(self.history[i])
333 return self 258 return self
334 259
335 def __repr__(self): 260 def __repr__(self):
336 if len(self.history) > 1: 261 if len(self.history) > 1: return str(self.history)
337 return str(self.history) 262 else: return str(self.value)
338 else:
339 return str(self.value)
340 263
341 def __int__(self): 264 def __int__(self):
342 return self.value 265 return self.value
343 266
344 267
345 def __lt__(self,other): 268 def __lt__(self,other):
346 if type(other) == type(3) or type(other) == type(3.0): 269 if type(other) == type(3) or type(other) == type(3.0): return self.value < other
347 return self.value < other 270 elif hasattr(other,"value"): return self.value < other.value
348 elif hasattr(other,"value"): 271 else: return self < other
349 return self.value < other.value
350 else:
351 return self < other
352 272
353 def __le__(self,other): 273 def __le__(self,other):
354 if type(other) == type(3) or type(other) == type(3.0): 274 if type(other) == type(3) or type(other) == type(3.0): return self.value <= other
355 return self.value <= other 275 elif hasattr(other,"value"): return self.value <= other.value
356 elif hasattr(other,"value"): 276 else: return self <= other
357 return self.value <= other.value
358 else:
359 return self <= other
360 277
361 def __eq__(self,other): 278 def __eq__(self,other):
362 if type(other) == type(3) or type(other) == type(3.0): 279 if type(other) == type(3) or type(other) == type(3.0): return self.value == other
363 return self.value == other 280 elif hasattr(other,"value"): return self.value == other.value
364 elif hasattr(other,"value"): 281 else: return self == other
365 return self.value == other.value
366 else:
367 return self == other
368 282
369 def __ne__(self,other): 283 def __ne__(self,other):
370 if type(other) == type(3) or type(other) == type(3.0): 284 if type(other) == type(3) or type(other) == type(3.0): return self.value != other
371 return self.value != other 285 elif hasattr(other,"value"): return self.value != other.value
372 elif hasattr(other,"value"): 286 else: return self != other
373 return self.value != other.value
374 else:
375 return self != other
376 287
377 def __gt__(self,other): 288 def __gt__(self,other):
378 if type(other) == type(3) or type(other) == type(3.0): 289 if type(other) == type(3) or type(other) == type(3.0): return self.value > other
379 return self.value > other 290 elif hasattr(other,"value"): return self.value > other.value
380 elif hasattr(other,"value"): 291 else: return self > other
381 return self.value > other.value
382 else:
383 return self > other
384 292
385 def __ge__(self,other): 293 def __ge__(self,other):
386 if type(other) == type(3) or type(other) == type(3.0): 294 if type(other) == type(3) or type(other) == type(3.0): return self.value >= other
387 return self.value >= other 295 elif hasattr(other,"value"): return self.value >= other.value
388 elif hasattr(other,"value"): 296 else: return self >= other
389 return self.value >= other.value
390 else:
391 return self >= other
392 297
393 def __cmp__(self,other): 298 def __cmp__(self,other):
394 # this function included for backwards compatibility 299 # this function included for backwards compatibility
395 # As of 2.1, lists implement the "rich comparison" 300 # As of 2.1, lists implement the "rich comparison"
396 # methods overloaded above. 301 # methods overloaded above.
397 if type(other) == type(3) or type(other) == type(3.0): 302 if type(other) == type(3) or type(other) == type(3.0): return cmp(self.value, other)
398 return cmp(self.value, other) 303 elif hasattr(other,"value"): return cmp(self.value, other.value)
399 elif hasattr(other,"value"): 304 else: return cmp(self,other)
400 return cmp(self.value, other.value)
401 else:
402 return cmp(self,other)
403 305
404 def roll(self,min=1): 306 def roll(self,min=1):
405 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': 307 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': self.value = random.randint(-1, 1)
406 self.value = random.randint(-1, 1) 308 else: self.value = int(random.uniform(min, self.sides+1))
407 else:
408 #self.value = random.randint(min, self.sides)
409 self.value = int(random.uniform(min, self.sides+1))
410 self.history = [] 309 self.history = []
411 self.history.append(self.value) 310 self.history.append(self.value)
412 311
413 def extraroll(self): 312 def extraroll(self):
414 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': 313 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': result = random.randint(-1, 1)
415 result = random.randint(-1, 1) 314 else: result = int(random.uniform(1,self.sides+1))
416 else:
417 #result = random.randint(1, self.sides)
418 result = int(random.uniform(1,self.sides+1))
419
420 self.value += result 315 self.value += result
421 self.history.append(result) 316 self.history.append(result)
422 317
423 def lastroll(self): 318 def lastroll(self):
424 return self.history[len(self.history)-1] 319 return self.history[len(self.history)-1]
442 337
443 class DieRollers(object): 338 class DieRollers(object):
444 _rollers = {} 339 _rollers = {}
445 def __new__(cls): 340 def __new__(cls):
446 it = cls.__dict__.get("__it__") 341 it = cls.__dict__.get("__it__")
447 if it is not None: 342 if it is not None: return it
448 return it
449 cls.__it__ = it = object.__new__(cls) 343 cls.__it__ = it = object.__new__(cls)
450 return it 344 return it
451 345
452 def keys(self): 346 def keys(self):
453 return self._rollers.keys() 347 return self._rollers.keys()