def after_data_loaded(self): """ Load goods data. Returns: None """ self.available = False self.shop_key = getattr(self.system, "shop", "") self.goods_key = getattr(self.system, "goods", "") self.goods_level = getattr(self.system, "level", 0) if not self.shop_key or not self.goods_key: if self.db.goods: self.db.goods.delete() self.db.goods = None return # set goods information self.price = getattr(self.system, "price", 0) self.unit_key = getattr(self.system, "unit", "") self.number = getattr(self.system, "number", 0) self.condition = getattr(self.system, "condition", "") # get price unit information unit_record = get_object_record(self.unit_key) if not unit_record: logger.log_errmsg("Can not find %s price unit %s." % (self.goods_key, self.unit_key)) return self.unit_name = unit_record.name # load goods object goods = self.db.goods if goods: if goods.get_data_key() == self.goods_key: # Load data. try: # Load db data. goods.load_data() except Exception as e: logger.log_errmsg("%s(%s) can not load data:%s" % (self.goods_key, self.dbref, e)) else: goods.set_data_key(self.goods_key, self.goods_level) else: goods = build_object(self.goods_key) if goods: self.db.goods = goods else: logger.log_err("Can not create goods %s." % self.goods_key) return self.name = goods.get_name() self.desc = goods.db.desc self.icon = getattr(goods, "icon", None) self.available = True
def after_data_loaded(self): """ Load goods data. Returns: None """ self.available = False self.shop_key = getattr(self.dfield, "shop", "") self.goods_key = getattr(self.dfield, "goods", "") if not self.shop_key or not self.goods_key: if self.db.goods: self.db.goods.delete() self.db.goods = None return # set goods information self.price = getattr(self.dfield, "price", 0) self.unit_key = getattr(self.dfield, "unit", "") self.number = getattr(self.dfield, "number", 0) self.condition = getattr(self.dfield, "condition", "") # get price unit information unit_record = get_object_record(self.unit_key) if not unit_record: logger.log_errmsg("Can not find %s price unit %s." % (self.goods_key, self.unit_key)) return self.unit_name = unit_record.name # load goods object goods = self.db.goods if goods: if goods.get_data_key() == self.goods_key: goods.load_data() else: goods.set_data_key(self.goods_key) else: goods = build_object(self.goods_key) if goods: self.db.goods = goods else: logger.log_err("Can not create goods %s." % self.goods_key) return self.name = goods.get_name() self.desc = goods.db.desc self.icon = getattr(goods, "icon", None) self.available = True
def after_data_loaded(self): """ Load goods data. Returns: None """ self.available = False self.shop_key = getattr(self.dfield, "shop", "") self.goods_key = getattr(self.dfield, "goods", "") if not self.shop_key or not self.goods_key: if self.db.goods: self.db.goods.delete() self.db.goods = None return # set goods information self.price = getattr(self.dfield, "price", 0) self.unit_key = getattr(self.dfield, "unit", "") self.number = getattr(self.dfield, "number", 0) self.condition = getattr(self.dfield, "condition", "") # get price unit information unit_record = get_object_record(self.unit_key) if not unit_record: logger.log_errmsg("Can not find %s price unit %s." % (self.goods_key, self.unit_key)) return self.unit_name = unit_record.name # load goods object goods = self.db.goods if goods: if goods.get_data_key() == self.goods_key: # Load data. try: # Load db data. goods.load_data() except Exception, e: logger.log_errmsg("%s(%s) can not load data:%s" % (self.goods_key, self.dbref, e)) else: goods.set_data_key(self.goods_key)
def receive_objects(self, obj_list, mute=False, combat=False): """ Add objects to the inventory. Args: obj_list: (list) a list of object keys and there numbers. list item: {"object": object's key "number": object's number} mute: (boolean) do not send messages to the owner combat: (boolean) get objects in combat. Returns: (dict) a list of objects that not have been received and their reasons. """ accepted_keys = {} # the keys of objects that have been accepted accepted_names = {} # the names of objects that have been accepted rejected_keys = {} # the keys of objects that have been rejected reject_reason = {} # the reasons of why objects have been rejected # check what the character has now inventory = {} for item in self.contents: key = item.get_data_key() if key in inventory: # if the character has more than one item of the same kind, # get the smallest stack. if inventory[key].db.number > item.db.number: inventory[key] = item else: inventory[key] = item for obj in obj_list: key = obj["object"] available = obj["number"] number = available accepted = 0 name = "" unique = False if number == 0: # it is an empty object if key in inventory: # already has this object accepted_keys[key] = 0 accepted_names[name] = 0 continue object_record = get_object_record(key) if not object_record: # can not find object's data record reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason continue if object_record.can_remove: # remove this empty object accepted_keys[key] = 0 accepted_names[name] = 0 continue # create a new content new_obj = build_object(key) if not new_obj: reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason continue name = new_obj.get_name() # move the new object to the character if not new_obj.move_to(self, quiet=True, emit_to_obj=self): new_obj.delete() reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason break # accept this object accepted_keys[key] = 0 accepted_names[name] = 0 else: # common number # if already has this kind of object if key in inventory: # add to current object name = inventory[key].name unique = inventory[key].unique add = number if add > inventory[key].max_stack - inventory[key].db.number: add = inventory[key].max_stack - inventory[key].db.number if add > 0: # increase stack number inventory[key].increase_num(add) number -= add accepted += add # if does not have this kind of object, or stack is full reason = "" while number > 0: if unique: # can not have more than one unique objects reason = _("Can not get more %s.") % name break # create a new content new_obj = build_object(key) if not new_obj: reason = _("Can not get %s.") % name break name = new_obj.get_name() unique = new_obj.unique # move the new object to the character if not new_obj.move_to(self, quiet=True, emit_to_obj=self): new_obj.delete() reason = _("Can not get %s.") % name break # Get the number that actually added. add = number if add > new_obj.max_stack: add = new_obj.max_stack if add <= 0: break new_obj.increase_num(add) number -= add accepted += add if accepted > 0: accepted_keys[key] = accepted accepted_names[name] = accepted if accepted < available: rejected_keys[key] = available - accepted reject_reason[name] = reason if not mute: # Send results to the player. message = {"get_objects": {"accepted": accepted_names, "rejected": reject_reason, "combat": combat}} self.msg(message) self.show_inventory() # call quest handler for key in accepted_keys: self.quest_handler.at_objective(defines.OBJECTIVE_OBJECT, key, accepted_keys[key]) return rejected_keys
def receive_objects(self, obj_list, mute=False, combat=False): """ Add objects to the inventory. Args: obj_list: (list) a list of object keys and there numbers. list item: {"object": object's key "number": object's number} mute: (boolean) do not send messages to the owner combat: (boolean) get objects in combat. Returns: (dict) a list of objects that not have been received and their reasons. """ accepted_keys = {} # the keys of objects that have been accepted accepted_names = {} # the names of objects that have been accepted rejected_keys = {} # the keys of objects that have been rejected reject_reason = {} # the reasons of why objects have been rejected # check what the character has now inventory = {} for item in self.contents: key = item.get_data_key() if key in inventory: # if the character has more than one item of the same kind, # get the smallest stack. if inventory[key].db.number > item.db.number: inventory[key] = item else: inventory[key] = item for obj in obj_list: key = obj["object"] available = obj["number"] number = available accepted = 0 name = "" unique = False if number == 0: # it is an empty object if key in inventory: # already has this object accepted_keys[key] = 0 accepted_names[name] = 0 continue object_record = get_object_record(key) if not object_record: # can not find object's data record reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason continue if object_record.can_remove: # remove this empty object accepted_keys[key] = 0 accepted_names[name] = 0 continue # create a new content new_obj = build_object(key) if not new_obj: reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason continue name = new_obj.get_name() # move the new object to the character if not new_obj.move_to(self, quiet=True, emit_to_obj=self): new_obj.delete() reason = _("Can not get %s.") % name rejected_keys[key] = 0 reject_reason[name] = reason break # accept this object accepted_keys[key] = 0 accepted_names[name] = 0 else: # common number # if already has this kind of object if key in inventory: # add to current object name = inventory[key].name unique = inventory[key].unique add = number if add > inventory[key].max_stack - inventory[key].db.number: add = inventory[key].max_stack - inventory[key].db.number if add > 0: # increase stack number inventory[key].increase_num(add) number -= add accepted += add # if does not have this kind of object, or stack is full reason = "" while number > 0: if unique: # can not have more than one unique objects reason = _("Can not get more %s.") % name break # create a new content new_obj = build_object(key) if not new_obj: reason = _("Can not get %s.") % name break name = new_obj.get_name() unique = new_obj.unique # move the new object to the character if not new_obj.move_to(self, quiet=True, emit_to_obj=self): new_obj.delete() reason = _("Can not get %s.") % name break # Get the number that actually added. add = number if add > new_obj.max_stack: add = new_obj.max_stack if add <= 0: break new_obj.increase_num(add) number -= add accepted += add if accepted > 0: accepted_keys[key] = accepted accepted_names[name] = accepted if accepted < available: rejected_keys[key] = available - accepted reject_reason[name] = reason if not mute: # Send results to the player. message = {"get_objects": {"accepted": accepted_names, "rejected": reject_reason, "combat": combat}} self.msg(message) self.show_inventory() # call quest handler for key in accepted_keys: self.quest_handler.at_objective(defines.OBJECTIVE_OBJECT, key, accepted_keys[key]) return rejected_keys
goods_key = self.db.goods_key # Get goods record. goods_record = None try: goods_model = apps.get_model(settings.WORLD_DATA_APP, settings.SHOP_GOODS) if goods_model: # Get records. goods_record = goods_model.objects.get(shop=shop_key, goods=goods_key) except Exception, e: logger.log_errmsg("Can not find goods %s in shop %s." % (shop_key, goods_key)) return # get price unit information unit_name = None unit_record = get_object_record(goods_record.unit) if not unit_record: logger.log_errmsg("Can not find %s price unit %s." % (goods_key, goods_record.unit)) return unit_name = unit_record.name # load goods object goods = self.db.goods if goods: if goods.get_data_key == goods_key: goods.load_data() else: goods.set_data_key(goods_key) else: goods = build_object(goods_key) if goods: