예제 #1
0
파일: item.py 프로젝트: hw233/test-2
def gain_item(data, infos, str, use_type):
    """获得新物品
    如果原来已经拥有物品,物品数量增加
    如果原来没有这件物品,添加物品
    Args:
        infos[list(basic_id, num)]: 奖励的物品信息
    Returns:
        True: 成功
        False: 失败
    """
    output_items = []
    for (basic_id, num) in infos:
        id = ItemInfo.generate_id(data.id, basic_id)
        item = data.item_list.get(id)
        if item is not None:
            item.acquire(num)
        else:
            item = ItemInfo.create(data.id, basic_id, num)
            data.item_list.add(item)
        item = [basic_id, num, item.num]
        output_items.append("[item=")
        output_items.append(utils.join_to_string(item))
        output_items.append("]")
    item = ''.join(output_items)
    log = log_formater.output_item(data, str, use_type, item)
    logger.notice(log)
    #logger.notice("gain items %s"% ''.join(output_items))
    return True
예제 #2
0
    def _pack_signin_response(self, data, hero_list, item_list, now):
        """封装签到响应
        """
        #为了封装 response 构造 HeroInfo 和 ItemInfo
        win_hero = []
        for (basic_id, num) in hero_list:
            soldier_basic_id = HeroInfo.get_default_soldier_basic_id(basic_id)
            soldier_id = SoldierInfo.generate_id(data.id, soldier_basic_id)
            soldier = data.soldier_list.get(soldier_id, True)
            hero = HeroInfo.create(data.id, basic_id, soldier, [])
            for i in range(0, num):
                win_hero.append(hero)

        win_item = []
        for (basic_id, num) in item_list:
            item = ItemInfo.create(data.id, basic_id, num)
            win_item.append(item)

        res = user_pb2.SignInRes()
        res.status = 0
        if len(win_hero) > 0:
            assert len(win_hero) == 1
            pack.pack_hero_info(win_hero[0], res.hero, now)
        if len(win_item) > 0:
            assert len(win_item) == 1
            pack.pack_item_info(win_item[0], res.item)

        return res
예제 #3
0
    def _pack_draw_response(self, data, hero_list, item_list,
            resource, search_num, next_time, now):
        """封装响应
        返回客户端的是抽奖获得的英雄和物品(并未将已经拥有的英雄分解成将魂石)
        客户端会自行进行分解
        """
        win_hero = []
        for (basic_id, num) in hero_list:
            soldier_basic_id = HeroInfo.get_default_soldier_basic_id(basic_id)
            soldier_id = SoldierInfo.generate_id(data.id, soldier_basic_id)
            soldier = data.soldier_list.get(soldier_id, True)

            hero = HeroInfo.create(data.id, basic_id, soldier, technology_basic_ids = [])
            for i in range(0, num):
                win_hero.append(hero)

        win_item = []
        for (basic_id, num) in item_list:
            item = ItemInfo.create(data.id, basic_id, num)
            win_item.append(item)

        res = wineShop_pb2.WineShopDrawRes()
        res.status = 0

        for item in win_item:
            pack.pack_item_info(item, res.items.add())
        for hero in win_hero:
            pack.pack_hero_info(hero, res.heroes.add(), now)
        pack.pack_resource_info(resource, res.resource)
        res.draw.search_num = search_num
        res.draw.next_left_time = max(0, next_time - now)
        return res
예제 #4
0
파일: item.py 프로젝트: hw233/test-2
def compose(data, src_info, dest_basic_id, now, dest_num=1):
    """物品合成
    Args:
        src_info[list(basic_id)]: 原材料物品信息
        dest_basic_id[int]: 合成物品的 basic id
        dest_num[int]: 合成的物品的数量,默认是1
    Returns:
        True: 合成成功
        False:合成失败
    """
    src_basic_id = data_loader.ItemComposeBasicInfo_dict[dest_basic_id].srcId
    src_num = data_loader.ItemComposeBasicInfo_dict[dest_basic_id].srcNum
    money_cost = data_loader.ItemComposeBasicInfo_dict[
        dest_basic_id].moneyCost * dest_num
    assert len(src_basic_id) == len(src_num)

    #花费金钱
    resource = data.resource.get()
    resource.update_current_resource(now)
    if not resource.cost_money(money_cost):
        logger.warning("not enough money[money=%d][need=%d]" %
                       (resource.money, money_cost))
        return False

    if set(src_info) != set(src_basic_id):
        logger.debug("Source item error[expect=%s][source=%s]" %
                     (src_basic_id, src_info))
        return False
    output_items = []
    for index in range(0, len(src_basic_id)):
        src_id = ItemInfo.generate_id(data.id, src_basic_id[index])
        src_item = data.item_list.get(src_id)
        consume = src_item.consume(src_num[index] * dest_num)
        if not consume[0]:
            return False
        output_items.append("[item=")
        output_items.append(utils.join_to_string(list(consume[1])))
        output_items.append("]")
    item = ''.join(output_items)
    log = log_formater.output_item(data, "compose consume",
                                   log_formater.COMPOSE_CONSUME, item)
    logger.notice(log)

    dest_id = ItemInfo.generate_id(data.id, dest_basic_id)
    dest = data.item_list.get(dest_id)
    if dest is None:
        #新物品
        dest = ItemInfo.create(data.id, dest_basic_id, dest_num)
        data.item_list.add(dest)
    else:
        dest.acquire(dest_num)
    compose_item = "[item=" + utils.join_to_string(
        [dest_basic_id, dest_num, dest.num]) + "]"
    log = log_formater.output_item(data, "compose gain",
                                   log_formater.COMPOSE_GAIN, compose_item)
    logger.notice(log)
    return dest
