def query_object_form(base_typeclass, obj_typeclass, obj_key): """ Query all data of an object. Args: base_typeclass: (string) candidate typeclass group. obj_typeclass: (string, optional) object's typeclass. If it is empty, use the typeclass of the object or use base typeclass as object's typeclass. obj_key: (string) object's key. If it is empty, query an empty form. """ candidate_typeclasses = TYPECLASS_SET.get_group(base_typeclass) if not candidate_typeclasses: raise MudderyError(ERR.no_table, "Can not find typeclass: %s" % base_typeclass) if not obj_typeclass: if obj_key: # Get typeclass from the object's record table_name = TYPECLASS("OBJECT").model_name record = general_query_mapper.get_record_by_key( table_name, obj_key) obj_typeclass = record.typeclass else: # Or use the base typeclass obj_typeclass = base_typeclass typeclass = TYPECLASS_SET.get(obj_typeclass) if not typeclass: raise MudderyError(ERR.no_table, "Can not find typeclass: %s" % obj_typeclass) table_names = typeclass.get_models() forms = [] for table_name in table_names: if obj_key: object_form = query_form(table_name, key=obj_key) else: object_form = query_form(table_name) forms.append({"table": table_name, "fields": object_form}) # add typeclasses if len(forms) > 0: for field in forms[0]["fields"]: if field["name"] == "typeclass": # set typeclass to the new value field["value"] = obj_typeclass field["type"] = "Select" field["choices"] = [ (key, cls.typeclass_name + " (" + key + ")") for key, cls in candidate_typeclasses.items() ] break return forms
def query_object_form(base_typeclass, obj_typeclass, obj_key): """ Query all data of an object. Args: base_typeclass: (string) candidate typeclass group. obj_typeclass: (string, optional) object's typeclass. If it is empty, use the typeclass of the object or use base typeclass as object's typeclass. obj_key: (string) object's key. If it is empty, query an empty form. """ candidate_typeclasses = TYPECLASS_SET.get_group(base_typeclass) if not candidate_typeclasses: raise MudderyError(ERR.no_table, "Can not find typeclass: %s" % base_typeclass) if not obj_typeclass: if obj_key: # Get typeclass from the object's record table_name = TYPECLASS("OBJECT").model_name record = general_query_mapper.get_record_by_key(table_name, obj_key) obj_typeclass = record.typeclass else: # Or use the base typeclass obj_typeclass = base_typeclass typeclass = TYPECLASS_SET.get(obj_typeclass) if not typeclass: raise MudderyError(ERR.no_table, "Can not find typeclass: %s" % obj_typeclass) table_names = typeclass.get_models() forms = [] for table_name in table_names: if obj_key: object_form = query_form(table_name, key=obj_key) else: object_form = query_form(table_name) forms.append({"table": table_name, "fields": object_form}) # add typeclasses if len(forms) > 0: for field in forms[0]["fields"]: if field["name"] == "typeclass": # set typeclass to the new value field["value"] = obj_typeclass field["type"] = "Select" field["choices"] = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in candidate_typeclasses.items()] break return forms
def __init__(self, *args, **kwargs): super(WorldRoomsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("ROOM") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) choices = [("", "---------")] objects = CM.WORLD_AREAS.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['location'] = forms.ChoiceField(choices=choices) choices = [("", "---------")] objects = CM.ICON_RESOURCES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['icon'] = forms.ChoiceField(choices=choices, required=False) choices = [("", "---------")] objects = CM.IMAGE_RESOURCES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['background'] = forms.ChoiceField(choices=choices, required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(WorldNPCsForm, self).__init__(*args, **kwargs) # NPC's typeclass typeclasses = TYPECLASS_SET.get_group("NON_PLAYER") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) # NPC's location rooms = CM.WORLD_ROOMS.objects.all() choices = [(r.key, r.name + " (" + r.key + ")") for r in rooms] self.fields['location'] = LocationField(choices=choices) # NPC's model choices = [("", "---------")] objects = CM.CHARACTER_MODELS.objects.all().values("key", "name").distinct() choices.extend([(obj["key"], obj["name"] + " (" + obj["key"] + ")") for obj in objects]) self.fields['model'] = forms.ChoiceField(choices=choices, required=False) # NPC's icon choices = [("", "---------")] objects = CM.ICON_RESOURCES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['icon'] = forms.ChoiceField(choices=choices, required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(ShopGoodsForm, self).__init__(*args, **kwargs) # all shops objects = CM.SHOPS.objects.all() choices = [(obj.key, obj.name + " (" + obj.key + ")") for obj in objects] self.fields['shop'] = forms.ChoiceField(choices=choices) # available objects choices = get_all_pocketable_objects() self.fields['object'] = forms.ChoiceField(choices=choices) # Goods typeclasses typeclasses = TYPECLASS_SET.get_group("SHOP_GOODS") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) # available units are common objects objects = CM.COMMON_OBJECTS.objects.all() choices = [(obj.key, obj.name + " (" + obj.key + ")") for obj in objects] self.fields['unit'] = forms.ChoiceField(choices=choices) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(SkillsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("SKILL") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) choices = [("", "---------")] objects = CM.ICON_RESOURCES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['icon'] = forms.ChoiceField(choices=choices, required=False) choices = [("", "---------")] objects = CM.SKILL_TYPES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['main_type'] = forms.ChoiceField(choices=choices, required=False) self.fields['sub_type'] = forms.ChoiceField(choices=choices, required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(EquipmentsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("EQUIPMENT") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) objects = CM.EQUIPMENT_POSITIONS.objects.all() choices = [(obj.key, obj.name + " (" + obj.key + ")") for obj in objects] self.fields['position'] = forms.ChoiceField(choices=choices) objects = CM.EQUIPMENT_TYPES.objects.all() choices = [(obj.key, obj.name + " (" + obj.key + ")") for obj in objects] self.fields['type'] = forms.ChoiceField(choices=choices) choices = [("", "---------")] objects = CM.ICON_RESOURCES.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) self.fields['icon'] = forms.ChoiceField(choices=choices, required=False) localize_form_fields(self) EQUIPMENT_ATTRIBUTES_INFO.set_form_fields(self)
def query_event_triggers(typeclass_key): """ Query all event triggers of the given typeclass. Args: typeclass_key: (string) the object's typeclass_key. """ return TYPECLASS_SET.get_trigger_types(typeclass_key)
def query_event_triggers(typeclass_key): """ Query all event triggers of the given typeclass. Args: typeclass_key: (string) the object's typeclass_key. """ return TYPECLASS_SET.get_trigger_types(typeclass_key)
def __init__(self, *args, **kwargs): super(QuestsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("QUEST") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) localize_form_fields(self)
def build_object(obj_key, level=None, caller=None, reset_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. level: (number) The object's level. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception as e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass if not record or not typeclass_path: ostring = "Can not find the data of %s." % obj_key print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return # Create object. try: name = getattr(record, "name", "") obj = create.create_object(typeclass_path, name) except Exception as e: ostring = "Can not create obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return try: # Set data info. obj.set_data_key(record.key, level, reset_location=reset_location) except Exception as e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return return obj
def __init__(self, *args, **kwargs): super(ShopsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("SHOP") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) self.fields['icon'] = IconField(required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(WorldAreasForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("AREA") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) self.fields['background'] = ImageField(required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(CommonObjectsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("COMMON_OBJECT") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) self.fields['icon'] = ImageField(image_type="icon", required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(FoodsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("FOOD") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) self.fields['icon'] = ImageField(image_type="icon", required=False) localize_form_fields(self) FOOD_ATTRIBUTES_INFO.set_form_fields(self)
def delete_object(base_typeclass, obj_key): """ Delete an object from all tables under the base typeclass. """ typeclasses = TYPECLASS_SET.get_group(base_typeclass) tables = set() for key, value in typeclasses.items(): tables.update(value.get_models()) for table in tables: try: general_query_mapper.delete_record_by_key(table, obj_key) except ObjectDoesNotExist: pass
def __init__(self, *args, **kwargs): super(WorldExitsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("EXIT") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) rooms = CM.WORLD_ROOMS.objects.all() choices = [(r.key, r.name + " (" + r.key + ")") for r in rooms] self.fields['location'] = LocationField(choices=choices) self.fields['destination'] = LocationField(choices=choices) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(WorldObjectsForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("WORLD_OBJECT") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) rooms = CM.WORLD_ROOMS.objects.all() choices = [(r.key, r.name + " (" + r.key + ")") for r in rooms] self.fields['location'] = LocationField(choices=choices) self.fields['icon'] = ImageField(image_type="icon", required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(SkillBooksForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("SKILL_BOOK") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) # skills objects = CM.SKILLS.objects.all() choices = [(obj.key, obj.name + " (" + obj.key + ")") for obj in objects] self.fields['skill'] = forms.ChoiceField(choices=choices) # icons self.fields['icon'] = IconField(required=False) localize_form_fields(self)
def __init__(self, *args, **kwargs): super(CommonCharacterForm, self).__init__(*args, **kwargs) typeclasses = TYPECLASS_SET.get_group("CHARACTER") choices = [(key, cls.typeclass_name + " (" + key + ")") for key, cls in typeclasses.items()] self.fields['typeclass'] = forms.ChoiceField(choices=choices) # models choices = [("", "---------")] objects = CM.CHARACTER_MODELS.objects.all() model_keys = set([obj.key for obj in objects]) choices.extend([(model_key, model_key) for model_key in model_keys]) self.fields['model'] = forms.ChoiceField(choices=choices, required=False) self.fields['icon'] = IconField(required=False) localize_form_fields(self)
def delete_object(obj_key, base_typeclass=None): """ Delete an object from all tables under the base typeclass. """ if not base_typeclass: table_name = TYPECLASS("OBJECT").model_name record = general_query_mapper.get_record_by_key(table_name, obj_key) base_typeclass = record.typeclass typeclasses = TYPECLASS_SET.get_group(base_typeclass) tables = set() for key, value in typeclasses.items(): tables.update(value.get_models()) with transaction.atomic(): for table in tables: try: general_query_mapper.delete_record_by_key(table, obj_key) except ObjectDoesNotExist: pass
def build_object(obj_key, caller=None, set_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception, e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass
def build_object(obj_key, caller=None, set_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception, e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass
def query_all_typeclasses(): """ Query all typeclasses. """ return TYPECLASS_SET.get_all_info()
def build_unique_objects(objects_data, type_name, caller=None): """ Build all objects in a model. Args: model_name: (string) The name of the data model. caller: (command caller) If provide, running messages will send to the caller. """ # new objects new_obj_keys = set(record.key for record in objects_data) # current objects current_objs = utils.search_obj_unique_type(type_name) # remove objects count_remove = 0 count_update = 0 count_create = 0 current_obj_keys = set() for obj in current_objs: obj_key = obj.get_data_key() if obj_key in current_obj_keys: # This object is duplcated. ostring = "Deleting %s" % obj_key print(ostring) if caller: caller.msg(ostring) # If default home will be removed, set default home to the Limbo. if obj.dbref == settings.DEFAULT_HOME: settings.DEFAULT_HOME = "#2" obj.delete() count_remove += 1 continue if not obj_key in new_obj_keys: # This object should be removed ostring = "Deleting %s" % obj_key print(ostring) if caller: caller.msg(ostring) # If default home will be removed, set default home to the Limbo. if obj.dbref == settings.DEFAULT_HOME: settings.DEFAULT_HOME = "#2" obj.delete() count_remove += 1 continue try: # set data obj.load_data() # put obj to its default location obj.reset_location() except Exception as e: ostring = "%s can not load data:%s" % (obj.dbref, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module( object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception as e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key) utils.set_obj_unique_type(obj, type_name) except Exception as e: ostring = "Can not set data info to obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue ostring = "Removed %d object(s). Created %d object(s). Updated %d object(s). Total %d objects.\n"\ % (count_remove, count_create, count_update, len(objects_data)) print(ostring) if caller: caller.msg(ostring)
obj.set_data_key(record.key, set_location=set_location) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return if record.typeclass == settings.TWO_WAY_EXIT_TYPECLASS_KEY: # If it's a two way exit, create the reverse exit. # Create object. try: obj = create.create_object( TYPECLASS_SET.get_module(settings.REVERSE_EXIT_TYPECLASS_KEY), record.name) except Exception, e: ostring = "Can not create obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return try: # Set data info. obj.set_data_key(settings.REVERSE_EXIT_PREFIX + record.key) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring)
current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module( object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception, e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key, getattr(record, "level", 0)) utils.set_obj_unique_type(obj, type_name) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (record.key,
current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module(object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception, e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key) utils.set_obj_unique_type(obj, type_name) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (record.key, e)