def __init__(self, char_id, city_id): from core.affairs import Affairs from core.battle.hero import BattleHero self.city_id = city_id if char_id: char = Char(char_id) self.char_id = char_id self.name = char.mc.name self.level = char.mc.level self.power = char.power self.leader = char.leader_oid f = Formation(char_id) self.formation = f.in_formation_hero_ids() self.hero_original_ids = f.in_formation_hero_original_ids() self.gold = Affairs(self.char_id).get_drop()['gold'] self.msg_char_information = create_character_infomation_message( self.char_id).SerializeToString() battle_heros = [] for hid in self.formation: if hid == 0: battle_heros.append(None) else: battle_heros.append(BattleHero(hid)) self.battle_heros = base64.b64encode(dill.dumps(battle_heros)) else: self.char_id = 0 self.name = "" self.level = 0 self.power = 0 self.leader = 0 self.formation = [] self.hero_original_ids = [] self.gold = 0 self.msg_char_information = "" self.battle_heros = base64.b64encode(dill.dumps([None] * 9))
def __init__(self, char_id, city_id): from core.affairs import Affairs from core.battle.hero import BattleHero self.city_id = city_id if char_id: char = Char(char_id) self.char_id = char_id self.name = char.mc.name self.level = char.mc.level self.power = char.power self.leader = char.leader_oid f = Formation(char_id) self.formation = f.in_formation_hero_ids() self.hero_original_ids = f.in_formation_hero_original_ids() self.gold = Affairs(self.char_id).get_drop()['gold'] self.msg_char_information = create_character_infomation_message(self.char_id).SerializeToString() battle_heros = [] for hid in self.formation: if hid == 0: battle_heros.append(None) else: battle_heros.append(BattleHero(hid)) self.battle_heros = base64.b64encode(dill.dumps(battle_heros)) else: self.char_id = 0 self.name = "" self.level = 0 self.power = 0 self.leader = 0 self.formation = [] self.hero_original_ids = [] self.gold = 0 self.msg_char_information = "" self.battle_heros = base64.b64encode(dill.dumps([None] * 9))
def __init__(self, char_id, city_id): self.city_id = city_id if char_id: char = Char(char_id) self.char_id = char_id self.name = char.mc.name self.level = char.mc.level self.power = char.power self.leader = char.leader_oid f = Formation(char_id) self.formation = f.in_formation_hero_ids() self.hero_original_ids = f.in_formation_hero_original_ids() else: self.char_id = 0 self.name = "" self.level = 0 self.power = 0 self.leader = 0 self.formation = [] self.hero_original_ids = []
def _msg_friend(self, msg, fid, status): char_f = Char(fid) cache_f = char_f.cacheobj msg.id = fid msg.name = cache_f.name msg.level = cache_f.level msg.official = cache_f.official if status == FRIEND_OK or status == FRIEND_NOT: msg.power = char_f.power msg.status = status f = Formation(fid) in_formation_hero_ids = [h for h in f.in_formation_hero_ids() if h] hero_list = [(Hero.cache_obj(hid).power, hid) for hid in in_formation_hero_ids] hero_list.sort(key=lambda item: -item[0]) leader_oid = Hero.cache_obj(hero_list[0][1]).oid if status == FRIEND_OK: f = Formation(fid) msg.formation.extend(f.in_formation_hero_original_ids()) msg.leader = leader_oid
def _msg_friend(self, msg, fid, status): char_f = Char(fid) cache_f = char_f.cacheobj msg.id = fid msg.name = cache_f.name msg.level = cache_f.level msg.official = cache_f.official if status == FRIEND_OK or status == FRIEND_NOT: msg.power = char_f.power msg.status = status f = Formation(fid) in_formation_hero_ids = [h for h in f.in_formation_hero_ids() if h] hero_list = [(Hero.cache_obj(hid).power, hid) for hid in in_formation_hero_ids] hero_list.sort(key=lambda item: -item[0]) leader_oid = Hero.cache_obj(hero_list[0][1]).oid if status == FRIEND_OK: f = Formation(fid) msg.formation.extend(f.in_formation_hero_original_ids()) msg.leader = leader_oid if status == FRIEND_OK and fid not in self.mf.plunder_gives: msg.can_give_plunder_times = True else: msg.can_give_plunder_times = False if fid in self.mf.plunder_senders: msg.got_plunder_times_status = MsgFriend.CAN_GET elif fid in self.mf.plunder_gots: msg.got_plunder_times_status = MsgFriend.ALREADY_GET else: msg.got_plunder_times_status = MsgFriend.CAN_NOT_GET
def get_plunder_list(self): """ @return: [[id, name, power, formation, is_robot, gold], ...] @rtype: list """ char = Char(self.char_id) cache_char = char.cacheobj char_level = cache_char.level choosing_list = MongoHangDoing.objects( Q(char_level__gte=char_level - PLUNDER_LEVEL_DIFF) & Q(char_level__lte=char_level + PLUNDER_LEVEL_DIFF) & Q(id__ne=self.char_id)) choosing_id_list = [c.id for c in choosing_list] ids = [] while True: if len(ids) >= 20 or not choosing_id_list: break c = random.choice(choosing_id_list) choosing_id_list.remove(c) if c == self.char_id: continue if c not in ids: ids.append(c) random.shuffle(ids) ids = ids[:10] res = [] for i in ids: char = Char(i) f = Formation(i) res.append([ i, char.cacheobj.name, char.power, f.in_formation_hero_original_ids(), False ]) robot_ids = [] robot_amount = 10 - len(ids) if len(robot_ids) < robot_amount: # 添加机器人 min_level = char_level - 20 if min_level <= 1: min_level = 1 real_chars = get_char_ids_by_level_range(cache_char.server_id, min_level, char_level + 10) for c in real_chars: if c == self.char_id: continue if c in ids: continue robot_ids.append(c) if len(robot_ids) >= robot_amount: break for i in robot_ids: char = Char(i) f = Formation(i) res.append([ i, char.cacheobj.name, char.power, f.in_formation_hero_original_ids(), True ]) final_ids = [r[0] for r in res] this_stages = MongoStage.objects.filter(id__in=final_ids) this_stages_dict = {s.id: s.max_star_stage for s in this_stages} for r in res: _id = r[0] max_star_stage = this_stages_dict.get(_id, 1) if not max_star_stage: max_star_stage = 1 got_gold = max_star_stage * 400 * random.uniform(1.0, 1.2) got_gold = int(got_gold) r.append(got_gold) for _id, _, _, _, is_robot, gold in res: c = MongoEmbededPlunderChars() c.is_robot = is_robot c.gold = gold self.mongo_plunder.chars[str(_id)] = c self.mongo_plunder.save() return res
def get_reward(self, tp): if not self.mongo_plunder.target_char: raise SanguoException(errormsg.PLUNDER_GET_REWARD_NO_TARGET, self.char_id, "Plunder Get Reward", "no Target char") if tp in self.mongo_plunder.got_reward: raise SanguoException(errormsg.PLUNDER_GET_REWARD_ALREADY_GOT, self.char_id, "Plunder Get Reward", "tp {0} already got".format(tp)) need_points = PLUNDER_REWARD_NEEDS_POINT[tp] if self.mongo_plunder.points < need_points: raise SanguoException( errormsg.PLUNDER_GET_REWARD_POINTS_NOT_ENOUGH, self.char_id, "Plunder Get Reward", "points not enough. {0} < {1}".format( self.mongo_plunder.points, need_points)) self.mongo_plunder.points -= need_points self.mongo_plunder.got_reward.append(tp) self.mongo_plunder.save() standard_drop = make_standard_drop_from_template() plunder_gold = self.mongo_plunder.chars[str( self.mongo_plunder.target_char)].gold char = Char(self.char_id).mc vip_plus = VIP_FUNCTION[char.vip].plunder_addition got_hero_id = 0 if tp == PLUNDER_HERO: f = Formation(self.mongo_plunder.target_char) heros = f.in_formation_hero_original_ids() got_hero_id = random.choice([hid for hid in heros if hid]) p = Prison(self.char_id) p.prisoner_add(got_hero_id, plunder_gold / 2) elif tp == PLUNDER_STUFF: stage = Stage(self.mongo_plunder.target_char) max_star_stage = stage.stage.max_star_stage if not max_star_stage: max_star_stage = 1 drop_ids = [ int(i) for i in STAGES[max_star_stage].normal_drop.split(',') ] drop = get_drop(drop_ids, multi=int(PLUNDER_GOT_ITEMS_HOUR * 3600 * (1 + vip_plus / 100.0) / 15)) standard_drop.update(drop) elif tp == PLUNDER_GOLD: standard_drop['gold'] = int(plunder_gold * (1 + vip_plus / 100.0)) resource = Resource(self.char_id, "Plunder Reward") resource.add(**standard_drop) self.send_notify() if got_hero_id: standard_drop['heros'] = [got_hero_id] return standard_drop_to_attachment_protomsg(standard_drop)
def get_plunder_list(self): """ @return: [[id, name, power, formation, is_robot, gold], ...] @rtype: list """ char = Char(self.char_id) cache_char = char.cacheobj char_level = cache_char.level choosing_list = MongoHangDoing.objects(Q(char_level__gte=char_level-PLUNDER_LEVEL_DIFF) & Q(char_level__lte=char_level+PLUNDER_LEVEL_DIFF) & Q(id__ne=self.char_id)) choosing_id_list = [c.id for c in choosing_list] ids = [] while True: if len(ids) >= 20 or not choosing_id_list: break c = random.choice(choosing_id_list) choosing_id_list.remove(c) if c == self.char_id: continue if c not in ids: ids.append(c) random.shuffle(ids) ids = ids[:10] res = [] for i in ids: char = Char(i) f = Formation(i) res.append([i, char.cacheobj.name, char.power, f.in_formation_hero_original_ids(), False]) robot_ids = [] robot_amount = 10 - len(ids) if len(robot_ids) < robot_amount: # 添加机器人 min_level = char_level - 20 if min_level <= 1: min_level = 1 real_chars = get_char_ids_by_level_range(cache_char.server_id, min_level, char_level + 10) for c in real_chars: if c == self.char_id: continue if c in ids: continue robot_ids.append(c) if len(robot_ids) >= robot_amount: break for i in robot_ids: char = Char(i) f = Formation(i) res.append([i, char.cacheobj.name, char.power, f.in_formation_hero_original_ids(), True]) final_ids = [r[0] for r in res] this_stages = MongoStage.objects.filter(id__in=final_ids) this_stages_dict = {s.id: s.max_star_stage for s in this_stages} for r in res: _id = r[0] max_star_stage = this_stages_dict.get(_id, 1) if not max_star_stage: max_star_stage = 1 got_gold = max_star_stage * 400 * random.uniform(1.0, 1.2) got_gold = int(got_gold) r.append(got_gold) for _id, _, _, _, is_robot, gold in res: c = MongoEmbededPlunderChars() c.is_robot = is_robot c.gold = gold self.mongo_plunder.chars[str(_id)] = c self.mongo_plunder.save() return res
def get_reward(self, tp): if not self.mongo_plunder.target_char: raise SanguoException( errormsg.PLUNDER_GET_REWARD_NO_TARGET, self.char_id, "Plunder Get Reward", "no Target char" ) if tp in self.mongo_plunder.got_reward: raise SanguoException( errormsg.PLUNDER_GET_REWARD_ALREADY_GOT, self.char_id, "Plunder Get Reward", "tp {0} already got".format(tp) ) need_points = PLUNDER_REWARD_NEEDS_POINT[tp] if self.mongo_plunder.points < need_points: raise SanguoException( errormsg.PLUNDER_GET_REWARD_POINTS_NOT_ENOUGH, self.char_id, "Plunder Get Reward", "points not enough. {0} < {1}".format(self.mongo_plunder.points, need_points) ) self.mongo_plunder.points -= need_points self.mongo_plunder.got_reward.append(tp) self.mongo_plunder.save() standard_drop = make_standard_drop_from_template() plunder_gold = self.mongo_plunder.chars[str(self.mongo_plunder.target_char)].gold char = Char(self.char_id).mc vip_plus = VIP_FUNCTION[char.vip].plunder_addition got_hero_id = 0 if tp == PLUNDER_HERO: f = Formation(self.mongo_plunder.target_char) heros = f.in_formation_hero_original_ids() got_hero_id = random.choice([hid for hid in heros if hid]) p = Prison(self.char_id) p.prisoner_add(got_hero_id, plunder_gold/2) elif tp == PLUNDER_STUFF: stage = Stage(self.mongo_plunder.target_char) max_star_stage = stage.stage.max_star_stage if not max_star_stage: max_star_stage = 1 drop_ids = [int(i) for i in STAGES[max_star_stage].normal_drop.split(',')] drop = get_drop(drop_ids, multi=int(PLUNDER_GOT_ITEMS_HOUR * 3600 * (1+vip_plus/100.0) / 15)) standard_drop.update(drop) elif tp == PLUNDER_GOLD: standard_drop['gold'] = int(plunder_gold * (1+vip_plus/100.0)) resource = Resource(self.char_id, "Plunder Reward") resource.add(**standard_drop) self.send_notify() if got_hero_id: standard_drop['heros'] = [got_hero_id] return standard_drop_to_attachment_protomsg(standard_drop)