def buy_cd_time(self): ''' 清除连战冷却时间 60s = 1 credits ''' cur_time = int(time()) old_data = yield redis.hget(HASH_SCENE_COOLDOWN_TIME, self.cid) end_cooldown_time = int( old_data or 0 ) if end_cooldown_time <= cur_time: defer.returnValue( [self.user.base_att.credits] ) need_credits = (end_cooldown_time - cur_time) if (need_credits % COOLDOWN_TIME_COST): need_credits = (need_credits / COOLDOWN_TIME_COST) + 1 else: need_credits = (need_credits / COOLDOWN_TIME_COST) if need_credits > self.user.base_att.credits: log.error('Credits not enough. need: {0}, cur: {1}.'.format( total_credits, self.user.base_att.credits )) defer.returnValue( CHAR_CREDIT_NOT_ENOUGH ) yield self.user.consume_credits( need_credits, WAY_SCENE_CD_TIME ) # 删除redis冷却时间记录 yield redis.hdel( HASH_SCENE_COOLDOWN_TIME, self.cid ) defer.returnValue( [self.user.base_att.credits] )
def write_mail(p, req): ''' 保存新的邮件 page_type: 1:全部; 2:好友; 3:系统; 4:战斗 ''' try: rcv_cid, page_type, module_id, detail = req send_time = int(time()) except: rcv_cid, page_type, module_id, detail, send_time = req _key = HASH_MAIL_CONTENT % (page_type, rcv_cid) _primary = yield redis.hincrby(_key, MAIL_PRIMARY_INC, 1) content = dumps((_primary, send_time, page_type, module_id, detail)) yield redis.hset(_key, _primary, content) # 最多保存100条 _del_fields = [] _all_fields = yield redis.hkeys(_key) _all_fields = sorted(_all_fields, reverse=True) if len(_all_fields) > 101: _del_fields = _all_fields[101:] if MAIL_PRIMARY_INC in _del_fields: _del_fields.remove(MAIL_PRIMARY_INC) if _del_fields: yield redis.hdel(_key, *_del_fields) # 通知在线玩家有新邮件 rcv_user = g_UserMgr.getUserByCid(rcv_cid) if rcv_user and not rcv_user.notify_mail: rcv_user.notify_mail = True gw_send(rcv_cid, 'new_mail_notify', None)
def check_grant_items(cid, user): ''' 检查是否有全服发放的道具可领, 有时发放到领奖中心 ''' _register_ts = datetime2time( user.base_att.register_time ) _grant_keys_got = yield redis.smembers( SET_GOT_GRANT_KEYS%cid ) _all_grants = yield redis.hgetall( HASH_GM_GRANT_ITEMS ) if not _all_grants: defer.returnValue( NO_ERROR ) for _grant_key, _grant_info in _all_grants.iteritems(): _grant_key = int(_grant_key) # 判断注册时间点是否在发送道具时间点之后 if _register_ts and _grant_key < _register_ts: continue if _grant_key in _grant_keys_got: continue # 检查时间是否过期, 14天 if check_valid_time(_grant_key, hour=AWARD_VALID_HOURS): yield redis.hdel( HASH_GM_GRANT_ITEMS, _grant_key ) continue # _grant_info=[玩家cid列表, 发放的道具, 过期时间戳] _grant_info = loads(_grant_info) if not _grant_info[0] or cid in _grant_info[0]: yield redis.sadd( SET_GOT_GRANT_KEYS%cid, _grant_key ) yield g_AwardCenterMgr.new_award( cid, AWARD_TYPE_GM, [_grant_key, _grant_info[1]], flag=False ) defer.returnValue( NO_ERROR )
def buyed_count_today(self, cid): _buyed_cnt_list = [] _group = self.current_group() if _group and _group.is_open: _stream = yield redis.hget(DICT_TIME_LIMITED_SHOP_BUYED, cid) if _stream: _tuple_all_buyed = loads(_stream) for _id, _buyed_cnt, _last_buy_t in _tuple_all_buyed: if timestamp_is_today(_last_buy_t): _buyed_cnt_list.append([_id, _buyed_cnt, _last_buy_t]) else: redis.hdel(DICT_TIME_LIMITED_SHOP_BUYED, cid) defer.returnValue(_buyed_cnt_list)
def write_mail(p, req): ''' 保存新的邮件 page_type: 1:全部; 2:好友; 3:系统; 4:战斗 ''' try: rcv_cid, page_type, module_id, detail = req send_time = int(time()) except: rcv_cid, page_type, module_id, detail, send_time = req _key = HASH_MAIL_CONTENT % (page_type, rcv_cid ) _primary = yield redis.hincrby( _key, MAIL_PRIMARY_INC, 1 ) content = dumps( (_primary, send_time, page_type, module_id, detail) ) yield redis.hset(_key, _primary, content) # 最多保存100条 _del_fields = [] _all_fields = yield redis.hkeys( _key ) _all_fields = sorted( _all_fields, reverse=True ) if len(_all_fields) > 101: _del_fields = _all_fields[101:] if MAIL_PRIMARY_INC in _del_fields: _del_fields.remove( MAIL_PRIMARY_INC ) if _del_fields: yield redis.hdel( _key, *_del_fields ) # 通知在线玩家有新邮件 rcv_user = g_UserMgr.getUserByCid(rcv_cid) if rcv_user and not rcv_user.notify_mail: rcv_user.notify_mail = True gw_send(rcv_cid, 'new_mail_notify', None)
def buy_cd_time(self): ''' 清除连战冷却时间 60s = 1 credits ''' cur_time = int(time()) old_data = yield redis.hget(HASH_SCENE_COOLDOWN_TIME, self.cid) end_cooldown_time = int(old_data or 0) if end_cooldown_time <= cur_time: defer.returnValue([self.user.base_att.credits]) need_credits = (end_cooldown_time - cur_time) if (need_credits % COOLDOWN_TIME_COST): need_credits = (need_credits / COOLDOWN_TIME_COST) + 1 else: need_credits = (need_credits / COOLDOWN_TIME_COST) if need_credits > self.user.base_att.credits: log.error('Credits not enough. need: {0}, cur: {1}.'.format( total_credits, self.user.base_att.credits)) defer.returnValue(CHAR_CREDIT_NOT_ENOUGH) yield self.user.consume_credits(need_credits, WAY_SCENE_CD_TIME) # 删除redis冷却时间记录 yield redis.hdel(HASH_SCENE_COOLDOWN_TIME, self.cid) defer.returnValue([self.user.base_att.credits])
def update_avoid_cache(self, treasure_id): ''' @summary: 维护宝物碎片列表的同时, 维护redis中的玩家阵营数据 ''' if self.user.base_att.level <= AVOID_WAR_LEVEL: log.warn('User level <= 12 could not be plunder. cid: {0}.'.format( self.cid )) defer.returnValue( REQUEST_LIMIT_ERROR ) treasure_conf = get_item_by_itemid( treasure_id ) if not treasure_conf: log.error('Can not find conf. treasure_id: {0}.'.format( treasure_id )) defer.returnValue( NOT_FOUND_CONF ) _shard_list = [splited[1] for splited in treasure_conf['ChangeList'] ] # 只有同时拥有“一个宝物的2个或者2个以上,不同位置碎片”的玩家才会被加入被抢夺列表 _haved = 0 _plunder_list = [] # 可被抢夺的碎片 for _shard_id in _shard_list: for attrib in self.__gsattribs.itervalues(): if attrib.item_id == _shard_id: _haved += 1 _plunder_list.append( _shard_id ) break #_treasure_ids = yield redis.hget( HASH_TREASURE_CHARACTER_IDS, self.cid ) #if _treasure_ids: # _treasure_ids = loads(_treasure_ids) #else: # _treasure_ids = [] #log.info('For Test. _haved: {0}, cid: {1}, _shard_id: {2}, _shard_list: {3}, _plunder_list: {4}.'.format( _haved, self.cid, _shard_id, _shard_list, _plunder_list )) for _shard_id in _shard_list: yield redis.hdel( TPL_TREASURE_SHARD_GOT % _shard_id, self.cid ) #if treasure_id in _treasure_ids: # _treasure_ids.remove( treasure_id ) if _haved > 1: #_flag = True # 成功获取玩家有效阵容 #_exists = yield redis.hexists( HASH_TREASURE_CHARACTER_CAMP, self.cid ) #if (not _exists): # _all_camps = yield self.user.get_camp() # fellow_ids = [] # if _all_camps: # for camp_data in _all_camps[1]: # if not camp_data or not camp_data[1]: # continue # fellow_ids.append( camp_data[1][1] ) # # 有效的玩家阵容 # if fellow_ids: # yield redis.hset( HASH_TREASURE_CHARACTER_CAMP, self.cid, dumps(_all_camps) ) # #log.info('For Test. save treasure camp. cid: {0}.'.format( self.cid )) # else: # _flag = False # log.error('Save char treasure camp data fail. cid: {0}, _all_camps: {1}.'.format( self.cid, _all_camps )) for _shard_id in _plunder_list: yield redis.hset( TPL_TREASURE_SHARD_GOT % _shard_id, self.cid, dumps( (self.cid, self.user.level, self.user.base_att.nick_name) ) )
def reset_dungeon_count(self, scene_id, dungeon_id, reset_way): dungeon = yield self.get_dungeon(scene_id, dungeon_id) if not dungeon: log.error('Unknown dungeon id. scene_id: {0}, dungeon_id: {1}.'.format( scene_id, dungeon_id )) defer.returnValue( REQUEST_LIMIT_ERROR ) # 判断是否还有剩余挑战次数 #if 1 > dungeon.dungeon_today_count: # defer.returnValue( REQUEST_LIMIT_ERROR ) daily_info = yield self.get_scene_reset_daily() had_reset = daily_info[2] + daily_info[3] # 检查当天最大可重置次数 vip_conf = get_vip_conf( self.user.vip_level ) if not vip_conf or (had_reset >= vip_conf['DungeonReset']): log.error('Dungeon reset count limit. cid:{0}, vip_level:{1}, max_count:{2}, item_reset:{3}, credit_reset:{4}.'.format( \ self.cid, self.user.vip_level, vip_conf['DungeonReset'] if vip_conf else 0, daily_info[2], daily_info[3])) defer.returnValue( CHAR_VIP_LEVEL_LIMIT ) items_return = [] # 检查reset_way if reset_way == SCENE_RESET_ITEM: daily_info[2] += 1 # 扣道具 total_item, item_attribs = yield self.user.bag_item_mgr.get_items( ITEM_SCENE_RESET_ID ) if 1 > total_item: defer.returnValue( CHAR_ITEM_NOT_ENOUGH ) res_err, used_attribs = yield self.user.bag_item_mgr.use(ITEM_SCENE_RESET_ID, 1) if res_err: defer.returnValue( res_err ) # used_attribs-已使用的道具 for _a in used_attribs: items_return.append( [_a.attrib_id, ITEM_TYPE_ITEM, ITEM_SCENE_RESET_ID, _a.item_num] ) # add syslog syslogger(LOG_ITEM_LOSE, self.cid, self.user.level, self.user.vip_level, \ self.user.alliance_id, _a.attrib_id, ITEM_SCENE_RESET_ID, 1, WAY_SCENE_RESET) elif reset_way == SCENE_RESET_CREDIT: daily_info[3] += 1 # 扣钻石 need_credits = get_scene_reset_conf(daily_info[3]) if 0 == need_credits: defer.returnValue( SCENE_RESET_ERROR ) if need_credits > self.user.credits: log.error('credits not enough. cid:{0}, need:{1}, curr:{2}, credit_reset:{3}.'.format( self.cid, need_credits, self.user.credits, daily_info[3] )) defer.returnValue( CHAR_CREDIT_NOT_ENOUGH ) yield self.user.consume_credits( need_credits, WAY_SCENE_RESET ) else: log.error('Unknown args. cid: {0}, reset_way: {1}.'.format( self.cid, reset_way )) defer.returnValue( REQUEST_LIMIT_ERROR ) dungeon.dungeon_today_count = 0 yield redis.hset(HASH_SCENE_RESET_DAILY, self.cid, dumps(daily_info)) # 删除redis冷却时间记录 yield redis.hdel( HASH_SCENE_COOLDOWN_TIME, self.cid ) defer.returnValue( (self.user.credits, items_return) )
def award_center_info(self, cid): ''' @summary: 获取玩家的奖励信息 @param : 详见redis_data_record.py说明 ''' _award_info = [] # 玩家的奖励列表 _expired_ids = [] # 过期的award_ids _all_data = yield redis.hgetall(HASH_AWARD_CENTER % cid) if _all_data: for _data in _all_data.itervalues(): _data = loads(_data) if len(_data) != 3: continue res_err, items_list = get_award_conf(_data[1], *_data[2]) if res_err: _expired_ids.append(_data[0]) continue if not items_list: log.error('No conf in award center. cid: {0}, _data: {1}.'. format(cid, _data)) continue ## 过滤掉天外天中的塔层 #if _data[1] == AWARD_TYPE_CLIMBING: # _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]] ) ## 过滤掉VIP福利的VIP Level #elif _data[1] == AWARD_TYPE_VIP_WELFARE: # _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]] ) ## 过滤掉限时神将的活动ID、排名 #elif _data[1] in [AWARD_TYPE_LIMIT_FELLOW_RANK, AWARD_TYPE_LIMIT_FELLOW_SCORE]: # _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]] ) ## 过滤掉累计充值、累计消费的活动ID、奖励ID列表 #elif _data[1] in [AWARD_TYPE_PAY_ACTIVITY, AWARD_TYPE_CONSUME_ACTIVITY]: # _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]] ) ## 过滤掉每日任务中的奖励档次ID #elif _data[1] == AWARD_TYPE_DAILY_QUEST: # _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]] ) # 组织奖励信息 if _data[1] in [AWARD_TYPE_CLIMBING, AWARD_TYPE_VIP_WELFARE, AWARD_TYPE_LIMIT_FELLOW_RANK, \ AWARD_TYPE_LIMIT_FELLOW_SCORE, AWARD_TYPE_PAY_ACTIVITY, AWARD_TYPE_CONSUME_ACTIVITY, \ AWARD_TYPE_DAILY_QUEST, AWARD_TYPE_PAY_LOGIN, AWARD_TYPE_JOUSTRANK, AWARD_TYPE_GROUP_BUY]: _award_info.append( [_data[0], _data[1], [_data[2][0], items_list]]) elif _data[1] in [ AWARD_TYPE_FIRSTPAY, AWARD_TYPE_ARENARANK, AWARD_TYPE_MONTGLY_CARD, AWARD_TYPE_RESOURCE_REWARD ]: _data[2].append(items_list) _award_info.append(_data) else: # 不需要任何处理 _award_info.append(_data) if _expired_ids: yield redis.hdel(HASH_AWARD_CENTER % cid, *_expired_ids) #log.info('For Test. Del Redis Data. _expired_ids: {0}.'.format( _expired_ids )) defer.returnValue(_award_info)
def mail_list(p, req): ''' 分页获取邮件列表 page_type: 1:全部; 2:好友; 3:系统; 4:战斗 ''' cid, [page_type, index] = req user = g_UserMgr.getUserByCid(cid) if not user: log.error('Can not find user. cid: {0}.'.format(cid)) defer.returnValue(CONNECTION_LOSE) user.notify_mail = False all_types = [] now_time = int(time()) mails_time = [] # for sort all_mails = [] if page_type == MAIL_PAGE_ALL: all_types = [MAIL_PAGE_FRIEND, MAIL_PAGE_SYSTEM, MAIL_PAGE_BATTLE] else: all_types = [page_type] for _type in all_types: # 每个type 最多保存100条 del_mail = [] # 已过期的邮件ID _all = yield redis.hgetall(HASH_MAIL_CONTENT % (_type, cid)) for _field, _value in _all.iteritems(): if _field == MAIL_PRIMARY_INC: continue _value = loads(_value) # 判断邮件是否过期 if _value[1] + MAIL_VALID_SECONDS < now_time: del_mail.append(_field) continue # 根据时间点先后组织数据 for _i, _time in enumerate(mails_time): if _value[1] > _time: all_mails.insert(_i, _value) break else: _i = len(mails_time) all_mails.append(_value) mails_time.insert(_i, _value[1]) if del_mail: yield redis.hdel(HASH_MAIL_CONTENT % (_type, cid), *del_mail) defer.returnValue( (page_type, index, len(all_mails), all_mails[index:index + 10]))
def mail_list(p, req): ''' 分页获取邮件列表 page_type: 1:全部; 2:好友; 3:系统; 4:战斗 ''' cid, [page_type, index] = req user = g_UserMgr.getUserByCid(cid) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) user.notify_mail = False all_types = [] now_time = int(time()) mails_time = [] # for sort all_mails = [] if page_type == MAIL_PAGE_ALL: all_types = [MAIL_PAGE_FRIEND, MAIL_PAGE_SYSTEM, MAIL_PAGE_BATTLE] else: all_types = [page_type] for _type in all_types: # 每个type 最多保存100条 del_mail = [] # 已过期的邮件ID _all = yield redis.hgetall( HASH_MAIL_CONTENT % (_type, cid) ) for _field, _value in _all.iteritems(): if _field == MAIL_PRIMARY_INC: continue _value = loads(_value) # 判断邮件是否过期 if _value[1] + MAIL_VALID_SECONDS < now_time: del_mail.append( _field ) continue # 根据时间点先后组织数据 for _i, _time in enumerate(mails_time): if _value[1] > _time: all_mails.insert(_i, _value) break else: _i = len(mails_time) all_mails.append( _value ) mails_time.insert(_i, _value[1]) if del_mail: yield redis.hdel( HASH_MAIL_CONTENT % (_type, cid), *del_mail ) defer.returnValue( (page_type, index, len(all_mails), all_mails[index:index+10]) )
def friend_get_douzhan(p, req): ''' 领取斗战点 ''' cid, [send_cids] = req if not isinstance(send_cids, (list, tuple)): defer.returnValue( CLIENT_DATA_ERROR ) user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) if user.douzhan >= 65535: log.error("User's douzhan had been max. cid: {0}.".format( cid )) defer.returnValue( DOUZHAN_MAX_ERROR ) add_douzhan = yield user.goodwill_mgr.check_achieve() dt_now = datetime.now() _data = yield redis.hget( HASH_FRIENDS_GIFT_DOUZHAN % cid, FRIENDS_DOUZHAN_GET ) if _data: last_time, left_count = loads( _data ) dt_last = datetime.fromtimestamp( last_time ) if dt_last.date() != dt_now.date(): left_count = GET_DOUZHAN_DAILY_LIMIT + add_douzhan else: left_count = GET_DOUZHAN_DAILY_LIMIT + add_douzhan if left_count < len(send_cids): log.error("User's gift douzhan count has used up today. left_count: {0}.".format( left_count )) defer.returnValue( DOUZHAN_DAILY_ERROR ) for send_cid in send_cids: _data = yield redis.hget( HASH_FRIENDS_GIFT_DOUZHAN % cid, send_cid ) if not _data: log.error('Can not find the gift douzhan. cid: {0}, send_cid: {1}.'.format( cid, send_cid )) continue #defer.returnValue( REQUEST_LIMIT_ERROR ) yield redis.hdel( HASH_FRIENDS_GIFT_DOUZHAN % cid, send_cid ) # 策划修改-好友赠送,每次改为2点 user.add_douzhan( 2 ) # 回赠 yield gift_douzhan(cid, send_cid) # 更新今日可领次数 left_count -= 1 yield redis.hset( HASH_FRIENDS_GIFT_DOUZHAN % cid, FRIENDS_DOUZHAN_GET, dumps((int(time()), left_count)) ) defer.returnValue( [user.douzhan] )
def friend_discard(p, req): ''' 删除好友 ''' cid, [target_cid] = req user = g_UserMgr.getUser( cid ) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) user.del_friends( target_cid ) offline_user = yield gs_offline_login( target_cid ) if offline_user: ms_send('write_mail', (target_cid, MAIL_PAGE_FRIEND, MAIL_FRIEND_6, [user.lead_id, user.nick_name])) offline_user.del_friends( cid ) ms_send('write_mail', (cid, MAIL_PAGE_FRIEND, MAIL_FRIEND_5, [offline_user.lead_id, offline_user.nick_name])) reactor.callLater(SESSION_LOGOUT_REAL, gs_offline_logout, target_cid) # 删除双方互相赠送的斗战点 yield redis.hdel( HASH_FRIENDS_GIFT_DOUZHAN % cid, target_cid ) yield redis.hdel( HASH_FRIENDS_GIFT_DOUZHAN % target_cid, cid ) defer.returnValue( NO_ERROR )
def dissolve_alliance(self, alliance_id): yield redis.hdel( HASH_ALLIANCE_INFO, alliance_id ) _a = self.get_alliance( alliance_id ) if not _a: defer.returnValue( ALLIANCE_UNKNOWN_ERROR ) if len(_a.members) > 1: defer.returnValue( ALLIANCE_DISSOLVE_ERROR ) # 更新公会的状态值 _a.deleted = True # 清除公会的入会申请 yield _a.del_all_requests() if _a in Server.__alliances: Server.__alliances.remove( _a ) defer.returnValue( NO_ERROR )
def check_character_forbidden(cid): ''' return 0-未封号, 1-封号中 ''' _status = 0 _time_now = int(time()) _end_timestamp = yield redis.hget(HASH_SERVER_FORBIDDEN, cid) if _end_timestamp is None: pass elif _end_timestamp < 0: _status = 1 elif _end_timestamp > _time_now: _status = 1 else: yield redis.hdel(HASH_SERVER_FORBIDDEN, cid) defer.returnValue(_status)
def dissolve_alliance(self, alliance_id): yield redis.hdel(HASH_ALLIANCE_INFO, alliance_id) _a = self.get_alliance(alliance_id) if not _a: defer.returnValue(ALLIANCE_UNKNOWN_ERROR) if len(_a.members) > 1: defer.returnValue(ALLIANCE_DISSOLVE_ERROR) # 更新公会的状态值 _a.deleted = True # 清除公会的入会申请 yield _a.del_all_requests() if _a in Server.__alliances: Server.__alliances.remove(_a) defer.returnValue(NO_ERROR)
def check_character_forbidden(cid): ''' return 0-未封号, 1-封号中 ''' _status = 0 _time_now = int(time()) _end_timestamp = yield redis.hget(HASH_SERVER_FORBIDDEN, cid) if _end_timestamp is None: pass elif _end_timestamp < 0: _status = 1 elif _end_timestamp > _time_now: _status = 1 else: yield redis.hdel(HASH_SERVER_FORBIDDEN, cid) defer.returnValue( _status )
def check_character_mute(cid): ''' return 0-未禁言, 1-禁言中 ''' _status = 0 _time_now = int(time()) _end_timestamp = yield redis.hget(HASH_SERVER_MUTE, cid) if _end_timestamp is None: pass elif _end_timestamp < 0: _status = 1 elif _end_timestamp > _time_now: _status = 1 else: yield redis.hdel(HASH_SERVER_MUTE, cid) defer.returnValue( _status )
def get_pay_login_status(cid, redis_key, max_package_id): ''' @summary: 连续登录天数,连续登录中断后从1开始记,连续登录天数超过配置条数则领取最后一条奖励 ''' _data = yield redis.hget(redis_key, cid) if _data: _data = loads(_data) else: _data = [0, 1, 0] # 第一次充值签到 if not _data[0]: defer.returnValue((0, 1)) # 判断时间点 _d_now = datetime.now() _d_time = datetime.fromtimestamp(_data[0]) _delta = (_d_now.date() - _d_time.date()) if _delta.days == 0: # 同一天重复登录 if _data[2]: # 已签到 _status = (_data[1], 0) else: # 未签到 _status = (0 if _data[1] < 1 else _data[1] - 1, _data[1]) defer.returnValue(_status) # 判断是否满足签到条件 if not _data[2]: res_err = yield reward_to_center(cid, _d_time, _data[1]) if res_err or _delta.days > 1: yield redis.hdel(redis_key, cid) else: _data[2] = 1 if _data[2] and _delta.days == 1: # 前一天已签到, 连续登录 if _data[1] >= max_package_id: _next_package_id = max_package_id else: _next_package_id = _data[1] + 1 _status = (_data[1], _next_package_id) yield redis.hset(redis_key, cid, dumps([int(time()), _next_package_id, 0])) else: _status = (0, 1) defer.returnValue(_status)
def get_pay_login_status(cid, redis_key, max_package_id): ''' @summary: 连续登录天数,连续登录中断后从1开始记,连续登录天数超过配置条数则领取最后一条奖励 ''' _data = yield redis.hget(redis_key, cid) if _data: _data = loads(_data) else: _data = [0, 1, 0] # 第一次充值签到 if not _data[0]: defer.returnValue( (0, 1) ) # 判断时间点 _d_now = datetime.now() _d_time = datetime.fromtimestamp( _data[0] ) _delta = (_d_now.date() - _d_time.date()) if _delta.days == 0: # 同一天重复登录 if _data[2]: # 已签到 _status = (_data[1], 0) else: # 未签到 _status = ( 0 if _data[1] < 1 else _data[1] -1, _data[1]) defer.returnValue( _status ) # 判断是否满足签到条件 if not _data[2]: res_err = yield reward_to_center(cid, _d_time, _data[1]) if res_err or _delta.days > 1: yield redis.hdel(redis_key, cid) else: _data[2] = 1 if _data[2] and _delta.days == 1: # 前一天已签到, 连续登录 if _data[1] >= max_package_id: _next_package_id = max_package_id else: _next_package_id = _data[1] + 1 _status = (_data[1], _next_package_id) yield redis.hset(redis_key, cid, dumps([int(time()), _next_package_id, 0])) else: _status = (0, 1) defer.returnValue( _status )
def status(self): ''' 获取玩家的每日任务当前的状态, 含奖励和任务进度 ''' data = yield redis.hget(HASH_DAILY_QUEST, self.cid) if not data: defer.returnValue( [int(time()), [], [], 0] ) _data = loads( data ) # 判断时间是否已过刷新时间 _reset_timestamp = get_reset_timestamp() # 计算已获得的总积分 _score = self.calc_total_score( _data[2] ) _data[3] = _score if _data[0] <= _reset_timestamp: yield self.reward_to_center( _score, _data[1] ) yield redis.hdel( HASH_DAILY_QUEST, self.cid ) defer.returnValue( [int(time()), [], [], 0] ) defer.returnValue( _data )
def get_award_center(self, user, get_award_type, award_id): ''' @summary: 领奖 @param : get_award_type 1:单次领奖; 2:全部领取; award_id : get_award_type为2时, 此字段无意义 ''' items_return = [] # 返回背包的道具信息 awarded_ids = [] # 已领奖的award_ids if get_award_type == 1: # 单次领奖 _data = yield redis.hget(HASH_AWARD_CENTER % user.cid, award_id) if not _data: log.error('Can not find the award. cid:{0}.'.format(user.cid)) defer.returnValue(REQUEST_LIMIT_ERROR) _data = loads(_data) if len(_data) != 3: log.error('redis data error. cid:{0}, _data:{1}.'.format( user.cid, _data)) defer.returnValue(UNKNOWN_ERROR) awarded_ids.append(_data[0]) items_return = yield self.get_one_award(user, _data, items_return) elif get_award_type == 2: # 全部领取 _all_data = yield redis.hgetall(HASH_AWARD_CENTER % user.cid) if not _all_data: log.error('Can not find the award. cid:{0}.'.format(user.cid)) defer.returnValue(REQUEST_LIMIT_ERROR) for _data in _all_data.itervalues(): _data = loads(_data) if len(_data) != 3: log.error('redis data error. cid:{0}, _data: {1}.'.format( user.cid, _data)) continue awarded_ids.append(_data[0]) items_return = yield self.get_one_award( user, _data, items_return) if awarded_ids: yield redis.hdel(HASH_AWARD_CENTER % user.cid, *awarded_ids) #log.info('For Test. get_award_center. Del Redis Data. awarded_ids: {0}.'.format( awarded_ids )) defer.returnValue(items_return)
def users_got_shards(shard_id, user_level, user_cid): ''' @param: user_cid-玩家的cid @param: user_level-玩家的等级 ''' _users = [] _hour = datetime.now().hour # 碎片对应的宝物配置 treasure_id = 0 item_conf = get_item_by_itemid(shard_id) if item_conf: _, treasure_id, _ = item_conf['ChangeList'][0] plunder_cids = AvoidWarMgr.In_Plunders.get(treasure_id, []) robots_added = 0 if _hour >= AVOID_USER_TIME_SPLIT and user_level > AVOID_WAR_LEVEL: _all = yield redis.hvals(TPL_TREASURE_SHARD_GOT % shard_id) _tmp_users = [] for _iter in _all: _cid, _level, _name = loads(_iter) # 自己不能加入被抢夺列表 if _cid == user_cid: continue # 随机的玩家范围, 根据主角等级, 加减10级的范围内随机 if abs(user_level - _level) <= 10: # 免战后的玩家不会出现在任何人的夺宝列表中 _remain = yield AvoidWarMgr.remain_avoid_war_time(_cid) if _remain > 0: log.info( 'User in avoid war time. shard_id: {0}, treasure_id: {1}, cid: {2}.' .format(shard_id, treasure_id, _cid)) continue # 出现在其它人列表中的玩家过滤掉 if _cid in plunder_cids: log.info( 'User in plundered. shard_id: {0}, treasure_id: {1}, cid: {2}.' .format(shard_id, treasure_id, _cid)) continue _tmp_users.append([_cid, _level, _name, tuple()]) #从满足条件的玩家中随机出10个玩家的下标 _len = len(_tmp_users) if _len > MAX_USERS_FOR_AVOID: _indexs = range(_len) random.shuffle(_indexs) for _idx in _indexs[:MAX_USERS_FOR_AVOID]: #随机后的前10个 major_level, _tmp_users[_idx][ 3], _ = yield load_offline_user_info( _tmp_users[_idx][0]) # 保存阵容失败的玩家 if _tmp_users[_idx][3]: _tmp_users[_idx][ 1] = major_level if major_level else _tmp_users[ _idx][1] _users.append(_tmp_users[_idx]) else: log.warn('Error char camp data. data: {0}.'.format( _tmp_users[_idx])) yield redis.hdel(TPL_TREASURE_SHARD_GOT % shard_id, _tmp_users[_idx][0]) else: for _t in _tmp_users: major_level, _t[3], _ = yield load_offline_user_info(_t[0]) # 保存阵容失败的玩家 if _t[3]: _t[1] = major_level if major_level else _t[1] _users.append(_t) else: log.warn('Error char camp data. data: {0}.'.format(_t)) yield redis.hdel(TPL_TREASURE_SHARD_GOT % shard_id, _t[0]) robots_added = len(_users) log.info('Have user: {0}, user_ids: {1}.'.format( robots_added, [_u[0] for _u in _users])) _len_robots = len(AvoidWarMgr.Robots) _start_robot = 0 if user_level <= 20: _start_robot = 3001 elif user_level <= 30: _start_robot = 1001 elif user_level <= 50: _start_robot = 101 if _start_robot >= _len_robots: _start_robot = 0 while 1: if not AvoidWarMgr.Robots or robots_added >= _len_robots: break if len( _users ) >= MAX_MEMBER_FOR_AVOID or robots_added >= MAX_MEMBER_FOR_AVOID: break robots_added += 1 _key = AvoidWarMgr.Robots[random.randint(_start_robot, _len_robots - 1)] _robot = get_robot_conf(_key) if _robot: _camp = map(int, _robot['RobotList'].split(',')) _users.append((0, user_level, _robot['RobotName'], _camp)) else: log.warn( "[ AvoidWarMgr.users_got_shards ]:No such robot in db. key:", _key, 'and type:', type(_key)) defer.returnValue(_users)
def users_got_shards(shard_id, user_level, user_cid): ''' @param: user_cid-玩家的cid @param: user_level-玩家的等级 ''' _users = [] _hour = datetime.now().hour # 碎片对应的宝物配置 treasure_id = 0 item_conf = get_item_by_itemid( shard_id ) if item_conf: _, treasure_id, _ = item_conf['ChangeList'][0] plunder_cids = AvoidWarMgr.In_Plunders.get(treasure_id, []) robots_added = 0 if _hour >= AVOID_USER_TIME_SPLIT and user_level > AVOID_WAR_LEVEL: _all = yield redis.hvals( TPL_TREASURE_SHARD_GOT % shard_id ) _tmp_users = [] for _iter in _all: _cid, _level, _name = loads( _iter ) # 自己不能加入被抢夺列表 if _cid == user_cid: continue # 随机的玩家范围, 根据主角等级, 加减10级的范围内随机 if abs(user_level - _level) <= 10: # 免战后的玩家不会出现在任何人的夺宝列表中 _remain = yield AvoidWarMgr.remain_avoid_war_time( _cid ) if _remain > 0: log.info('User in avoid war time. shard_id: {0}, treasure_id: {1}, cid: {2}.'.format( shard_id, treasure_id, _cid )) continue # 出现在其它人列表中的玩家过滤掉 if _cid in plunder_cids: log.info('User in plundered. shard_id: {0}, treasure_id: {1}, cid: {2}.'.format( shard_id, treasure_id, _cid )) continue _tmp_users.append( [_cid, _level, _name, tuple()] ) #从满足条件的玩家中随机出10个玩家的下标 _len = len(_tmp_users) if _len > MAX_USERS_FOR_AVOID: _indexs = range(_len) random.shuffle( _indexs ) for _idx in _indexs[:MAX_USERS_FOR_AVOID]: #随机后的前10个 major_level, _tmp_users[_idx][3], _ = yield load_offline_user_info(_tmp_users[_idx][0]) # 保存阵容失败的玩家 if _tmp_users[_idx][3]: _tmp_users[_idx][1] = major_level if major_level else _tmp_users[_idx][1] _users.append( _tmp_users[_idx] ) else: log.warn('Error char camp data. data: {0}.'.format( _tmp_users[_idx] )) yield redis.hdel( TPL_TREASURE_SHARD_GOT % shard_id, _tmp_users[_idx][0] ) else: for _t in _tmp_users: major_level, _t[3], _ = yield load_offline_user_info(_t[0]) # 保存阵容失败的玩家 if _t[3]: _t[1] = major_level if major_level else _t[1] _users.append( _t ) else: log.warn('Error char camp data. data: {0}.'.format( _t )) yield redis.hdel( TPL_TREASURE_SHARD_GOT % shard_id, _t[0] ) robots_added = len(_users) log.info('Have user: {0}, user_ids: {1}.'.format( robots_added, [_u[0] for _u in _users] )) _len_robots = len( AvoidWarMgr.Robots ) _start_robot = 0 if user_level <= 20: _start_robot = 3001 elif user_level <= 30: _start_robot = 1001 elif user_level <= 50: _start_robot = 101 if _start_robot >= _len_robots: _start_robot = 0 while 1: if not AvoidWarMgr.Robots or robots_added >= _len_robots: break if len( _users ) >= MAX_MEMBER_FOR_AVOID or robots_added >= MAX_MEMBER_FOR_AVOID: break robots_added += 1 _key = AvoidWarMgr.Robots[ random.randint(_start_robot, _len_robots-1) ] _robot = get_robot_conf( _key ) if _robot: _camp = map( int, _robot['RobotList'].split(',') ) _users.append( ( 0, user_level, _robot['RobotName'], _camp ) ) else: log.warn( "[ AvoidWarMgr.users_got_shards ]:No such robot in db. key:", _key, 'and type:', type(_key) ) defer.returnValue( _users )
def plunder_streak(p, req): ''' @summary: 连续10次抢夺机器人, 非玩家 ''' res_err = UNKNOWN_ERROR cid, [plunder_cid, shard_id] = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( res_err ) # 等级限制 或者 VIP等级开关 vip_conf = get_vip_conf( user.vip_level ) res_err = user.check_function_open(FUNCTION_TEN_PLUNDER) if res_err and (not vip_conf.get('TreasureTenPlunder', 0)): log.error('cid:{0}, level:{1}, vip_level:{2}.'.format( cid, user.level, user.vip_level )) defer.returnValue( CHAR_VIP_LEVEL_LIMIT ) # 玩家的斗战点不足 if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format( cid )) defer.returnValue( CHAR_DOUZHAN_NOT_ENOUGH ) # 只能连续抢机器人 if plunder_cid > 0: log.error('Can not plunder streak user. plunder_cid: {0}.'.format( plunder_cid )) defer.returnValue( REQUEST_LIMIT_ERROR ) #抢夺,自动去除自己的免战 yield redis.hdel( HASH_AVOID_WAR_TIME, cid ) battle_count = 0 # 抢夺的次数 plunder_result = 0 # 未抢到 new_shard = [] # 翻牌得到的幸运道具 lottery_items = [] for _i in range(0, 10): # 扣斗战点 user.base_att.douzhan -= PVP_NEED_DOUZHAN # 抢夺次数加1 battle_count += 1 # 幸运道具奖励 new_item = yield random_lottery_items( cid, user.level, user.base_att.vip_level, rand_count=1 ) if len(new_item) > 0: lottery_items.append( new_item[0] ) else: log.error('No lottery items. cid: {0}, level: {1}, vip_level: {2}.'.format( cid, level, vip_level )) # 随机碎片 err, add_items = yield AvoidWarMgr.rand_shard( user, shard_id, plunder_cid ) if not err and (len(add_items) > 0): #抢到 plunder_result = 1 syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 1, 1, add_items[0][0], add_items[0][2]) new_shard = [add_items[0][0], add_items[0][2]] break # 玩家的斗战点不足 if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format( cid )) break # 幸运道具进背包 items_return = [] for _type, _id, _num, _ in lottery_items: model = ITEM_MODELs.get( _type, None ) if not model: log.error('Unknown decomposition item type. item type: {0}.'.format( _type )) continue res_err, res_value = yield model(user, ItemID=_id, ItemNum=_num, AddType=WAY_LOTTERY_AWARD, CapacityFlag=False) if not res_err: for _v in res_value: items_return = total_new_items(_v, items_return) # 每日任务计数 yield user.daily_quest_mgr.update_daily_quest( DAILY_QUEST_ID_5, battle_count ) # 开服七天 yield user.open_server_mgr.update_open_server_activity_quest( OPEN_SERVER_QUEST_ID_8, battle_count) yield user.achievement_mgr.update_achievement_status(ACHIEVEMENT_QUEST_ID_8, battle_count) defer.returnValue( (plunder_result, user.base_att.douzhan, new_shard, items_return, lottery_items) )
def plunder(p, req): ''' @summary: 夺宝结算 ''' res_err = UNKNOWN_ERROR cid, ( plunder_cid, shard_id, won ) = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( res_err ) if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format( cid )) defer.returnValue( CHAR_DOUZHAN_NOT_ENOUGH ) item_conf = get_item_by_itemid( shard_id ) treasure_id = 0 if item_conf: _, treasure_id, _ = item_conf['ChangeList'][0] response = [0, 0, [], []] # 对玩家进行抢夺,免战自动解除 if plunder_cid > 0: # 玩家 yield redis.hdel( HASH_AVOID_WAR_TIME, cid ) AvoidWarMgr.stop_plunder_shard( treasure_id, plunder_cid ) _user_plundered = yield gs_offline_login( plunder_cid ) if _user_plundered: reactor.callLater(SESSION_LOGOUT_REAL, gs_offline_logout, plunder_cid) else: _user_plundered = None if won: response[3] = yield random_lottery_items( cid, user.level, user.base_att.vip_level ) err, add_items = yield AvoidWarMgr.rand_shard( user, shard_id, plunder_cid ) if not err and (len(add_items) > 0): #抢到 response[0] = 1 response[2] = add_items[0][0], add_items[0][2] syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, add_items[0][0],add_items[0][2]) if _user_plundered: ms_send('write_mail', (plunder_cid, MAIL_PAGE_BATTLE, MAIL_BATTLE_7, [user.lead_id, user.nick_name, shard_id])) yield _user_plundered.bag_treasureshard_mgr.dec_shard( shard_id ) else: log.warn('No user be plundered. cid: {0}, plunder_cid: {1}.'.format( cid, plunder_cid )) else: # 未抢到 response[0] = 0 syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, 0, 0) else: # 战斗输了 if _user_plundered: syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, 0, 0) ms_send('write_mail', (plunder_cid, MAIL_PAGE_BATTLE, MAIL_BATTLE_6, [user.lead_id, user.nick_name, shard_id])) if user.base_att.douzhan >= PVP_NEED_DOUZHAN: user.base_att.douzhan -= PVP_NEED_DOUZHAN # 每日任务计数 yield user.daily_quest_mgr.update_daily_quest( DAILY_QUEST_ID_5, 1 ) # 开服七天 yield user.open_server_mgr.update_open_server_activity_quest( OPEN_SERVER_QUEST_ID_8, 1) yield user.achievement_mgr.update_achievement_status(ACHIEVEMENT_QUEST_ID_8, 1) response[1] = user.base_att.douzhan defer.returnValue( response )
def update_avoid_cache(self, treasure_id): ''' @summary: 维护宝物碎片列表的同时, 维护redis中的玩家阵营数据 ''' if self.user.base_att.level <= AVOID_WAR_LEVEL: log.warn('User level <= 12 could not be plunder. cid: {0}.'.format( self.cid)) defer.returnValue(REQUEST_LIMIT_ERROR) treasure_conf = get_item_by_itemid(treasure_id) if not treasure_conf: log.error( 'Can not find conf. treasure_id: {0}.'.format(treasure_id)) defer.returnValue(NOT_FOUND_CONF) _shard_list = [splited[1] for splited in treasure_conf['ChangeList']] # 只有同时拥有“一个宝物的2个或者2个以上,不同位置碎片”的玩家才会被加入被抢夺列表 _haved = 0 _plunder_list = [] # 可被抢夺的碎片 for _shard_id in _shard_list: for attrib in self.__gsattribs.itervalues(): if attrib.item_id == _shard_id: _haved += 1 _plunder_list.append(_shard_id) break #_treasure_ids = yield redis.hget( HASH_TREASURE_CHARACTER_IDS, self.cid ) #if _treasure_ids: # _treasure_ids = loads(_treasure_ids) #else: # _treasure_ids = [] #log.info('For Test. _haved: {0}, cid: {1}, _shard_id: {2}, _shard_list: {3}, _plunder_list: {4}.'.format( _haved, self.cid, _shard_id, _shard_list, _plunder_list )) for _shard_id in _shard_list: yield redis.hdel(TPL_TREASURE_SHARD_GOT % _shard_id, self.cid) #if treasure_id in _treasure_ids: # _treasure_ids.remove( treasure_id ) if _haved > 1: #_flag = True # 成功获取玩家有效阵容 #_exists = yield redis.hexists( HASH_TREASURE_CHARACTER_CAMP, self.cid ) #if (not _exists): # _all_camps = yield self.user.get_camp() # fellow_ids = [] # if _all_camps: # for camp_data in _all_camps[1]: # if not camp_data or not camp_data[1]: # continue # fellow_ids.append( camp_data[1][1] ) # # 有效的玩家阵容 # if fellow_ids: # yield redis.hset( HASH_TREASURE_CHARACTER_CAMP, self.cid, dumps(_all_camps) ) # #log.info('For Test. save treasure camp. cid: {0}.'.format( self.cid )) # else: # _flag = False # log.error('Save char treasure camp data fail. cid: {0}, _all_camps: {1}.'.format( self.cid, _all_camps )) for _shard_id in _plunder_list: yield redis.hset( TPL_TREASURE_SHARD_GOT % _shard_id, self.cid, dumps((self.cid, self.user.level, self.user.base_att.nick_name)))
def plunder_streak(p, req): ''' @summary: 连续10次抢夺机器人, 非玩家 ''' res_err = UNKNOWN_ERROR cid, [plunder_cid, shard_id] = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format(cid)) defer.returnValue(res_err) # 等级限制 或者 VIP等级开关 vip_conf = get_vip_conf(user.vip_level) res_err = user.check_function_open(FUNCTION_TEN_PLUNDER) if res_err and (not vip_conf.get('TreasureTenPlunder', 0)): log.error('cid:{0}, level:{1}, vip_level:{2}.'.format( cid, user.level, user.vip_level)) defer.returnValue(CHAR_VIP_LEVEL_LIMIT) # 玩家的斗战点不足 if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format(cid)) defer.returnValue(CHAR_DOUZHAN_NOT_ENOUGH) # 只能连续抢机器人 if plunder_cid > 0: log.error('Can not plunder streak user. plunder_cid: {0}.'.format( plunder_cid)) defer.returnValue(REQUEST_LIMIT_ERROR) #抢夺,自动去除自己的免战 yield redis.hdel(HASH_AVOID_WAR_TIME, cid) battle_count = 0 # 抢夺的次数 plunder_result = 0 # 未抢到 new_shard = [] # 翻牌得到的幸运道具 lottery_items = [] for _i in range(0, 10): # 扣斗战点 user.base_att.douzhan -= PVP_NEED_DOUZHAN # 抢夺次数加1 battle_count += 1 # 幸运道具奖励 new_item = yield random_lottery_items(cid, user.level, user.base_att.vip_level, rand_count=1) if len(new_item) > 0: lottery_items.append(new_item[0]) else: log.error( 'No lottery items. cid: {0}, level: {1}, vip_level: {2}.'. format(cid, level, vip_level)) # 随机碎片 err, add_items = yield AvoidWarMgr.rand_shard(user, shard_id, plunder_cid) if not err and (len(add_items) > 0): #抢到 plunder_result = 1 syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 1, 1, add_items[0][0], add_items[0][2]) new_shard = [add_items[0][0], add_items[0][2]] break # 玩家的斗战点不足 if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format(cid)) break # 幸运道具进背包 items_return = [] for _type, _id, _num, _ in lottery_items: model = ITEM_MODELs.get(_type, None) if not model: log.error( 'Unknown decomposition item type. item type: {0}.'.format( _type)) continue res_err, res_value = yield model(user, ItemID=_id, ItemNum=_num, AddType=WAY_LOTTERY_AWARD, CapacityFlag=False) if not res_err: for _v in res_value: items_return = total_new_items(_v, items_return) # 每日任务计数 yield user.daily_quest_mgr.update_daily_quest(DAILY_QUEST_ID_5, battle_count) # 开服七天 yield user.open_server_mgr.update_open_server_activity_quest( OPEN_SERVER_QUEST_ID_8, battle_count) yield user.achievement_mgr.update_achievement_status( ACHIEVEMENT_QUEST_ID_8, battle_count) defer.returnValue((plunder_result, user.base_att.douzhan, new_shard, items_return, lottery_items))
def reset_dungeon_count(self, scene_id, dungeon_id, reset_way): dungeon = yield self.get_dungeon(scene_id, dungeon_id) if not dungeon: log.error( 'Unknown dungeon id. scene_id: {0}, dungeon_id: {1}.'.format( scene_id, dungeon_id)) defer.returnValue(REQUEST_LIMIT_ERROR) # 判断是否还有剩余挑战次数 #if 1 > dungeon.dungeon_today_count: # defer.returnValue( REQUEST_LIMIT_ERROR ) daily_info = yield self.get_scene_reset_daily() had_reset = daily_info[2] + daily_info[3] # 检查当天最大可重置次数 vip_conf = get_vip_conf(self.user.vip_level) if not vip_conf or (had_reset >= vip_conf['DungeonReset']): log.error('Dungeon reset count limit. cid:{0}, vip_level:{1}, max_count:{2}, item_reset:{3}, credit_reset:{4}.'.format( \ self.cid, self.user.vip_level, vip_conf['DungeonReset'] if vip_conf else 0, daily_info[2], daily_info[3])) defer.returnValue(CHAR_VIP_LEVEL_LIMIT) items_return = [] # 检查reset_way if reset_way == SCENE_RESET_ITEM: daily_info[2] += 1 # 扣道具 total_item, item_attribs = yield self.user.bag_item_mgr.get_items( ITEM_SCENE_RESET_ID) if 1 > total_item: defer.returnValue(CHAR_ITEM_NOT_ENOUGH) res_err, used_attribs = yield self.user.bag_item_mgr.use( ITEM_SCENE_RESET_ID, 1) if res_err: defer.returnValue(res_err) # used_attribs-已使用的道具 for _a in used_attribs: items_return.append([ _a.attrib_id, ITEM_TYPE_ITEM, ITEM_SCENE_RESET_ID, _a.item_num ]) # add syslog syslogger(LOG_ITEM_LOSE, self.cid, self.user.level, self.user.vip_level, \ self.user.alliance_id, _a.attrib_id, ITEM_SCENE_RESET_ID, 1, WAY_SCENE_RESET) elif reset_way == SCENE_RESET_CREDIT: daily_info[3] += 1 # 扣钻石 need_credits = get_scene_reset_conf(daily_info[3]) if 0 == need_credits: defer.returnValue(SCENE_RESET_ERROR) if need_credits > self.user.credits: log.error( 'credits not enough. cid:{0}, need:{1}, curr:{2}, credit_reset:{3}.' .format(self.cid, need_credits, self.user.credits, daily_info[3])) defer.returnValue(CHAR_CREDIT_NOT_ENOUGH) yield self.user.consume_credits(need_credits, WAY_SCENE_RESET) else: log.error('Unknown args. cid: {0}, reset_way: {1}.'.format( self.cid, reset_way)) defer.returnValue(REQUEST_LIMIT_ERROR) dungeon.dungeon_today_count = 0 yield redis.hset(HASH_SCENE_RESET_DAILY, self.cid, dumps(daily_info)) # 删除redis冷却时间记录 yield redis.hdel(HASH_SCENE_COOLDOWN_TIME, self.cid) defer.returnValue((self.user.credits, items_return))
def use_named_card(p, req): cid, [c_type, card_id, new_name] = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) if c_type == 0 or c_type == 2: totalnum, attrs = yield user.bag_item_mgr.get_items(card_id) if totalnum == 0: defer.returnValue(CHAR_ITEM_NOT_ENOUGH) elif (c_type == 1 or c_type == 3 ) and card_id == 0: cost_credits = get_game_limit_value(16) if c_type == 1 else get_game_limit_value(17) if user.credits < cost_credits: defer.returnValue(CHAR_CREDIT_NOT_ENOUGH) else: defer.returnValue(NAMED_TYPE_IS_WRONG) left_res = [] if c_type == 0 or c_type == 1: had_cid = yield redis.hget(DICT_NICKNAME_REGISTERED, new_name) if had_cid: defer.returnValue(NICK_NAME_IS_USED) if c_type == 0: res = yield item_use_normal_item(user, ItemType = ITEM_TYPE_ITEM, ItemID = card_id, ItemNum = 1) left_res = (res[1][0][0], res[1][0][1], res[1][0][2], res[1][0][3]) else: yield user.consume_credits(cost_credits, WAY_TO_NAMED) yield redis.hdel(DICT_NICKNAME_REGISTERED, user.nick_name) user.base_att.nick_name = new_name yield redis.hset(DICT_NICKNAME_REGISTERED, new_name, user.cid) #alliance _s = yield redis.hget(HASH_ALLIANCE_MEMBER, cid) if _s: _s = list(loads(_s)) _s[1] = new_name yield redis.hset(HASH_ALLIANCE_MEMBER, cid, dumps(_s)) alli_send('revise_member_name', [cid, new_name]) #msg ms_send('revise_name', [cid, new_name]) #arena _rank = yield g_ArenaMgr.get_char_rank(cid) _detail = yield redis.hget( HASH_ARENA_RANK, _rank) if _detail: _detail = loads(_detail) _detail[2] = new_name yield redis.hset( HASH_ARENA_RANK, _rank, dumps(_detail)) alliance_info = yield alli_call('get_alliance_info', [cid]) if alliance_info[0] != 0: if alliance_info[2] == ALLIANCE_POSITION_LEADER: res = yield alli_call('revise_leader', [user.alliance_id, new_name]) elif c_type == 2 or c_type ==3: alliance_info = yield alli_call('get_alliance_info', [cid]) if alliance_info[0] != 0: if alliance_info[2] != ALLIANCE_POSITION_LEADER: defer.returnValue(ALLIANCE_AUTHORITY_ERROR) res = yield alli_call('check_alliance_name', [new_name, user.alliance_id]) user.alliance_name = new_name if res != 0: defer.returnValue(res) if c_type == 2: res = yield item_use_normal_item(user, ItemType = ITEM_TYPE_ITEM, ItemID = card_id, ItemNum = 1) left_res = (res[1][0][0], res[1][0][1], res[1][0][2], res[1][0][3]) else: yield user.consume_credits(cost_credits, WAY_TO_NAMED) else: defer.returnValue(ALLIANCE_UNKNOWN_ERROR) else: defer.returnValue(NAMED_TYPE_IS_WRONG) defer.returnValue([left_res, user.credits])
def plunder(p, req): ''' @summary: 夺宝结算 ''' res_err = UNKNOWN_ERROR cid, (plunder_cid, shard_id, won) = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format(cid)) defer.returnValue(res_err) if user.base_att.douzhan < PVP_NEED_DOUZHAN: log.error('douzhan of user {0} have been zero!!!'.format(cid)) defer.returnValue(CHAR_DOUZHAN_NOT_ENOUGH) item_conf = get_item_by_itemid(shard_id) treasure_id = 0 if item_conf: _, treasure_id, _ = item_conf['ChangeList'][0] response = [0, 0, [], []] # 对玩家进行抢夺,免战自动解除 if plunder_cid > 0: # 玩家 yield redis.hdel(HASH_AVOID_WAR_TIME, cid) AvoidWarMgr.stop_plunder_shard(treasure_id, plunder_cid) _user_plundered = yield gs_offline_login(plunder_cid) if _user_plundered: reactor.callLater(SESSION_LOGOUT_REAL, gs_offline_logout, plunder_cid) else: _user_plundered = None if won: response[3] = yield random_lottery_items(cid, user.level, user.base_att.vip_level) err, add_items = yield AvoidWarMgr.rand_shard(user, shard_id, plunder_cid) if not err and (len(add_items) > 0): #抢到 response[0] = 1 response[2] = add_items[0][0], add_items[0][2] syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, add_items[0][0], add_items[0][2]) if _user_plundered: ms_send('write_mail', (plunder_cid, MAIL_PAGE_BATTLE, MAIL_BATTLE_7, [user.lead_id, user.nick_name, shard_id])) yield _user_plundered.bag_treasureshard_mgr.dec_shard(shard_id) else: log.warn( 'No user be plundered. cid: {0}, plunder_cid: {1}.'.format( cid, plunder_cid)) else: # 未抢到 response[0] = 0 syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, 0, 0) else: # 战斗输了 if _user_plundered: syslogger(LOG_AVOID_WAR, cid, user.level, user.vip_level, user.alliance_id, 0, won, 0, 0) ms_send('write_mail', (plunder_cid, MAIL_PAGE_BATTLE, MAIL_BATTLE_6, [user.lead_id, user.nick_name, shard_id])) if user.base_att.douzhan >= PVP_NEED_DOUZHAN: user.base_att.douzhan -= PVP_NEED_DOUZHAN # 每日任务计数 yield user.daily_quest_mgr.update_daily_quest(DAILY_QUEST_ID_5, 1) # 开服七天 yield user.open_server_mgr.update_open_server_activity_quest( OPEN_SERVER_QUEST_ID_8, 1) yield user.achievement_mgr.update_achievement_status( ACHIEVEMENT_QUEST_ID_8, 1) response[1] = user.base_att.douzhan defer.returnValue(response)
def use_named_card(p, req): cid, [c_type, card_id, new_name] = req user = g_UserMgr.getUser(cid) if not user: log.error('Can not find user. cid: {0}.'.format(cid)) defer.returnValue(CONNECTION_LOSE) if c_type == 0 or c_type == 2: totalnum, attrs = yield user.bag_item_mgr.get_items(card_id) if totalnum == 0: defer.returnValue(CHAR_ITEM_NOT_ENOUGH) elif (c_type == 1 or c_type == 3) and card_id == 0: cost_credits = get_game_limit_value( 16) if c_type == 1 else get_game_limit_value(17) if user.credits < cost_credits: defer.returnValue(CHAR_CREDIT_NOT_ENOUGH) else: defer.returnValue(NAMED_TYPE_IS_WRONG) left_res = [] if c_type == 0 or c_type == 1: had_cid = yield redis.hget(DICT_NICKNAME_REGISTERED, new_name) if had_cid: defer.returnValue(NICK_NAME_IS_USED) if c_type == 0: res = yield item_use_normal_item(user, ItemType=ITEM_TYPE_ITEM, ItemID=card_id, ItemNum=1) left_res = (res[1][0][0], res[1][0][1], res[1][0][2], res[1][0][3]) else: yield user.consume_credits(cost_credits, WAY_TO_NAMED) yield redis.hdel(DICT_NICKNAME_REGISTERED, user.nick_name) user.base_att.nick_name = new_name yield redis.hset(DICT_NICKNAME_REGISTERED, new_name, user.cid) #alliance _s = yield redis.hget(HASH_ALLIANCE_MEMBER, cid) if _s: _s = list(loads(_s)) _s[1] = new_name yield redis.hset(HASH_ALLIANCE_MEMBER, cid, dumps(_s)) alli_send('revise_member_name', [cid, new_name]) #msg ms_send('revise_name', [cid, new_name]) #arena _rank = yield g_ArenaMgr.get_char_rank(cid) _detail = yield redis.hget(HASH_ARENA_RANK, _rank) if _detail: _detail = loads(_detail) _detail[2] = new_name yield redis.hset(HASH_ARENA_RANK, _rank, dumps(_detail)) alliance_info = yield alli_call('get_alliance_info', [cid]) if alliance_info[0] != 0: if alliance_info[2] == ALLIANCE_POSITION_LEADER: res = yield alli_call('revise_leader', [user.alliance_id, new_name]) elif c_type == 2 or c_type == 3: alliance_info = yield alli_call('get_alliance_info', [cid]) if alliance_info[0] != 0: if alliance_info[2] != ALLIANCE_POSITION_LEADER: defer.returnValue(ALLIANCE_AUTHORITY_ERROR) res = yield alli_call('check_alliance_name', [new_name, user.alliance_id]) user.alliance_name = new_name if res != 0: defer.returnValue(res) if c_type == 2: res = yield item_use_normal_item(user, ItemType=ITEM_TYPE_ITEM, ItemID=card_id, ItemNum=1) left_res = (res[1][0][0], res[1][0][1], res[1][0][2], res[1][0][3]) else: yield user.consume_credits(cost_credits, WAY_TO_NAMED) else: defer.returnValue(ALLIANCE_UNKNOWN_ERROR) else: defer.returnValue(NAMED_TYPE_IS_WRONG) defer.returnValue([left_res, user.credits])