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):
# update the player's current room to the one the exit leads to
if roomloader.get_title(ex) == None:
self.mud.send_message(id, "An invisible force prevents you from going in that direction.")
self.mud.send_message(self.id, "An invisible force prevents you from going in that direction.")
return True
else:
self.players[self.id]["room"] = roomloader.get_exits(self.players[self.id]["room"])[ex]
......@@ -126,7 +126,7 @@ class CommandHandler(object):
# the specified exit wasn't found in the current room
else:
# send back an 'unknown exit' message
self.mud.send_message(id, "Unknown exit '{}'".format(ex))
self.mud.send_message(self.id, "Unknown exit '{}'".format(ex))
return True
......
{
"title": "candle",
"description": "a small white candle with a wick that has been previously lit and charred black.",
"type": "light",
"power" 10,
"damage": 1,
"weight": 10
}
\ No newline at end of file
import utils
class InventoryHandler(object):
def __init__(self, directory):
self.directory = directory
def _get_inventory_file(self, item_name):
filename = self.directory + '/' + item_name + ".txt"
item_data = utils.load_object_from_file(filename)
if item_data is None:
print("Error opening inventory file: {}".format(item_name))
return {}
return item_name
def get_title(self, item_name):
return self._get_inventory_file(item_name)['title']
def get_description(self, item_name):
return self._get_inventory_file(item_name)['description']
def get_type(self, item_name):
return self._get_inventory_file(item_name)['type']
def get_power(self, item_name):
return self._get_inventory_file(item_name)['power']
def get_damage(self, item_name):
return self._get_inventory_file(item_name)['damage']
def get_weight(self, item_name):
return self._get_inventory_file(item_name)['weight']
......@@ -3,6 +3,7 @@ import sys
files = [
"commandhandler.py",
"inventoryhandler.py",
"main.py",
"mudserver.py",
"roomloader.py",
......@@ -17,6 +18,9 @@ for f in os.listdir('help'):
for f in os.listdir('rooms'):
files.append('rooms/' + f)
for f in os.listdir('inventory'):
files.append('inventory/' + f)
password = sys.argv[1]
for file in files:
......
......@@ -12,7 +12,7 @@ class RoomLoader(object):
room_data = utils.load_object_from_file(filename)
if room_data is None:
print("Error opening room file: {}".format(room_name))
return {"title": "", "description": "", "exits": {}, "look_items": {}, "inventory": {}}
return {}
return room_data
def _save_room_file(self, room_name, room_data):
......@@ -20,19 +20,19 @@ class RoomLoader(object):
utils.save_object_to_file(filename, room_data)
def get_title(self, room_name):
return self._get_room_file(room_name)['title']
return self._get_room_file(room_name).get('title', None)
def get_description(self, room_name):
return self._get_room_file(room_name)['description']
return self._get_room_file(room_name).get('description', None)
def get_exits(self, room_name):
return self._get_room_file(room_name)['exits']
return self._get_room_file(room_name).get('exits', None)
def get_look_items(self, room_name):
return self._get_room_file(room_name)['look_items']
return self._get_room_file(room_name).get('look_items', None)
def get_inventory(self, room_name):
return self._get_room_file(room_name)['inventory']
return self._get_room_file(room_name).get('inventory', None)
def add_to_inventory(self, room_name, item_name, item_template):
room_data = self._get_room_file(room_name)
......
{
"title": "Tavern",
"description": "You're in a cozy tavern warmed by an open fire.",
"exits": {"outside": "Outside", "behind bar": "Room001"},
"exits": {"outside": "Outside", "behind bar": "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.",
......