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