3ea3b548 by Barry

Added inventory handler and basic candle object.

Fixed some bugs with the go command.
1 parent 368910e2
...@@ -101,7 +101,7 @@ class CommandHandler(object): ...@@ -101,7 +101,7 @@ class CommandHandler(object):
101 # update the player's current room to the one the exit leads to 101 # update the player's current room to the one the exit leads to
102 102
103 if roomloader.get_title(ex) == None: 103 if roomloader.get_title(ex) == None:
104 self.mud.send_message(id, "An invisible force prevents you from going in that direction.") 104 self.mud.send_message(self.id, "An invisible force prevents you from going in that direction.")
105 return True 105 return True
106 else: 106 else:
107 self.players[self.id]["room"] = roomloader.get_exits(self.players[self.id]["room"])[ex] 107 self.players[self.id]["room"] = roomloader.get_exits(self.players[self.id]["room"])[ex]
...@@ -126,7 +126,7 @@ class CommandHandler(object): ...@@ -126,7 +126,7 @@ class CommandHandler(object):
126 # the specified exit wasn't found in the current room 126 # the specified exit wasn't found in the current room
127 else: 127 else:
128 # send back an 'unknown exit' message 128 # send back an 'unknown exit' message
129 self.mud.send_message(id, "Unknown exit '{}'".format(ex)) 129 self.mud.send_message(self.id, "Unknown exit '{}'".format(ex))
130 return True 130 return True
131 131
132 132
......
1 {
2 "title": "candle",
3 "description": "a small white candle with a wick that has been previously lit and charred black.",
4 "type": "light",
5 "power" 10,
6 "damage": 1,
7 "weight": 10
8 }
...\ No newline at end of file ...\ No newline at end of file
1 import utils
2
3 class InventoryHandler(object):
4
5 def __init__(self, directory):
6 self.directory = directory
7
8 def _get_inventory_file(self, item_name):
9 filename = self.directory + '/' + item_name + ".txt"
10 item_data = utils.load_object_from_file(filename)
11 if item_data is None:
12 print("Error opening inventory file: {}".format(item_name))
13 return {}
14 return item_name
15
16 def get_title(self, item_name):
17 return self._get_inventory_file(item_name)['title']
18
19 def get_description(self, item_name):
20 return self._get_inventory_file(item_name)['description']
21
22 def get_type(self, item_name):
23 return self._get_inventory_file(item_name)['type']
24
25 def get_power(self, item_name):
26 return self._get_inventory_file(item_name)['power']
27
28 def get_damage(self, item_name):
29 return self._get_inventory_file(item_name)['damage']
30
31 def get_weight(self, item_name):
32 return self._get_inventory_file(item_name)['weight']
...@@ -3,6 +3,7 @@ import sys ...@@ -3,6 +3,7 @@ import sys
3 3
4 files = [ 4 files = [
5 "commandhandler.py", 5 "commandhandler.py",
6 "inventoryhandler.py",
6 "main.py", 7 "main.py",
7 "mudserver.py", 8 "mudserver.py",
8 "roomloader.py", 9 "roomloader.py",
...@@ -17,6 +18,9 @@ for f in os.listdir('help'): ...@@ -17,6 +18,9 @@ for f in os.listdir('help'):
17 for f in os.listdir('rooms'): 18 for f in os.listdir('rooms'):
18 files.append('rooms/' + f) 19 files.append('rooms/' + f)
19 20
21 for f in os.listdir('inventory'):
22 files.append('inventory/' + f)
23
20 password = sys.argv[1] 24 password = sys.argv[1]
21 25
22 for file in files: 26 for file in files:
......
...@@ -12,7 +12,7 @@ class RoomLoader(object): ...@@ -12,7 +12,7 @@ class RoomLoader(object):
12 room_data = utils.load_object_from_file(filename) 12 room_data = utils.load_object_from_file(filename)
13 if room_data is None: 13 if room_data is None:
14 print("Error opening room file: {}".format(room_name)) 14 print("Error opening room file: {}".format(room_name))
15 return {"title": "", "description": "", "exits": {}, "look_items": {}, "inventory": {}} 15 return {}
16 return room_data 16 return room_data
17 17
18 def _save_room_file(self, room_name, room_data): 18 def _save_room_file(self, room_name, room_data):
...@@ -20,19 +20,19 @@ class RoomLoader(object): ...@@ -20,19 +20,19 @@ class RoomLoader(object):
20 utils.save_object_to_file(filename, room_data) 20 utils.save_object_to_file(filename, room_data)
21 21
22 def get_title(self, room_name): 22 def get_title(self, room_name):
23 return self._get_room_file(room_name)['title'] 23 return self._get_room_file(room_name).get('title', None)
24 24
25 def get_description(self, room_name): 25 def get_description(self, room_name):
26 return self._get_room_file(room_name)['description'] 26 return self._get_room_file(room_name).get('description', None)
27 27
28 def get_exits(self, room_name): 28 def get_exits(self, room_name):
29 return self._get_room_file(room_name)['exits'] 29 return self._get_room_file(room_name).get('exits', None)
30 30
31 def get_look_items(self, room_name): 31 def get_look_items(self, room_name):
32 return self._get_room_file(room_name)['look_items'] 32 return self._get_room_file(room_name).get('look_items', None)
33 33
34 def get_inventory(self, room_name): 34 def get_inventory(self, room_name):
35 return self._get_room_file(room_name)['inventory'] 35 return self._get_room_file(room_name).get('inventory', None)
36 36
37 def add_to_inventory(self, room_name, item_name, item_template): 37 def add_to_inventory(self, room_name, item_name, item_template):
38 room_data = self._get_room_file(room_name) 38 room_data = self._get_room_file(room_name)
......
1 { 1 {
2 "title": "Tavern", 2 "title": "Tavern",
3 "description": "You're in a cozy tavern warmed by an open fire.", 3 "description": "You're in a cozy tavern warmed by an open fire.",
4 "exits": {"outside": "Outside", "behind bar": "Room001"}, 4 "exits": {"outside": "Outside", "behind bar": "Room001", "dark": "Dark"},
5 "look_items": { 5 "look_items": {
6 "bar": "The bar is a long wooden plank thrown over roughly hewn barrels.", 6 "bar": "The bar is a long wooden plank thrown over roughly hewn barrels.",
7 "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", 7 "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.",
......