def create_exit(self): content = self._content() area, room = find_area_room(content.start_room, self.player) new_dir = content.direction if room.find_exit(content.direction): raise DataError("Room already has " + new_dir + " exit.") rev_dir = Direction.ref_map[new_dir].rev_key other_id = content.dest_id if content.is_new: other_room = create_object(Room, {'dbo_id': other_id, 'title': content.dest_title}, False) publish_edit('create', other_room, self.session, True) update_next_room_id(area, self.session) else: other_area, other_room = find_area_room(other_id, self.player) if not content.one_way and other_room.find_exit(rev_dir): raise DataError("Room " + other_id + " already has a " + rev_dir + " exit.") this_exit = get_dbo_class('exit')() this_exit.dbo_owner = room this_exit.direction = new_dir this_exit.destination = other_id this_exit.on_loaded() room.exits.append(this_exit) save_object(room) publish_edit('update', room, self.session) if not content.one_way: other_exit = get_dbo_class('exit')() other_exit.dbo_owner = room other_exit.direction = rev_dir other_exit.destination = room.dbo_id other_exit.on_loaded() other_room.exits.append(other_exit) save_object(other_room) publish_edit('update', other_room, self.session, True) return this_exit.dto_value
def update(update_cls, set_key=None): nonlocal updated for dbo in load_object_set(update_cls, set_key): save_object(dbo) updated += 1 for child_type in getattr(dbo_cls, 'dbo_children_types', ()): update(get_dbo_class(child_type), '{}_{}s:{}'.format(dbo_key_type, child_type, dbo.dbo_id))
def main(self): constants = {key: config.get_value(key) for key in ['attributes', 'resource_pools', 'equip_types', 'equip_slots', 'weapon_types', 'damage_types', 'damage_delivery', 'damage_groups', 'affinities', 'imm_levels']} constants['weapon_options'] = constants['weapon_types'] + [{'dbo_id': 'unused'}, {'dbo_id': 'unarmed'}, {'dbo_id': 'any'}] constants['skill_calculation'] = constants['attributes'] + [{'dbo_id': 'roll', 'name': 'Dice Roll'}, {'dbo_id': 'skill', 'name': 'Skill Level'}] constants['defense_damage_types'] = constants['damage_types'] + constants['damage_groups'] constants['directions'] = Direction.ordered constants['article_load_types'] = ['equip', 'default'] constants['broadcast_types'] = broadcast_types constants['broadcast_tokens'] = broadcast_tokens constants['skill_types'] = [skill_template.dbo_key_type for skill_template in dbo_types(SkillTemplate)] constants['features'] = [get_dbo_class(feature_id)().edit_dto for feature_id in ['touchstone', 'entrance', 'store']] constants['shadow_types'] = ['any'] + list(implementors(Scriptable)) self._return(constants)
def _template_init(cls): template_cls = get_dbo_class(cls.template_id) old_class = getattr(template_cls, 'instance_cls', None) if old_class: existing_fields = old_class.dbot_fields.values() log.info("Overriding existing instance class {} with {} for template {}", old_class.__name__, cls.__name__, cls.template_id) else: log.info("Initializing instance class {} for template {}", cls.__name__, cls.template_id) existing_fields = () new_dbo_fields = {name: DBOField(*dbo_field.args, **dbo_field.kwargs) for name, dbo_field in cls.dbot_fields.items() if dbo_field not in existing_fields} template_cls.add_dbo_fields(new_dbo_fields) template_cls.instance_cls = cls cls.template_cls = template_cls
def delete_object(self, dbo): key = dbo.dbo_key dbo.db_deleted() self.delete_key(key) self._clear_old_refs(dbo) if dbo.dbo_set_key: self.redis.srem(dbo.dbo_set_key, dbo.dbo_id) for children_type in dbo.dbo_children_types: self.delete_object_set(get_dbo_class(children_type), "{}_{}s:{}".format(dbo.dbo_key_type, children_type, dbo.dbo_id)) for ix_name in dbo.dbo_indexes: ix_value = getattr(dbo, ix_name, None) if ix_value is not None and ix_value != '': self.delete_index('ix:{}:{}'.format(dbo.dbo_key_type, ix_name), ix_value) debug("object deleted: {}", key) self.evict_object(dbo)
def load_any(class_id, dbo_owner, dto_repr): if not dto_repr: return dbo_ref_id = None try: # The class_id passed in is what the field thinks it should hold # This can be overridden in the actual stored dictionary class_id = dto_repr["class_id"] except TypeError: # A dto_repr is either a string or a dictionary. If it's a string, # it must be reference, so capture the reference id dbo_ref_id = dto_repr except KeyError: pass dbo_class = get_dbo_class(class_id) if not dbo_class: return error("Unable to load reference for {}", class_id) # If this class has a key_type, it should always be a reference and we should load it from the database # The dto_representation in this case should always be a dbo_id if hasattr(dbo_class, "dbo_key_type"): return load_object(dbo_ref_id, dbo_class) # If we still have a dbo_ref_id, this must be part of an untyped collection, so the dbo_ref_id includes # both the class name and dbo_id and we should be able to load it if dbo_ref_id: return load_object(dbo_ref_id) # If this is a template, it should have a template key, so we load the template from the database using # the full key, then hydrate any non-template fields from the dictionary template_key = dto_repr.get("tk") if template_key: template = load_object(template_key) if template: return template.create_instance(dbo_owner).hydrate(dto_repr) else: warn("Missing template for template_key {}", template_key) return # Finally, it's not a template and it is not a reference to an independent DB object, it must be a pure child # object of this class, just set the owner and hydrate it instance = dbo_class() instance.dbo_owner = dbo_owner return instance.hydrate(dto_repr)
def create_object(self, dbo_class, dbo_dict, update_timestamp=True): dbo_class = get_dbo_class(getattr(dbo_class, 'dbo_key_type', dbo_class)) if not dbo_class: return try: dbo_id = dbo_dict['dbo_id'] except KeyError: dbo_id, dbo_dict = dbo_dict, {} if dbo_id is None or dbo_id == '': warn("create_object called with empty dbo_id") return dbo_id = str(dbo_id).lower() if self.object_exists(dbo_class.dbo_key_type, dbo_id): raise ObjectExistsError(dbo_id) dbo = dbo_class() dbo.dbo_id = dbo_id dbo.hydrate(dbo_dict) dbo.db_created() if dbo.dbo_set_key: self.redis.sadd(dbo.dbo_set_key, dbo.dbo_id) self.save_object(dbo, update_timestamp) return dbo
def dbo_child_keys(self, child_type): child_class = get_dbo_class(child_type) return sorted(fetch_set_keys("{}_{}s:{}".format(self.dbo_key_type, child_type, self.dbo_id)), key=child_class.dbo_key_sort)
def rebuild_immortal_list(): delete_key('immortals') player_cls = get_dbo_class('player') for player in load_object_set(player_cls): if player.imm_level: set_db_hash('immortals', player.dbo_id, player.imm_level)
def metadata(self): new_object_cls = get_dbo_class(self.dbo_key_type) shadows = [{'name': name, 'args': func.shadow_args.args} for name, func in inspect.getmembers(new_object_cls) if hasattr(func, 'shadow_args')] return {'perms': self._permissions(), 'parent_type': self.parent_type, 'children_types': self.children_types, 'new_object': new_object_cls.new_dto(), 'shadows': shadows}