예제 #5
0
    def _pack_treasure_draw_response(self, data, item_list, resource, alltimes, timer):
        win_item = []
        for (basic_id, num) in item_list:
            item = ItemInfo.create(data.id, basic_id, num)
            win_item.append(item)

        res = activity_pb2.TurntableDrawRes()
        res.status = 0
        for item in win_item:
            pack.pack_item_info(item, res.items.add())
        pack.pack_resource_info(resource, res.resource)
        res.all_times = alltimes
        return res
예제 #6
0
파일: item.py 프로젝트: hw233/test-2
def init_default_items(data, pattern):
    """创建一个新帐号时,初始化赠送的物品
    Args:
        data[UserData]: 用户信息
        pattern[int]: 初始化模式
    """
    basic_ids = data_loader.InitUserBasicInfo_dict[pattern].itembasicId
    nums = data_loader.InitUserBasicInfo_dict[pattern].itemNum
    assert len(basic_ids) == len(nums)

    for index in range(0, len(basic_ids)):
        item = ItemInfo.create(data.id, basic_ids[index], nums[index])
        data.item_list.add(item)

    return True
예제 #7
0
파일: item.py 프로젝트: hw233/test-2
def casting(data, src_info, dest_basic_id, dest_num, building_level):
    """物品熔铸
    Args:
        src_info[list((basic_id, casting_num))]: 原材料物品信息
        dest_basic_id[int]: 熔铸物品的 basic id
        building_level[int]:铁匠铺等级
    Returns:
        True: 合成成功
        False:合成失败
    """
    if not data_loader.BlackSmithBasicInfo_dict.has_key(building_level):
        logger.warning(
            "BlackSmith building level is error[building_level=%d]" %
            building_level)
        return (False, None)

    #dest
    dest_item_basic = data_loader.ItemBasicInfo_dict[dest_basic_id]
    dest_casting_value = dest_item_basic.value
    if (dest_item_basic.type != int(
            float(data_loader.OtherBasicInfo_dict["item_equipment_upgrade"].
                  value)) or dest_casting_value == 0):
        logger.warning("Casting dest item error[basic_id=%d]" % dest_basic_id)
        return (False, None)
    if dest_basic_id not in data_loader.BlackSmithBasicInfo_dict[
            building_level].itemIds:
        logger.warning(
            "BlackSmith building level is not reached[building_level=%d][dest_basic_id=%d]"
            % (building_level, dest_basic_id))
        return (False, None)

    #p
    p = float(data_loader.OtherBasicInfo_dict["P_BlackSmith"].value)

    #src
    src_casting_value = 0
    output_items = []
    for info in src_info:
        src_id = ItemInfo.generate_id(data.id, info[0])
        src_item = data.item_list.get(src_id)
        if not src_item.is_equipment_upgrade_item():
            logger.warning("Casting source item type error[basic_id=%d]" %
                           info[0])
            return (False, None)

        #消耗熔铸的原材料
        consume = src_item.consume(info[1])
        if not consume[0]:
            logger.warning(
                "Casting source item num error[basic_id=%d][num=%d]" %
                (src_item.basic_id, src_item.num))
            return (False, None)
        output_items.append("[item=")
        output_items.append(utils.join_to_string(list(consume[1])))
        output_items.append("]")
        src_casting_value += min(
            data_loader.ItemBasicInfo_dict[info[0]].value * p,
            dest_casting_value) * info[1]
    item = ''.join(output_items)
    log = log_formater.output_item(data, "casting consume",
                                   log_formater.CASTING_CONSUME, item)
    logger.notice(log)
    #logger.notice("casting %s"%''.join(output_items))
    #熔铸成功率
    p_casting = 1.0 * src_casting_value / (dest_casting_value * dest_num)

    random.seed()
    p_random = random.random()
    dest_item = None
    if p_random < p_casting:
        #熔铸成功
        dest_id = ItemInfo.generate_id(data.id, dest_basic_id)
        dest_item = data.item_list.get(dest_id)
        if dest_item is None:
            #新物品
            dest_item = ItemInfo.create(data.id, dest_basic_id, dest_num)
            data.item_list.add(dest_item)
        else:
            dest_item.acquire(dest_num)
        casting_item = "[item=" + utils.join_to_string(
            [dest_basic_id, dest_num, dest_item.num]) + "]"
        log = log_formater.output_item(data, "casting gain",
                                       log_formater.CASTING_GAIN, casting_item)
        logger.notice(log)

#logger.notice("gain item[item=%d#%d#%d]"%(dest_basic_id, dest_num, dest_item.num))

    logger.debug(
        "Casting result[src_value=%d][dest_value=%d][p_casting=%f][p_random=%f]"
        % (src_casting_value, dest_casting_value, p_casting, p_random))
    return (True, dest_item)