Beispiel #1
0
 def _load_pc_schema(self, schema):
     try:
         self.pc_template = schema
     except KeyError as e:
         raise SchemaError(
             'pc state template missing required section ``state``',
             schema=schema) from e
Beispiel #2
0
 def load_events():
     events = list()
     try:
         event_id = schema['state']['encounter_event']
         try:
             get_eventstream_by_id(event_id)
         except ObjectNotFoundError as e:
             raise ObjectNotFoundError(
                 "tried to load npc object (id ``{0}``) with a "
                 "nonexistant event_id in its event registry - "
                 "requested id was ``{1}``"
                 .format(self.id_, event_id),
                 schema=schema) from e
         events.append(event_id)
     except KeyError as e:
         if e.args[0] == 'events':
             # This is an error because eventless locales are not
             # interactable and this error indicates that no events are
             # attached to this npc.
             raise SchemaError(
                 "tried to load npc object (id ``{0}``) with no"
                 " registered events".format(self.id_),
                 schema=schema)
         raise
     return events
Beispiel #3
0
 def __init__(self, schema):
     super().__init__(schema)
     state_defaults = {
             'counters': 0,
             'flags': False
             }
     self.state = dict()
     try:
         self.state['encounter_event_id'] = \
             schema['state']['encounter_event']
     except KeyError:
         raise SchemaError("{0} schema missing required initial state "
                           "``encounter_event``".format(type(self)),
                           schema=schema)
     try:
         self.event_path = schema['load_events']
     except KeyError:
         self.event_path = None
     for key, default in state_defaults.items():
         try:
             self.state[key] = {
                     id_: default for id_ in schema['state'][key]
             }
         except KeyError:
             pass
Beispiel #4
0
 def __init__(self, schema):
     super().__init__(schema)
     try:
         self.state['victory_event_id'] = schema['state']['victory_event']
         self.state['defeat_event_id'] = schema['state']['defeat_event']
     except KeyError as e:
         raise SchemaError(
             "{0} schema missing required field ``{1}``".format(
                 type(self), e.args[0]),
             schema=schema) from e
     self.initialized = True
Beispiel #5
0
def parse(schema):
    if type(schema) == str:
        return Expr(schema)
    elif type(schema) == list:
        return All(schema)
    elif type(schema) == dict:
        try:
            return Any(schema['any'])
        except KeyError:
            return All(schema['all'])
    else:
        raise SchemaError("conditional schema tree is of an unrecognized type",
                          schema=schema)
Beispiel #6
0
 def _load_schema(self, path, schema):
     schema_handlers = {
         'town': town.Town,
         'dungeon': dungeon.Dungeon,
         'npc': npc.NPC,
         'monster': monster.Monster,
         'event_stream': eventstream.EventStream,
         'world': self._load_world_schema,
         'pc': self._load_pc_schema
     }
     try:
         schema_handlers[schema['type']](schema)
     except KeyError as e:
         if e.args[0] == 'type':
             raise SchemaError(
                     'unable to load schema from {0} with missing '
                     '``type`` parameter'.format(path), schema=schema)\
                 from e
         else:
             raise SchemaError(
                 'unable to load schema from {0}. ``{1}`` is not a '
                 'supported object type.'.format(path, schema['type']),
                 schema=schema) from e
Beispiel #7
0
 def load_strings():
     strings = dict()
     try:
         for string in schema['state']['strings']:
             try:
                 pair = string.popitem()
                 strings[pair[0]] = str(pair[1])
             except AttributeError:
                 strings[string] = ''
             except (TypeError, IndexError):
                 raise SchemaError(
                     "malformed string state parameter ``{0}`` in town"
                     " object (id ``{1}``)".format(pair[0], self.id_),
                     schema=schema)
     except KeyError:
         pass
     return strings
