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):
|
|
155 reg = re.compile("(!=(.*?)=!)")
|
|
156 matches = reg.findall(s)
|
|
157 tree_map = node.get('map')
|
|
158 for i in xrange(0,len(matches)):
|
|
159 ## Build the new tree_map
|
|
160 new_map = tree_map.split('::')
|
|
161 find = matches[i][1].split('::')
|
|
162 ## Backwards Reference the Parent Children
|
|
163 node = self.get_node(new_map)
|
|
164 newstr = self.LocationCheck(node, tree_map, new_map, find)
|
|
165 s = s.replace(matches[i][0], newstr, 1)
|
|
166 return s
|
|
167
|
|
168 def NameSpaceE(self, s):
|
|
169 reg = re.compile("(!&(.*?)&!)")
|
|
170 matches = reg.findall(s)
|
|
171 newstr = False
|
|
172 nodeable = ['rpg_grid_handler', 'container_handler',
|
|
173 'group_handler', 'tabber_handler',
|
|
174 'splitter_handler', 'form_handler', 'textctrl_handler']
|
|
175 for i in xrange(0,len(matches)):
|
|
176 find = matches[i][1].split('::')
|
|
177 node = component.get('tree').xml_root
|
|
178 if not iselement(node):
|
|
179 s = s.replace(matches[i][0], 'Invalid Reference!', 1);
|
|
180 s = self.NameSpaceE(s)
|
|
181 return s
|
|
182 for x in xrange(0, len(find)):
|
|
183 namespace = node.getiterator('nodehandler')
|
|
184 for node in namespace:
|
|
185 if find[x] == node.get('name'):
|
|
186 if node.get('class') not in nodeable: continue
|
|
187 if node.get('class') == 'rpg_grid_handler':
|
|
188 try: newstr = self.NameSpaceGrid(find[x+1], node); break
|
|
189 except: newstr = 'Invalid Grid Reference!'
|
|
190 try:
|
|
191 if self.FutureCheck(node, find[x+1]): break
|
|
192 else: continue
|
|
193 except:
|
|
194 if x == len(find)-1:
|
|
195 if node.find('text') != None: newstr = str(node.find('text').text)
|
|
196 else: newstr = 'Invalid Reference!'
|
|
197 break
|
|
198 else: break
|
|
199 if not newstr: newstr = 'Invalid Reference!'
|
|
200 s = s.replace(matches[i][0], newstr, 1)
|
|
201 s = self.ParseLogic(s, node)
|
|
202 return s
|
|
203
|
|
204 def NameSpaceGrid(self, s, node):
|
|
205 cell = tuple(s.strip('(').strip(')').split(','))
|
|
206 grid = node.find('grid')
|
|
207 rows = grid.findall('row')
|
|
208 try:
|
|
209 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
|
210 s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
|
211 except: s = 'Invalid Grid Reference!'
|
|
212 return s
|
|
213
|
191
|
214 def NodeMap(self, s, node):
|
|
215 """Parses player input for embedded nodes rolls"""
|
|
216 cur_loc = 0
|
|
217 reg = re.compile("(!!(.*?)!!)")
|
|
218 matches = reg.findall(s)
|
|
219 for i in xrange(0,len(matches)):
|
|
220 tree_map = node.get('map')
|
|
221 tree_map = tree_map + '::' + matches[i][1]
|
|
222 newstr = '!@'+ tree_map +'@!'
|
|
223 s = s.replace(matches[i][0], newstr, 1)
|
|
224 s = self.Node(s)
|
|
225 s = self.NodeParent(s, tree_map)
|
|
226 return s
|
|
227
|
|
228 def NodeParent(self, s, tree_map):
|
|
229 """Parses player input for embedded nodes rolls"""
|
|
230 cur_loc = 0
|
|
231 reg = re.compile("(!#(.*?)#!)")
|
|
232 matches = reg.findall(s)
|
|
233 for i in xrange(0,len(matches)):
|
|
234 ## Build the new tree_map
|
|
235 new_map = tree_map.split('::')
|
|
236 del new_map[len(new_map)-1]
|
|
237 parent_map = matches[i][1].split('::')
|
|
238 ## Backwards Reference the Parent Children
|
212
|
239 child_node = self.get_node(new_map)
|
191
|
240 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
241 s = s.replace(matches[i][0], newstr, 1)
|
|
242 s = self.Node(s)
|
|
243 return s
|
|
244
|
|
245 def get_root(self, child_node, tree_map, new_map, parent_map):
|
|
246 if child_node == 'Invalid Reference!': return child_node
|
|
247 roots = child_node.getchildren(); tr = tree_map.split('::')
|
|
248 newstr = ''
|
|
249 for root in roots:
|
|
250 try: t = new_map.index(root.get('name'))
|
|
251 except: t = 1
|
|
252 if parent_map[0] == root.get('name'):
|
|
253 newstr = '!@' + '::'.join(new_map[:len(tr)-t]) + '::' + '::'.join(parent_map) + '@!'
|
|
254 if newstr != '': return newstr
|
|
255 else:
|
|
256 del new_map[len(new_map)-1]
|
212
|
257 child_node = self.get_node(new_map)
|
191
|
258 newstr = self.get_root(child_node, tree_map, new_map, parent_map)
|
|
259 return newstr
|
|
260
|
212
|
261 def get_node(self, path):
|
191
|
262 return_node = 'Invalid Reference!'
|
|
263 value = ""
|
|
264 depth = len(path)
|
|
265 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
266 except Exception, e: return return_node
|
|
267 return_node = self.resolve_get_loop(node, path, 1, depth)
|
|
268 return return_node
|
|
269
|
|
270 def resolve_get_loop(self, node, path, step, depth):
|
|
271 if step == depth: return node
|
|
272 else:
|
212
|
273 child_list = node.getchildren()
|
191
|
274 for child in child_list:
|
|
275 if step == depth: break
|
|
276 if child.get('name') == path[step]:
|
|
277 node = self.resolve_get_loop(child, path, step+1, depth)
|
|
278 return node
|
|
279
|
|
280 def resolve_nodes(self, s):
|
|
281 self.passed = False
|
|
282 string = 'Invalid Reference!'
|
|
283 value = ""
|
|
284 path = s.split('::')
|
|
285 depth = len(path)
|
|
286 try: node = component.get('tree').tree_map[path[0]]['node']
|
|
287 except Exception, e: return string
|
|
288 if node.get('class') in ('dnd35char_handler',
|
|
289 "SWd20char_handler",
|
|
290 "d20char_handler",
|
|
291 "dnd3echar_handler"): string = self.resolve_cust_loop(node, path, 1, depth)
|
|
292 elif node.get('class') == 'rpg_grid_handler': self.resolve_grid(node, path, 1, depth)
|
|
293 else: string = self.resolve_loop(node, path, 1, depth)
|
|
294 return string
|
|
295
|
|
296 def resolve_loop(self, node, path, step, depth):
|
|
297 if step == depth: return self.resolution(node)
|
|
298 else:
|
|
299 child_list = node.findall('nodehandler')
|
|
300 for child in child_list:
|
|
301 if step == depth: break
|
|
302 if child.get('name') == path[step]:
|
|
303 node = child
|
|
304 step += 1
|
|
305 if node.get('class') in ('dnd35char_handler',
|
|
306 "SWd20char_handler",
|
|
307 "d20char_handler",
|
|
308 "dnd3echar_handler"):
|
|
309 string = self.resolve_cust_loop(node, path, step, depth)
|
|
310 elif node.get('class') == 'rpg_grid_handler':
|
|
311 string = self.resolve_grid(node, path, step, depth)
|
|
312 else: string = self.resolve_loop(node, path, step, depth)
|
|
313 return string
|
|
314
|
|
315 def resolution(self, node):
|
|
316 if self.passed == False:
|
|
317 self.passed = True
|
|
318 if node.get('class') == 'textctrl_handler':
|
|
319 s = str(node.find('text').text)
|
|
320 else: s = 'Nodehandler for '+ node.get('class') + ' not done!' or 'Invalid Reference!'
|
212
|
321 else: s = ''
|
|
322 s = self.ParseLogic(s, node)
|
191
|
323 return s
|
|
324
|
|
325 def resolve_grid(self, node, path, step, depth):
|
|
326 if step == depth:
|
|
327 return 'Invalid Grid Reference!'
|
|
328 cell = tuple(path[step].strip('(').strip(')').split(','))
|
|
329 grid = node.find('grid')
|
|
330 rows = grid.findall('row')
|
|
331 col = rows[int(self.Dice(cell[0]))-1].findall('cell')
|
212
|
332 try: s = self.ParseLogic(col[int(self.Dice(cell[1]))-1].text, node) or 'No Cell Data'
|
191
|
333 except: s = 'Invalid Grid Reference!'
|
|
334 return s
|
|
335
|
|
336 def resolve_cust_loop(self, node, path, step, depth):
|
|
337 s = 'Invalid Reference!'
|
|
338 node_class = node.get('class')
|
|
339 ## Code needs clean up. Either choose .lower() or .title(), then reset the path list's content ##
|
|
340 if step == depth: self.resolution(node)
|
|
341 ##Build Abilities dictionary##
|
|
342 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('abilities')
|
|
343 else: ab = node.find('abilities')
|
|
344 ab_list = ab.findall('stat'); pc_stats = {}
|
|
345
|
|
346 for ability in ab_list:
|
|
347 pc_stats[ability.get('name')] = (
|
|
348 str(ability.get('base')),
|
|
349 str((int(ability.get('base'))-10)/2) )
|
|
350 pc_stats[ability.get('abbr')] = (
|
|
351 str(ability.get('base')),
|
|
352 str((int(ability.get('base'))-10)/2) )
|
|
353
|
|
354 if node_class not in ('d20char_handler', "SWd20char_handler"): ab = node.find('character').find('saves')
|
|
355 else: ab = node.find('saves')
|
|
356 ab_list = ab.findall('save')
|
|
357 for save in ab_list:
|
|
358 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]) ) )
|
|
359 if save.get('name') == 'Fortitude': abbr = 'Fort'
|
|
360 if save.get('name') == 'Reflex': abbr = 'Ref'
|
|
361 if save.get('name') == 'Will': abbr = 'Will'
|
|
362 pc_stats[abbr] = ( str(save.get('base')), str(int(save.get('magmod')) + int(save.get('miscmod')) + int(pc_stats[save.get('stat')][1]) ) )
|
|
363
|
|
364 if path[step].lower() == 'skill':
|
|
365 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
366 node = node.find('skills')
|
|
367 child_list = node.findall('skill')
|
|
368 for child in child_list:
|
|
369 if path[step+1].lower() == child.get('name').lower():
|
|
370 if step+2 == depth: s = child.get('rank')
|
|
371 elif path[step+2].lower() == 'check':
|
|
372 s = '<b>Skill Check:</b> ' + child.get('name') + ' [1d20+'+str( int(child.get('rank')) + int(pc_stats[child.get('stat')][1]) )+']'
|
|
373 return s
|
|
374
|
|
375 if path[step].lower() == 'feat':
|
|
376 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snf')
|
|
377 node = node.find('feats')
|
|
378 child_list = node.findall('feat')
|
|
379 for child in child_list:
|
|
380 if path[step+1].lower() == child.get('name').lower():
|
|
381 if step+2 == depth: s = '<b>'+child.get('name')+'</b>'+': '+child.get('desc')
|
|
382 return s
|
|
383 if path[step].lower() == 'cast':
|
|
384 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('snp')
|
|
385 node = node.find('spells')
|
|
386 child_list = node.findall('spell')
|
|
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() == 'attack':
|
|
392 if node_class not in ('d20char_handler', "SWd20char_handler"): node = node.find('combat')
|
|
393 if path[step+1].lower() == 'melee' or path[step+1].lower() == 'm':
|
|
394 bonus_text = '(Melee)'
|
|
395 bonus = node.find('attacks')
|
|
396 bonus = bonus.find('melee')
|
|
397 bonus = bonus.attrib; d = int(pc_stats['Str'][1])
|
|
398 elif path[step+1].lower() == 'ranged' or path[step+1].lower() == 'r':
|
|
399 bonus_text = '(Ranged)'
|
|
400 bonus = node.find('attacks')
|
|
401 bonus = bonus.find('ranged')
|
|
402 bonus = bonus.attrib; d = int(pc_stats['Dex'][1])
|
|
403 for b in bonus:
|
|
404 d += int(bonus[b])
|
|
405 bonus = str(d)
|
|
406 if path[step+2] == None: s= bonus
|
|
407 else:
|
|
408 weapons = node.find('attacks')
|
|
409 weapons = weapons.findall('weapon')
|
|
410 for child in weapons:
|
|
411 if path[step+2].lower() == child.get('name').lower():
|
|
412 s = '<b>Attack: '+bonus_text+'</b> '+child.get('name')+' [1d20+'+bonus+'] ' + 'Damage: ['+child.get('damage')+']'
|
|
413 return s
|
|
414 elif pc_stats.has_key(path[step].title()):
|
|
415 if step+1 == depth: s = pc_stats[path[step].title()][0] + ' +('+pc_stats[path[step].title()][1]+')'
|
|
416 elif path[step+1].title() == 'Mod': s = pc_stats[path[step].title()][1]
|
|
417 elif path[step+1].title() == 'Check': s = '<b>'+path[step].title()+' Check:</b> [1d20+'+str(pc_stats[path[step].title()][1])+']'
|
|
418 return s
|
|
419 return s
|
|
420
|
|
421 Parse = InterParse()
|