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