Beispiel #8
0
 def load_numbers():
     numbers = dict()
     try:
         for number in schema['state']['numbers']:
             try:
                 pair = number.popitem()
                 numbers[pair[0]] = bool(pair[1])
             except AttributeError:
                 numbers[number] = 0.0
             except (TypeError, IndexError):
                 raise SchemaError(
                     "malformed number state parameter ``{0}`` in town "
                     "object (id ``{1}``)".format(pair[0], self.id_),
                     schema=schema)
     except KeyError:
         pass
     return numbers
Beispiel #9
0
 def load_counters():
     counters = dict()
     try:
         for counter in schema['state']['counters']:
             try:
                 pair = counter.popitem()
                 counters[pair[0]] = int(pair[1])
             except AttributeError:
                 counters[counter] = 0
             except (TypeError, IndexError):
                 raise SchemaError(
                     "malformed counter state parameter ``{0}`` in town"
                     " object (id ``{1}``)".format(pair[0], self.id_),
                     schema=schema)
     except KeyError:
         pass
     return counters
Beispiel #10
0
 def __init__(self, schema):
     super().__init__()
     self.id_ = schema['id']
     try:
         self.events = [
                 Event.construct(item, self) for item in schema['events']
         ]
     except KeyError as e:
         raise SchemaError("encountered an unknown event type ``{0}`` "
                           "while attempting to load event ``{1}``"
                           .format(e.args[0], schema['id']))
     global eventstream_registry
     if schema['id'] in eventstream_registry:
         raise LoadError("attempted to load event_stream ``{0}`` but that "
                         "event id already exists".format(schema['id']))
     self.initialized = True
     eventstream_registry[self.id_] = self
Beispiel #11
0
 def load_flags():
     flags = dict()
     try:
         for flag in schema['state']['flags']:
             try:
                 pair = flag.popitem()
                 flags[pair[0]] = bool(pair[1])
             except AttributeError:
                 # this is expected to occur because this flag has no
                 # associated value (i.e. it's implicitly false)
                 flags[flag] = False
             except (TypeError, IndexError):
                 raise SchemaError(
                     "malformed flag state parameter ``{0}`` in town"
                     " object (id ``{1}``)".format(pair[0], self.id_),
                     schema=schema)
     except KeyError:
         pass
     return flags
Beispiel #12
0
 def load_events():
     events = list()
     try:
         for event_id in schema['state']['events']:
             if not is_eventstream_loaded(event_id):
                 raise ObjectNotFoundError(
                     "tried to load town object (id ``{0}``) with a "
                     "nonexistant event_id in its event registry - "
                     "requested id was ``{1}``".format(
                         self.id_, event_id),
                     schema=schema)
             events.append(event_id)
     except KeyError as e:
         if e.args[0] == 'events':
             raise SchemaError(
                 "tried to load town object (id ``{0}``) with no"
                 " registered events".format(self.id_),
                 schema=schema)
         raise
     return events
Beispiel #13
0
 def __init__(self, schema_root):
     super().__init__()
     file_paths = glob.glob(os.path.join(schema_root, '*.yaml'))
     loaded_paths = list()
     subdirs = [d.name for d in os.scandir(schema_root) if d.is_dir()]
     for subdir in subdirs:
         file_paths.extend(
             glob.glob(os.path.join(schema_root, subdir, '*.yaml')))
     schema_types = [
         'event_stream', 'town', 'dungeon', 'npc', 'monster', 'world', 'pc'
     ]
     schema_sets = {key: [] for key in schema_types}
     while file_paths:
         path = file_paths.pop()
         loaded_paths.append(path)
         with open(path, 'r') as file:
             loaded = list(yaml.safe_load_all(file.read()))
         for schema in loaded:
             try:
                 for path_ in schema['load_paths']:
                     additional_path = os.path.join(os.path.dirname(path),
                                                    path_)
                     if additional_path not in loaded_paths:
                         file_paths.extend(glob.glob(additional_path))
                         loaded_paths.append(additional_path)
             except KeyError:
                 pass
         try:
             for schema_type in schema_types:
                 schema_sets[schema_type].extend(
                     filter(lambda x: x['type'] == schema_type, loaded))
         except KeyError:
             raise SchemaError("An object schema at path ``{0}`` is "
                               "missing a 'type'` property")
     for schema_type in schema_types:
         for schema in schema_sets[schema_type]:
             self._load_schema(schema_type, schema)
     self.world_template = None
     self.id_ = hashlib.sha256(repr(
         self.get_state_template()).encode()).hexdigest()
     self.initialized = True
