Crossfire Server, Trunk
dialogc.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """
3 dialogc.py -- Crossfire dialog compiler
4 
5 Complies simple dialogs into CFDialog-compatible JSON. Works on standard input
6 and output when called from the command-line.
7 """
8 
9 def parse_dialog(s):
10  rules = []
11 
12  # current block
13  matches = None # None or list of string matches
14  pre = []
15  msgbuilder = [] # list of strings, to be joined with newlines
16  replies = [] # list of reply keywords and reply text
17  post = []
18 
19  def new_block():
20  "Reset local variables when entering a new @match block."
21  nonlocal pre, msgbuilder, replies, post
22  pre = []
23  msgbuilder = []
24  replies = []
25  post = []
26 
27  def end_block():
28  "Add the current block to the rules dict."
29  block = {}
30  nonlocal matches
31  if matches is None:
32  if len(msgbuilder) > 0:
33  # not in a @match block, and there is text: create a @match * block
34  matches = ['*']
35  else:
36  return # empty initial block, don't generate it
37  block['match'] = matches
38  if len(pre) > 0:
39  block['pre'] = pre
40  block['msg'] = ["\n".join(msgbuilder)]
41  if len(replies) > 0:
42  block['replies'] = replies
43  if len(post) > 0:
44  block['post'] = post
45  rules.append(block)
46 
47  new_block()
48  lines = s.split("\n")
49  for line in lines:
50  if line.startswith('@match'):
51  end_block()
52  parts = line.split(' ', maxsplit=1)
53  matches = parts[1].split('|')
54  new_block()
55  elif line.startswith('@reply'):
56  parts = line.split(' ', maxsplit=2)
57  key = parts[1]
58  msg = parts[2]
59  replies.append([key, msg])
60  elif line.startswith('@identify'):
61  post.append(["identify"])
62  elif line.startswith('@pre'):
63  parts = line.split(' ')
64  pre.append(parts[1:])
65  elif line.startswith('@post'):
66  parts = line.split(' ')
67  post.append(parts[1:])
68  else:
69  msgbuilder.append(line)
70  end_block()
71 
72  dialog = {}
73  dialog['rules'] = rules
74  return dialog
75 
76 if __name__ == '__main__':
77  import json
78  import sys
79  dialog = parse_dialog(sys.stdin.read().strip())
80  json.dump(dialog, sys.stdout)
81  print('') # newline
dialogc.parse_dialog
def parse_dialog(s)
Definition: dialogc.py:9
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2609