def buy_growup_plan(rk_user, params): """购买成长计划""" ua = rk_user.user_activity ua_growup_info = ua.growup_info has_bought = ua_growup_info.get('has_bought', False) # 是否已购买 成长计划 if has_bought: raise GameLogicError('has bought') operat_config = game_config.operat_config need_coin = operat_config.get('growup_award_need_coin', 0) if not rk_user.user_property.is_coin_enough(need_coin): raise GameLogicError('user', 'not_enough_coin') # vip 等级限制 user_property_obj = rk_user.user_property vip_cur_level = user_property_obj.vip_cur_level need_vip_lv = operat_config.get('growup_award_need_vip_lv', 0) if vip_cur_level < need_vip_lv: raise GameLogicError('vip lv not reached') ua_growup_info['has_bought'] = True # 扣元宝 rk_user.user_property.minus_coin(need_coin, 'buy_growup_plan') ua.put() return {}
def get_fudai_things(rk_user, params): """开启福袋获得物品 Args: fudai_id 开启的福袋id times 开启次数 """ fudai_id = params.get('fudai_id', '') open_times = int(params.get('times', 0)) # 判断是否是福袋 if not game_config.props_config.get(fudai_id, {}).get('used_by', '') == 'fudai': raise GameLogicError('pack', 'wrong_used') # 判断用户是否有该道具 user_pack_obj = rk_user.user_pack if not user_pack_obj.is_props_enough(fudai_id, open_times): raise GameLogicError('pack', 'not_enough_props') fudai_conf = game_config.operat_config.get('fudai_conf', {}).get(fudai_id, {}) get_things_conf = fudai_conf.get('get_things', {}) get_things_list = [] for cnt in range(open_times): get_things_dict = utils.get_items_by_weight_dict(get_things_conf, 1)[0] things_id = get_things_dict['id'] things_num = get_things_dict['num'] get_things_list.append({'_id': things_id, 'num': things_num}) all_get_things = tools.add_things(rk_user, get_things_list, where=u"open_fudai_%s" % fudai_id) # 减素材 user_pack_obj.minus_props(fudai_id, open_times, where="open_fudai") return {'get_info': all_get_things}
def check_limit_recover(rk_user, where, dtype='', floor_id=''): """根据用户的vip等级判断用户当天是否达到回复次数""" user_property_obj = rk_user.user_property vip_lv = str(user_property_obj.vip_cur_level) today_str = utils.get_today_str() now_recover_times = 0 if today_str == user_property_obj.property_info['recover_times'][ 'today_str']: if where in ['recover_stamina', 'recover_mystery_store']: # 回复体力的处理 max_recover_times = game_config.user_vip_config[vip_lv].get( 'can_' + where + '_cnt', 0) now_recover_times = user_property_obj.property_info[ 'recover_times'][where] if max_recover_times <= now_recover_times: raise GameLogicError('user', 'max_times') elif where == 'recover_copy': # 重置战场的处理 if dtype == 'normal': max_recover_times = game_config.user_vip_config[vip_lv][ 'can_recover_copy_cnt']['normal'] now_recover_times = user_property_obj.property_info[ 'recover_times']['recover_copy']['normal'] elif dtype == 'daily': max_recover_times = game_config.user_vip_config[vip_lv][ 'can_recover_copy_cnt']['daily'][floor_id] now_recover_times = user_property_obj.property_info[ 'recover_times']['recover_copy']['daily'].get(floor_id, 0) if max_recover_times <= now_recover_times: raise GameLogicError('user', 'max_times') else: # 重置时间和副本次数 user_property_obj.reset_recover_times() return now_recover_times
def set_name(rk_user, params): ''' 新手引导设置名称和性别 name 名字 sex 性别 man / woman ''' name = params.get('name', '') step = int(params.get('step', 0)) if len(name.strip()) <= 0: raise GameLogicError('user', 'name_cannot_null') if utils.is_sense_word(name): raise GameLogicError('user', 'wrong_words') if Names.get(name): #rk_user调用 UserName.set_name, UserName的 pk 是 name, 若重复,报错 raise GameLogicError('user', 'name_exist') rk_user.set_name(name) Names.set_name(rk_user.uid, name) if step: #设置新手引导的步骤 rk_user.user_property.set_newbie_steps(step, "set_name") sex = params.get('sex', 'man') rk_user.set_sex(sex) return {}
def recover_stamina(rk_user, params): """花费元宝回复体力 每次回复的体力值是固定的,但消耗的元宝数 是根据当天第几次回体配置的 """ user_property_obj = rk_user.user_property recover_stamina = game_config.system_config['coin_recover_stamina'] has_recover_times = user_property_obj.property_info['recover_times'][ 'recover_stamina'] need_coin = game_config.system_config['recover_stamina_need'][ has_recover_times + 1] # 检查用户体力是否已满 if user_property_obj.max_recover(): raise GameLogicError('user', 'max_recover') # 检查vip可以回复的次数到了没 vip.check_limit_recover(rk_user, 'recover_stamina') # 检查用户coin是否足够 并扣钱 if not user_property_obj.minus_coin(need_coin, 'recover_stamina'): raise GameLogicError('user', 'not_enough_coin') # 添加回复次数 user_property_obj.add_recover_times('recover_stamina') user_property_obj.add_stamina(recover_stamina) return {}
def check_start(rk_user, params, user_dungeon_obj): """检查是否可以进入战场""" user_card_obj = UserCards.get(rk_user.uid) user_lv = rk_user.user_property.lv #获取战场的配置信息 dungeon_type = params['dungeon_type'] floor_id = params['floor_id'] room_id = params['room_id'] conf = get_conf(params, rk_user.uid, user_dungeon_obj) if not conf: raise GameLogicError('dungeon', 'invalid_dungeon') #校验每日限次战场 if user_dungeon_obj.check_limit_dungeon(rk_user, params) is False: raise GameLogicError('dungeon', 'invalid_limit_dungeon') now_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #活动迷宫时,检查迷宫是否开放状态 if dungeon_type == 'special': if now_str > conf['end_time'] or now_str < conf['start_time']: raise GameLogicError('dungeon', 'not_open') elif dungeon_type == 'daily': #每日活动副本 用户等级 #开启战场的最小等级 start_dungeon_lv = game_config.daily_dungeon_config.get( floor_id, {})['rooms'].get(room_id, {}).get('user_lv', 1) #判断条件是否达到 if user_lv < start_dungeon_lv: raise GameLogicError('dungeon', 'not_arrived_lv') else: # 开启战场的最小等级 start_dungeon_lv = game_config.normal_dungeon_config[floor_id][ 'rooms'][room_id].get('need_lv', 1) #判断条件是否达到 if user_lv < start_dungeon_lv: raise GameLogicError('dungeon', 'not_arrived_lv') #检查是否达到该战场 比如说以前打到11关 现在是15关则不能打 if int(floor_id) > int(user_dungeon_obj.normal_current['floor_id']): raise GameLogicError('dungeon', 'not_arrived') elif floor_id == user_dungeon_obj.normal_current['floor_id']: if int(room_id) > int(user_dungeon_obj.normal_current['room_id']): raise GameLogicError('dungeon', 'not_arrived') #检查用户体力是否足够 need_stamina = int(conf['stamina']) if rk_user.user_property.stamina < need_stamina: raise GameLogicError('user', 'not_enough_stamina') # 检查是否有 进入战场所需的 武将 if not has_needed_cards(conf, user_card_obj): raise GameLogicError('dungeon', 'needed_special_cards') # 进入战场要扣一点体力 rk_user.user_property.minus_stamina(1 if need_stamina > 0 else 0) return conf
def binding_or_not(rk_user, params): """装备 武将 ucid = ucid1|ucid2|ucid3 ueid = ueid,ueid,ueid|ueid,ueid,ueid|ueid,ueid,ueid """ params_ucid = params['ucid'] params_ueids = params['ueid'] ucids = params_ucid.split("|") ueids_group = params_ueids.split("|") user_equip_obj = rk_user.user_equips user_card_obj = rk_user.user_cards user_equips = user_equip_obj.equips all_cards = user_card_obj.cards equip_config = game_config.equip_config # 如果武将为空 if not ucids or (len(ucids) != len(ueids_group)): raise ParamsError() ucid_ueids = dict(zip(ucids, ueids_group)) for ucid, ueids in ucid_ueids.items(): ueids = utils.remove_null(ueids.split(',')) #获取用户的所有的武将和装备的 id if ucid not in all_cards: print ucid, ueids print all_cards raise GameLogicError('card', 'no_card') all_eqtype = [] # 判断装备是否存在 for ueid in ueids: if ueid not in user_equips: raise GameLogicError('equip', 'no_equip') eid = user_equips[ueid]['eid'] eqtype = equip_config[eid].get('eqtype', 1) if eqtype in all_eqtype: raise GameLogicError('equip', 'invalid_deck') else: all_eqtype.append(eqtype) #开始绑定信息 user_equip_obj.bind_equip(ucid, ueids) #判断新手引导 newbie_step = int(params.get('newbie_step', 0)) if newbie_step: rk_user.user_property.set_newbie_steps(newbie_step, "binding_equip") return {}
def buy_props(rk_user, params): """购买道具的接口""" #获取道具 id 和购买的组数 props_index = params['props_indexs'] #获取商店配置 this_props_shop_config = game_config.props_store_config['props_sale'].get( props_index, {}) # 获取道具id props_id = this_props_shop_config['id'] # 获取单价 price = this_props_shop_config.get('need_coin', 0) # 获取道具数量 num = this_props_shop_config.get('num', 1) user_pack_obj = rk_user.user_pack # vip等级购买次数 是否足够 user_pack_obj.add_store_has_bought_cnt(props_index) print price, num, props_index # 判断元宝是否足够 if not rk_user.user_property.minus_coin(price, 'buy_props'): raise GameLogicError('user', 'not_enough_coin') # 添加道具 user_pack_obj.add_props(props_id, num, where='buy_props') return {}
def set_yuanjun_decks(rk_user, params): """设置援军编队 params: yuanjun_deck: 'ucid,ucid,ucid_ucid,,ucid' """ new_deck = params['yuanjun_deck'] # 没有援军槽时前端传的是 '____' if new_deck == '____': return 0, {} new_decks = new_deck.split('_') new_decks_lst = [] uc = rk_user.user_cards if len(new_decks) != 5: raise GameLogicError('card', 'invalid_deck') for i, deck in enumerate(new_decks): ucids = deck.split(',') #[:uc.slot_num] if len(ucids) != uc.slot_num: raise GameLogicError('card', 'invalid_deck') ucid_repeat_check = [] cid_repeat_check = [] for ucid in ucids: # check : ucid是否存在,ucid是否重复,cid是否重复 #if ucid and ucid not in rk_user.user_cards.cards: # raise GameLogicError('card', 'no_card') if ucid: if ucid not in rk_user.user_cards.cards: raise GameLogicError('card', 'no_card') ucid_repeat_check.append(ucid) cid_repeat_check.append(uc.cards[ucid]['cid']) for item in uc.decks[i]: if item.get('ucid'): ucid_repeat_check.append(item['ucid']) cid_repeat_check.append(uc.cards[item['ucid']]['cid']) if len(ucid_repeat_check) != len(set(ucid_repeat_check)): raise GameLogicError('card', 'repeated_ucid') if len(cid_repeat_check) != len(set(cid_repeat_check)): raise GameLogicError('card', 'repeated_cid') new_decks_lst.append(ucids) #print new_decks_lst, 'new_decks_lst ---------*************------------' uc.set_yuanjun_decks(new_decks_lst) return 0, {}
def rename(rk_user, params): """ 使用更名令进行重命名""" cost_props = params.get('cost_props') new_name = params.get('new_name', '') #判断是否是更命令道具 if not game_config.props_config.get(cost_props, {}).get('used_by', '') == 'rename': raise GameLogicError('pack', 'wrong_used') #判断用户是否有该道具 user_pack_obj = UserPack.get_instance(rk_user.uid) if not user_pack_obj.is_props_enough(cost_props, 1): raise GameLogicError('pack', 'not_enough_props') #有道具的情况进行重命名 main.set_name(rk_user, {"name": new_name}) #减道具 user_pack_obj.minus_props(cost_props, 1, where='pack.rename') return {'new_name': new_name}
def buy_store_goods(rk_user, params): """ 用功勋兑换 Pk 商店物品 params 参数需包含 goods_index: 所兑换的物品index 返回 data 包括所兑换物品和商店信息 """ goods_index = int(params['goods_index']) user_pk_store = rk_user.user_pk_store user_pk_goods = user_pk_store.goods # pk商店无此物品 if not (0 <= goods_index < len(user_pk_goods)): return 11, {'msg': utils.get_msg('pk_store', 'no_this_goods')} goods_info = user_pk_goods[goods_index] # 已兑换过 if not user_pk_store.can_buy_this_goods(goods_index): raise GameLogicError('pk_store', 'can_not_buy_again') need_honor = goods_info['need_honor'] # 兑换所需功勋点不够 user_real_pvp = rk_user.user_real_pvp if not user_real_pvp.is_honor_enough(need_honor): raise GameLogicError('pk_store', 'not_enough_honor') # 扣除功勋 user_real_pvp.minus_honor(need_honor, where='buy_PkStore_goods') # 兑换物品 user_pk_store.goods_has_bought(goods_index) # 发放兑换物品 all_get_goods = tools.add_things(rk_user, [goods_info['goods']], where="buy_pk_store_goods") # 记录log log_data = {"cost_honor": need_honor, "goods": all_get_goods} data_log_mod.set_log("PkStore", rk_user, **log_data) data = {'get_info': all_get_goods} data.update(_pack_store_info(user_pk_store.store_info())) return data
def buy_vip_gift(rk_user, params): "购买vip礼品包" vip_gift_id = params.get('vip_gift_id', '') vip_gift_sale_config = game_config.vip_store_config.get('vip_sale', {}) user_property_obj = rk_user.user_property vip_cur_level = user_property_obj.vip_cur_level # 已购买的vip礼包id list has_bought_ids = user_property_obj.property_info['has_bought_vip_package'] # 判断vip礼包id是否有效 if not vip_gift_id: raise GameLogicError('gift', 'gift_code_error') # 判断是否存在该vip礼包 if vip_gift_id not in vip_gift_sale_config: raise GameLogicError('gift', 'gift_code_not_exist') # 判断玩家的vip等级是否达到 if int(vip_gift_id) > int(vip_cur_level): raise GameLogicError('gift', 'level_not_enough') # 判断玩家是否已经购买相应的vip礼包 if vip_gift_id in has_bought_ids: raise GameLogicError('gift', 'gift_already_buy') need_coin = vip_gift_sale_config[vip_gift_id]['coin'] # 判断玩家的元宝是否达到 if not user_property_obj.is_coin_enough(need_coin): raise GameLogicError('user', 'not_enough_coin') goods_list = vip_gift_sale_config[vip_gift_id]['goods_list'] all_get_goods = tools.add_things(rk_user, [{ "_id": good_id, "num": num } for good_id, num in goods_list.items()], where='buy_vip_gift') # 扣除元宝 user_property_obj.minus_coin(need_coin, where="buy_vip_gift") user_property_obj.add_bought_vip_package(vip_gift_id) return {'get_info': all_get_goods}
def get_growup_awards(rk_user, params): """ 领取等级成长奖励 Args: award_lv: 领取是第几等级的奖励 """ award_lv = params.get('award_lv', '0') ua = rk_user.user_activity ua_growup_info = ua.growup_info all_growup_awards = game_config.operat_config.get('growup_award', {}) # 没有购买成长计划 if not ua_growup_info.get('has_bought', False): raise GameLogicError('has not bought') # 不存在该等级奖励 if award_lv not in all_growup_awards: raise GameLogicError('lv award no exist') # 等级未到达 user_property_obj = rk_user.user_property if int(user_property_obj.lv) < int(award_lv): raise GameLogicError('lv not reached') # 已领取的等级奖励 不能再领 has_got_award_lvs = ua_growup_info.get('has_got_lvs', []) if award_lv in has_got_award_lvs: raise GameLogicError('has got this award') has_got_award_lvs.append(award_lv) ua_growup_info['has_got_lvs'] = has_got_award_lvs ua.put() # 加物品 get_things = [{ '_id': thing_id, 'num': num } for thing_id, num in all_growup_awards[award_lv].get( 'awards', {}).items()] all_get_goods = tools.add_things(rk_user, get_things, where='get_growup_awards_%s' % award_lv) return {"get_info": all_get_goods}
def get_box(rk_user, params): ''' 领取每日任务的宝箱 ''' box_type = params['box_type'] ut = rk_user.user_task points = ut.daily_info['points'] box_conf = game_config.task_config['daily_task_box'][box_type] if points < box_conf['need_point']: raise GameLogicError('task','not_enough_points') if ut.daily_info['has_got'][box_type] == True: raise GameLogicError('task','box_has_got') award = box_conf['award'] data = tools.add_things( rk_user, [{"_id": goods, "num": award[goods]} for goods in award if goods], where="daily_task_award" ) ut.daily_info['has_got'][box_type] = True ut.put() return 0, data
def charge_multi(rk_user, params): """ 收费gacha多连抽 """ user_gacha_obj = rk_user.user_gacha user_card_obj = UserCards.get_instance(rk_user.uid) #元宝是否足够连抽 multi_gacha_cnt = game_config.gacha_config.get('multi_gacha_cnt', 10) cost_coin = game_config.gacha_config['cost_coin'] total_cost_coin = cost_coin * multi_gacha_cnt * game_config.gacha_config[ 'multi_discount'] # 10连抽打折(9折) if not rk_user.user_property.is_coin_enough(total_cost_coin): raise GameLogicError('user', 'not_enough_coin') cids = [] get_list = [] for cnt in range(multi_gacha_cnt): if cnt == 9: rate_conf = __multi_tenth_charge_rate( rk_user, cids) or __get_rate_conf('charge', rk_user) else: rate_conf = __get_rate_conf('charge', rk_user) thing_id, num = __select_gacha_thing(rate_conf) get_list.append({'_id': thing_id, 'num': num}) user_gacha_obj.set_gacha_cnt() # success_fg, p1, ucid, is_first = user_card_obj.add_card(cid, clv, where='gacha_multi') # new_card = { # 'ucid':ucid, # 'is_first':is_first, # } # if is_open_safty(): # __set_safety_coin(rk_user, cid, cost_coin) # cids.append(cid) # new_card.update(user_card_obj.get_card_dict(ucid)) # new_cards.append(new_card) add_info = tools.add_things(rk_user, get_list, where="gacha_multi") # cardSoul 加传 card 的星级 if 'cardSoul' in add_info: card_config = rk_user.game_config.card_config for card_id in add_info['cardSoul']: num = add_info['cardSoul'][card_id] star = card_config[card_id]['star'] add_info['cardSoul'][card_id] = {'num': num, 'star': star} rk_user.user_property.minus_coin(total_cost_coin, 'gacha_multi') get_things = __gacha_must_get(rk_user, multi_gacha_cnt) return {'add_info': add_info, 'get_things': get_things}
def buy_store_goods(rk_user, params): """ 玩家购买指定商品时逻辑 params 参数需包含 store_type: 可选 "packages" "gold_store" or "coin_store" goods_index: int 为所买商品在所属类型的index """ #store_type = params['store_type'] goods_index = int(params['goods_index']) buy_goods_info = {} goods_list = [] user_mystery_store_obj = rk_user.user_mystery_store buy_goods_info = user_mystery_store_obj.store_info()['store'][goods_index] goods_list.append(buy_goods_info['goods']) need = "need_coin" if buy_goods_info.get("need_coin", 0) else "need_fight_soul" needed_cost = buy_goods_info.get(need, 0) fight_or_coin = need.replace("need_", "") # 根据store_type 决定是 消耗元宝还是战魂 minus_func = getattr(rk_user.user_property, "minus_" + fight_or_coin) if not minus_func(needed_cost, 'buy_mystery_store_goods'): raise GameLogicError('user', 'not_enough_' + fight_or_coin) # 发商品 # 前端通过rc 是否等于 0 判断是否购买成功 if not user_mystery_store_obj.update_goods_info_by_index(goods_index): raise GameLogicError('has bought this item') all_get_goods = tools.add_things(rk_user, goods_list, where=u"buy_from_mystery_store") # 记录log log_data = {"cost": fight_or_coin, "cost_num": needed_cost, "goods": goods_list} data_log_mod.set_log("MysteryStore", rk_user, **log_data) return {'get_info': all_get_goods}
def add_store_has_bought_cnt(self, props_index): self.store_has_bought.setdefault(props_index, 0) vip_can_buy_cnt_conf = self.game_config.props_store_config[ 'props_sale'][props_index]['vip_can_buy_cnt'] user_property_obj = self.user_property vip_cur_level = user_property_obj.vip_cur_level print "debug guochen", self.store_has_bought.get( props_index, 0), vip_can_buy_cnt_conf.get(str(vip_cur_level), 0) if self.store_has_bought.get(props_index, 0) >= vip_can_buy_cnt_conf.get( str(vip_cur_level), 0): raise GameLogicError(u'购买次数已达到当前vip等级上限') self.store_has_bought[props_index] += 1 self.put()
def get_award(rk_user, params): '''领取主线任务奖励''' task_set = params['task'] award_result = rk_user.user_task.get_award(task_set) # 已做领奖次数+1的处理 print 'award_result', award_result if award_result != 0 : raise GameLogicError('task','cant_get_award') conf = game_config.task_config['main_task'] current_award = str(rk_user.user_task.main_task[task_set]['got_award']) print 'current_award',current_award award = conf[task_set]['award'][current_award] print 'awardinfo', [{"_id": goods, "num": award[goods]} for goods in award if goods] data = {} data['get_things'] = tools.add_things( rk_user, [{"_id": goods, "num": award[goods]} for goods in award if goods], where="main_task_award" ) data.update(show_main_task(rk_user, params)[1]) return 0, data
def recover_copy(rk_user, params): """重置副本可挑战次数 Args: dungeon_type: 战场类型 (normal 普通战场/ daily试炼) floor_id room_id (试炼可无) """ #检测参数是否合法 dungeon_type = params['dungeon_type'] floor_id = params['floor_id'] room_id = params.get('room_id', '') if room_id: where = 'recover_copy_' + "_".join([dungeon_type, floor_id, room_id]) else: where = 'recover_copy_' + "_".join([dungeon_type, floor_id]) # 检查vip可以回复的次数到了没 has_recover_times = vip.check_limit_recover(rk_user, 'recover_copy', dungeon_type, floor_id) next_recover_times = has_recover_times + 1 if dungeon_type == 'normal': recover_conf = game_config.system_config['recover_normal_copy_need'] elif dungeon_type == 'daily': recover_conf = game_config.system_config['recover_daily_copy_need'] # 检查重置次数是否达到上限 if next_recover_times not in recover_conf: raise GameLogicError('dungeon', 'reach_max_recover_%s' % dungeon_type) #检查用户coin是否足够 recover_need_coin = recover_conf[next_recover_times] if not rk_user.user_property.minus_coin(recover_need_coin, where): return 11, {'msg': utils.get_msg('user', 'not_enough_coin')} #添加回复次数 rk_user.user_property.add_recover_times('recover_copy', dungeon_type, floor_id) user_dungeon_obj = UserDungeon.get(rk_user.uid) user_dungeon_obj.recover_copy(dungeon_type, floor_id, room_id) return {}
def refresh_store_by_self(rk_user,params): """ 玩家主动刷新时 调用此接口 1. 若有免费刷新次数, 消耗刷新次数 2. 若有刷新令, 消耗刷新令28_props 3. 以上都没有, 用元宝刷新 """ user_property_obj = rk_user.user_property user_mystery_store_obj = rk_user.user_mystery_store user_pack_obj = rk_user.user_pack needed_cost = int(game_config.mystery_store_config["store_refresh_cost"]) props_config = game_config.props_config refresh_mystery_props_id = '' #获取用户的道具配置 for props_id in props_config: if props_config[props_id].get('used_by','') == 'refresh_mystery': refresh_mystery_props_id = props_id if user_mystery_store_obj.free_refresh_cnt: user_mystery_store_obj.free_refresh_cnt -= 1 user_mystery_store_obj.put() elif user_pack_obj.has_props(refresh_mystery_props_id): user_pack_obj.minus_props(refresh_mystery_props_id, 1, 'refresh_mystery_store') # 根据vip用元宝可刷新次数判断是否可以刷新 elif vip.check_limit_recover(rk_user, 'recover_mystery_store') >= 0 and \ user_property_obj.minus_coin(needed_cost, 'refresh_mystery_store'): # 已刷新次数+1 user_property_obj.add_recover_times('recover_mystery_store') else: raise GameLogicError('user', 'not_enough_coin') user_mystery_store_obj.refresh_store() return _pack_store_info(user_mystery_store_obj.store_info())
def charge(rk_user, params): """ 收费gacha抽将 """ ug = rk_user.user_gacha # 先尝试倒计免费时求将 timer_rate_conf = __free_gacha(rk_user, params) if timer_rate_conf: cost_coin = 0 rate_conf = timer_rate_conf # 更新免费抽的上次时间 ug.set_last_gacha_time() else: cost_coin = game_config.gacha_config['cost_coin'] rate_conf = __get_rate_conf('charge', rk_user) if not rk_user.user_property.is_coin_enough(cost_coin): raise GameLogicError('user', 'not_enough_coin') # 随机抽取, 获得武将或武将碎片 thing_id, num = __select_gacha_thing(rate_conf) add_info = tools.add_things(rk_user, [{ "_id": thing_id, "num": num }], where="gacha") # 扣元宝 rk_user.user_property.minus_coin(cost_coin, 'gacha') # user_card_obj = UserCards.get_instance(rk_user.uid) # # 加卡 # success_fg,p1,ucid,is_first = user_card_obj.add_card(cid, where='gacha_charge') # new_card = { # 'ucid':ucid, # 'is_first':is_first, # } # new_card.update(user_card_obj.get_card_dict(ucid)) if 'card' in add_info: # 保底开关 if is_open_safty(): for card_info in add_info['card']: cid = card_info['cid'] __set_safety_coin(rk_user, cid, cost_coin) # 写入跑马灯 user_marquee_obj = UserMarquee.get(rk_user.subarea) marquee_params = { 'type': 'gacha_charge', 'username': rk_user.username, 'cid': cid, } user_marquee_obj.record(marquee_params) # cardSoul 加传 card 的星级 elif 'cardSoul' in add_info: card_config = rk_user.game_config.card_config for card_id in add_info['cardSoul']: num = add_info['cardSoul'][card_id] star = card_config[card_id]['star'] add_info['cardSoul'][card_id] = {'num': num, 'star': star} # 抽将必送的物品. get_things = __gacha_must_get(rk_user) ug.set_gacha_cnt() # 判断新手引导 newbie_step = int(params.get('newbie_step', 0)) if newbie_step: rk_user.user_property.set_newbie_steps(newbie_step, "gacha") return { 'add_info': add_info, 'get_things': get_things, 'gacha_cnt': ug.gacha_cnt, }
def wipe_out(rk_user, params): ''' 扫荡 玩家能够在已经完美过关的关卡上使用扫荡功能,扫荡不用进入战斗画面,直接返回关卡奖励 与普通战斗不同的是扫荡并不需要更新战场的关卡信息, params: dungeon_type:'normal' 战场类型,有普通战场 normal , 试炼 daily floor_id:'1' room_id:'1' do_times: '1' 扫荡次数 ''' # ------------------------------参数check start------------------------- d_type = params.get('dungeon_type') floor_id = params.get('floor_id') room_id = params.get('room_id') do_times = params.get('do_times') if d_type not in ['normal', 'daily'] or not floor_id or not room_id: return 6, {'msg': utils.get_msg('dungeon', 'invalid_params')} try: do_times = int(do_times) except ValueError: return 6, {'msg': utils.get_msg('dungeon', 'invalid_params')} if do_times < 1 or do_times > 10: return 6, {'msg': utils.get_msg('dungeon', 'invalid_params')} # 读配置 conf_dict = { 'normal': game_config.normal_dungeon_config, 'daily': game_config.daily_dungeon_config } dungeon_config = conf_dict[d_type] # 战场不存在 try: dungeon_config[floor_id]['rooms'][room_id] except KeyError: return 6, {'msg': utils.get_msg('dungeon', 'invalid_dungeon')} # ------------------------------参数check end---------------------------- # ------------------------------扫荡逻辑check start---------------- user_dungeon = rk_user.user_dungeon has_played_info = user_dungeon.has_played_info user_property = rk_user.user_property # 玩家未达到15级,不开启扫荡 open_lv = game_config.user_init_config['open_lv'].get('wipe_out', 0) if user_property.lv < open_lv: return 11, { 'msg': utils.get_msg('dungeon', 'wipe_out_open_lv') % open_lv } # vip未达到4级,不开启多次扫荡 open_lv = game_config.user_init_config['open_lv'].get( 'vip_multi_wipe_out', 0) if do_times > 1 and user_property.vip_cur_level < open_lv: return 11, {'msg': utils.get_msg('dungeon', 'vip_wipe_out') % open_lv} # 未达到此战场 try: has_played_info[d_type][floor_id]['rooms'][room_id] except KeyError: return 11, {'msg': utils.get_msg('dungeon', 'not_arrived')} # 没达到3星 if not has_played_info[d_type][floor_id]['rooms'][room_id]['perfect']: return 11, {'msg': utils.get_msg('dungeon', 'not_three_star')} # 当前扫荡券不足 user_pack = rk_user.user_pack if not user_pack.is_props_enough('24_props', do_times): return 11, {'msg': utils.get_msg('dungeon', 'wipe_out_not_enough')} floor_config = dungeon_config[floor_id] room_config = dungeon_config[floor_id]['rooms'][room_id] # 检查用户体力是否足够 need_stamina = int(room_config['stamina']) * do_times if user_property.stamina < need_stamina: raise GameLogicError('user', 'not_enough_stamina') # 进入战场次数用完,不能扫荡, 或者更新repeat_info的当天日期 if user_dungeon.check_limit_dungeon(rk_user, params) is False: raise GameLogicError('dungeon', 'invalid_limit_dungeon') # 计算能够扫荡次数 if d_type == 'normal': try: # 判断dungeon_repeat_info中有无此关卡记录 user_dungeon.dungeon_repeat_info[d_type][floor_id][room_id] except KeyError: # 无此关卡记录时,把此关卡今日完成次数置零 user_dungeon.dungeon_repeat_info \ .setdefault(d_type, {}) \ .setdefault(floor_id, {}) \ .setdefault(room_id, 0) user_dungeon.put() can_make_copy_cnt = room_config['can_make_copy_cn'] has_played_cnt = user_dungeon.dungeon_repeat_info[d_type][floor_id][ room_id] elif d_type == 'daily': try: # 判断dungeon_repeat_info中有无此关卡记录 user_dungeon.dungeon_repeat_info[d_type][floor_id] except KeyError: # 无此关卡记录时,把此关卡今日完成次数置零 user_dungeon.dungeon_repeat_info \ .setdefault(d_type, {}) \ .setdefault(floor_id, 0) user_dungeon.put() can_make_copy_cnt = floor_config['can_make_copy_cn'] has_played_cnt = user_dungeon.dungeon_repeat_info[d_type][floor_id] can_wipe_out_cnt = can_make_copy_cnt - has_played_cnt data = {} # 剩余战场次数不够扫荡n次 if can_wipe_out_cnt < do_times: return 11, { 'msg': utils.get_msg('dungeon', 'cant_do_multi_times') % do_times } data['can_wipe_out_cnt'] = can_wipe_out_cnt # ------------------------------扫荡逻辑check end---------------- # 能扫荡次数和vip等级有关 #if utils.get_today_str() == user_property.property_info['recover_times']['today_str']: # wipe_out_times = user_property.property_info.get('wipe_out_times', 0) # vip_lv = user_property.vip_cur_level # can_wipe_out_cnt = game_config.user_vip_config[str(vip_lv)]['can_wipe_out_cnt'] # if can_wipe_out_cnt - wipe_out_times < do_times: # return 11,{'msg':utils.get_msg('dungeon', 'vip_wipe_out')} # user_property.property_info['wipe_out_times'] += do_times #else: # user_property.property_info['wipe_out_times'] = do_times # vip.check_limit_recover(rk_user, 'wipe_out') # 更新一下,防止出错 #user_property.put() # ------------------------------添加扫荡物品 start---------------- # 扫荡战利品 get_goods = { 'exp': 0, 'exp_point': 0, 'gold': 0, 'card': {}, 'equip': {}, 'soul': { 'card': {}, 'equip': {}, }, 'mat': {}, 'props': {}, } # 扫荡一次能够得到的经验,卡经验点,和钱币 get_goods['exp'] += room_config.get('exp', 0) * do_times get_goods['exp_point'] += room_config.get('exp_point', 0) * do_times get_goods['gold'] += room_config.get('gold', 0) * do_times ######## 运营活动 特定时间额外掉落物品 万恶的代码 more_drop = activity.more_dungeon_drop(d_type, floor_id, room_id, times=do_times) if more_drop: for thing_type, things_id_num in more_drop.items(): if thing_type == 'gold': get_goods['gold'] += more_drop['gold']['num'] continue for thing_id, num in things_id_num.items(): if thing_type == 'soul': if 'card' in thing_id: soul_type = 'card' elif 'equip' in thing_id: soul_type = 'equip' get_goods[thing_type][soul_type][ thing_id] = get_goods[thing_type][soul_type].get( thing_id, 0) + num else: get_goods[ thing_type][thing_id] = get_goods[thing_type].get( thing_id, 0) + num ################### ############ # 扫荡能够得到的物品 drop_info = _pack_drop_info(room_config.get('drop_info', {})) invisible_drop = _pack_drop_info(room_config.get('invisible_drop', {})) drop_info.extend(invisible_drop) # 有掉落物品(包括可见和不可见)时计算掉落, 不用战斗所以不区分可见不可见 if drop_info: # sample ['12002_equip', '5_card_soul', '53001_equip_1_soul', '6_card', '23_props', '8_props'] drop_info = list(set(drop_info)) # list去重 drop_info_config = game_config.drop_info_config['normal_dungeon'] # 检查战场掉落配置中是否有此物品 for goods_id in drop_info: if 'soul' in goods_id: if goods_id[:-5] not in drop_info_config['soul']: return 11, { 'msg': utils.get_msg('dungeon', 'no_this_goods') } else: if goods_id not in drop_info_config: return 11, { 'msg': utils.get_msg('dungeon', 'no_this_goods') } # 计算掉落数量 for n in range(do_times): for goods_id in drop_info: if 'soul' in goods_id: goods_id = goods_id[:-5] # 去掉'_soul'后缀 value_config = drop_info_config['soul'][goods_id].get( 'visible', drop_info_config['soul'][goods_id].get('invisible', 0)) # 根据配置概率判断是否得到 if utils.is_happen(value_config[1]): num = random.randint(value_config[0][0], value_config[0][1]) if 'card' in goods_id: soul_type = 'card' elif 'equip' in goods_id: soul_type = 'equip' else: continue if goods_id not in get_goods['soul'][soul_type]: get_goods['soul'][soul_type][goods_id] = num else: get_goods['soul'][soul_type][goods_id] += num else: continue else: value_config = drop_info_config[goods_id].get( 'visible', drop_info_config[goods_id].get('invisible', 0)) # 根据配置概率判断是否得到 if utils.is_happen(value_config[1]): num = random.randint(value_config[0][0], value_config[0][1]) for t in ['card', 'equip', 'props', 'mat']: if t in goods_id: get_type = t if goods_id not in get_goods[get_type]: get_goods[get_type][goods_id] = num else: get_goods[get_type][goods_id] += num else: continue # 添加must_drop必掉物品 md = room_config.get('must_drop', {}) for goods_type in md: if goods_type == 'soul': for key in md[goods_type]: if 'card' in key: soul_type = 'card' elif 'equip' in key: soul_type = 'equip' else: continue if key in get_goods[goods_type][soul_type]: get_goods[goods_type][soul_type][key] += md[goods_type][ key] else: get_goods[goods_type][soul_type][key] = md[goods_type][key] else: for key in md[goods_type]: if key in get_goods[goods_type]: get_goods[goods_type][key] += md[goods_type][key] else: get_goods[goods_type][key] = md[goods_type][key] # ------------------------------添加扫荡物品 end---------------- # 添加扫荡获得物品 tmpdata = user_property.test_give_award(get_goods, where='wipe_out') # 减扫荡券 user_pack.minus_props('24_props', do_times, 'wipe_out') # 扣体力 user_property.minus_stamina(need_stamina) # 记录repeat info user_dungeon.add_repeat_cnt(d_type, floor_id, room_id, do_times) # 给前端 data['get_exp'] = tmpdata['exp'] data['get_exp_point'] = tmpdata['exp_point'] data['get_gold'] = tmpdata['gold'] data['get_card'] = tmpdata['card'] data['get_equip'] = tmpdata['equip'] data['get_souls'] = tmpdata['soul'] data['get_material'] = tmpdata['mat'] data['get_props'] = tmpdata['props'] return 0, data
def set_decks(rk_user, params): """设置编队 params: deck_index:当前编队编号0-9 new_deck: 武将id:是否是主将 1是 0 否 ucid:0,ucid:1,ucid:0,,ucid:0_ucid:0,ucid:1,ucid:0,,ucid:0 """ #获取参数 #new_decks = params['new_deck'] new_deck = params.get('new_deck') cur_deck_index = int(params["deck_index"]) #判断编队是否符合要求 if cur_deck_index < 0 or cur_deck_index > 4: raise GameLogicError('card', 'invalid_deck') if new_deck: new_decks = new_deck.split('_') user_card_obj = UserCards.get_instance(rk_user.uid) decks = user_card_obj.decks if len(decks) == 5: new_decks_lst = [[{}] * 5] * 5 else: raise GameLogicError('card', 'invalid_deck') for i, new_deck in enumerate(new_decks): new_new_deck = new_deck.split(',') #检查队伍长度是否合法 if len(new_new_deck) == 0: continue if len(new_new_deck) < game_config.system_config['deck_length']: new_new_deck.extend([':0'] * (game_config.system_config['deck_length'] - len(new_new_deck))) # return 11,{'msg':utils.get_msg('card','invalid_deck')} #检查这些武将是否存在 new_deck_copy = [] for card_info in new_new_deck: card_info_ls = card_info.split(':') if card_info_ls: ucid, is_leader = card_info_ls else: ucid = '' is_leader = 0 if ucid and not user_card_obj.has_card(ucid): raise GameLogicError('card', 'no_card') #判断升级和强化素材不能上阵 if ucid: tmp_dict = {'ucid': ucid} if int(is_leader) == 1: tmp_dict['leader'] = 1 new_deck_copy.append(tmp_dict) else: new_deck_copy.append({}) #队伍中不能出现重复的ucid和重复的ueid和多个leader no_repeat_deck = utils.remove_null(new_deck_copy) ucids = [ card['ucid'] for card in no_repeat_deck if card.get('ucid', '') ] leader_cnt = len([ card['leader'] for card in no_repeat_deck if card.get('leader', 0) ]) if len(ucids) != len(set(ucids)) or leader_cnt != 1: raise GameLogicError('card', 'invalid_deck') new_decks_lst[i] = copy.deepcopy(new_deck_copy) #设置最新队伍 user_card_obj.set_decks(new_decks_lst) #设置当前编队索引 user_card_obj.set_cur_deck_index(cur_deck_index) if params.get('yuanjun_deck'): set_yuanjun_decks(rk_user, params) return {}