Mmoved all commands to their own files to load dynamically to save memory.
Showing
18 changed files
with
233 additions
and
13 deletions
This diff is collapsed.
Click to expand it.
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