Пример #1
0
    def load_shops(self):
        """
        Load character's shop.
        """
        # shops records
        shop_records = NPCShops.get(self.get_data_key())

        shop_keys = set([record.shop for record in shop_records])

        # remove old shops
        for shop_key in self.db.shops:
            if shop_key not in shop_keys:
                # remove this shop
                self.db.shops[shop_key].delete()
                del self.db.shops[shop_key]

        # add new shop
        for shop_record in shop_records:
            shop_key = shop_record.shop
            if shop_key not in self.db.shops:
                # Create shop object.
                shop_obj = build_object(shop_key)
                if not shop_obj:
                    logger.log_errmsg("Can't create shop: %s" % shop_key)
                    continue

                self.db.shops[shop_key] = shop_obj
                shop_obj.set_owner(self)
Пример #2
0
    def load_goods(self):
        """
        Load shop goods.
        """
        # shops records
        goods_records = ShopGoods.get(self.get_data_key())

        goods_keys = set([record.key for record in goods_records])

        # search current goods
        current_goods = {}
        for key, obj in self.db.goods.items():
            if key in goods_keys:
                current_goods[key] = obj
            else:
                # remove goods that is not in goods_keys
                obj.delete()

        # add new goods
        for goods_record in goods_records:
            goods_key = goods_record.key
            if goods_key not in current_goods:
                # Create shop_goods object.
                goods_obj = build_object(goods_key)
                if not goods_obj:
                    logger.log_errmsg("Can't create goods: %s" % goods_key)
                    continue

                current_goods[goods_key] = goods_obj

        self.db.goods = current_goods
Пример #3
0
    def attack_temp_target(self, target_key, target_level=0, desc=""):
        """
        Attack a temporary clone of a target. This creates a new character object for attack.
        The origin target will not be affected.

        Args:
            target_key: (string) the info key of the target object.
            target_level: (int) target object's level
            desc: (string) string to describe this attack

        Returns:
            (boolean) fight begins
        """
        if target_level == 0:
            # Find the target and get its level.
            obj = search_obj_data_key(target_key)
            if obj:
                obj = obj[0]
                target_level = obj.db.level

        # Create a target.
        target = build_object(target_key, target_level, reset_location=False)
        if not target:
            logger.log_errmsg("Can not create the target %s." % target_key)
            return False

        target.is_temp = True
        return self.attack_target(target, desc)
Пример #4
0
    def learn_skill(self, skill_key, is_default, silent):
        """
        Learn a new skill.

        Args:
            skill_key: (string) skill's key
            is_default: (boolean) if it is a default skill
            silent: (boolean) do not show messages to the player

        Returns:
            (boolean) learned skill
        """
        if skill_key in self.db.skills:
            self.msg({"msg": _("You have already learned this skill.")})
            return False

        # Create skill object.
        skill_obj = build_object(skill_key)
        if not skill_obj:
            self.msg({"msg": _("Can not learn this skill.")})
            return False

        # set default
        if is_default:
            skill_obj.set_default(is_default)

        # Store new skill.
        skill_obj.set_owner(self)
        self.db.skills[skill_key] = skill_obj

        # If it is a passive skill, player's status may change.
        if skill_obj.passive:
            self.refresh_properties(True)

        # Notify the player
        if not silent and self.has_account:
            self.show_status()
            self.show_skills()
            self.msg(
                {"msg": _("You learned skill {C%s{n.") % skill_obj.get_name()})

        return True
Пример #5
0
    def accept(self, quest_key):
        """
        Accept a quest.

        Args:
            quest_key: (string) quest's key

        Returns:
            None
        """
        if quest_key in self.current_quests:
            return

        # Create quest object.
        new_quest = build_object(quest_key)
        if not new_quest:
            return

        new_quest.set_owner(self.owner)
        self.current_quests[quest_key] = new_quest

        self.owner.msg({"msg": _("Accepted quest {C%s{n.") % new_quest.get_name()})
        self.show_quests()
        self.owner.show_location()
Пример #6
0
    def receive_objects(self, obj_list, mute=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

        Returns:
            (list) a list of objects that not have been received and their reasons.
            [{
                "key": key,
                "name": name,
                "level": level,
                "number": number,
                "icon": icon,
                "reject": reason,
            }]
        """
        objects = []  # objects that have been accepted

        # 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"]
            level = obj.get("level")
            available = obj["number"]
            name = ""
            icon = ""
            number = available
            accepted = 0
            reject = False
            unique = False

            if number == 0:
                # it is an empty object
                if key in inventory:
                    # already has this object
                    continue

                object_record = None
                try:
                    common_model_name = TYPECLASS("COMMON_OBJECT").model_name
                    object_record = WorldData.get_table_data(common_model_name,
                                                             key=key)
                    object_record = object_record[0]
                except Exception as e:
                    pass

                if not object_record:
                    # can not find object's data record
                    continue

                if object_record.can_remove:
                    # remove this empty object
                    continue

                # create a new content
                new_obj = build_object(key, level=level)
                if not new_obj:
                    reject = _("Can not get %s.") % key
                else:
                    name = new_obj.get_name()
                    icon = new_obj.icon

                # move the new object to the character
                if not new_obj.move_to(self, quiet=True, emit_to_obj=self):
                    new_obj.delete()
                    reject = _("Can not get %s.") % name
            else:
                # common number
                # if already has this kind of object
                if key in inventory:
                    # add to current object
                    name = inventory[key].name
                    icon = inventory[key].icon
                    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
                while number > 0:
                    if unique:
                        # can not have more than one unique objects
                        reject = _("Can not get more %s.") % name
                        break

                    # create a new content
                    new_obj = build_object(key, level=level)
                    if not new_obj:
                        reject = _("Can not get %s.") % name
                        break

                    name = new_obj.get_name()
                    icon = new_obj.icon
                    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()
                        reject = _("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

            objects.append({
                "key": key,
                "name": name,
                "icon": icon,
                "number": accepted,
                "reject": reject,
            })

        if not mute:
            # Send results to the player.
            message = {"get_objects": objects}
            self.msg(message)

        self.show_inventory()

        # call quest handler
        for item in objects:
            if not item["reject"]:
                self.quest_handler.at_objective(defines.OBJECTIVE_OBJECT,
                                                item["key"], item["number"])

        return objects