664e6bd3 by Barry

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

Added inventory, get, and drop commands
1 parent 17133446
import utils
from roomloader import RoomLoader
from inventoryhandler import InventoryHandler
global_aliases = {
'n': 'north',
......@@ -11,9 +11,12 @@ global_aliases = {
'd': 'down',
'l': 'look',
'\'': 'say',
'i': 'inventory',
'inv': 'inventory',
}
roomloader = RoomLoader('rooms')
inventoryhandler = InventoryHandler('inventory')
class CommandHandler(object):
......@@ -50,6 +53,14 @@ class CommandHandler(object):
if subject in item:
self.mud.send_message(self.id, items[item])
return True
if subject in roomloader.get_inventory(room_name):
self.mud.send_message(self.id, 'You the area see a {}:'.format(subject))
self.mud.send_message(self.id, inventoryhandler.get_description(subject))
return True
if subject in self.players[self.id]['inventory']:
self.mud.send_message(self.id, 'You check your inventory and see a {}:'.format(subject))
self.mud.send_message(self.id, inventoryhandler.get_description(subject))
return True
self.mud.send_message(self.id, "That doesn't seem to be here.")
playershere = []
......@@ -70,6 +81,9 @@ class CommandHandler(object):
self.mud.send_message(self.id, "Exits are: {}".format(
", ".join(roomloader.get_exits(room_name))))
# send player a message containing the list of exits from this room
self.mud.send_message(self.id, "Items here: {}".format(
", ".join(roomloader.get_inventory(room_name).keys())))
return True
def who(self):
......@@ -130,6 +144,56 @@ class CommandHandler(object):
self.mud.send_message(self.id, "Unknown exit '{}'".format(ex))
return True
def get(self):
room_name = self.players[self.id]["room"]
if len(self.tokens) == 0:
self.mud.send_message(self.id, 'What do you want to get?')
return True
room_inv = roomloader.get_inventory(room_name)
if self.tokens[0] in room_inv:
if self.tokens[0] in self.players[self.id]['inventory']:
self.players[self.id]['inventory'][self.tokens[0]] += 1
else:
self.players[self.id]['inventory'][self.tokens[0]] = 1
for pid, pl in self.players.items():
# if they're in the same room as the player
if self.players[pid]["room"] == self.players[self.id]["room"]:
# send them a message telling them what the player said
self.mud.send_message(pid, "{} picked up a {}.".format(
self.players[self.id]["name"], self.tokens[0]))
utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"]))
else:
self.mud.send_message(self.id, 'There is no {} here to get.'.format(self.tokens[0]))
return True
def inventory(self):
self.mud.send_message(self.id, '\r\n-Item----------=Inventory=-----------Qty-\r\n')
for item, qty in self.players[self.id]['inventory'].items():
self.mud.send_message(self.id, '{} {}'.format(item.ljust(37), qty))
self.mud.send_message(self.id, '\r\n-----------------------------------------')
return True
def drop(self):
room_name = self.players[self.id]["room"]
if len(self.tokens) == 0:
self.mud.send_message(self.id, 'What do you want to drop?')
return True
room_inv = roomloader.get_inventory(room_name)
if self.tokens[0] in self.players[self.id]['inventory']:
self.players[self.id]['inventory'][self.tokens[0]] -= 1
if self.players[self.id]['inventory'][self.tokens[0]] <= 0:
del self.players[self.id]['inventory'][self.tokens[0]]
roomloader.add_to_inventory(room_name, self.tokens[0], 1)
for pid, pl in self.players.items():
# if they're in the same room as the player
if self.players[pid]["room"] == self.players[self.id]["room"]:
# send them a message telling them what the player said
self.mud.send_message(pid, "{} dropped a {}.".format(
self.players[self.id]["name"], self.tokens[0]))
utils.save_object_to_file(self.players[self.id], "players/{}.json".format(self.players[self.id]["name"]))
else:
self.mud.send_message(self.id, 'You do not have {} in your inventory.'.format(self.tokens[0]))
return True
def prompt(self):
if len(self.tokens) == 0:
......
......@@ -2,7 +2,7 @@
"title": "candle",
"description": "a small white candle with a wick that has been previously lit and charred black.",
"type": "light",
"power" 10,
"power": 10,
"damage": 1,
"weight": 10
}
\ No newline at end of file
......
......@@ -11,7 +11,7 @@ class InventoryHandler(object):
if item_data is None:
print("Error opening inventory file: {}".format(item_name))
return {}
return item_name
return item_data
def get_title(self, item_name):
return self._get_inventory_file(item_name)['title']
......
{"name": "test", "room": "Tavern", "inventory": {}, "prompt": "hp:%hp mp:%mp> ", "aliases": {}, "hp": 100, "mp": 100}
\ No newline at end of file
{"name": "test", "room": "Tavern", "inventory": {"candle": 1}, "prompt": "hp:%hp mp:%mp> ", "aliases": {}, "hp": 100, "mp": 100}
\ No newline at end of file
......
......@@ -17,7 +17,7 @@ class RoomLoader(object):
def _save_room_file(self, room_name, room_data):
filename = self.directory + '/' + room_name + ".txt"
utils.save_object_to_file(filename, room_data)
utils.save_object_to_file(room_data, filename)
def get_title(self, room_name):
return self._get_room_file(room_name).get('title', None)
......@@ -34,13 +34,20 @@ class RoomLoader(object):
def get_inventory(self, room_name):
return self._get_room_file(room_name).get('inventory', None)
def add_to_inventory(self, room_name, item_name, item_template):
def add_to_inventory(self, room_name, item_name, quantity):
room_data = self._get_room_file(room_name)
if item_name in room_data['inventory']:
# the item_name should always be unique. Let this happen just in case so it will
# prune a dupe from player
return True
room_data['inventory'][item_name] += quantity
else:
room_data['inventory']['item_name'] = item_template
self._save_room_file(room_name, room_data)
return
\ No newline at end of file
room_data['inventory'][item_name] = 1
self._save_room_file(room_name, room_data)
return True
def remove_from_inventory(self, room_name, item_name, quantity):
room_data = self._get_room_file(room_name)
if item_name in room_data['inventory']:
room_data['inventory'][item_name] -= quantity
if room_data['inventory'][item_name] <= 0:
del room_data['inventory'][item_name]
self._save_room_file(room_name, room_data)
return True
\ No newline at end of file
......
{
"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": {}
}
\ No newline at end of file
{"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
......
......@@ -5,8 +5,8 @@ def save_object_to_file(obj, filename):
f.write(json.dumps(obj))
def load_object_from_file(filename):
try:
with open(filename, 'r', encoding='utf-8') as f:
return json.loads(f.read())
except Exception:
return None
#try:
with open(filename, 'r', encoding='utf-8') as f:
return json.loads(f.read())
#except Exception:
# return None
......