def loot_target(player, power, loot, count): dvalue = 100 + power * 0.06 s, e = int(max(0, power - dvalue)), int(power + dvalue) samples = set(PlayerPowerRanking.get_range_by_score(s, e, count=16)) - set( [player.entityID]) zombies = set(get_zombies_by_power(s, e, count=5)) # s = max(player.level - 12, 1) # e = max(player.level - 6, 1) # samples = set( # PlayerLevelRanking.get_range_by_score( # s, e, count=16) # ) - set([player.entityID]) # zombies = set(reduce( # lambda x, y: x+y, # [get_zombies_by_level(l)[:5] # for l in range(s, e + 1)])) samples = samples.union(zombies) chosen = choice_one(list(samples)) logger.debug("search targets is {}".format(samples)) if not is_zombie(chosen): rest = search_targets_loot([chosen], loot)[0] p = Player.simple_load(chosen, "loot_protect_time") now = int(time.time()) if PlayerLootLock.locked(chosen) or \ now < p.loot_protect_time and rest < 50 and rest < count: chosen = choice_one(list(zombies)) if not chosen: chosen = choice_one(list(get_zombies())) # # NOTE FIXME FOR DEBUG # chosen = player.entityID return get_opponent_detail(chosen)
def search_target(p, fbID): detail = p.dlc_detail_cache.get(fbID, {}) if detail: return detail dlc_fb = get_config(DlcFbInfoConfig)[fbID] lo, hi = dlc_fb.range if lo or hi: spower = (1 + lo / float(100)) * p.max_power epower = (1 + hi / float(100)) * p.max_power if p.max_power > spower and p.max_power < epower: count = 11 else: count = 10 ranks = PlayerMaxPowerRanking.get_range_by_score( spower, epower, count=count) try: ranks.remove(p.entityID) except ValueError: pass if ranks: if dlc_fb.rule == 1: target = ranks[-1] elif dlc_fb.rule == 2: target = ranks[0] else: target = choice_one(ranks) detail = get_opponent_detail(target) if not detail: # robot lo, hi = dlc_fb.rob_range if lo or hi: spower = (1 + lo / float(100)) * p.max_power epower = (1 + hi / float(100)) * p.max_power zombies = get_zombies_by_power( spower, epower, count=5, hide=False, group=dlc_fb.robot_group) logger.debug("zombies by power: %r", zombies) if not zombies: zombies = get_zombies_by_level( p.level, hide=False, group=dlc_fb.robot_group) logger.debug("zombies by level: %r", zombies) if dlc_fb.rob_rule == 1: target = zombies[-1] elif dlc_fb.rob_rule == 2: target = zombies[0] else: target = choice_one(zombies) detail = get_zombie(target) p.dlc_detail_cache[fbID] = detail return detail
def trigger_chests_recv(self, msgtype, body): p = self.player if not check_trigger_event_type(p, EventType.Chests): return fail_msg(msgtype, reason="无触发事件或错误的事件类型") config = get_config(TriggerEventConfig)[p.trigger_event] chest = get_config(TriggerChestsConfig).get(config.event_param) if len(p.trigger_chests) > 0: cost = {"gold": get_cons_value("TriggerChestsMoreCost")} else: cost = {} rests = set(range(len(chest.rewards))) - p.trigger_chests if not rests: return fail_msg(msgtype, reason="已经没有宝箱了") index = choice_one(list(rests)) is_best = False if index == 0: p.trigger_event = 0 is_best = True gain = parse_reward([chest.rewards[index]]) try: apply_reward(p, gain, cost=cost, type=RewardType.TriggerChests) except AttrNotEnoughError: return fail_msg(msgtype, reason="钻石不足") p.trigger_chests.add(index) p.save() p.sync() rsp = poem_pb.TriggerChestsRecv(is_best=is_best) build_reward_msg(rsp, gain) return success_msg(msgtype, rsp)
def apply(sock, player=None): rsp = search_faction() factionIDs = [i.factionID for i in rsp.infos] player = player or get_player() factionID = choice_one(factionIDs) #review_member() return apply_faction(factionID)
def get_target(self, p): if p.daily_cache_targetID: targetID = p.daily_cache_targetID else: # 下区间 = int(500/(2*最高连胜数+10)) # 上区间 = int(最高连胜数/2) top = int(p.daily_max_win_count / 2) + 2 bot = int(500/(2 * p.daily_max_win_count + 10)) need = bot + top targets = map( int, PlayerDailyRankRanking.get_range_by_score( p.daily_rank, "+inf", count=top)) need = need - len(targets) targets.extend(map( int, PlayerDailyRankRanking.get_range_by_score( "-inf", max(p.daily_rank - 1, 0), count=need))) try: targets = list(set(targets)) targets.remove(p.entityID) except ValueError: pass if not targets: return None targetID = choice_one(targets) p.daily_cache_targetID = targetID p.save() detail = get_opponent_detail(targetID, type=LineupType.Daily) if not detail.get("pets"): detail = get_opponent_detail(targetID) return detail
def enter_treasure(p): rsp = poem_pb.EnterTreasure() if p.treasure_cache: grids = p.treasure_cache else: used = set() pending = [] grids = [EmptyGrid] * 25 treasure = choice_one( get_config(TreasureByTypeConfig)[p.treasure_type]) logger.debug("treasure.id %r", treasure.id) treasure = get_config(TreasureConfig)[treasure.id] for type, subtype, count, pos_or_times in treasure.data: if pos_or_times > 0: # pos_or_times means position pos = pos_or_times - 1 grids[pos] = [type, subtype, count] used.add(pos) else: # pos_or_times means appear times # 零算一次 times = abs(pos_or_times or 1) for i in range(times): pending.append([type, subtype, count]) rest = list(set(range(0, 25)).difference(used)) for index, each in enumerate(pending): want = choice_one(rest) if not want: break pos = rest.pop(rest.index(want)) grids[pos] = pending[index] p.treasure_cache = grids for type, subtype, count in grids: data = {"type": type} if type == TreasureGrid.TreasureGridTypeChest: data["chest"] = p.treasure_type elif type == TreasureGrid.TreasureGridTypeMonster: data["monster"] = subtype elif type == TreasureGrid.TreasureGridTypeBuff: data["buff"] = subtype elif type == TreasureGrid.TreasureGridTypeReward: data["reward"] = poem_pb.RewardData(type=subtype, count=count) rsp.grids.add(**data) p.save() return rsp
def _proxy(*args, **kwargs): regionID = int(request.GET["regionID"]) region = g_regions.get(regionID) if not region: return FAILURE worldIDs = region.worlds.keys() w = choice_one(worldIDs) worlds = [] if w: worlds.append(region.worlds[w]) return self.batch_query(*self.prepare_query(worlds))
def get_opponent_detail_by_strategy(self, player): from config.configs import get_config, PvpDegreeConfig configs = get_config(PvpDegreeConfig) opps = [] score = player.totalbp logger.debug("score: %d", score) for config in configs: logger.debug("start:%d, end:%d", config.start, config.end) if score >= config.start and score < config.end: logger.debug("prob:%f", config.prob) if guess(config.prob): logger.debug("guess:true") opps = get_zombies_by_level(player.level) break if not opps: opps = self.get_opponents_by_strategy(player.level, player.totalbp) opps = filter(lambda s: s != player.entityID, opps) logger.debug('random opps {}'.format(opps)) oppID = choice_one(opps) assert oppID, 'not opp' logger.debug("choose opp:%d", oppID) return get_opponent_detail(oppID, type=LineupType.DEF)
def _proxy(*args, **kwargs): # from player.model import Player val = int(getcallargs(f, *args, **kwargs)[key]) regionID = int(request.GET["regionID"]) # w = Player.simple_load(val, ["worldID"]).worldID w = int( index_pool.execute( "HGET", "index_p_online{%d}{%d}" % (regionID, settings.SESSION["ID"]), val) or 0) region = g_regions.get(regionID) if not region: return FAILURE if not w: worldIDs = region.worlds.keys() w = choice_one(worldIDs) if w: worlds = [region.worlds[w]] else: worlds = [] else: worlds = [region.worlds[w]] return self.batch_query(*self.prepare_query(worlds))
def get_rank_targets(self, p, count=10): if p.rank_targets: return p.rank_targets power = p.max_power score = p.totalbp or 1000 configs = get_config(PvpRuleConfig) high_scores = [] low_scores = [] targets = set() for ID, config in configs.items(): if config.score > score: high_scores.append((config.score, ID)) else: low_scores.append((config.score, ID)) if low_scores: floor = max(low_scores) high_scores.insert(0, floor) current = configs[min(high_scores)[1]] logger.debug("current config: %r", current) high_score_rest = current.high_score_count high_scores = sorted([i for i, j in high_scores]) logger.debug("high_scores %r", high_scores) for index, s in enumerate(high_scores): if index > 1: break if not high_score_rest: logger.debug("high_score_rest:%r", high_score_rest) continue spower = int(power * (1 + current.power_down / float(100))) epower = int(power * (1 + current.power_up / float(100))) sscore = max(s, score) escore = high_scores[index + 1]\ if index + 1 < len(high_scores) else "+inf" scores = set( PlayerRankRanking.get_range_by_score( sscore, '%s' % escore if isinstance(escore, basestring) else "(%d" % escore)) logger.debug("sscore:%r", sscore) logger.debug("escore:%r", escore) logger.debug("scores:%r", scores) logger.debug("spower:%r", spower) logger.debug("epower:%r", epower) scores = scores - {p.entityID} ranks = scores if s != 1500: ranks = PlayerMaxPowerRanking.get_range_by_score(spower, epower, count=100) logger.debug("ranks :%r", ranks) ranks = list((set(ranks) & scores)) ranks = random.sample(ranks, min(len(ranks), high_score_rest)) logger.debug("high_score_rest:%r", high_score_rest) logger.debug("rranks :%r", ranks) high_score_rest -= len(ranks) targets.update(ranks) low_score_rest = 0 low_scores = sorted([i for i, j in low_scores], reverse=True) logger.debug("low_scores %r", low_scores) low_score_rest = high_score_rest + current.low_score_count for index, s in enumerate(low_scores): if index > 0: break if not low_score_rest: logger.debug("low_score_rest:%r", low_score_rest) continue spower = power * (1 + current.power_down / float(100)) epower = power * (1 + current.power_up / float(100)) sscore = low_scores[index + 1]\ if index + 1 < len(low_scores) else "-inf" escore = max(s, score) scores = set( PlayerRankRanking.get_range_by_score(sscore, "(%d" % escore)) ranks = PlayerMaxPowerRanking.get_range_by_score(spower, epower, count=100) logger.debug("sscore:%r", sscore) logger.debug("escore:%r", escore) logger.debug("scores:%r", scores) logger.debug("spower:%r", spower) logger.debug("epower:%r", epower) logger.debug("ranks :%r", ranks) ranks = list((set(ranks) & scores) - {p.entityID}) ranks = random.sample(ranks, min(len(ranks), low_score_rest)) logger.debug("rranks :%r", ranks) logger.debug("low_score_rest:%r", low_score_rest) low_score_rest -= len(ranks) targets.update(ranks) def choice_one(): level = max(p.level - current.robot_level, 15) while level >= get_open_level("pvp"): logger.debug("level match %d", level) samples = get_zombies_by_level(level) while samples: try: one = random.choice(samples) samples.remove(one) yield one except IndexError: pass level -= 1 for each in choice_one(): if len(targets) >= count: break else: logger.debug("only level match %r", each) targets.add(each) logger.debug("final %r", targets) p.rank_targets = list(targets) p.save() return p.rank_targets
def search_targets(player): # dvalue = 100 + player.power * 0.06 # s, e = int(max(0, player.power - dvalue)), int(player.power + dvalue) # sample = set( # PlayerPowerRanking.get_range_by_score( # s, e, count=SAMPLE_COUNT) # ) - set([player.entityID]) s, e = max(0, player.level - 2), player.level + 2 sample = set( PlayerLevelRanking.get_range_by_score( s, e, count=SAMPLE_COUNT) ) - set([player.entityID]) try: sample = random.sample(sample, DETAIL_COUNT) except ValueError: pass logger.debug("random sample is {}".format(sample)) from player.formulas import get_mine_level # 过滤未开启的 types = [i for i in poem_pb.MineType1, poem_pb.MineType2 if get_mine_level(i, player.level) > 0] or [poem_pb.MineType1] targets = g_entityManager.get_players_info(sample, [ "entityID", "name", "level", "prototypeID", 'mine_protect_time' ] + reduce(lambda x, y: x + y, [[ "mine_products%d" % t, "mine_productivity%d" % t, "mine_time%d" % t, "mine_maximum%d" % t, "mine_safety%d" % t, ] for t in types])) _targets = [] for target in targets: for t in types: _target = dict(target) _target['type'] = t _target['mine_products'] = _target['mine_products%d' % t] _target['mine_productivity'] = _target['mine_productivity%d' % t] _target['mine_time'] = _target['mine_time%d' % t] mine_level = get_mine_level(t, _target['level']) _target['mine_safety'] = get_mine_safety(t, mine_level) _target['mine_maximum'] = get_mine_maximum(t, mine_level) if not _target['mine_productivity']: continue _targets.append(_target) now = int(time.time()) targets = sorted(_targets, key=lambda s: int(s['mine_time'] or now)) chosens = [] filterset = set() # 过滤重复的玩家, 只能被匹配一次 for t in targets: if t['entityID'] in filterset: continue if PlayerMineLock.locked(t["entityID"]): continue if t['mine_protect_time'] > now: continue if len(chosens) >= SEARCH_COUNT: break booty = calc_booty(now, t['mine_products'], t['mine_time'], t['mine_productivity'], t['mine_maximum'], t['mine_safety'] ) if booty > 0: chosens.append({ 'booty': max(booty, 0), 'fought': False, 'entityID': t['entityID'], 'name': t['name'], 'prototypeID': t['prototypeID'], 'level': t['level'], 'type': t['type'], }) filterset.add(t['entityID']) if len(chosens) >= 2: break if len(chosens) < SEARCH_COUNT: lv = get_open_level("get_money1") zombies1 = get_zombies_by_level(max(player.level - 2, lv), hide=True) lv = get_open_level("get_exp1") zombies2 = get_zombies_by_level(max(player.level - 2, lv), hide=True) if not zombies1: zombies1 = get_zombies() logger.debug("zombies1 is {}".format(zombies1)) if not zombies2: zombies2 = get_zombies() logger.debug("zombies2 is {}".format(zombies2)) if zombies1 or zombies2: configs = get_config(PvpGroupConfig) while len(chosens) < SEARCH_COUNT: type = choice_one(types) if type == poem_pb.MineType1: zombies = zombies1 booty_field = "rob_money" else: zombies = zombies2 booty_field = "rob_soul" if not zombies1 and not zombies2: break got = choice_one(zombies) config = configs[got] if not config.visible: continue booty = getattr(config, booty_field, 0) zombies.remove(got) chosens.append({ 'booty': booty, 'fought': False, 'entityID': config.ID, 'name': config.name, 'prototypeID': config.monster_id1, 'level': config.level, 'type': type, }) random.shuffle(chosens) logger.debug("search targets is {}".format(chosens)) player.mine_targets_detail_cache = chosens player.save() player.sync() return chosens