212
|
1 #!/usr/bin/env python
|
|
2 # Copyright (C) 2000-2010 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: InterParse.py
|
|
22 # Author:
|
|
23 # Maintainer: Tyler Starke (Traipse)
|
|
24 # Version:
|
|
25 # $Id: InterParse.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
|
|
26 #
|
|
27 # Description: InterParse = Interpretor Parser. This class parses all of the node referencing.
|
|
28 #
|
|
29
|
191
|
30 from orpg.orpgCore import component
|
|
31 import re
|
|
32 from orpg.tools.orpg_log import logger
|
194
|
33 from wx import TextEntryDialog, ID_OK
|
212
|
34 from xml.etree.ElementTree import iselement
|
191
|
35
|
|
36 class InterParse():
|
|
37
|
|
38 def __init__(self):
|
|
39 pass
|
|
40
|
212
|
41 def Post(self, s, tab=False, send=False, myself=False):
|
|
42 if not tab: tab = component.get('chat')
|
|
43 s = self.Normalize(s, tab)
|
|
44 tab.set_colors()
|
|
45 tab.Post(s, send, myself)
|
191
|
46
|
212
|
47 def ParseLogic(self, s, node):
|
|
48 'Nodes now parse through ParsLogic. Easily add new parse rules right here!!'
|
|
49 s = self.NameSpaceE(s)
|
|
50 s = self.NameSpaceI(s, node)
|
|
51 s = self.NodeMap(s, node)
|
|
52 s = self.NodeParent(s, node.get('map'))
|
|
53 return s
|
|
54
|
|
55 def Normalize(self, s, tab):
|
|
56 for plugin_fname in tab.activeplugins.keys():
|
|
57 plugin = tab.activeplugins[plugin_fname]
|
191
|
58 try: s = plugin.pre_parse(s)
|
|
59 except Exception, e:
|
|
60 if str(e) != "'module' object has no attribute 'post_msg'":
|
212
|
61 #logger.general(traceback.format_exc())
|
191
|
62 logger.general("EXCEPTION: " + str(e))
|
212
|
63 if tab.parsed == 0:
|
|
64 s = self.NameSpaceE(s)
|
191
|
65 s = self.Node(s)
|
|
66 s = self.Dice(s)
|
212
|
67 s = self.Filter(s, tab)
|
|
68 tab.parsed = 1
|
191
|
69 return s
|
|
70
|
212
|
71 def Filter(self, s, tab):
|
|
72 s = tab.GetFilteredText(s)
|
191
|
73 return s
|
|
74
|
|
75 def Node(self, s):
|
|
76 """Parses player input for embedded nodes rolls"""
|
|
77 cur_loc = 0
|
|
78 #[a-zA-Z0-9 _\-\.]
|
|
79 reg = re.compile("(!@(.*?)@!)")
|
|
80 matches = reg.findall(s)
|
|
81 for i in xrange(0,len(matches)):
|
|
82 newstr = self.Node(self.resolve_nodes(matches[i][1]))
|
|
83 s = s.replace(matches[i][0], newstr, 1)
|
|
84 return s
|
|
85
|
|
86 def Dice(self, s):
|
|
87 """Parses player input for embedded dice rolls"""
|
|
88 reg = re.compile("\[([^]]*?)\]")
|
|
89 matches = reg.findall(s)
|
|
90 for i in xrange(0,len(matches)):
|
|
91 newstr = self.Unknown(matches[i])
|
|
92 qmode = 0
|
|
93 newstr1 = newstr
|
|
94 if newstr[0].lower() == 'q':
|
|
95 newstr = newstr[1:]
|
|
96 qmode = 1
|
|
97 if newstr[0].lower() == '#':
|
|
98 newstr = newstr[1:]
|
|
99 qmode = 2
|
|
100 try: newstr = component.get('DiceManager').proccessRoll(newstr)
|
|
101 except: pass
|
|
102 if qmode == 1:
|
|
103 s = s.replace("[" + matches[i] + "]",
|
|
104 "<!-- Official Roll [" + newstr1 + "] => " + newstr + "-->" + newstr, 1)
|
|
105 elif qmode == 2:
|
|
106 s = s.replace("[" + matches[i] + "]", newstr[len(newstr)-2:-1], 1)
|
|
107 else: s = s.replace("[" + matches[i] + "]",
|
|
108 "[" + newstr1 + "<!-- Official Roll -->] => " + newstr, 1)
|
|
109 return s
|
|
110
|
|
111 def Unknown(self, s):
|
|
112 # Uses a tuple. Usage: ?Label}dY. If no Label is assigned then use ?}DY
|
|
113 newstr = "0"
|
|
114 reg = re.compile("(\?\{*)([a-zA-Z ]*)(\}*)")
|
|
115 matches = reg.findall(s)
|
|
116 for i in xrange(0,len(matches)):
|
|
117 lb = "Replace '?' with: "
|
|
118 if len(matches[i][0]):
|
|
119 lb = matches[i][1] + "?: "
|
212
|
120 dlg = TextEntryDialog(component.get('chat'), lb, "Missing Value?")
|
191
|
121 dlg.SetValue('')
|
|
122 if matches[i][0] != '':
|
|
123 dlg.SetTitle("Enter Value for " + matches[i][1])
|
194
|
124 if dlg.ShowModal() == ID_OK: newstr = dlg.GetValue()
|
191
|
125 if newstr == '': newstr = '0'
|
|
126 s = s.replace(matches[i][0], newstr, 1).replace(matches[i][1], '', 1).replace(matches[i][2], '', 1)
|
|
127 dlg.Destroy()
|
|
128 return s
|
|
129
|
212
|
130 def LocationCheck(self, node, tree_map, new_map, find):
|
|
131 if node == 'Invalid Reference!': return node
|
|
132 namespace = node.getiterator('nodehandler'); tr = tree_map.split('::')
|
|
133 newstr = ''
|
|
134 for name in namespace:
|
|
135 try: t = new_map.index(name.get('name'))-1
|
|
136 except: t = 0
|
|
137 if find[0] == name.get('name'):
|
|
138 s = '::'.join(new_map[:len(tr)-t])+'::'+'::'.join(find)
|
|
139 newstr = self.NameSpaceE('!&' +s+ '&!')
|
|
140 break
|
|
141 if newstr != '': return newstr
|
|
142 else:
|
|
143 del new_map[len(new_map)-1]
|
|
144 node = self.get_node(new_map)
|
|
145 newstr = self.LocationCheck(node, tree_map, new_map, find)
|
|
146 return newstr
|
|
147
|
|
148 def FutureCheck(self, node, next):
|
|
149 future = node.getiterator('nodehandler')
|
|
150 for advance in future:
|
|
151 if next == advance.get('name'): return True
|
|
152 return False
|
|
153
|
|
154 def NameSpaceI(self, s, node):
|
222
|
155 reg1 = re.compile('(!"(.*?)"!)') ## Easter Egg!
|
|
156 """If you found this you found my first easter egg. I was tired of people telling me multiple
|
|
157 references syntax for the game tree is confusing, so I dropped this in there without telling
|
|
158 anyone. Using !" :: "! will allow you to use an internal namespace from within another internal
|
|
159 namespace -- TaS, Prof. Ebral"""
|
|
160 reg2 = re.compile("(!=(.*?)=!)")
|
|
161 matches = reg1.findall(s) + reg2.findall(s)
|
212
|
162 tree_map = node.get('map')
|
|
163 for i in xrange(0,len(matches)):
|
|
164 ## Build the new tree_map
|
|
165 new_map = tree_map.split('::')
|
|
166 find = matches[i][1].split('::')
|
|
167 ## Backwards Reference the Parent Children
|
|
168 node = self.get_node(new_map)
|
|
169 newstr = self.LocationCheck(node, tree_map, new_map, find)
|
|
170 s = s.replace(matches[i][0], newstr, 1)
|
222
|
171 s = self.ParseLogic(s, node)
|
212
|
172 return s
|
|
173
|
|
174 def NameSpaceE(self, s):
|
|
175 reg = re.compile("(!&(.*?)&!)")
|
|
176 matches = reg.findall(s)
|
|
177 newstr = False
|
|
178 nodeable = ['rpg_grid_handler', 'container_handler',
|
|
179 'group_handler', 'tabber_handler',
|
|
180 'splitter_handler', 'form_handler', 'textctrl_handler']
|
|
181 for i in xrange(0,len(matches)):
|
|
182 find = matches[i][1].split('::')
|
|
183 node = component.get('tree').xml_root
|
|
184 if not iselement(node):
|
|
185 s = s.replace(matches[i][0], 'Invalid Reference!', 1);
|
|
186 s = self.NameSpaceE(s)
|
|
187 return s
|
|
188 for x in xrange(0, len(find)):
|
|
189 namespace = node.getiterator('nodehandler')
|
|
190 for node in namespace:
|
|
191 if find[x] == node.get('name'):
|
|
192 if node.get('class') not in nodeable: continue
|
|
193 if node.get('class') == 'rpg_grid_handler':
|
|
194 try: newstr = self.NameSpaceGrid(find[x+1], node); break
|
|
195 except: newstr = 'Invalid Grid Reference!'
|
|
196 try:
|
|
197 if self.FutureCheck(node, find[x+1]): break
|
|
198 else: continue
|
|
199 except:
|
|
200 if x == len(find)-1:
|
|
201 if node.find('text') != None: newstr = str(node.find('text').text)
|
|
202 else: newstr = 'Invalid Reference!'
|
|
203 break
|
|
204 else: break
|
|
205 if not newstr: newstr = 'Invalid Reference!'
|
|
206 s = s.replace(matches[i][0], newstr, 1)
|
|
207 s = self.ParseLogic(s, node)
|
|
208 return s
|
|
209
|
|
210 def NameSpaceGrid(self, s, node):
|
|
211 cell = tuple(s.strip('(').strip(')').split(','))
|
|
212 grid = node.find('grid')
|
|
213 rows = grid.findall('row')
|
|
214 try:
|
|
215 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
|
216 s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
|
217 except: s = 'Invalid Grid Reference!'
|
|
218 return s
|
|
219
|
191
|
220 def NodeMap(self, s, node):
|
|
221 """Parses player input for embedded nodes rolls"""
|
|
222 cur_loc = 0
|
|
223 reg = re.compile("(!!(.*?)!!)")
|
|
224 matches = reg.findall(s)
|
|
225 for i in xrange(0,len(matches)):
|
|
226 tree_map = node.get('map')
|
|
227 tree_map = tree_map + '::' + matches[i][1]
|
|
228 newstr = '!@'+ tree_map +'@!'
|
|
229 s = s.replace(matches[i][0], newstr, 1)
|
|
230 s = self.Node(s)
|
|
231 s = self.NodeParent(s, tree_map)
|
|
232 return s
|
|
233
|
|
234 def NodeParent(self, s, tree_map):
|
|
235 """Parses player input for embedded nodes rolls"""
|
|
236 cur_loc = 0
|
|
237 reg = re.compile("(!#(.*?)#!)")
|
|
238 matches = reg.findall(s)
|
|
239 for i in xrange(0,len(matches)):
|
|
240 ## Build the new tree_map
|
|
241 new_map = tree_map.split('::')
|
|
242 del new_map[len(new_map)-1]
|
|
243 parent_map = matches[i][1].split('::')
|
|
244 ## Backwards Reference the Parent Children
|
212
|
245 child_node = self.get_node(new_map)
|
191
|
246 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
247 s = s.replace(matches[i][0], newstr, 1)
|
|
248 s = self.Node(s)
|
|
249 return s
|
|
250
|
|
251 def get_root(self, child_node, tree_map, new_map, parent_map):
|
|
252 if child_node == 'Invalid Reference!': return child_node
|
|
253 roots = child_node.getchildren(); tr = tree_map.split('::')
|
|
254 newstr = ''
|
|
255 for root in roots:
|
|
256 try: t = new_map.index(root.get('name'))
|
|
257 except: t = 1
|
|
258 if parent_map[0] == root.get('name'):
|
|
259 newstr = '!@' + '::'.join(new_map[:len(tr)-t]) + '::' + '::'.join(parent_map) + '@!'
|
|
260 if newstr != '': return newstr
|
|
261 else:
|
|
262 del new_map[len(new_map)-1]
|
212
|
263 child_node = self.get_node(new_map)
|
191
|
264 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
265 return newstr
|
|
266
|
212
|
267 def get_node(self, path):
|
191
|
268 return_node = 'Invalid Reference!'
|
|
269 value = ""
|
|
270 depth = len(path)
|
|
271 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
272 except Exception, e: return return_node
|
|
273 return_node = self.resolve_get_loop(node, path, 1, depth)
|
|
274 return return_node
|
|
275
|
|
276 def resolve_get_loop(self, node, path, step, depth):
|
|
277 if step == depth: return node
|
|
278 else:
|
212
|
279 child_list = node.getchildren()
|
191
|
280 for child in child_list:
|
|
281 if step == depth: break
|
|
282 if child.get('name') == path[step]:
|
|
283 node = self.resolve_get_loop(child, path, step+1, depth)
|
|
284 return node
|
|
285
|
|
286 def resolve_nodes(self, s):
|
|
287 self.passed = False
|
|
288 string = 'Invalid Reference!'
|
|
289 value = ""
|
|
290 path = s.split('::')
|
|
291 depth = len(path)
|
|
292 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
293 except Exception, e: return string
|
|
294 if node.get('class') in ('dnd35char_handler',
|
|
295 "SWd20char_handler",
|
|
296 "d20char_handler",
|
|
297 "dnd3echar_handler"): string = self.resolve_cust_loop(node, path, 1, depth)
|
|
298 elif node.get('class') == 'rpg_grid_handler': self.resolve_grid(node, path, 1, depth)
|
|
299 else: string = self.resolve_loop(node, path, 1, depth)
|
|
300 return string
|
|
301
|
|
302 def resolve_loop(self, node, path, step, depth):
|
|
303 if step == depth: return self.resolution(node)
|
|
304 else:
|
|
305 child_list = node.findall('nodehandler')
|
|
306 for child in child_list:
|
|
307 if step == depth: break
|
|
308 if child.get('name') == path[step]:
|
|
309 node = child
|
|
310 step += 1
|
|
311 if node.get('class') in ('dnd35char_handler',
|
|
312 "SWd20char_handler",
|
|
313 "d20char_handler",
|
|
314 "dnd3echar_handler"):
|
|
315 string = self.resolve_cust_loop(node, path, step, depth)
|
|
316 elif node.get('class') == 'rpg_grid_handler':
|
|
317 string = self.resolve_grid(node, path, step, depth)
|
|
318 else: string = self.resolve_loop(node, path, step, depth)
|
|
319 return string
|
|
320
|
|
321 def resolution(self, node):
|
|
322 if self.passed == False:
|
|
323 self.passed = True
|
|
324 if node.get('class') == 'textctrl_handler':
|
|
325 s = str(node.find('text').text)
|
|
326 else: s = 'Nodehandler for '+ node.get('class') + ' not done!' or 'Invalid Reference!'
|
212
|
327 else: s = ''
|
|
328 s = self.ParseLogic(s, node)
|
191
|
329 return s
|
|
330
|
|
331 def resolve_grid(self, node, path, step, depth):
|
|
332 if step == depth:
|
|
333 return 'Invalid Grid Reference!'
|
|
334 cell = tuple(path[step].strip('(').strip(')').split(','))
|
|
335 grid = node.find('grid')
|
|
336 rows = grid.findall('row')
|
|
337 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
212
|
338 try: s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
191
|
339 except: s = 'Invalid Grid Reference!'
|
|
340 return s
|
|
341
|
|
342 def resolve_cust_loop(self, node, path, step, depth):
|
|
343 s = 'Invalid Reference!'
|
|
344 node_class = node.get('class')
|
|
345 ## Code needs clean up. Either choose .lower() or .title(), then reset the path list's content ##
|
|
346 if step == depth: self.resolution(node)
|
|
347 ##Build Abilities dictionary##
|
|
348 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('abilities')
|
|
349 else: ab = node.find('abilities')
|
|
350 ab_list = ab.findall('stat'); pc_stats = {}
|
|
351
|
|
352 for ability in ab_list:
|
|
353 pc_stats[ability.get('name')] = (
|
|
354 str(ability.get('base')),
|
|
355 str((int(ability.get('base'))-10)/2) )
|
|
356 pc_stats[ability.get('abbr')] = (
|
|
357 str(ability.get('base')),
|
|
358 str((int(ability.get('base'))-10)/2) )
|
|
359
|
|
360 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('saves')
|
|
361 else: ab = node.find('saves')
|
|
362 ab_list = ab.findall('save')
|
|
363 for save in ab_list:
|
|
364 pc_stats[save.get('name')] = (str(save.get('base')), str(int(save.get('magmod')) + int(save.get('miscmod')) + int(pc_stats[save.get('stat')][1]) ) )
|
|
365 if save.get('name') == 'Fortitude': abbr = 'Fort'
|
|
366 if save.get('name') == 'Reflex': abbr = 'Ref'
|
|
367 if save.get('name') == 'Will': abbr = 'Will'
|
|
368 pc_stats[abbr] = ( str(save.get('base')), str(int(save.get('magmod')) + int(save.get('miscmod')) + int(pc_stats[save.get('stat')][1]) ) )
|
|
369
|
|
370 if path[step].lower() == 'skill':
|
|
371 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
372 node = node.find('skills')
|
|
373 child_list = node.findall('skill')
|
|
374 for child in child_list:
|
|
375 if path[step+1].lower() == child.get('name').lower():
|
|
376 if step+2 == depth: s = child.get('rank')
|
|
377 elif path[step+2].lower() == 'check':
|
|
378 s = '<b>Skill Check:</b> ' + child.get('name') + ' [1d20+'+str( int(child.get('rank')) + int(pc_stats[child.get('stat')][1]) )+']'
|
|
379 return s
|
|
380
|
|
381 if path[step].lower() == 'feat':
|
|
382 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
383 node = node.find('feats')
|
|
384 child_list = node.findall('feat')
|
|
385 for child in child_list:
|
|
386 if path[step+1].lower() == child.get('name').lower():
|
|
387 if step+2 == depth: s = '<b>'+child.get('name')+'</b>'+': '+child.get('desc')
|
|
388 return s
|
|
389 if path[step].lower() == 'cast':
|
|
390 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snp')
|
|
391 node = node.find('spells')
|
|
392 child_list = node.findall('spell')
|
|
393 for child in child_list:
|
|
394 if path[step+1].lower() == child.get('name').lower():
|
|
395 if step+2 == depth: s = '<b>'+child.get('name')+'</b>'+': '+child.get('desc')
|
|
396 return s
|
|
397 if path[step].lower() == 'attack':
|
|
398 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('combat')
|
|
399 if path[step+1].lower() == 'melee' or path[step+1].lower() == 'm':
|
|
400 bonus_text = '(Melee)'
|
|
401 bonus = node.find('attacks')
|
|
402 bonus = bonus.find('melee')
|
|
403 bonus = bonus.attrib; d = int(pc_stats['Str'][1])
|
|
404 elif path[step+1].lower() == 'ranged' or path[step+1].lower() == 'r':
|
|
405 bonus_text = '(Ranged)'
|
|
406 bonus = node.find('attacks')
|
|
407 bonus = bonus.find('ranged')
|
|
408 bonus = bonus.attrib; d = int(pc_stats['Dex'][1])
|
|
409 for b in bonus:
|
|
410 d += int(bonus[b])
|
|
411 bonus = str(d)
|
|
412 if path[step+2] == None: s= bonus
|
|
413 else:
|
|
414 weapons = node.find('attacks')
|
|
415 weapons = weapons.findall('weapon')
|
|
416 for child in weapons:
|
|
417 if path[step+2].lower() == child.get('name').lower():
|
|
418 s = '<b>Attack: '+bonus_text+'</b> '+child.get('name')+' [1d20+'+bonus+'] ' + 'Damage: ['+child.get('damage')+']'
|
|
419 return s
|
|
420 elif pc_stats.has_key(path[step].title()):
|
|
421 if step+1 == depth: s = pc_stats[path[step].title()][0] + ' +('+pc_stats[path[step].title()][1]+')'
|
|
422 elif path[step+1].title() == 'Mod': s = pc_stats[path[step].title()][1]
|
|
423 elif path[step+1].title() == 'Check': s = '<b>'+path[step].title()+' Check:</b> [1d20+'+str(pc_stats[path[step].title()][1])+']'
|
|
424 return s
|
|
425 return s
|
|
426
|
|
427 Parse = InterParse()
|