Mmoved all commands to their own files to load dynamically to save memory.
Showing
18 changed files
with
267 additions
and
260 deletions
1 | import utils | 1 | import utils |
2 | from roomloader import RoomLoader | ||
3 | from inventoryhandler import InventoryHandler | ||
4 | 2 | ||
5 | global_aliases = { | 3 | global_aliases = { |
6 | 'n': 'north', | 4 | 'n': 'north', |
... | @@ -15,259 +13,48 @@ global_aliases = { | ... | @@ -15,259 +13,48 @@ global_aliases = { |
15 | 'inv': 'inventory', | 13 | 'inv': 'inventory', |
16 | } | 14 | } |
17 | 15 | ||
18 | roomloader = RoomLoader('rooms') | ||
19 | inventoryhandler = InventoryHandler('inventory') | ||
20 | |||
21 | class CommandHandler(object): | 16 | class CommandHandler(object): |
22 | 17 | ||
18 | def parse(self, id, cmd, params, mud, players): | ||
19 | if cmd in global_aliases: | ||
20 | cmd = global_aliases[cmd] | ||
21 | else: | ||
22 | if 'aliases' not in players[id]: | ||
23 | players[id]["aliases"] = {} | ||
24 | if cmd in players[id]["aliases"]: | ||
25 | cmd = players[id]["aliases"][cmd] | ||
23 | 26 | ||
24 | def alias(self, command): | ||
25 | if command in global_aliases: | ||
26 | return global_aliases[command] | ||
27 | if 'aliases' not in self.players[self.id]: | ||
28 | self.players[self.id]["aliases"] = {} | ||
29 | if command in self.players[self.id]["aliases"]: | ||
30 | return self.players[self.id]["aliases"][command] | ||
31 | return command | ||
32 | |||
33 | def tokenize_params(self): | ||
34 | cleaned = self.params.lower().strip() | ||
35 | tokens = [] | 27 | tokens = [] |
36 | for token in cleaned.split(' '): | 28 | for token in params.lower().strip().split(' '): |
37 | token = token.strip() | 29 | token = token.strip() |
38 | if len(token) > 0: | 30 | if len(token) > 0: |
39 | tokens.append(token) | 31 | tokens.append(token) |
40 | self.tokens = tokens | 32 | locals()['tokens'] = tokens |
41 | |||
42 | def look(self): | ||
43 | room_name = self.players[self.id]["room"] | ||
44 | self.mud.send_message(self.id, "") | ||
45 | if len(self.tokens) > 0 and self.tokens[0] == 'at': | ||
46 | del self.tokens[0] | ||
47 | if len(self.tokens) == 0: | ||
48 | self.mud.send_message(self.id, roomloader.get_description(room_name)) | ||
49 | else: | ||
50 | subject = self.tokens[0] | ||
51 | items = roomloader.get_look_items(room_name) | ||
52 | for item in items: | ||
53 | if subject in item: | ||
54 | self.mud.send_message(self.id, items[item]) | ||
55 | return True | ||
56 | if subject in roomloader.get_inventory(room_name): | ||
57 | self.mud.send_message(self.id, 'You the area see a {}:'.format(subject)) | ||
58 | self.mud.send_message(self.id, inventoryhandler.get_description(subject)) | ||
59 | return True | ||
60 | if subject in self.players[self.id]['inventory']: | ||
61 | self.mud.send_message(self.id, 'You check your inventory and see a {}:'.format(subject)) | ||
62 | self.mud.send_message(self.id, inventoryhandler.get_description(subject)) | ||
63 | return True | ||
64 | self.mud.send_message(self.id, "That doesn't seem to be here.") | ||
65 | |||
66 | playershere = [] | ||
67 | # go through every player in the game | ||
68 | for pid, pl in self.players.items(): | ||
69 | # if they're in the same room as the player | ||
70 | if self.players[pid]["room"] == self.players[self.id]["room"]: | ||
71 | # ... and they have a name to be shown | ||
72 | if self.players[pid]["name"] is not None: | ||
73 | # add their name to the list | ||
74 | playershere.append(self.players[pid]["name"]) | ||
75 | |||
76 | # send player a message containing the list of players in the room | ||
77 | self.mud.send_message(self.id, "Players here: {}".format( | ||
78 | ", ".join(playershere))) | ||
79 | |||
80 | # send player a message containing the list of exits from this room | ||
81 | self.mud.send_message(self.id, "Exits are: {}".format( | ||
82 | ", ".join(roomloader.get_exits(room_name)))) | ||
83 | |||
84 | # send player a message containing the list of exits from this room | ||
85 | self.mud.send_message(self.id, "Items here: {}".format( | ||
86 | ", ".join(roomloader.get_inventory(room_name).keys()))) | ||
87 | return True | ||
88 | |||
89 | def who(self): | ||
90 | self.mud.send_message(self.id, "") | ||
91 | self.mud.send_message(self.id, "-------- {} Players Currently Online --------".format(len(self.players))) | ||
92 | for pid, player in self.players.items(): | ||
93 | self.mud.send_message(self.id, " " + player["name"]) | ||
94 | self.mud.send_message(self.id, "---------------------------------------------".format(len(self.players))) | ||
95 | self.mud.send_message(self.id, "") | ||
96 | return True | ||
97 | 33 | ||
98 | def go(self): | 34 | locals()['next_command'] = None |
99 | # store the exit name | ||
100 | ex = self.params.lower().strip() | ||
101 | |||
102 | exits = roomloader.get_exits(self.players[self.id]["room"]) | ||
103 | # if the specified exit is found in the room's exits list | ||
104 | if ex in exits: | ||
105 | # go through all the players in the game | ||
106 | for pid, pl in self.players.items(): | ||
107 | # if player is in the same room and isn't the player | ||
108 | # sending the command | ||
109 | if self.players[pid]["room"] == self.players[self.id]["room"] \ | ||
110 | and pid != self.id: | ||
111 | # send them a message telling them that the player | ||
112 | # left the room | ||
113 | self.mud.send_message(pid, "{} left via exit '{}'".format( | ||
114 | self.players[self.id]["name"], ex)) | ||
115 | |||
116 | # update the player's current room to the one the exit leads to | ||
117 | |||
118 | if roomloader.get_title(exits[ex]) == None: | ||
119 | self.mud.send_message(self.id, "An invisible force prevents you from going in that direction.") | ||
120 | return True | ||
121 | else: | ||
122 | self.players[self.id]["room"] = exits[ex] | ||
123 | self.room = ex | ||
124 | |||
125 | # go through all the players in the game | ||
126 | for pid, pl in self.players.items(): | ||
127 | # if player is in the same (new) room and isn't the player | ||
128 | # sending the command | ||
129 | if self.players[pid]["room"] == self.players[self.id]["room"] \ | ||
130 | and pid != self.id: | ||
131 | # send them a message telling them that the player | ||
132 | # entered the room | ||
133 | self.mud.send_message(pid, | ||
134 | "{} arrived via exit '{}'".format( | ||
135 | self.players[self.id]["name"], ex)) | ||
136 | |||
137 | # send the player a message telling them where they are now | ||
138 | self.mud.send_message(self.id, "You arrive at '{}'".format(roomloader.get_title(self.players[self.id]["room"]))) | ||
139 | self.tokens = [] | ||
140 | return self.look() | ||
141 | # the specified exit wasn't found in the current room | ||
142 | else: | ||
143 | # send back an 'unknown exit' message | ||
144 | self.mud.send_message(self.id, "Unknown exit '{}'".format(ex)) | ||
145 | return True | ||
146 | |||
147 | def get(self): | ||
148 | room_name = self.players[self.id]["room"] | ||
149 | if len(self.tokens) == 0: | ||
150 | self.mud.send_message(self.id, 'What do you want to get?') | ||
151 | return True | ||
152 | room_inv = roomloader.get_inventory(room_name) | ||
153 | if self.tokens[0] in room_inv: | ||
154 | if self.tokens[0] in self.players[self.id]['inventory']: | ||
155 | self.players[self.id]['inventory'][self.tokens[0]] += 1 | ||
156 | else: | ||
157 | self.players[self.id]['inventory'][self.tokens[0]] = 1 | ||
158 | for pid, pl in self.players.items(): | ||
159 | # if they're in the same room as the player | ||
160 | if self.players[pid]["room"] == self.players[self.id]["room"]: | ||
161 | # send them a message telling them what the player said | ||
162 | self.mud.send_message(pid, "{} picked up a {}.".format( | ||
163 | self.players[self.id]["name"], self.tokens[0])) | ||
164 | utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"])) | ||
165 | else: | ||
166 | self.mud.send_message(self.id, 'There is no {} here to get.'.format(self.tokens[0])) | ||
167 | return True | ||
168 | |||
169 | def inventory(self): | ||
170 | self.mud.send_message(self.id, '\r\n-Item----------=Inventory=-----------Qty-\r\n') | ||
171 | for item, qty in self.players[self.id]['inventory'].items(): | ||
172 | self.mud.send_message(self.id, '{} {}'.format(item.ljust(37), qty)) | ||
173 | self.mud.send_message(self.id, '\r\n-----------------------------------------') | ||
174 | return True | ||
175 | |||
176 | def drop(self): | ||
177 | room_name = self.players[self.id]["room"] | ||
178 | if len(self.tokens) == 0: | ||
179 | self.mud.send_message(self.id, 'What do you want to drop?') | ||
180 | return True | ||
181 | room_inv = roomloader.get_inventory(room_name) | ||
182 | if self.tokens[0] in self.players[self.id]['inventory']: | ||
183 | self.players[self.id]['inventory'][self.tokens[0]] -= 1 | ||
184 | if self.players[self.id]['inventory'][self.tokens[0]] <= 0: | ||
185 | del self.players[self.id]['inventory'][self.tokens[0]] | ||
186 | roomloader.add_to_inventory(room_name, self.tokens[0], 1) | ||
187 | for pid, pl in self.players.items(): | ||
188 | # if they're in the same room as the player | ||
189 | if self.players[pid]["room"] == self.players[self.id]["room"]: | ||
190 | # send them a message telling them what the player said | ||
191 | self.mud.send_message(pid, "{} dropped a {}.".format( | ||
192 | self.players[self.id]["name"], self.tokens[0])) | ||
193 | utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"])) | ||
194 | else: | ||
195 | self.mud.send_message(self.id, 'You do not have {} in your inventory.'.format(self.tokens[0])) | ||
196 | return True | ||
197 | |||
198 | def prompt(self): | ||
199 | if len(self.tokens) == 0: | ||
200 | self.mud.send_message(self.id, 'No prompt provided, no changes will be made.\r\nEx: prompt %hp>') | ||
201 | |||
202 | self.players[self.id]['prompt'] = self.params + ' ' | ||
203 | utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"])) | ||
204 | |||
205 | return True | ||
206 | |||
207 | def help(self): | ||
208 | if len(self.tokens) > 0: | ||
209 | filename = 'help/' + self.tokens[0] + '.txt' | ||
210 | else: | ||
211 | filename = 'help/help.txt' | ||
212 | try: | 35 | try: |
213 | with open(filename, 'r', encoding='utf-8') as f: | 36 | if cmd in utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt').get('exits'): |
214 | for line in f: | 37 | params = cmd + " " + params.lower().strip() |
215 | self.mud.send_message(self.id, line, '\r') | 38 | cmd = "go" |
216 | self.mud.send_message(self.id, '') | 39 | ldict = locals() |
217 | except: | 40 | with open('commands/{}.txt'.format(cmd), 'r', encoding='utf-8') as f: |
218 | self.mud.send_message(self.id, 'No help topics available for \"{}\". A list\r\n of all commands is available at: help index'.format(self.tokens[0])) | 41 | exec(f.read(), globals(), ldict) |
219 | 42 | if ldict['next_command'] != None: | |
220 | def say(self): | 43 | with open('commands/{}.txt'.format(ldict['next_command']), 'r', encoding='utf-8') as f: |
221 | # go through every player in the game | 44 | exec(f.read()) |
222 | for pid, pl in self.players.items(): | 45 | return True |
223 | # if they're in the same room as the player | 46 | except FileNotFoundError as e: |
224 | if self.players[pid]["room"] == self.players[self.id]["room"]: | 47 | print(e) |
225 | # send them a message telling them what the player said | 48 | mud.send_message(id, "Unknown command '{}'".format(cmd)) |
226 | self.mud.send_message(pid, "{} says: {}".format( | 49 | return False |
227 | self.players[self.id]["name"], self.params)) | ||
228 | return True | ||
229 | |||
230 | def whisper(self): | ||
231 | # go through every player in the game | ||
232 | for pid, pl in self.players.items(): | ||
233 | # if they're in the same room as the player | ||
234 | if self.players[pid]["name"] == self.tokens[0]: | ||
235 | # send them a message telling them what the player said | ||
236 | self.mud.send_message(pid, "{} whispers: {}".format( | ||
237 | self.players[self.id]["name"], ' '.join(self.tokens[1:]))) | ||
238 | return True | ||
239 | |||
240 | def quit(self): | ||
241 | # send the player back the list of possible commands | ||
242 | self.mud.send_message(id, "Saving...") | ||
243 | utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"])) | ||
244 | self.mud.disconnect_player(self.id) | ||
245 | return True | ||
246 | |||
247 | def save(self): | ||
248 | # send the player back the list of possible commands | ||
249 | self.mud.send_message(self.id, "Saving...") | ||
250 | utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"])) | ||
251 | self.mud.send_message(self.id, "Save complete") | ||
252 | return True | ||
253 | |||
254 | |||
255 | |||
256 | def parse(self, id, cmd, params, mud, players): | ||
257 | self.id = id | ||
258 | self.params = params | ||
259 | self.tokenize_params() | ||
260 | self.mud = mud | ||
261 | self.players = players | ||
262 | self.cmd = self.alias(cmd) | ||
263 | try: | ||
264 | if self.cmd in roomloader.get_exits(self.players[id]["room"]): | ||
265 | self.params = self.cmd + " " + self.params | ||
266 | self.cmd = "go" | ||
267 | method = getattr(self, self.cmd) | ||
268 | return method() | ||
269 | except AttributeError as e: | 50 | except AttributeError as e: |
270 | print(e) | 51 | print(e) |
271 | mud.send_message(id, "Unknown command '{}'".format(self.cmd)) | 52 | mud.send_message(id, "Unknown command '{}'".format(cmd)) |
272 | return False | 53 | return False |
54 | except Exception as e: | ||
55 | print('Something terrible happened...') | ||
56 | print(e) | ||
57 | mud.send_message(id, "Unknown command '{}'".format(cmd)) | ||
58 | return False | ||
59 | |||
273 | 60 | ... | ... |
commands/drop.txt
0 → 100644
1 | def drop(id, tokens, players, mud): | ||
2 | room_name = players[id]["room"] | ||
3 | if len(tokens) == 0: | ||
4 | mud.send_message(id, 'What do you want to drop?') | ||
5 | return True | ||
6 | room_data = utils.load_object_from_file('rooms/' + room_name + '.txt') | ||
7 | if tokens[0] in players[id]['inventory']: | ||
8 | players[id]['inventory'][tokens[0]] -= 1 | ||
9 | if players[id]['inventory'][tokens[0]] <= 0: | ||
10 | del players[id]['inventory'][tokens[0]] | ||
11 | if tokens[0] in room_data['inventory']: | ||
12 | room_data['inventory'][tokens[0]] += 1 | ||
13 | else: | ||
14 | room_data['inventory'][tokens[0]] = 1 | ||
15 | utils.save_object_to_file(room_data, 'rooms/' + room_name + '.txt') | ||
16 | for pid, pl in players.items(): | ||
17 | # if they're in the same room as the player | ||
18 | if players[pid]["room"] == players[id]["room"]: | ||
19 | # send them a message telling them what the player said | ||
20 | mud.send_message(pid, "{} dropped a {}.".format( | ||
21 | players[id]["name"], tokens[0])) | ||
22 | utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) | ||
23 | else: | ||
24 | mud.send_message(id, 'You do not have {} in your inventory.'.format(tokens[0])) | ||
25 | |||
26 | drop(id, tokens, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/get.txt
0 → 100644
1 | def get(id, tokens, players, mud): | ||
2 | room_name = players[id]["room"] | ||
3 | if len(tokens) == 0: | ||
4 | mud.send_message(id, 'What do you want to get?') | ||
5 | return True | ||
6 | room_data = utils.load_object_from_file('rooms/' + room_name + '.txt') | ||
7 | if tokens[0] in room_data.get('inventory'): | ||
8 | if tokens[0] in players[id]['inventory']: | ||
9 | players[id]['inventory'][tokens[0]] += 1 | ||
10 | else: | ||
11 | players[id]['inventory'][tokens[0]] = 1 | ||
12 | for pid, pl in players.items(): | ||
13 | # if they're in the same room as the player | ||
14 | if players[pid]["room"] == players[id]["room"]: | ||
15 | # send them a message telling them what the player said | ||
16 | mud.send_message(pid, "{} picked up a {}.".format( | ||
17 | players[id]["name"], tokens[0])) | ||
18 | utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) | ||
19 | else: | ||
20 | mud.send_message(id, 'There is no {} here to get.'.format(tokens[0])) | ||
21 | |||
22 | get(id, tokens, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/go.txt
0 → 100644
1 | def go(id, params, players, mud, tokens): | ||
2 | # store the exit name | ||
3 | params = params.lower().strip() | ||
4 | |||
5 | room_data = utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt') | ||
6 | exits = room_data['exits'] | ||
7 | # if the specified exit is found in the room's exits list | ||
8 | if params in exits: | ||
9 | # go through all the players in the game | ||
10 | for pid, pl in players.items(): | ||
11 | # if player is in the same room and isn't the player | ||
12 | # sending the command | ||
13 | if players[pid]["room"] == players[id]["room"] \ | ||
14 | and pid != id: | ||
15 | # send them a message telling them that the player | ||
16 | # left the room | ||
17 | mud.send_message(pid, "{} left via exit '{}'".format( | ||
18 | players[id]["name"], params)) | ||
19 | |||
20 | # update the player's current room to the one the exit leads to | ||
21 | |||
22 | if room_data['exits'][params] == None: | ||
23 | mud.send_message(id, "An invisible force prevents you from going in that direction.") | ||
24 | return None | ||
25 | else: | ||
26 | players[id]["room"] = exits[params] | ||
27 | room = params | ||
28 | |||
29 | # go through all the players in the game | ||
30 | for pid, pl in players.items(): | ||
31 | # if player is in the same (new) room and isn't the player | ||
32 | # sending the command | ||
33 | if players[pid]["room"] == players[id]["room"] \ | ||
34 | and pid != id: | ||
35 | # send them a message telling them that the player | ||
36 | # entered the room | ||
37 | mud.send_message(pid, | ||
38 | "{} arrived via exit '{}'".format( | ||
39 | players[id]["name"], params)) | ||
40 | |||
41 | # send the player a message telling them where they are now | ||
42 | title = utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt')['title'] | ||
43 | mud.send_message(id, "You arrive at '{}'".format(title)) | ||
44 | tokens = [] | ||
45 | return 'look' | ||
46 | # the specified exit wasn't found in the current room | ||
47 | else: | ||
48 | # send back an 'unknown exit' message | ||
49 | mud.send_message(id, "Unknown exit '{}'".format(params)) | ||
50 | next_command = go(id, params, players, mud, tokens) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/help.txt
0 → 100644
1 | def help(id, tokens, mud): | ||
2 | if len(tokens) > 0: | ||
3 | filename = 'help/' + tokens[0] + '.txt' | ||
4 | else: | ||
5 | filename = 'help/help.txt' | ||
6 | try: | ||
7 | with open(filename, 'r', encoding='utf-8') as f: | ||
8 | for line in f: | ||
9 | mud.send_message(id, line, '\r') | ||
10 | mud.send_message(id, '') | ||
11 | except: | ||
12 | mud.send_message(id, 'No help topics available for \"{}\". A list\r\n of all commands is available at: help index'.format(tokens[0])) | ||
13 | |||
14 | help(id, tokens, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/inventory.txt
0 → 100644
1 | def inventory(id, players, mud): | ||
2 | mud.send_message(id, '\r\n-Item----------=Inventory=-----------Qty-\r\n') | ||
3 | for item, qty in players[id]['inventory'].items(): | ||
4 | mud.send_message(id, '{} {}'.format(item.ljust(37), qty)) | ||
5 | mud.send_message(id, '\r\n-----------------------------------------') | ||
6 | |||
7 | inventory(id, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/look.txt
0 → 100644
1 | def look(id, mud, players, tokens): | ||
2 | room_name = players[id]["room"] | ||
3 | room_data = utils.load_object_from_file('rooms/' + room_name + '.txt') | ||
4 | mud.send_message(id, "") | ||
5 | if len(tokens) > 0 and tokens[0] == 'at': | ||
6 | del tokens[0] | ||
7 | if len(tokens) == 0: | ||
8 | mud.send_message(id, room_data.get('description')) | ||
9 | else: | ||
10 | subject = tokens[0] | ||
11 | items = room_data.get('look_items') | ||
12 | for item in items: | ||
13 | if subject in item: | ||
14 | mud.send_message(id, items[item]) | ||
15 | return True | ||
16 | if subject in ['inventory']: | ||
17 | mud.send_message(id, 'You the area see a {}:'.format(subject)) | ||
18 | mud.send_message(id, room_data.get('description')) | ||
19 | return True | ||
20 | if subject in players[id]['inventory']: | ||
21 | mud.send_message(id, 'You check your inventory and see a {}:'.format(subject)) | ||
22 | mud.send_message(id, room_data.get('description')) | ||
23 | return True | ||
24 | mud.send_message(id, "That doesn't seem to be here.") | ||
25 | |||
26 | playershere = [] | ||
27 | # go through every player in the game | ||
28 | for pid, pl in players.items(): | ||
29 | # if they're in the same room as the player | ||
30 | if players[pid]["room"] == players[id]["room"]: | ||
31 | # ... and they have a name to be shown | ||
32 | if players[pid]["name"] is not None: | ||
33 | # add their name to the list | ||
34 | playershere.append(players[pid]["name"]) | ||
35 | |||
36 | # send player a message containing the list of players in the room | ||
37 | mud.send_message(id, "Players here: {}".format( | ||
38 | ", ".join(playershere))) | ||
39 | |||
40 | # send player a message containing the list of exits from this room | ||
41 | mud.send_message(id, "Exits are: {}".format( | ||
42 | ", ".join(room_data.get('exits')))) | ||
43 | |||
44 | # send player a message containing the list of exits from this room | ||
45 | mud.send_message(id, "Items here: {}".format( | ||
46 | ", ".join(room_data.get('inventory').keys()))) | ||
47 | |||
48 | |||
49 | look(id, mud, players, tokens) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/prompt.txt
0 → 100644
1 | def prompt(id, tokens, params, players, mud): | ||
2 | if len(tokens) == 0: | ||
3 | mud.send_message(id, 'No prompt provided, no changes will be made.\r\nEx: prompt %hp>') | ||
4 | |||
5 | players[id]['prompt'] = params + ' ' | ||
6 | utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) | ||
7 | |||
8 | prompt(id, tokens, params, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/quit.txt
0 → 100644
1 | def quit(id, players, mud): | ||
2 | # send the player back the list of possible commands | ||
3 | mud.send_message(id, "Saving...") | ||
4 | utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) | ||
5 | mud.disconnect_player(id) | ||
6 | |||
7 | quit(id, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/save.txt
0 → 100644
1 | def save(id, players, mud): | ||
2 | # send the player back the list of possible commands | ||
3 | mud.send_message(id, "Saving...") | ||
4 | utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) | ||
5 | mud.send_message(id, "Save complete") | ||
6 | |||
7 | save(id, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/say.txt
0 → 100644
1 | def say(id, params, players, mud): | ||
2 | # go through every player in the game | ||
3 | for pid, pl in players.items(): | ||
4 | # if they're in the same room as the player | ||
5 | if players[pid]["room"] == players[id]["room"]: | ||
6 | # send them a message telling them what the player said | ||
7 | mud.send_message(pid, "{} says: {}".format( | ||
8 | players[id]["name"], params)) | ||
9 | say(id, params, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/whisper.txt
0 → 100644
1 | def whisper(id, tokens, players, mud): | ||
2 | # go through every player in the game | ||
3 | for pid, pl in players.items(): | ||
4 | # if they're in the same room as the player | ||
5 | if players[pid]["name"] == tokens[0]: | ||
6 | # send them a message telling them what the player said | ||
7 | mud.send_message(pid, "{} whispers: {}".format( | ||
8 | players[id]["name"], ' '.join(tokens[1:]))) | ||
9 | |||
10 | whisper(id, tokens, players, mud) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
commands/who.txt
0 → 100644
1 | def who(self): | ||
2 | self.mud.send_message(self.id, "") | ||
3 | self.mud.send_message(self.id, "-------- {} Players Currently Online --------".format(len(self.players))) | ||
4 | for pid, player in self.players.items(): | ||
5 | self.mud.send_message(self.id, " " + player["name"]) | ||
6 | self.mud.send_message(self.id, "---------------------------------------------".format(len(self.players))) | ||
7 | self.mud.send_message(self.id, "") | ||
8 | who(self) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -28,8 +28,7 @@ if 'esp' in sys.platform: | ... | @@ -28,8 +28,7 @@ if 'esp' in sys.platform: |
28 | import machine | 28 | import machine |
29 | # import the MUD server class | 29 | # import the MUD server class |
30 | from mudserver import MudServer | 30 | from mudserver import MudServer |
31 | from commandhandler import CommandHandler | 31 | |
32 | from roomloader import RoomLoader | ||
33 | import utils | 32 | import utils |
34 | 33 | ||
35 | print('STARTING MUD\r\n\r\n\r\n') | 34 | print('STARTING MUD\r\n\r\n\r\n') |
... | @@ -41,11 +40,7 @@ if 'esp' in sys.platform: | ... | @@ -41,11 +40,7 @@ if 'esp' in sys.platform: |
41 | players = {} | 40 | players = {} |
42 | 41 | ||
43 | # start the server | 42 | # start the server |
44 | mud = MudServer() | 43 | globals()['mud'] = MudServer() |
45 | |||
46 | cmd_handler = CommandHandler() | ||
47 | |||
48 | roomloader = RoomLoader('rooms') | ||
49 | 44 | ||
50 | def prompt(pid): | 45 | def prompt(pid): |
51 | if "prompt" not in players[pid]: | 46 | if "prompt" not in players[pid]: |
... | @@ -157,9 +152,12 @@ while True: | ... | @@ -157,9 +152,12 @@ while True: |
157 | + "\r\nType 'help' for a list of commands. Have fun!\r\n\r\n") | 152 | + "\r\nType 'help' for a list of commands. Have fun!\r\n\r\n") |
158 | 153 | ||
159 | # send the new player the description of their current room | 154 | # send the new player the description of their current room |
160 | mud.send_message(id, roomloader.get_description(players[id]["room"])) | 155 | |
156 | mud.send_message(id, utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt')['description']) | ||
161 | 157 | ||
162 | else: | 158 | else: |
159 | from commandhandler import CommandHandler | ||
160 | cmd_handler = CommandHandler() | ||
163 | 161 | ||
164 | handler_results = cmd_handler.parse(id, command, params, mud, players) | 162 | handler_results = cmd_handler.parse(id, command, params, mud, players) |
165 | 163 | ... | ... |
1 | {"name": "test", "room": "Tavern", "inventory": {"candle": 1}, "prompt": "hp:%hp mp:%mp> ", "aliases": {}, "hp": 100, "mp": 100} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | {"name": "test", "room": "Tavern", "inventory": {"candle": 1}, "prompt": "%hp> ", "aliases": {}, "hp": 100, "mp": 100} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -21,6 +21,9 @@ for f in os.listdir('rooms'): | ... | @@ -21,6 +21,9 @@ for f in os.listdir('rooms'): |
21 | for f in os.listdir('inventory'): | 21 | for f in os.listdir('inventory'): |
22 | files.append('inventory/' + f) | 22 | files.append('inventory/' + f) |
23 | 23 | ||
24 | for f in os.listdir('commands'): | ||
25 | files.append('commands/' + f) | ||
26 | |||
24 | with open('releasepw.conf', 'r', encoding='utf-8') as f: | 27 | with open('releasepw.conf', 'r', encoding='utf-8') as f: |
25 | password = f.read() | 28 | password = f.read() |
26 | 29 | ... | ... |
1 | {"title": "Tavern", "description": "You're in a cozy tavern warmed by an open fire.", "exits": {"outside": "Outside", "behind": "Room001", "dark": "Dark"}, "look_items": {"bar": "The bar is a long wooden plank thrown over roughly hewn barrels.", "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", "wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.", "fire": "The fire crackles quietly in the corner providing a small amount of light and heat."}, "inventory": {"candle": 11}} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | {"title": "Tavern", "description": "You're in a cozy tavern warmed by an open fire.", "exits": {"outside": "Outside", "behind": "Room001", "dark": "Dark"}, "look_items": {"bar": "The bar is a long wooden plank thrown over roughly hewn barrels.", "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", "wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.", "fire": "The fire crackles quietly in the corner providing a small amount of light and heat."}, "inventory": {"candle": 13}} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -5,8 +5,10 @@ def save_object_to_file(obj, filename): | ... | @@ -5,8 +5,10 @@ def save_object_to_file(obj, filename): |
5 | f.write(json.dumps(obj)) | 5 | f.write(json.dumps(obj)) |
6 | 6 | ||
7 | def load_object_from_file(filename): | 7 | def load_object_from_file(filename): |
8 | #try: | 8 | try: |
9 | with open(filename, 'r', encoding='utf-8') as f: | 9 | with open(filename, 'r', encoding='utf-8') as f: |
10 | return json.loads(f.read()) | 10 | return json.loads(f.read()) |
11 | #except Exception: | 11 | except Exception as e: |
12 | # return None | 12 | print('Error opening file: ' + filename) |
13 | print(e) | ||
14 | return None | ... | ... |
-
Please register or sign in to post a comment