def api_reallot_nature(natures): """ api/charactor/reallot_nature Args: natures(dir): 元素类别-新重置的数量 例如: "natures": { "0": 1, "1": 4, "3": 2, .... } """ ubase = request.user # 消耗元素重置道具 tools.del_user_things(ubase, 'elementrefresh_item', 1, 'reallot_nature') uproperty = ubase.user_property nature_types = ["0", "1", "2", "3", "4", "5", "remain"] has_nature_num = sum([getattr(uproperty, "nature_" + nature_type, 0) for nature_type in nature_types]) consume_nature_num = sum(natures.values()) remain_nature = has_nature_num - consume_nature_num if remain_nature < 0: raise LogicError("Not enough natures!") for nature_type, num in natures.items(): if nature_type not in nature_types[:-1]: raise LogicError("Unkonew nature type!") tools.update_user_info(uproperty, "nature_" + nature_type, num, "reallot_nature") tools.update_user_info(uproperty, "nature_remain", remain_nature, "reallot_nature") return {}
def api_new_arena(): """ api/arena/new_arena 开启新的竞技场 Returns: cards_pool(list): 此次竞技候选卡 """ uarena = request.user.user_arena if uarena.is_in_arena(): raise LogicError('Aready in arena') common_config = uarena._common_config # 扣竞技场道具 若无 扣金币 uitems = uarena.user_items if uitems.get_item_num('arenaticket_item'): tools.del_user_things(uarena, 'arenaticket_item', 1, 'new_arena') else: need_coin = common_config['open_arena_coin'] tools.del_user_things(uarena, 'coin', need_coin, 'new_arena') team_length = common_config['team_length'] cards_pool = [] for step in range(1, team_length + 1): cards_pool.append(get_arena_cards(uarena, step)) uarena.set_cards_pool(cards_pool) uarena.set_step(1) return {'cards_pool': cards_pool}
def api_reallot_nature(natures): """ api/charactor/reallot_nature Args: natures(dir): 元素类别-新重置的数量 例如: "natures": { "0": 1, "1": 4, "3": 2, .... } """ ubase = request.user # 消耗元素重置道具 tools.del_user_things(ubase, 'elementrefresh_item', 1, 'reallot_nature') uproperty = ubase.user_property nature_types = ["0", "1", "2", "3", "4", "5", "remain"] has_nature_num = sum([ getattr(uproperty, "nature_" + nature_type, 0) for nature_type in nature_types ]) consume_nature_num = sum(natures.values()) remain_nature = has_nature_num - consume_nature_num if remain_nature < 0: raise LogicError("Not enough natures!") for nature_type, num in natures.items(): if nature_type not in nature_types[:-1]: raise LogicError("Unkonew nature type!") tools.update_user_info(uproperty, "nature_" + nature_type, num, "reallot_nature") tools.update_user_info(uproperty, "nature_remain", remain_nature, "reallot_nature") return {}
def api_upgrade(card_id, lv_num): """ api/card/upgrade 升级卡片 Args: card_id(str): 要升级的卡片id lv_num(int): 要升级的等级 """ ucards = request.user.user_cards consume_conf = ucards._cardup_config['lvup_consume_heroSoul'] max_card_lv = ucards._common_config.get('max_card_lv', 15) card_quality = str(ucards._card_config[card_id]["quality"]) now_lv = ucards.get_card_lv(card_id) if now_lv + lv_num > max_card_lv: raise LogicError("The card got the top lv") need_heroSoul = 0 # 计算需消耗英魂 for add_lv_cnt in range(lv_num): need_heroSoul += consume_conf[card_quality][now_lv - 1] now_lv += 1 tools.del_user_things(ucards, 'heroSoul', need_heroSoul, 'card_upgrade') new_card_info = ucards.add_card_lv(card_id, lv_num) # 升满级后加对应城市等级 if new_card_info['lv'] == max_card_lv: camp = str(ucards._card_config[card_id]['camp']) ucities = ucards.user_cities if ucities.has_show_city(camp): ucities.up_city_lv(camp) umodified = request.user.user_modified umodified.set_modify_info('cards', {card_id: new_card_info}) return {}
def api_buy_discount_cards(): """ api/arena/buy_discount_cards 购买竞技场卡包 """ uarena = request.user.user_arena if uarena.step != 7: return {} all_price = 0 sell_cards = [] price_conf = uarena._cardup_config['price'] card_conf = uarena._card_config discount_rate = uarena._common_config['arena_discount_rate'] for cid in uarena.selected_cards: own_cards = uarena.user_cards.cards if cid in own_cards and own_cards[cid]['num'] != 0: continue quality = str(card_conf[cid]['quality']) all_price += price_conf[quality] sell_cards.append(cid) all_price = int(all_price * discount_rate) tools.del_user_things(uarena, 'diamond', all_price, 'arena_buy_discount_cards') for cid in sell_cards: tools.add_user_things(uarena, cid, 1, 'arena_buy_discount_cards') uarena.reset_arena() return {}
def api_buy_discount_cards(): """ api/arena/buy_discount_cards 购买竞技场卡包 """ uarena = request.user.user_arena if uarena.step != 7: return {} all_price = 0 sell_cards = [] price_conf = uarena._cardup_config['price'] card_conf = uarena._card_config discount_rate = uarena._common_config['arena_discount_rate'] for cid in uarena.selected_cards: own_cards = uarena.user_cards.cards if cid in own_cards and own_cards[cid]['num'] != 0: continue quality = str(card_conf[cid]['quality']) all_price += price_conf[quality] sell_cards.append(cid) all_price = int(all_price * discount_rate) tools.del_user_things(uarena, 'diamond', all_price, 'arena_buy_discount_cards') for cid in sell_cards: tools.add_user_things(uarena, cid, 1, 'arena_buy_discount_cards') uarena.reset_arena() return {}
def api_new_arena(): """ api/arena/new_arena 开启新的竞技场 Returns: cards_pool(list): 此次竞技候选卡 """ uarena = request.user.user_arena if uarena.is_in_arena(): raise LogicError('Aready in arena') common_config = uarena._common_config # 扣竞技场道具 若无 扣金币 uitems = uarena.user_items if uitems.get_item_num('arenaticket_item'): tools.del_user_things(uarena, 'arenaticket_item', 1, 'new_arena') else: need_coin = common_config['open_arena_coin'] tools.del_user_things(uarena, 'coin', need_coin, 'new_arena') team_length = common_config['team_length'] cards_pool = [] for step in range(1, team_length + 1): cards_pool.append(get_arena_cards(uarena, step)) uarena.set_cards_pool(cards_pool) uarena.set_step(1) return {'cards_pool': cards_pool}
def api_upgrade(card_id, lv_num): """ api/card/upgrade 升级卡片 Args: card_id(str): 要升级的卡片id lv_num(int): 要升级的等级 """ ucards = request.user.user_cards consume_conf = ucards._cardup_config['lvup_consume_heroSoul'] max_card_lv = ucards._common_config.get('max_card_lv', 15) card_quality = str(ucards._card_config[card_id]["quality"]) now_lv = ucards.get_card_lv(card_id) if now_lv + lv_num > max_card_lv: raise LogicError("The card got the top lv") need_heroSoul = 0 # 计算需消耗英魂 for add_lv_cnt in range(lv_num): need_heroSoul += consume_conf[card_quality][now_lv - 1] now_lv += 1 tools.del_user_things(ucards, 'heroSoul', need_heroSoul, 'card_upgrade') new_card_info = ucards.add_card_lv(card_id, lv_num) # 升满级后加对应城市等级 if new_card_info['lv'] == max_card_lv: camp = str(ucards._card_config[card_id]['camp']) ucities = ucards.user_cities if ucities.has_show_city(camp): ucities.up_city_lv(camp) umodified = request.user.user_modified umodified.set_modify_info('cards', {card_id: new_card_info}) return {}
def api_start_invade(team_index='', new_team=None): """ api/invade/start_invade 开打 Args: new_team(list): 战斗编队 team_index(str): 选取的那个城编队去打 Returns: enemy(dict): nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 team_index: 敌人队伍军旗 team_index_lv: 敌人队伍军旗等级 card_lv(list->int): 各卡片等级 card_favor(list->int): 各卡片好感度 city_lv: 城战发生城市等级 """ if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team) uInvade = request.user.user_invade # 消耗体力 need_stamina = uInvade._common_config['invade_fight_stamina'] tools.del_user_things(uInvade, 'stamina', need_stamina, 'invade') opponent_info = uInvade.opponent opponent_uid = opponent_info['uid'] # 记录战前信息 umodified = uInvade.user_modified umodified.add_dungeon_info('invade', { 'opponent_info': opponent_info, }) # 如果是虚拟玩家,造一个数据 if not opponent_uid: opponent_team_info = { 'lv': 10, 'nature_0': 5, 'nature_1': 5, 'nature_2': 5, 'nature_3': 5, 'nature_4': 5, 'nature_5': 5, 'team': ['1_card', '2_card', '3_card', '4_card'], 'team_index': '', 'team_index_lv': 0, 'city_lv': 0, 'card_lv': [2, 4, 6, 8], 'card_favor': [0, 1, 0, 1], } else: opponent_invade = UserInvade.get(opponent_uid) opponent_team_info = opponent_invade.watch_team_info() # 重置 连续寻找对手次数 清空已找到对手 uInvade.reset_refresh_cnt() uInvade.clear_opponent() return {'enemy': opponent_team_info}
def api_start_invade(team_index='', new_team=None): """ api/invade/start_invade 开打 Args: new_team(list): 战斗编队 team_index(str): 选取的那个城编队去打 Returns: enemy(dict): nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 team_index: 敌人队伍军旗 team_index_lv: 敌人队伍军旗等级 card_lv(list->int): 各卡片等级 card_favor(list->int): 各卡片好感度 city_lv: 城战发生城市等级 """ if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team) uInvade = request.user.user_invade # 消耗体力 need_stamina = uInvade._common_config['invade_fight_stamina'] tools.del_user_things(uInvade, 'stamina', need_stamina, 'invade') opponent_info = uInvade.opponent opponent_uid = opponent_info['uid'] # 记录战前信息 umodified = uInvade.user_modified umodified.add_dungeon_info('invade', { 'opponent_info': opponent_info, }) # 如果是虚拟玩家,造一个数据 if not opponent_uid: opponent_team_info = { 'lv': 10, 'nature_0': 5, 'nature_1': 5, 'nature_2': 5, 'nature_3': 5, 'nature_4': 5, 'nature_5': 5, 'team': ['1_card', '2_card', '3_card', '4_card'], 'team_index': '', 'team_index_lv': 0, 'city_lv': 0, 'card_lv': [2, 4, 6, 8], 'card_favor':[0, 1, 0, 1], } else: opponent_invade = UserInvade.get(opponent_uid) opponent_team_info = opponent_invade.watch_team_info() # 重置 连续寻找对手次数 清空已找到对手 uInvade.reset_refresh_cnt() uInvade.clear_opponent() return {'enemy': opponent_team_info}
def api_refresh_challenge_floor(city_id): """ api/dungeon/refresh_challenge_floor 刷新挑战模式大关卡 Argvs: city_id(str): 需刷新的城市id """ ubase = request.user ucities = ubase.user_cities tools.del_user_things(ubase, 'challengerefresh_item', 1, 'refresh_challenge_floor') new_info = ucities.refresh_challenge(city_id) umodified = ubase.user_modified umodified.set_modify_info('cities', new_info) return {}
def api_refresh_challenge_floor(city_id): """ api/dungeon/refresh_challenge_floor 刷新挑战模式大关卡 Argvs: city_id(str): 需刷新的城市id """ ubase = request.user ucities = ubase.user_cities tools.del_user_things(ubase, 'challengerefresh_item', 1, 'refresh_challenge_floor') new_info = ucities.refresh_challenge(city_id) umodified = ubase.user_modified umodified.set_modify_info('cities', new_info) return {}
def clear_player(player): del_all_player(player) del_pear_dict(player.opponent) # 已近进入战斗要扣体力 if player.fight_status >= 1: uproperty = UserProperty.get(player.uid) need_stamina = uproperty._common_config['pvp_stamina'] tools.del_user_things(uproperty, 'stamina', need_stamina, 'pvp_start') uproperty.do_put() pier_clear(player.uid) player.connecting = False if not player.websocket.closed: player.websocket.close()
def api_invade_shopping(index): """ api/shop/invade_shopping 购买城战商品,消耗城战代币 Args: index(str): 商品序号 """ uinvade = request.user.user_invade invadeshop_config = uinvade._invadeshop_config if index not in invadeshop_config: return {} shop = invadeshop_config[index] # 消耗城战代币 tools.del_user_things(uinvade, 'invade_jeton', shop['cost_invade_jeton'], 'invade_shopping') # 添加奖励 tools.add_user_awards(uinvade, shop['award'], 'invade_shopping') return {}
def api_find_opponent(): """ api/invade/find_opponent 寻找对手 Retures: 同api_info """ uInvade = request.user.user_invade common_config = uInvade._common_config umodified = uInvade.user_modified # 消耗金币 coin_conf = common_config['invade_refresh_coin'] refresh_coin = coin_conf[min(uInvade.refresh_cnt, len(coin_conf) - 1)] tools.del_user_things(uInvade, 'coin', refresh_coin, 'invade_find') # 自增连续寻找对手次数 uInvade.inc_refresh_cnt() # 需找对手, 添加过期时间 invade_user_instance = InvadeUser.get_instance() opponent_info = invade_user_instance.get_fight_user( except_uids=[uInvade.uid]) expire_time = common_config['invade_keep_opponent_seconds'] + int( time.time()) opponent_info['expire_time'] = expire_time # 此对手不能被别的玩家搜到 if opponent_info['uid']: invade_user_instance.add_user(opponent_info['uid'], expire_time) # 失败只损失一个奖杯 opponent_info['lose_award'] = { 'cup': -1, } # 掠夺的金币和对手主城进贡有关 userlv_config = uInvade._userlv_config coef = common_config.get('invade_coin_coef', 0.2) win_get_coin = int(userlv_config[str(opponent_info['lv'])]['reward_coin'] * coef) if opponent_info['uid'] else 0 # 连胜两次以上获得两个杯,否则一个 opponent_info['win_award'] = { 'cup': 1 if uInvade.consecutive_win < 2 else 2, 'coin': win_get_coin, } # 记录下对手信息,若有旧对手,将他的过期时间重置 if uInvade.opponent and uInvade.opponent['uid']: invade_user_instance.add_user(uInvade.opponent['uid']) uInvade.set_opponent(opponent_info) return api_info()
def api_allot_nature(nature_type, allot_num=1): """ api/charactor/allot_nature 元素掌握度加点, 每次加一点 蓝 "0" 红 "1" 绿 "2" 褐 "3" 黄 "4" 紫 "5" Args: nature_type(str): 元素类型代号 allot_num(str): 给该元素分配点数,缺省为1 """ ubase = request.user need_remain_num = allot_num tools.del_user_things(ubase, 'nature_remain', need_remain_num, 'allot_nature') tools.add_user_things(ubase, "nature_" + nature_type, allot_num, 'allot_nature') return {}
def api_start_fight(team_index='', new_team=None): """ api/arena/start_fight 开打 Args: team_index(str): 选择的编队号 new_team(list): 战斗编队 Returns: enemy(dict): uid: 敌人uid name: 敌人名字 lv: 敌人等级 nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 card_lv(int): 双方卡片等级 card_favor(int): 双方卡片好感度 """ uarena = request.user.user_arena if not uarena.can_fight(): raise LogicError('Cannot fight') api_set_team(new_team, team_index) common_config = uarena._common_config # 扣除体力 need_stamina = common_config['arena_fight_stamina'] tools.del_user_things(uarena, 'stamina', need_stamina, 'start_arena') uarena.inc_total() except_uids = uarena.has_fight_uids + [uarena.uid] arena_user = ArenaUser.get_instance() enemy_info = arena_user.get_random_user(except_uids=except_uids) arena_user.add_user(uarena.uid, uarena.selected_cards) # 记录战前信息 umodified = uarena.user_modified umodified.temp['dungeon'] = { 'type': 'arena', 'time': int(time.time()), } umodified.put() return { 'enemy': enemy_info, 'card_lv': common_config['arena_card_lv'], 'card_favor': common_config['arena_card_favor'], }
def api_start_fight(team_index='', new_team=None): """ api/arena/start_fight 开打 Args: team_index(str): 选择的编队号 new_team(list): 战斗编队 Returns: enemy(dict): uid: 敌人uid name: 敌人名字 lv: 敌人等级 nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 card_lv(int): 双方卡片等级 card_favor(int): 双方卡片好感度 """ uarena = request.user.user_arena if not uarena.can_fight(): raise LogicError('Cannot fight') api_set_team(new_team, team_index) common_config = uarena._common_config # 扣除体力 need_stamina = common_config['arena_fight_stamina'] tools.del_user_things(uarena, 'stamina', need_stamina, 'start_arena') uarena.inc_total() except_uids = uarena.has_fight_uids + [uarena.uid] arena_user = ArenaUser.get_instance() enemy_info = arena_user.get_random_user(except_uids=except_uids) arena_user.add_user(uarena.uid, uarena.selected_cards) # 记录战前信息 umodified = uarena.user_modified umodified.temp['dungeon'] = { 'type': 'arena', 'time': int(time.time()), } umodified.put() return {'enemy': enemy_info, 'card_lv': common_config['arena_card_lv'], 'card_favor': common_config['arena_card_favor'], }
def api_find_opponent(): """ api/invade/find_opponent 寻找对手 Retures: 同api_info """ uInvade = request.user.user_invade common_config = uInvade._common_config umodified = uInvade.user_modified # 消耗金币 coin_conf = common_config['invade_refresh_coin'] refresh_coin = coin_conf[min(uInvade.refresh_cnt, len(coin_conf) - 1)] tools.del_user_things(uInvade, 'coin', refresh_coin, 'invade_find') # 自增连续寻找对手次数 uInvade.inc_refresh_cnt() # 需找对手, 添加过期时间 invade_user_instance = InvadeUser.get_instance() opponent_info = invade_user_instance.get_fight_user(except_uids=[uInvade.uid]) expire_time = common_config['invade_keep_opponent_seconds'] + int(time.time()) opponent_info['expire_time'] = expire_time # 此对手不能被别的玩家搜到 if opponent_info['uid']: invade_user_instance.add_user(opponent_info['uid'], expire_time) # 失败只损失一个奖杯 opponent_info['lose_award'] = { 'cup': -1, } # 掠夺的金币和对手主城进贡有关 userlv_config = uInvade._userlv_config coef = common_config.get('invade_coin_coef', 0.2) win_get_coin = int(userlv_config[str(opponent_info['lv'])]['reward_coin'] * coef) if opponent_info['uid'] else 0 # 连胜两次以上获得两个杯,否则一个 opponent_info['win_award'] = { 'cup': 1 if uInvade.consecutive_win < 2 else 2, 'coin': win_get_coin, } # 记录下对手信息,若有旧对手,将他的过期时间重置 if uInvade.opponent and uInvade.opponent['uid']: invade_user_instance.add_user(uInvade.opponent['uid']) uInvade.set_opponent(opponent_info) return api_info()
def api_summon(card_id): """ api/card/summon 召唤所需金币=品质系数+200*(当前等级-1)+800*当前好感度 Args: card_id: 卡牌id """ print "summon_cards", card_id ucards = request.user.user_cards summon_coe =ucards._cardup_config['summon_coe'] card_quality = str(ucards._card_config[card_id]["quality"]) card_lv = ucards.get_card_lv(card_id) now_favor = int(ucards.cards[card_id]['favor']) need_coin = summon_coe[card_quality] + 200 * (card_lv - 1) + 800 * now_favor tools.del_user_things(ucards, 'coin', need_coin, 'summon_card') new_info = tools.add_user_things(ucards, card_id, 1, 'summon_card') # 召唤后数量应该为 1 if new_info['num'] != 1: raise LogicError("The num of this card sould be 0") return {}
def api_shopping(shop_type, index): """ api/shop/shopping 商城购买,消耗钻石 Args: shop_type(str): 商品类型 【item, package】 index(str): 商品序号 """ umodified = request.user.user_modified shop_config = umodified._shop_config if shop_type not in shop_config or index not in shop_config[shop_type]: return {} # 判断购买次数是否超过 conf = shop_config[shop_type][index] limit_type = '' for limit in ['daily_limit_cnt', 'forever_limit_cnt']: if limit in conf: limit_type = limit if limit_type: limit_cnt = conf.get(limit_type) purchased = umodified.temp.get('shop', {}).get(limit_type[:-4], {}) if limit_cnt <= purchased.get(shop_type, {}).get(index, 0): return {} else: umodified.temp.setdefault('shop', {}) umodified.temp['shop'].setdefault(limit_type[:-4], {}) umodified.temp['shop'][limit_type[:-4]].setdefault(shop_type, {}) umodified.temp['shop'][limit_type[:-4]][shop_type].setdefault( index, 0) umodified.temp['shop'][limit_type[:-4]][shop_type][index] += 1 # 判断限购 if 'start_sale_time' in conf and 'end_sale_time' in conf: now_str = str(datetime.datetime.now())[:-7] if not info['start_sale_time'] <= now_str <= info['end_sale_time']: return {} # 扣钻石 tools.del_user_things(umodified, 'diamond', conf['cost_diamond'], 'shopping_for_{}'.format(shop_type)) # 添加奖励 tools.add_user_awards(umodified, conf['award'], 'shopping_for_{}'.format(shop_type)) return {}
def api_allot_nature(nature_type, allot_num=1): """ api/charactor/allot_nature 元素掌握度加点, 每次加一点 蓝 "0" 红 "1" 绿 "2" 褐 "3" 黄 "4" 紫 "5" Args: nature_type(str): 元素类型代号 allot_num(str): 给该元素分配点数,缺省为1 """ ubase = request.user need_remain_num = allot_num tools.del_user_things(ubase, 'nature_remain', need_remain_num, 'allot_nature') tools.add_user_things(ubase, "nature_" + nature_type, allot_num, 'allot_nature') return {}
def api_summon(card_id): """ api/card/summon 召唤所需金币=品质系数+200*(当前等级-1)+800*当前好感度 Args: card_id: 卡牌id """ print "summon_cards", card_id ucards = request.user.user_cards summon_coe = ucards._cardup_config['summon_coe'] card_quality = str(ucards._card_config[card_id]["quality"]) card_lv = ucards.get_card_lv(card_id) now_favor = int(ucards.cards[card_id]['favor']) need_coin = summon_coe[card_quality] + 200 * (card_lv - 1) + 800 * now_favor tools.del_user_things(ucards, 'coin', need_coin, 'summon_card') new_info = tools.add_user_things(ucards, card_id, 1, 'summon_card') # 召唤后数量应该为 1 if new_info['num'] != 1: raise LogicError("The num of this card sould be 0") return {}
def api_start_defense(history_index, team_index='', new_team=None): """ api/invade/start_defense 开始反击战斗 Args: history_index(int): 所反击历史记录的index new_team(list): 战斗编队 team_index(str): 选取的那个城编队去打 Returns: enemy(dict): nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 team_index: 敌人队伍军旗 card_lv(list->int): 各卡片等级 card_favor(list->int): 各卡片好感度 city_lv: 此城等级 """ if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team) uInvade = request.user.user_invade history = uInvade.history if not 0 <= history_index <= len(history) - 1: raise ParamsError('Wrong history index') defe_history = history[history_index] if defe_history['status'] != 0: raise LogicError('Had been defensed') # 消耗体力 need_stamina = uInvade._common_config['invade_defense_stamina'] tools.del_user_things(uInvade, 'stamina', need_stamina, 'invade') # 记录战前信息 umodified = uInvade.user_modified umodified.add_dungeon_info('invade_defense', { 'history': defe_history, }) # 清除此次日志 uInvade.clear_history(index=history_index) return {'enemy': defe_history['team_info']}
def api_start_defense(history_index, team_index='', new_team=None): """ api/invade/start_defense 开始反击战斗 Args: history_index(int): 所反击历史记录的index new_team(list): 战斗编队 team_index(str): 选取的那个城编队去打 Returns: enemy(dict): nature_*: 敌人各元素掌握度 team: 敌人卡片队伍 team_index: 敌人队伍军旗 card_lv(list->int): 各卡片等级 card_favor(list->int): 各卡片好感度 city_lv: 此城等级 """ if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team) uInvade = request.user.user_invade history = uInvade.history if not 0 <= history_index <= len(history) - 1: raise ParamsError('Wrong history index') defe_history = history[history_index] if defe_history['status'] != 0: raise LogicError('Had been defensed') # 消耗体力 need_stamina = uInvade._common_config['invade_defense_stamina'] tools.del_user_things(uInvade, 'stamina', need_stamina, 'invade') # 记录战前信息 umodified = uInvade.user_modified umodified.add_dungeon_info('invade_defense', { 'history': defe_history, }) # 清除此次日志 uInvade.clear_history(index=history_index) return {'enemy': defe_history['team_info']}
def api_open_city(city_id): """ api/city/open_city 解锁城市 Args: city_id(str): 要解锁的城市id """ umodified = request.user.user_modified ucities = request.user.user_cities if ucities.has_open_city(city_id): return {} # 开城等级限制 need_lv = ucities._city_config[city_id]["need_ulv"] if ucities.user_property.lv < need_lv: raise LogicError("Should reach the open lv") need_coin_conf = ucities._common_config["open_city_cost_coin"] has_opened_num = ucities.get_opened_city_num() need_coin = need_coin_conf[has_opened_num] tools.del_user_things(ucities, 'coin', need_coin, 'open_city') new_info = ucities.open_city(city_id) umodified.set_modify_info('cities', new_info) return {}
def api_dismiss(dismiss_type, card_id=''): """ api/card/dismiss 1) 有三种分解方式 2) 分解所得英魂值只与卡牌的品质有关。 i 普通:5 ii 精良:10 iii 稀有:25 iv 史诗:50 v 传说:100 Args: dismiss_type(str): 分解方式 "dismiss_one" : 分解一张此卡 "keep_one" : 只保留一张此卡,其他分解 "all_keep_one": 所有卡牌只保留一张,其余全分解,card_id 缺省 card_id(str): 卡片id Returns: get_heroSoul: 分解后产生的英魂 """ print "dismiss_cards", card_id, dismiss_type ubase = request.user ucards = ubase.user_cards get_heroSoul = 0 product_conf = ubase._cardup_config['dismiss_product_heroSoul'] card_config = ubase._card_config if dismiss_type == 'dismiss_one': tools.del_user_things(ubase, card_id, 1, 'dismiss_card') card_quality = str(card_config[card_id]['quality']) get_heroSoul += product_conf[card_quality] adjust_team(ucards, card_id) elif dismiss_type == 'keep_one': now_num = ucards.cards.get(card_id, {}).get('num', 0) del_num = now_num - 1 tools.del_user_things(ucards, card_id, del_num, 'dismiss_card') card_quality = str(card_config[card_id]['quality']) get_heroSoul += del_num * product_conf[card_quality] adjust_team(ucards, card_id) elif dismiss_type == 'all_keep_one': for cid, cinfo in ucards.cards.items(): cnum = cinfo['num'] if cnum <= 1: continue del_num = cnum - 1 tools.del_user_things(ucards, cid, del_num, 'dismiss_card') card_quality = str(card_config[cid]['quality']) get_heroSoul += del_num * product_conf[card_quality] adjust_team(ucards, cid) tools.add_user_things(ubase, 'heroSoul', get_heroSoul, 'dismiss_card') return {'get_heroSoul': get_heroSoul}
def api_dismiss(dismiss_type, card_id=''): """ api/card/dismiss 1) 有三种分解方式 2) 分解所得英魂值只与卡牌的品质有关。 i 普通:5 ii 精良:10 iii 稀有:25 iv 史诗:50 v 传说:100 Args: dismiss_type(str): 分解方式 "dismiss_one" : 分解一张此卡 "keep_one" : 只保留一张此卡,其他分解 "all_keep_one": 所有卡牌只保留一张,其余全分解,card_id 缺省 card_id(str): 卡片id Returns: get_heroSoul: 分解后产生的英魂 """ print "dismiss_cards", card_id, dismiss_type ubase = request.user ucards = ubase.user_cards get_heroSoul = 0 product_conf = ubase._cardup_config['dismiss_product_heroSoul'] card_config = ubase._card_config if dismiss_type == 'dismiss_one': tools.del_user_things(ubase, card_id, 1, 'dismiss_card') card_quality = str(card_config[card_id]['quality']) get_heroSoul += product_conf[card_quality] adjust_team(ucards, card_id) elif dismiss_type == 'keep_one': now_num = ucards.cards.get(card_id, {}).get('num', 0) del_num = now_num - 1 tools.del_user_things(ucards, card_id, del_num, 'dismiss_card') card_quality = str(card_config[card_id]['quality']) get_heroSoul += del_num * product_conf[card_quality] adjust_team(ucards, card_id) elif dismiss_type == 'all_keep_one': for cid, cinfo in ucards.cards.items(): cnum = cinfo['num'] if cnum <= 1: continue del_num = cnum - 1 tools.del_user_things(ucards, cid, del_num, 'dismiss_card') card_quality = str(card_config[cid]['quality']) get_heroSoul += del_num * product_conf[card_quality] adjust_team(ucards, cid) tools.add_user_things(ubase, 'heroSoul', get_heroSoul, 'dismiss_card') return {'get_heroSoul': get_heroSoul}
def api_start(dungeon_type, city_id, team_index='', new_team=None, buff_city="0", floor=''): """ api/dungeon/start 进战场 1.进战场先扣体力 2.进战场会先给1/3经验值 Args: dungeon_type(str): 战斗类型 "conquer"征服模式 "challenge"挑战模式 city_id(str): 要打哪个城,城市id team_index(str): 选取的城市编队id new_team(list->str): 新的战斗卡片队伍 buff_city(list->str): 新的战斗卡片队伍选择的城市buff floor(str): 挑战模式选取的大关卡 Returns: enemy_team(list): 敌将编队 enemy_lv(list->int): 各敌将等级 enemy_favor(int): 敌将全好感度 enemy_nature(int): 敌将全元素掌握度 """ result = {} if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team, buff_city) ubase = request.user ucities = ubase.user_cities if dungeon_type == 'conquer': conquer_config = ubase._conquer_config if not ucities.can_conquer_city(city_id): raise LogicError("Can't conquer this city") cur_stage = str(ucities.cur_conquer_stage(city_id)) stage_conf = conquer_config[city_id][cur_stage] need_stamina = stage_conf['stamina'] # 扣体力 tools.del_user_things(ubase, 'stamina', need_stamina, 'conquer') enemy_favor = stage_conf['enemy_favor'] enemy_nature = stage_conf['enemy_nature'] enemy_team = [] base_enemy_lv = [] for enemy_cid, lv in stage_conf['enemy']: enemy_team.append(enemy_cid) base_enemy_lv.append(lv) enemy_lv = calcu_enemy_lv(ubase.user_cards, base_enemy_lv) elif dungeon_type == 'challenge': challenge_config = ubase._challenge_config if not ucities.can_challenge_city_floor(city_id, floor): raise LogicError("Can't challenge this city floor") cur_room = str(ucities.cities[city_id]['challenge'][floor]) room_conf = challenge_config[city_id][floor][cur_room] need_stamina = room_conf['stamina'] # 扣体力 tools.del_user_things(ubase, 'stamina', need_stamina, 'challenge') enemy_favor = room_conf['enemy_favor'] enemy_nature = room_conf['enemy_nature'] enemy_team = [] enemy_lv = [] for enemy_cid, lv in room_conf['enemy']: enemy_team.append(enemy_cid) enemy_lv.append(lv) umodified = ubase.user_modified umodified.temp['dungeon'] = { 'type': dungeon_type, 'time': int(time.time()), 'city_id': city_id, 'floor': floor, } umodified.put() result = { 'enemy_team': enemy_team, 'enemy_lv': enemy_lv, 'enemy_favor': enemy_favor, 'enemy_nature': enemy_nature, } return result
def api_end_invade(win): """ api/invade/end_invade 战斗胜利 Argvs: win(bool): 是否胜利 Returns: exp(int): 获得经验 cup(int): 获得奖杯数,可为负数 coin(int): 获得金币数 """ user = request.user common_config = user._common_config umodified = user.user_modified start_info = umodified.clear_dungeon_info('invade') now = int(time.time()) if now - start_info['time'] <= 1: raise LogicError("rush a dungeon to quick") uInvade = user.user_invade uProperty = uInvade.user_property opponent_info = start_info['opponent_info'] opponent_uid = opponent_info['uid'] opponent_invade_log = { 'status': 0, 'name': user.name, 'uid': user.uid, 'lv': uProperty.lv, 'cup': uInvade.cup, 'time': int(time.time()), } opponent_invade_log['team_info'] = uInvade.now_team_info() # 胜利获得全额经验1奖杯和对手金钱;失败扣奖杯,且对手加代币 full_exp = common_config['invade_fight_exp'] invade_user_instance = InvadeUser.get_instance() if win: # 连胜次数加1 uInvade.inc_consecutive_win() # 侵略成功加1 uInvade.inc_total_invade_win() award = opponent_info['win_award'] award['exp'] = full_exp opponent_invade_log['status'] = 0 opponent_invade_log['lose_coin'] = award.get('coin', 0) # 每次打赢别人, 自己的保护时间取消 uInvade.reset_shield_time() # 如果自己有主城,加入被搜寻中 #if uInvade.user_cities.capital_city: invade_user_instance.add_user(uInvade.uid) else: # 失败清空连胜次数 uInvade.reset_consecutive_win() award = opponent_info['lose_award'] award['exp'] = full_exp // 3 opponent_invade_log['status'] = 1 opponent_invade_log['win_invade_jeton'] = 1 # 改变对手数据! 加代币 或 减钱 if opponent_uid: opponentInvade = UserInvade.get(opponent_uid) # 日志中主城设为被打者主城 capital_city = opponentInvade.user_cities.capital_city opponent_invade_log['capital_city'] = capital_city # 加上入侵者此城等级 uCities = uInvade.user_cities city_lv = 0 if not uCities.has_show_city(capital_city) else uCities.cities[capital_city]['lv'] opponent_invade_log['team_info']['city_lv'] = city_lv # 要告诉对手他被打了 opponentInvade.add_history(opponent_invade_log) if win: opponent_coin = opponentInvade.user_property.coin award['coin'] = min(opponent_invade_log['lose_coin'], opponent_coin) tools.del_user_things(opponentInvade, 'coin', award['coin'], 'beinvaded') # 给被打人 加护盾时间 shield_gap = common_config['invade_keep_shield_seconds'] shield_time = int(time.time()) + shield_gap invade_user_instance.add_user(opponent_uid, shield_time) opponentInvade.reset_shield_time(shield_time) else: invade_user_instance.add_user(opponent_uid) opponentInvade.add_invade_jeton(1) else: award['coin'] = 0 # 给自己加奖励 if 'cup' in award: add_cup = uInvade.add_cup(award['cup']) award['cup'] = add_cup tools.add_user_awards(user, award, 'invade') return award
def api_start(dungeon_type, city_id, team_index='', new_team=None, buff_city="0", floor=''): """ api/dungeon/start 进战场 1.进战场先扣体力 2.进战场会先给1/3经验值 Args: dungeon_type(str): 战斗类型 "conquer"征服模式 "challenge"挑战模式 city_id(str): 要打哪个城,城市id team_index(str): 选取的城市编队id new_team(list->str): 新的战斗卡片队伍 buff_city(list->str): 新的战斗卡片队伍选择的城市buff floor(str): 挑战模式选取的大关卡 Returns: enemy_team(list): 敌将编队 enemy_lv(list->int): 各敌将等级 enemy_favor(int): 敌将全好感度 enemy_nature(int): 敌将全元素掌握度 """ result = {} if team_index != '' and new_team is not None: if set(new_team) == set(['']): raise ParmasError('Can\'t set empty team !') card.api_set_team(team_index, new_team, buff_city) ubase = request.user ucities = ubase.user_cities if dungeon_type == 'conquer': conquer_config = ubase._conquer_config if not ucities.can_conquer_city(city_id): raise LogicError("Can't conquer this city") cur_stage = str(ucities.cur_conquer_stage(city_id)) stage_conf = conquer_config[city_id][cur_stage] need_stamina = stage_conf['stamina'] # 扣体力 tools.del_user_things(ubase, 'stamina', need_stamina, 'conquer') enemy_favor = stage_conf['enemy_favor'] enemy_nature = stage_conf['enemy_nature'] enemy_team = [] base_enemy_lv = [] for enemy_cid, lv in stage_conf['enemy']: enemy_team.append(enemy_cid) base_enemy_lv.append(lv) enemy_lv = calcu_enemy_lv(ubase.user_cards, base_enemy_lv) elif dungeon_type == 'challenge': challenge_config = ubase._challenge_config if not ucities.can_challenge_city_floor(city_id, floor): raise LogicError("Can't challenge this city floor") cur_room = str(ucities.cities[city_id]['challenge'][floor]) room_conf = challenge_config[city_id][floor][cur_room] need_stamina = room_conf['stamina'] # 扣体力 tools.del_user_things(ubase, 'stamina', need_stamina, 'challenge') enemy_favor = room_conf['enemy_favor'] enemy_nature = room_conf['enemy_nature'] enemy_team = [] enemy_lv = [] for enemy_cid, lv in room_conf['enemy']: enemy_team.append(enemy_cid) enemy_lv.append(lv) umodified = ubase.user_modified umodified.temp['dungeon'] = { 'type': dungeon_type, 'time': int(time.time()), 'city_id': city_id, 'floor': floor, } umodified.put() result = { 'enemy_team': enemy_team, 'enemy_lv': enemy_lv, 'enemy_favor': enemy_favor, 'enemy_nature': enemy_nature, } return result
def api_end_invade(win): """ api/invade/end_invade 战斗胜利 Argvs: win(bool): 是否胜利 Returns: exp(int): 获得经验 cup(int): 获得奖杯数,可为负数 coin(int): 获得金币数 """ user = request.user common_config = user._common_config umodified = user.user_modified start_info = umodified.clear_dungeon_info('invade') now = int(time.time()) if now - start_info['time'] <= 1: raise LogicError("rush a dungeon to quick") uInvade = user.user_invade uProperty = uInvade.user_property opponent_info = start_info['opponent_info'] opponent_uid = opponent_info['uid'] opponent_invade_log = { 'status': 0, 'name': user.name, 'uid': user.uid, 'lv': uProperty.lv, 'cup': uInvade.cup, 'time': int(time.time()), } opponent_invade_log['team_info'] = uInvade.now_team_info() # 胜利获得全额经验1奖杯和对手金钱;失败扣奖杯,且对手加代币 full_exp = common_config['invade_fight_exp'] invade_user_instance = InvadeUser.get_instance() if win: # 连胜次数加1 uInvade.inc_consecutive_win() # 侵略成功加1 uInvade.inc_total_invade_win() award = opponent_info['win_award'] award['exp'] = full_exp opponent_invade_log['status'] = 0 opponent_invade_log['lose_coin'] = award.get('coin', 0) # 每次打赢别人, 自己的保护时间取消 uInvade.reset_shield_time() # 如果自己有主城,加入被搜寻中 #if uInvade.user_cities.capital_city: invade_user_instance.add_user(uInvade.uid) else: # 失败清空连胜次数 uInvade.reset_consecutive_win() award = opponent_info['lose_award'] award['exp'] = full_exp // 3 opponent_invade_log['status'] = 1 opponent_invade_log['win_invade_jeton'] = 1 # 改变对手数据! 加代币 或 减钱 if opponent_uid: opponentInvade = UserInvade.get(opponent_uid) # 日志中主城设为被打者主城 capital_city = opponentInvade.user_cities.capital_city opponent_invade_log['capital_city'] = capital_city # 加上入侵者此城等级 uCities = uInvade.user_cities city_lv = 0 if not uCities.has_show_city( capital_city) else uCities.cities[capital_city]['lv'] opponent_invade_log['team_info']['city_lv'] = city_lv # 要告诉对手他被打了 opponentInvade.add_history(opponent_invade_log) if win: opponent_coin = opponentInvade.user_property.coin award['coin'] = min(opponent_invade_log['lose_coin'], opponent_coin) tools.del_user_things(opponentInvade, 'coin', award['coin'], 'beinvaded') # 给被打人 加护盾时间 shield_gap = common_config['invade_keep_shield_seconds'] shield_time = int(time.time()) + shield_gap invade_user_instance.add_user(opponent_uid, shield_time) opponentInvade.reset_shield_time(shield_time) else: invade_user_instance.add_user(opponent_uid) opponentInvade.add_invade_jeton(1) else: award['coin'] = 0 # 给自己加奖励 if 'cup' in award: add_cup = uInvade.add_cup(award['cup']) award['cup'] = add_cup tools.add_user_awards(user, award, 'invade') return award