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