195
|
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
|
|
30 from orpg.orpgCore import component
|
|
31 import re
|
|
32 from orpg.tools.orpg_log import logger
|
|
33 from wx import TextEntryDialog, ID_OK
|
196
|
34 from xml.etree.ElementTree import iselement
|
195
|
35
|
|
36 class InterParse():
|
|
37
|
|
38 def __init__(self):
|
|
39 pass
|
|
40
|
199
|
41 def Post(self, s, tab=False, send=False, myself=False):
|
|
42 if not tab: tab = component.get('chat')
|
196
|
43 s = self.Normalize(s, tab)
|
|
44 tab.set_colors()
|
|
45 tab.Post(s, send, myself)
|
195
|
46
|
|
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
|
196
|
55 def Normalize(self, s, tab):
|
|
56 for plugin_fname in tab.activeplugins.keys():
|
|
57 plugin = tab.activeplugins[plugin_fname]
|
195
|
58 try: s = plugin.pre_parse(s)
|
|
59 except Exception, e:
|
|
60 if str(e) != "'module' object has no attribute 'post_msg'":
|
203
|
61 #logger.general(traceback.format_exc())
|
195
|
62 logger.general("EXCEPTION: " + str(e))
|
196
|
63 if tab.parsed == 0:
|
195
|
64 s = self.NameSpaceE(s)
|
|
65 s = self.Node(s)
|
|
66 s = self.Dice(s)
|
196
|
67 s = self.Filter(s, tab)
|
|
68 tab.parsed = 1
|
195
|
69 return s
|
|
70
|
196
|
71 def Filter(self, s, tab):
|
|
72 s = tab.GetFilteredText(s)
|
195
|
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] + "?: "
|
|
120 dlg = TextEntryDialog(component.get('chat'), lb, "Missing Value?")
|
|
121 dlg.SetValue('')
|
|
122 if matches[i][0] != '':
|
|
123 dlg.SetTitle("Enter Value for " + matches[i][1])
|
|
124 if dlg.ShowModal() == ID_OK: newstr = dlg.GetValue()
|
|
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
|
197
|
130 def LocationCheck(self, x, roots, root_list, tree_map):
|
|
131 roots.append(tree_map[x])
|
|
132 root_list.append(self.get_node(roots))
|
198
|
133 node = root_list[x]
|
|
134 return node, roots, root_list
|
197
|
135
|
|
136 def FutureCheck(self, node, next):
|
|
137 future = node.getiterator('nodehandler')
|
|
138 for advance in future:
|
207
|
139 print next, advance.get('name')
|
|
140 if next == advance.get('name'): print True; return True
|
197
|
141 return False
|
|
142
|
195
|
143 def NameSpaceI(self, s, node):
|
|
144 reg = re.compile("(!=(.*?)=!)")
|
|
145 matches = reg.findall(s)
|
196
|
146 newstr = False
|
198
|
147 nodeable = ['rpg_grid_handler', 'container_handler',
|
|
148 'group_handler', 'tabber_handler',
|
|
149 'splitter_handler', 'form_handler', 'textctrl_handler']
|
195
|
150 for i in xrange(0,len(matches)):
|
|
151 tree_map = node.get('map').split('::')
|
196
|
152 roots = []; root_list = []
|
195
|
153 find = matches[i][1].split('::')
|
197
|
154 node = self.get_node([tree_map[0]])
|
196
|
155 for x in xrange(0, len(tree_map)):
|
198
|
156 (node, roots, root_list) = self.LocationCheck(x, roots, root_list, tree_map)
|
197
|
157 for x in xrange(0, len(find)):
|
|
158 namespace = node.getiterator('nodehandler')
|
|
159 for node in namespace:
|
|
160 if find[x] == node.get('name'):
|
198
|
161 if node.get('class') not in nodeable: continue
|
197
|
162 if node.get('class') == 'rpg_grid_handler':
|
|
163 newstr = self.NameSpaceGrid(find[x+1], node); break
|
|
164 try:
|
|
165 if self.FutureCheck(node, find[x+1]): break
|
|
166 else: continue
|
|
167 except:
|
207
|
168 if x == len(find)-1:
|
|
169 if node.find('text') != None: newstr = str(node.find('text').text)
|
|
170 else: newstr = 'Invalid Reference!'
|
|
171 break
|
197
|
172 if not newstr: newstr = 'Invalid Reference!'; break
|
195
|
173 s = s.replace(matches[i][0], newstr, 1)
|
196
|
174 s = self.ParseLogic(s, node)
|
195
|
175 return s
|
|
176
|
|
177 def NameSpaceE(self, s):
|
|
178 reg = re.compile("(!&(.*?)&!)")
|
|
179 matches = reg.findall(s)
|
197
|
180 newstr = False
|
198
|
181 nodeable = ['rpg_grid_handler', 'container_handler',
|
|
182 'group_handler', 'tabber_handler',
|
|
183 'splitter_handler', 'form_handler', 'textctrl_handler']
|
195
|
184 for i in xrange(0,len(matches)):
|
|
185 find = matches[i][1].split('::')
|
197
|
186 node = component.get('tree').xml_root
|
196
|
187 if not iselement(node):
|
|
188 s = s.replace(matches[i][0], 'Invalid Reference!', 1);
|
|
189 s = self.NameSpaceE(s)
|
|
190 return s
|
197
|
191 for x in xrange(0, len(find)):
|
|
192 namespace = node.getiterator('nodehandler')
|
196
|
193 for node in namespace:
|
|
194 if find[x] == node.get('name'):
|
198
|
195 if node.get('class') not in nodeable: continue
|
197
|
196 if node.get('class') == 'rpg_grid_handler':
|
|
197 newstr = self.NameSpaceGrid(find[x+1], node); break
|
|
198 try:
|
|
199 if self.FutureCheck(node, find[x+1]): break
|
|
200 else: continue
|
|
201 except:
|
207
|
202 if x == len(find)-1:
|
|
203 if node.find('text') != None: newstr = str(node.find('text').text)
|
|
204 else: newstr = 'Invalid Reference!'
|
|
205 break
|
197
|
206 else: break
|
|
207 if not newstr: newstr = 'Invalid Reference!'
|
195
|
208 s = s.replace(matches[i][0], newstr, 1)
|
196
|
209 s = self.ParseLogic(s, node)
|
195
|
210 return s
|
|
211
|
|
212 def NameSpaceGrid(self, s, node):
|
196
|
213 cell = tuple(s.strip('(').strip(')').split(','))
|
195
|
214 grid = node.find('grid')
|
|
215 rows = grid.findall('row')
|
196
|
216 try:
|
|
217 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
|
218 s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
195
|
219 except: s = 'Invalid Grid Reference!'
|
|
220 return s
|
|
221
|
|
222 def NodeMap(self, s, node):
|
|
223 """Parses player input for embedded nodes rolls"""
|
|
224 cur_loc = 0
|
|
225 reg = re.compile("(!!(.*?)!!)")
|
|
226 matches = reg.findall(s)
|
|
227 for i in xrange(0,len(matches)):
|
|
228 tree_map = node.get('map')
|
|
229 tree_map = tree_map + '::' + matches[i][1]
|
|
230 newstr = '!@'+ tree_map +'@!'
|
|
231 s = s.replace(matches[i][0], newstr, 1)
|
|
232 s = self.Node(s)
|
|
233 s = self.NodeParent(s, tree_map)
|
|
234 return s
|
|
235
|
|
236 def NodeParent(self, s, tree_map):
|
|
237 """Parses player input for embedded nodes rolls"""
|
|
238 cur_loc = 0
|
|
239 reg = re.compile("(!#(.*?)#!)")
|
|
240 matches = reg.findall(s)
|
|
241 for i in xrange(0,len(matches)):
|
|
242 ## Build the new tree_map
|
|
243 new_map = tree_map.split('::')
|
|
244 del new_map[len(new_map)-1]
|
|
245 parent_map = matches[i][1].split('::')
|
|
246 ## Backwards Reference the Parent Children
|
196
|
247 child_node = self.get_node(new_map)
|
195
|
248 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
249 s = s.replace(matches[i][0], newstr, 1)
|
|
250 s = self.Node(s)
|
|
251 return s
|
|
252
|
|
253 def get_root(self, child_node, tree_map, new_map, parent_map):
|
|
254 if child_node == 'Invalid Reference!': return child_node
|
|
255 roots = child_node.getchildren(); tr = tree_map.split('::')
|
|
256 newstr = ''
|
|
257 for root in roots:
|
|
258 try: t = new_map.index(root.get('name'))
|
|
259 except: t = 1
|
|
260 if parent_map[0] == root.get('name'):
|
|
261 newstr = '!@' + '::'.join(new_map[:len(tr)-t]) + '::' + '::'.join(parent_map) + '@!'
|
|
262 if newstr != '': return newstr
|
|
263 else:
|
|
264 del new_map[len(new_map)-1]
|
196
|
265 child_node = self.get_node(new_map)
|
195
|
266 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
267 return newstr
|
|
268
|
196
|
269 def get_node(self, path):
|
195
|
270 return_node = 'Invalid Reference!'
|
|
271 value = ""
|
|
272 depth = len(path)
|
|
273 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
274 except Exception, e: return return_node
|
|
275 return_node = self.resolve_get_loop(node, path, 1, depth)
|
|
276 return return_node
|
|
277
|
|
278 def resolve_get_loop(self, node, path, step, depth):
|
|
279 if step == depth: return node
|
|
280 else:
|
198
|
281 child_list = node.getchildren()
|
195
|
282 for child in child_list:
|
|
283 if step == depth: break
|
|
284 if child.get('name') == path[step]:
|
|
285 node = self.resolve_get_loop(child, path, step+1, depth)
|
|
286 return node
|
|
287
|
|
288 def resolve_nodes(self, s):
|
|
289 self.passed = False
|
|
290 string = 'Invalid Reference!'
|
|
291 value = ""
|
|
292 path = s.split('::')
|
|
293 depth = len(path)
|
|
294 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
295 except Exception, e: return string
|
|
296 if node.get('class') in ('dnd35char_handler',
|
|
297 "SWd20char_handler",
|
|
298 "d20char_handler",
|
|
299 "dnd3echar_handler"): string = self.resolve_cust_loop(node, path, 1, depth)
|
|
300 elif node.get('class') == 'rpg_grid_handler': self.resolve_grid(node, path, 1, depth)
|
|
301 else: string = self.resolve_loop(node, path, 1, depth)
|
|
302 return string
|
|
303
|
|
304 def resolve_loop(self, node, path, step, depth):
|
|
305 if step == depth: return self.resolution(node)
|
|
306 else:
|
|
307 child_list = node.findall('nodehandler')
|
|
308 for child in child_list:
|
|
309 if step == depth: break
|
|
310 if child.get('name') == path[step]:
|
|
311 node = child
|
|
312 step += 1
|
|
313 if node.get('class') in ('dnd35char_handler',
|
|
314 "SWd20char_handler",
|
|
315 "d20char_handler",
|
|
316 "dnd3echar_handler"):
|
|
317 string = self.resolve_cust_loop(node, path, step, depth)
|
|
318 elif node.get('class') == 'rpg_grid_handler':
|
|
319 string = self.resolve_grid(node, path, step, depth)
|
|
320 else: string = self.resolve_loop(node, path, step, depth)
|
|
321 return string
|
|
322
|
|
323 def resolution(self, node):
|
|
324 if self.passed == False:
|
|
325 self.passed = True
|
|
326 if node.get('class') == 'textctrl_handler':
|
|
327 s = str(node.find('text').text)
|
|
328 else: s = 'Nodehandler for '+ node.get('class') + ' not done!' or 'Invalid Reference!'
|
|
329 else: s = ''
|
|
330 s = self.ParseLogic(s, node)
|
|
331 return s
|
|
332
|
|
333 def resolve_grid(self, node, path, step, depth):
|
|
334 if step == depth:
|
|
335 return 'Invalid Grid Reference!'
|
|
336 cell = tuple(path[step].strip('(').strip(')').split(','))
|
|
337 grid = node.find('grid')
|
|
338 rows = grid.findall('row')
|
|
339 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
|
340 try: s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
|
341 except: s = 'Invalid Grid Reference!'
|
|
342 return s
|
|
343
|
|
344 def resolve_cust_loop(self, node, path, step, depth):
|
|
345 s = 'Invalid Reference!'
|
|
346 node_class = node.get('class')
|
|
347 ## Code needs clean up. Either choose .lower() or .title(), then reset the path list's content ##
|
|
348 if step == depth: self.resolution(node)
|
|
349 ##Build Abilities dictionary##
|
|
350 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('abilities')
|
|
351 else: ab = node.find('abilities')
|
|
352 ab_list = ab.findall('stat'); pc_stats = {}
|
|
353
|
|
354 for ability in ab_list:
|
|
355 pc_stats[ability.get('name')] = (
|
|
356 str(ability.get('base')),
|
|
357 str((int(ability.get('base'))-10)/2) )
|
|
358 pc_stats[ability.get('abbr')] = (
|
|
359 str(ability.get('base')),
|
|
360 str((int(ability.get('base'))-10)/2) )
|
|
361
|
|
362 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('saves')
|
|
363 else: ab = node.find('saves')
|
|
364 ab_list = ab.findall('save')
|
|
365 for save in ab_list:
|
|
366 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]) ) )
|
|
367 if save.get('name') == 'Fortitude': abbr = 'Fort'
|
|
368 if save.get('name') == 'Reflex': abbr = 'Ref'
|
|
369 if save.get('name') == 'Will': abbr = 'Will'
|
|
370 pc_stats[abbr] = ( str(save.get('base')), str(int(save.get('magmod')) + int(save.get('miscmod')) + int(pc_stats[save.get('stat')][1]) ) )
|
|
371
|
|
372 if path[step].lower() == 'skill':
|
|
373 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
374 node = node.find('skills')
|
|
375 child_list = node.findall('skill')
|
|
376 for child in child_list:
|
|
377 if path[step+1].lower() == child.get('name').lower():
|
|
378 if step+2 == depth: s = child.get('rank')
|
|
379 elif path[step+2].lower() == 'check':
|
|
380 s = '<b>Skill Check:</b> ' + child.get('name') + ' [1d20+'+str( int(child.get('rank')) + int(pc_stats[child.get('stat')][1]) )+']'
|
|
381 return s
|
|
382
|
|
383 if path[step].lower() == 'feat':
|
|
384 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
385 node = node.find('feats')
|
|
386 child_list = node.findall('feat')
|
|
387 for child in child_list:
|
|
388 if path[step+1].lower() == child.get('name').lower():
|
|
389 if step+2 == depth: s = '<b>'+child.get('name')+'</b>'+': '+child.get('desc')
|
|
390 return s
|
|
391 if path[step].lower() == 'cast':
|
|
392 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snp')
|
|
393 node = node.find('spells')
|
|
394 child_list = node.findall('spell')
|
|
395 for child in child_list:
|
|
396 if path[step+1].lower() == child.get('name').lower():
|
|
397 if step+2 == depth: s = '<b>'+child.get('name')+'</b>'+': '+child.get('desc')
|
|
398 return s
|
|
399 if path[step].lower() == 'attack':
|
|
400 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('combat')
|
|
401 if path[step+1].lower() == 'melee' or path[step+1].lower() == 'm':
|
|
402 bonus_text = '(Melee)'
|
|
403 bonus = node.find('attacks')
|
|
404 bonus = bonus.find('melee')
|
|
405 bonus = bonus.attrib; d = int(pc_stats['Str'][1])
|
|
406 elif path[step+1].lower() == 'ranged' or path[step+1].lower() == 'r':
|
|
407 bonus_text = '(Ranged)'
|
|
408 bonus = node.find('attacks')
|
|
409 bonus = bonus.find('ranged')
|
|
410 bonus = bonus.attrib; d = int(pc_stats['Dex'][1])
|
|
411 for b in bonus:
|
|
412 d += int(bonus[b])
|
|
413 bonus = str(d)
|
|
414 if path[step+2] == None: s= bonus
|
|
415 else:
|
|
416 weapons = node.find('attacks')
|
|
417 weapons = weapons.findall('weapon')
|
|
418 for child in weapons:
|
|
419 if path[step+2].lower() == child.get('name').lower():
|
|
420 s = '<b>Attack: '+bonus_text+'</b> '+child.get('name')+' [1d20+'+bonus+'] ' + 'Damage: ['+child.get('damage')+']'
|
|
421 return s
|
|
422 elif pc_stats.has_key(path[step].title()):
|
|
423 if step+1 == depth: s = pc_stats[path[step].title()][0] + ' +('+pc_stats[path[step].title()][1]+')'
|
|
424 elif path[step+1].title() == 'Mod': s = pc_stats[path[step].title()][1]
|
|
425 elif path[step+1].title() == 'Check': s = '<b>'+path[step].title()+' Check:</b> [1d20+'+str(pc_stats[path[step].title()][1])+']'
|
|
426 return s
|
|
427 return s
|
|
428
|
|
429 Parse = InterParse()
|