roomloader.py 1.49 KB

import json
import utils

class RoomLoader(object):

  def __init__(self, directory):
    self.directory = directory

  def _get_room_file(self, room_name):
    filename = self.directory + '/' + room_name + ".txt"
    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 room_data

  def _save_room_file(self, room_name, room_data):
    filename = self.directory + '/' + room_name + ".txt"
    utils.save_object_to_file(filename, room_data)

  def get_title(self, room_name):
    return self._get_room_file(room_name)['title']

  def get_description(self, room_name):
    return self._get_room_file(room_name)['description']

  def get_exits(self, room_name):
    return self._get_room_file(room_name)['exits']

  def get_look_items(self, room_name):
    return self._get_room_file(room_name)['look_items']

  def get_inventory(self, room_name):
    return self._get_room_file(room_name)['inventory']

  def add_to_inventory(self, room_name, item_name, item_template):
    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
    else:
      room_data['inventory']['item_name'] = item_template
      self._save_room_file(room_name, room_data)
    return