Beispiel #14
0
    def __init__(self, schema):
        def load_flags():
            flags = dict()
            try:
                for flag in schema['state']['flags']:
                    try:
                        pair = flag.popitem()
                        flags[pair[0]] = bool(pair[1])
                    except AttributeError:
                        # this is expected to occur because this flag has no
                        # associated value (i.e. it's implicitly false)
                        flags[flag] = False
                    except (TypeError, IndexError):
                        raise SchemaError(
                            "malformed flag state parameter ``{0}`` in npc"
                            " object (id ``{1}``)".format(pair[0], self.id_),
                            schema=schema)
            except KeyError:
                pass
            return flags

        def load_counters():
            counters = dict()
            try:
                for counter in schema['state']['counters']:
                    try:
                        pair = counter.popitem()
                        counters[pair[0]] = int(pair[1])
                    except AttributeError:
                        counters[counter] = 0
                    except (TypeError, IndexError):
                        raise SchemaError(
                            "malformed counter state parameter ``{0}`` in npc"
                            " object (id ``{1}``)".format(pair[0], self.id_),
                            schema=schema)
            except KeyError:
                pass
            return counters

        def load_numbers():
            numbers = dict()
            try:
                for number in schema['state']['numbers']:
                    try:
                        pair = number.popitem()
                        numbers[pair[0]] = bool(pair[1])
                    except AttributeError:
                        numbers[number] = 0.0
                    except (TypeError, IndexError):
                        raise SchemaError(
                            "malformed number state parameter ``{0}`` in npc "
                            "object (id ``{1}``)".format(pair[0], self.id_),
                            schema=schema)
            except KeyError:
                pass
            return numbers

        def load_strings():
            strings = dict()
            try:
                for string in schema['state']['strings']:
                    try:
                        pair = string.popitem()
                        strings[pair[0]] = str(pair[1])
                    except AttributeError:
                        strings[string] = ''
                    except (TypeError, IndexError):
                        raise SchemaError(
                            "malformed string state parameter ``{0}`` in npc"
                            " object (id ``{1}``)".format(pair[0], self.id_),
                            schema=schema)
            except KeyError:
                pass
            return strings

        def load_events():
            events = list()
            try:
                event_id = schema['state']['encounter_event']
                try:
                    get_eventstream_by_id(event_id)
                except ObjectNotFoundError as e:
                    raise ObjectNotFoundError(
                        "tried to load npc object (id ``{0}``) with a "
                        "nonexistant event_id in its event registry - "
                        "requested id was ``{1}``"
                        .format(self.id_, event_id),
                        schema=schema) from e
                events.append(event_id)
            except KeyError as e:
                if e.args[0] == 'events':
                    # This is an error because eventless locales are not
                    # interactable and this error indicates that no events are
                    # attached to this npc.
                    raise SchemaError(
                        "tried to load npc object (id ``{0}``) with no"
                        " registered events".format(self.id_),
                        schema=schema)
                raise
            return events
        super().__init__(schema)
        try:
            self.id_ = schema['id']
        except KeyError:
            raise SchemaError("tried to load town object with no id field",
                              schema=schema)

        self.state['flags'] = load_flags()
        self.state['counters'] = load_counters()
        self.state['numbers'] = load_numbers()
        self.state['strings'] = load_strings()
        self.state['events'] = load_events()
        self.initialized = True
        npc_registry[self.id_] = self