664e6bd3 by Barry

Updated inventory so players can pickup and drop items in rooms.

Added inventory, get, and drop commands
1 parent 17133446
1 import utils 1 import utils
2 from roomloader import RoomLoader 2 from roomloader import RoomLoader
3 3 from inventoryhandler import InventoryHandler
4 4
5 global_aliases = { 5 global_aliases = {
6 'n': 'north', 6 'n': 'north',
...@@ -11,9 +11,12 @@ global_aliases = { ...@@ -11,9 +11,12 @@ global_aliases = {
11 'd': 'down', 11 'd': 'down',
12 'l': 'look', 12 'l': 'look',
13 '\'': 'say', 13 '\'': 'say',
14 'i': 'inventory',
15 'inv': 'inventory',
14 } 16 }
15 17
16 roomloader = RoomLoader('rooms') 18 roomloader = RoomLoader('rooms')
19 inventoryhandler = InventoryHandler('inventory')
17 20
18 class CommandHandler(object): 21 class CommandHandler(object):
19 22
...@@ -50,6 +53,14 @@ class CommandHandler(object): ...@@ -50,6 +53,14 @@ class CommandHandler(object):
50 if subject in item: 53 if subject in item:
51 self.mud.send_message(self.id, items[item]) 54 self.mud.send_message(self.id, items[item])
52 return True 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
53 self.mud.send_message(self.id, "That doesn't seem to be here.") 64 self.mud.send_message(self.id, "That doesn't seem to be here.")
54 65
55 playershere = [] 66 playershere = []
...@@ -70,6 +81,9 @@ class CommandHandler(object): ...@@ -70,6 +81,9 @@ class CommandHandler(object):
70 self.mud.send_message(self.id, "Exits are: {}".format( 81 self.mud.send_message(self.id, "Exits are: {}".format(
71 ", ".join(roomloader.get_exits(room_name)))) 82 ", ".join(roomloader.get_exits(room_name))))
72 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())))
73 return True 87 return True
74 88
75 def who(self): 89 def who(self):
...@@ -130,6 +144,56 @@ class CommandHandler(object): ...@@ -130,6 +144,56 @@ class CommandHandler(object):
130 self.mud.send_message(self.id, "Unknown exit '{}'".format(ex)) 144 self.mud.send_message(self.id, "Unknown exit '{}'".format(ex))
131 return True 145 return True
132 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
133 197
134 def prompt(self): 198 def prompt(self):
135 if len(self.tokens) == 0: 199 if len(self.tokens) == 0:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 "title": "candle", 2 "title": "candle",
3 "description": "a small white candle with a wick that has been previously lit and charred black.", 3 "description": "a small white candle with a wick that has been previously lit and charred black.",
4 "type": "light", 4 "type": "light",
5 "power" 10, 5 "power": 10,
6 "damage": 1, 6 "damage": 1,
7 "weight": 10 7 "weight": 10
8 } 8 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -11,7 +11,7 @@ class InventoryHandler(object): ...@@ -11,7 +11,7 @@ class InventoryHandler(object):
11 if item_data is None: 11 if item_data is None:
12 print("Error opening inventory file: {}".format(item_name)) 12 print("Error opening inventory file: {}".format(item_name))
13 return {} 13 return {}
14 return item_name 14 return item_data
15 15
16 def get_title(self, item_name): 16 def get_title(self, item_name):
17 return self._get_inventory_file(item_name)['title'] 17 return self._get_inventory_file(item_name)['title']
......
1 {"name": "test", "room": "Tavern", "inventory": {}, "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:%hp mp:%mp> ", "aliases": {}, "hp": 100, "mp": 100}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -17,7 +17,7 @@ class RoomLoader(object): ...@@ -17,7 +17,7 @@ class RoomLoader(object):
17 17
18 def _save_room_file(self, room_name, room_data): 18 def _save_room_file(self, room_name, room_data):
19 filename = self.directory + '/' + room_name + ".txt" 19 filename = self.directory + '/' + room_name + ".txt"
20 utils.save_object_to_file(filename, room_data) 20 utils.save_object_to_file(room_data, filename)
21 21
22 def get_title(self, room_name): 22 def get_title(self, room_name):
23 return self._get_room_file(room_name).get('title', None) 23 return self._get_room_file(room_name).get('title', None)
...@@ -34,13 +34,20 @@ class RoomLoader(object): ...@@ -34,13 +34,20 @@ class RoomLoader(object):
34 def get_inventory(self, room_name): 34 def get_inventory(self, room_name):
35 return self._get_room_file(room_name).get('inventory', None) 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, quantity):
38 room_data = self._get_room_file(room_name) 38 room_data = self._get_room_file(room_name)
39 if item_name in room_data['inventory']: 39 if item_name in room_data['inventory']:
40 # the item_name should always be unique. Let this happen just in case so it will 40 room_data['inventory'][item_name] += quantity
41 # prune a dupe from player
42 return True
43 else: 41 else:
44 room_data['inventory']['item_name'] = item_template 42 room_data['inventory'][item_name] = 1
45 self._save_room_file(room_name, room_data) 43 self._save_room_file(room_name, room_data)
46 return
...\ No newline at end of file ...\ No newline at end of file
44 return True
45
46 def remove_from_inventory(self, room_name, item_name, quantity):
47 room_data = self._get_room_file(room_name)
48 if item_name in room_data['inventory']:
49 room_data['inventory'][item_name] -= quantity
50 if room_data['inventory'][item_name] <= 0:
51 del room_data['inventory'][item_name]
52 self._save_room_file(room_name, room_data)
53 return True
...\ No newline at end of file ...\ No newline at end of file
......
1 {
2 "title": "Tavern",
3 "description": "You're in a cozy tavern warmed by an open fire.",
4 "exits": {"outside": "Outside", "behind": "Room001", "dark": "Dark"},
5 "look_items": {
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.",
8 "wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.",
9 "fire": "The fire crackles quietly in the corner providing a small amount of light and heat."
10 },
11 "inventory": {}
12 }
...\ 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": 11}}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -5,8 +5,8 @@ def save_object_to_file(obj, filename): ...@@ -5,8 +5,8 @@ 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:
12 return None 12 # return None
......