roomloader.py 1017 Bytes

import json

class RoomLoader(object):

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

  def _get_room_file(self, room_name):
    try:
      filename = self.directory + '/' + room_name + ".txt"
      with open(filename, 'r', encoding='utf-8') as f:
        return json.loads(f.read())
    except Exception as e:
      print("Error opening room file: {} with exception: {}".format(room_name, e))
      return {"title": "", "description": "", "exits": {}, "look_items": {}}

  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']
    # chunks, chunk_size = len(desc), 80 #len(x)/4
    # lines = [ desc[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
    # return '\r\n'.join(lines)

  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']