-
Notifications
You must be signed in to change notification settings - Fork 0
/
mud.py
125 lines (110 loc) · 3.89 KB
/
mud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import xml.etree.ElementTree as etree
class Mud():
class Room():
class Exit():
def __init__(self, name, direction, to):
self.name = name
self.direction = direction
self.to = to
def __str__(self):
return self.direction + "(" + self.name + ")"
def __init__(self, name, desc, exits):
self.name = name
self.desc = desc
self.exits = [Mud.Room.Exit(*i) for i in exits]
class Npc():
class Dialog():
def __init__(self, tag, text, condition):
self.condition = condition
self.tag = tag
self.text = text.split(' ')
def __str__(self):
return self.tag + " : " + self.condition + " => " + ' '.join(self.text)
def checkCondition(self, message, player):
if len(message) < 1:
return
if message[0] == self.tag and self.condition in message:
m = []
for i in self.text:
if i[:1] == "${" and i[-1] == "}":
m.append(eval(i[2:-1]))
else:
m.append(i)
return ' '.join(m)
def __init__(self, room, name, description, text):
self.name = name
self.desc = description
self.text = [Mud.Npc.Dialog(*i) for i in text]
self.room = room
def checkSpeech(self, message, player):
ret = []
for i in self.text:
a = i.checkCondition(message, player)
if a:
ret.append(a)
return ret
def __str__(self):
return self.name
def __init__(self, file):
tree = etree.parse(file)
root = tree.getroot()
self.rooms = {}
self.npcs = {}
self.begin = None
for child in root:
if child.tag == "room":
self.room(child)
elif child.tag == "npc":
self.npc(child)
def npc(self, child):
dialog = []
name = None
room = child.attrib["room"]
desc = None
for i in range(len(child)):
if child[i].tag == "name":
name = child[i].text
elif child[i].tag == "description":
desc = child[i].text
elif child[i].tag == "dialog":
for j in child[i]:
dialog.append((j.tag, j.text, j.attrib["condition"]))
if not name or not desc or not room:
return
self.npcs[name] = Mud.Npc(room, name, desc, dialog)
def room(self, child):
exits = []
name = None
desc = None
for i in range(len(child)):
if child[i].tag == "name":
name = child[i].text
elif child[i].tag == "description":
desc = child[i].text
elif child[i].tag == "exit":
exits.append((child[i].text, child[i].attrib["direction"],
child[i].attrib["to"]))
if not name or not desc:
return
self.rooms[name] = Mud.Room(name, desc, exits)
try:
if child.attrib['attribute'] == "begin":
self.begin = self.rooms[child[0].text]
except KeyError:
pass
def getNpcsInRoom(self, room):
ret = []
for j, i in self.npcs.items():
if i.room == room.name:
ret.append(i)
return ret
def print_rooms(self):
for j, i in self.rooms.items():
print(i.name)
print(i.desc)
print(*i.exits)
def print_npcs(self):
for j, i in self.npcs.items():
print(i.name)
print(i.desc)
print(*i.text)