def change_rivals(self, delete_rivals_id, rivals_info, now): """更改对手 """ rivals_id = utils.split_to_int(self.rivals_id) rivals_is_robot = utils.split_to_int(self.rivals_is_robot) rivals_position_level = utils.split_to_int(self.rivals_position_level) for id in delete_rivals_id: if id not in rivals_id: continue index = rivals_id.index(id) rivals_position_level.pop(index) rivals_is_robot.pop(index) rivals_id.pop(index) for (id, is_robot, position) in rivals_info: rivals_id.append(id) rivals_is_robot.append(int(is_robot)) rivals_position_level.append(position) self.rivals_id = utils.join_to_string(rivals_id) self.rivals_is_robot = utils.join_to_string(rivals_is_robot) self.rivals_position_level = utils.join_to_string( rivals_position_level) self.update_time = now logger.debug( "change rivals[rivals_id=%s][is_robot=%s][position_level=%s]" % (self.rivals_id, self.rivals_is_robot, self.rivals_position_level))
def pay_order(self, order): """完成支付,修改商店中下一件商品信息,支付金额信息 """ if order.type == PayPool.ORDER_TYPE_NORMAL: store = utils.split_to_int(self.order_normal_ids) elif order.type == PayPool.ORDER_TYPE_CARD: store = utils.split_to_int(self.order_card_ids) elif order.type == PayPool.ORDER_TYPE_PACKAGE: store = utils.split_to_int(self.order_package_ids) self.pre_order_number = "" self.pay_amount += order.truePrice #计算下一个可以购买的商品 id = order.id if id in store: index = store.index(id) if order.nextId == 0: store.remove(id) else: store[index] = order.nextId if order.type == PayPool.ORDER_TYPE_NORMAL: self.order_normal_ids = utils.join_to_string(store) elif order.type == PayPool.ORDER_TYPE_CARD: self.order_card_ids = utils.join_to_string(store) elif order.type == PayPool.ORDER_TYPE_PACKAGE: self.order_package_ids = utils.join_to_string(store)
def get_all(self): """获得所有可以购买的商品 id """ ids = utils.split_to_int(self.order_normal_ids) ids.extend(utils.split_to_int(self.order_card_ids)) ids.extend(utils.split_to_int(self.order_package_ids)) return ids
def buy_goods(self, id): """购买货物 Returns: GoodsInfo: 购买到的货物信息 None: 购买失败 """ goods_ids = utils.split_to_int(self.goods_ids) goods_status = utils.split_to_int(self.goods_status) goods_discount = utils.split_to_int(self.goods_discount) if id not in goods_ids: logger.warning("Goods not exist[id=%d]" % id) return None index = goods_ids.index(id) status = goods_status[index] discount = goods_discount[index] goods = self._create_goods(self.goods_pool, id, status, discount) if goods.is_sold: logger.warning("Goods is sold out") return None goods.is_sold = True if self.type != self.GOODS_TYPE_GOLD: #元宝商店物品不会售空 goods_status[index] = self.SOLDOUT self.goods_status = utils.join_to_string(goods_status) return goods
def finish_battle(self, attacker_user_id, kills_num): """结束战斗 """ #assert self.status == self.NODE_STATUS_ENGAGED if self.status != self.NODE_STATUS_ENGAGED: return else: self.current_soldier_num = max( 0, self.current_soldier_num - kills_num) self.attacker_num = max(0, self.attacker_num - 1) #删除记录的user_id和他的开始战斗时间 users_id = utils.split_to_int(self.attacker_user_id) start_times = utils.split_to_int(self.battle_start_time) del_index = users_id.index(attacker_user_id) del (users_id[del_index]) del (start_times[del_index]) self.attacker_user_id = utils.join_to_string(users_id) self.battle_start_time = utils.join_to_string(start_times) if self.current_soldier_num == 0: self.status = self.NODE_STATUS_BEATEN self.attacker_num = 0 return if self.attacker_num == 0: self.status = self.NODE_STATUS_DEPLOYED
def add_buff(self, buff_id, now): """添加 buff """ buffs = utils.split_to_int(self.buffs) buffs_start_time = utils.split_to_int(self.buffs_start_time) #清除已经过期的 buff for index in range(0, len(buffs)): id = buffs[index] if id == 0: continue end_time = (buffs_start_time[index] + data_loader.LegendCityBuffBasicInfo_dict[id].duration) if now >= end_time: buffs[index] = 0 ok = False for index in range(0, len(buffs)): if buffs[index] == 0: buffs[index] = buff_id buffs_start_time[index] = now ok = True break if not ok: logger.warning("Not able to add legendcity buff") return False self.buffs = utils.join_to_string(buffs) self.buffs_start_time = utils.join_to_string(buffs_start_time) return True
def update_invitee(self, invitee_id, level): """更新受请人等级""" invitees = utils.split_to_int(self.invitee) invitee_levels = utils.split_to_int(self.invitee_level) for i in range(0, len(invitees)): if invitees[i] == invitee_id: invitee_levels[i] = level self.invitee_level = utils.join_to_string(invitee_levels)
def get_rival_info(self, rival_id): info = [] rivals_id = utils.split_to_int(self.rivals_id) rivals_is_robot = utils.split_to_int(self.rivals_is_robot) rivals_position_level = utils.split_to_int(self.rivals_position_level) index = rivals_id.index(rival_id) return (rivals_id[index], bool(rivals_is_robot[index]), rivals_position_level[index])
def _calc_receive_chat_friend(self, data, req, timer): user = data.user.get() logger.notice("FRIEDNS= %s" % user.id) friend_index = utils.split_to_int(user.friends).index(req.friend_id) friend_id = FriendInfo.generate_id(data.id, friend_index) logger.notice("friend_id = %d" % friend_id) friend_msg = data.friend_list.get(friend_id) user_id = data.id message_list = data.message_list.get_all() friendstr = user.friends friend = utils.split_to_int(friendstr) msg_index = 1 if not len(message_list) == 0: msg_index = message_list[-1].message_index + 1 self._eliminate_msg(data, friend_id, friend_index, MAX_MSG_NUM - 1) message = MessageInfo.create(data.id, friend_index, msg_index, req.content, 1, 2) data.message_list.add(message) message_index = data.friend_list.get(friend_id).message_index_list message_index_list = utils.split_to_int(message_index) message_index_list.append(msg_index) data.friend_list.get( friend_id).message_index_list = utils.join_to_string( message_index_list) res = friend_pb2.ChatFriendRes() res.status = 0 res.ret = friend_pb2.FRIEND_OK message_index = friend_msg.message_index_list message_index_list = utils.split_to_int(message_index)[::-1] num = min(len(message_index_list), 20) msg_index_list = message_index_list[:num] for msg_index in msg_index_list[::-1]: message_info = res.messages.add() message_id = MessageInfo.generate_id(user_id, friend_index, msg_index) message = data.message_list.get(message_id) message_info.user_id = user_id message_info.friend_index = friend_index message_info.content = message.content message_info.status = message.status message_info.message_from = message.message_from logger.notice("message_info = %s" % message_info) # response = res.SerializeToString() # logger.notice("====================1") # defer = GlobalObject().root.callChild("portal", "push_chat_record", data.id, response) # logger.notice("====================2") defer = DataBase().commit(data) defer.addCallback(self._receive_chat_succeed, req, res, timer) return defer
def get_items(self): items_id = utils.split_to_int(self.items_id) items_num = utils.split_to_int(self.items_num) assert len(items_id) == len(items_num) items = [] for i in range(len(items_id)): items.append((items_id[i], items_num[i])) return items
def get_rivals_info(self): info = [] rivals_id = utils.split_to_int(self.rivals_id) rivals_is_robot = utils.split_to_int(self.rivals_is_robot) rivals_position_level = utils.split_to_int(self.rivals_position_level) for index in range(0, len(rivals_id)): info.append((rivals_id[index], bool(rivals_is_robot[index]), rivals_position_level[index])) return info
def get_items_info(self): """ """ items_id = utils.split_to_int(self.items_id) items_num = utils.split_to_int(self.items_num) items_info = [] for i in range(0, len(items_id)): items_info.append((items_id[i], items_num[i])) return items_info
def get_reward_record(self): """获取奖励领取记录""" members = utils.split_to_int(self.accepted_members) names = utils.split_to_string(self.accepted_names) icons = utils.split_to_int(self.accepted_icons) items_id = utils.split_to_int(self.reward_items) items_num = utils.split_to_int(self.reward_nums) times = utils.split_to_int(self.accept_times) names = [base64.b64decode(name) for name in names] return map(None, members, names, icons, items_id, items_num, times)
def get_own_soldier_info(self): own_soldier_basic_id = utils.split_to_int(self.own_soldier_basic_id) own_soldier_level = utils.split_to_int(self.own_soldier_level) own_soldier_survival = utils.split_to_int(self.own_soldier_survival) own_soldier_info = [] for i in range(len(own_soldier_basic_id)): own_soldier_info.append( (own_soldier_basic_id[i], own_soldier_level[i], own_soldier_survival[i])) return own_soldier_info
def can_accept_reward(self): """ """ step_progress = utils.split_to_int(self.progress) step_target = utils.split_to_int(self.target) step_status = utils.split_to_int(self.status) for i in range(len(step_progress)): if step_progress[i] >= step_target[i] and step_status[ i] == self.REWARD_AVAILABLE: return True return False
def get_next_step_id_unfinished(self, basic_activity): """获得下一个未完成的 step id """ step_progress = utils.split_to_int(self.progress) step_target = utils.split_to_int(self.target) steps_id = basic_activity.get_steps() for index in range(0, len(step_progress)): step_id = steps_id[index] if step_progress[index] < step_target[index]: return step_id return 0
def get_soldier_consume(self): conscripts_id = utils.split_to_int(self.conscripts_id) conscripts_soldier_num = utils.split_to_int( self.conscripts_soldier_num) count = len(conscripts_id) consume_soldier_info = {} for index in range(0, count): consume_soldier_info[ conscripts_id[index]] = conscripts_soldier_num[index] return consume_soldier_info
def get_reward_items(self): """战胜可以获得的物品奖励 """ items_basic_id = utils.split_to_int(self.reward_items_basic_id) items_num = utils.split_to_int(self.reward_items_num) assert len(items_basic_id) == len(items_num) items_count = len(items_basic_id) items = [] for i in range(0, items_count): items.append((items_basic_id[i], items_num[i])) return items
def add_invitee(self, invitee_id, level): """增加受请人,若受请数量已达上限则返回False,成功返回True """ invitees = utils.split_to_int(self.invitee) if len(invitees) >= 15: return False invitees.append(invitee_id) self.invitee = utils.join_to_string(invitees) invitee_levels = utils.split_to_int(self.invitee_level) invitee_levels.append(level) self.invitee_level = utils.join_to_string(invitee_levels) return True
def is_battle_timeout(self, attacker_user_id, now): """战斗是否超时 """ if self.status != self.NODE_STATUS_ENGAGED: return False users_id = utils.split_to_int(self.attacker_user_id) start_times = utils.split_to_int(self.battle_start_time) index = users_id.index(attacker_user_id) if self.get_battle_left_time(start_times[index], now) <= 0: return True else: return False
def get_enemy_soldier_info(self): enemy_soldier_basic_id = utils.split_to_int( self.enemy_soldier_basic_id) enemy_soldier_level = utils.split_to_int(self.enemy_soldier_level) enemy_soldier_survival = utils.split_to_int( self.enemy_soldier_survival) enemy_soldier_info = [] for i in range(len(enemy_soldier_basic_id)): enemy_soldier_info.append( (enemy_soldier_basic_id[i], enemy_soldier_level[i], enemy_soldier_survival[i])) return enemy_soldier_info
def start_battle(self, attacker_user_id, now): """开始战斗 """ #assert self.status == self.NODE_STATUS_DEPLOYED self.status = self.NODE_STATUS_ENGAGED self.attacker_num += 1 start_times = utils.split_to_int(self.battle_start_time) start_times.append(now) users_id = utils.split_to_int(self.attacker_user_id) users_id.append(attacker_user_id) self.battle_start_time = utils.join_to_string(start_times) self.attacker_user_id = utils.join_to_string(users_id)
def _calc_add_friend(self, data, req, timer): user = data.user.get() #friend_list = user.get_friends """过滤掉id 为0 的元素""" friends = [id for id in utils.split_to_int(user.friends) if id > 0] friends_apply = [ id for id in utils.split_to_int(user.friends_apply) if id > 0 ] if len(friends) >= 30: res = friend_pb2.AddFriendRes() res.status = 0 res.ret = friend_pb2.FRIENDFULL defer = DataBase().commit(data) defer.addCallback(self._add_friend_succeed, data, req, res, timer) return defer #当在好友申请中时直接添加 if req.friend_id in friends_apply: app_req = friend_pb2.ManageFriendsReq() app_req.user_id = req.user_id app_req.friend_id = req.friend_id app_req.status = 2 request = app_req.SerializeToString() self.manage_friend(req.user_id, request) res = friend_pb2.AddFriendRes() res.status = 0 res.ret = friend_pb2.FRIEND_OK defer = DataBase().commit(data) defer.addCallback(self._add_friend_succeed, data, req, res, timer) return defer app_req = friend_pb2.AddFriendReq() app_req.friend_id = req.user_id app_req.user_id = req.friend_id request = app_req.SerializeToString() defer = DataBase().commit(data) defer = GlobalObject().root.callChild('portal', "forward_receive_friend", req.friend_id, request) res = friend_pb2.AddFriendRes() res.status = 0 res.ret = friend_pb2.APPLYSUCCEED defer.addCallback(self._add_friend_succeed, data, req, res, timer) return defer
def get_reward_items(self): """ """ items = [] logger.debug("get reward:%s" % self.reward_items_basic_id) items_basic_id = utils.split_to_int(self.reward_items_basic_id) items_num = utils.split_to_int(self.reward_items_num) assert len(items_basic_id) == len(items_num) for index in range(0, len(items_basic_id)): items.append((items_basic_id[index], items_num[index])) return items
def calc_goods_price(self, id): """查询货物原价 """ goods_ids = utils.split_to_int(self.goods_ids) goods_status = utils.split_to_int(self.goods_status) goods_discount = utils.split_to_int(self.goods_discount) if id not in goods_ids: logger.warning("Goods not exist[id=%d]" % id) return -1 index = goods_ids.index(id) status = goods_status[index] discount = goods_discount[index] goods = self._create_goods(self.goods_pool, id, status, discount) return goods.price
def use_cdkey(self, bag_id): """使用激活码 """ ids = utils.split_to_int(self.cdkey) assert bag_id not in ids ids.append(bag_id) self.cdkey = utils.join_to_string(ids)
def get_heroes_position(self): """获得敌人英雄站位信息(乱斗场专用) Returns: list(int) 敌人站位信息的列表 """ if self.heroes_position == '': PVE_DEFAULT_POSITION = [1,2,4,5,6,7,8,9,12] heros_position = [] basic_ids = utils.split_to_int(self.heroes_basic_id) for i in range(len(basic_ids)): heros_position.append(PVE_DEFAULT_POSITION[i]) return heros_position else: return utils.split_to_int(self.heroes_position)
def calc_choose_card_use_item(self): """计算翻牌时的祈福令消耗 """ choose_list = utils.split_to_int(self.choose_list) index = len(choose_list) + 1 return (int(data_loader.ChooseCardCostBasicInfo_dict[index].itemId), int(data_loader.ChooseCardCostBasicInfo_dict[index].costNum))
def _pack_node_appoint_info(battle, message, now): team_indexes = utils.split_to_int(battle.teams_index) for team_index in team_indexes: message.team_indexes.append(team_index) message.total_time = battle.appoint_total_time message.passed_time = now - battle.time
def _pack_pray_status(pray, message): items_id = utils.split_to_int(pray.items_id) items_num = utils.split_to_int(pray.items_num) choose_list = utils.split_to_int(pray.choose_list) initial_list = utils.split_to_int(pray.initial_list) for i in range(0, len(items_id)): item = message.items.add() item.basic_id = items_id[i] item.num = items_num[i] for index in choose_list: message.choose_list.append(index) for index in initial_list: message.initial_list.append(index)