def finish(self, actual_seconds=None, set_hang=True): if not self.hang_doing: raise SanguoException(errormsg.HANG_NOT_EXIST, self.char_id, "Hang Finish", "Hang Finish. But no hang exist") if not actual_seconds: actual_seconds = arrow.utcnow().timestamp - self.hang_doing.start if set_hang: self.hang.used += actual_seconds self.hang.save() self.hang_doing.finished = True self.hang_doing.actual_seconds = actual_seconds self.hang_doing.save() self.send_notify() attachment = Attachment(self.char_id) attachment.save_to_prize(1) actual_hours = actual_seconds / 3600 achievement = Achievement(self.char_id) achievement.trig(28, actual_hours)
def save_drop(self): stage_id = self.hang_doing.stage_id actual_seconds = self.hang_doing.actual_seconds times = actual_seconds / 15 stage = STAGES[stage_id] drop_exp = stage.normal_exp * times drop_gold = stage.normal_gold * times drop_gold = self._actual_gold(drop_gold) drop_ids = [int(i) for i in stage.normal_drop.split(',')] prepare_drop = get_drop(drop_ids, multi=times, gaussian=True) prepare_drop['exp'] += drop_exp prepare_drop['gold'] += drop_gold self.hang_doing.delete() self.hang_doing = None self.send_notify() prepare_drop = drop_after_stage_type(stage_id, prepare_drop) resource = Resource(self.char_id, "Hang Reward", "actual seconds = {0}, times = {1}".format(actual_seconds, times)) standard_drop = resource.add(**prepare_drop) achievement = Achievement(self.char_id) achievement.trig(29, drop_exp) return standard_drop_to_attachment_protomsg(standard_drop)
def reset(): amount = MongoArenaWeek.objects.count() logger = Logger("reset_arena_week.log") logger.write("Reset Arena Week: Start. chars amount: {0}".format(amount)) data = MongoArenaWeek.objects.all() MongoArenaWeek.objects.delete() for d in data: reward = _get_reward(d.rank) gold = reward.week_gold attachment = Attachment(d.id) attachment.save_to_attachment(3, gold=gold) achievement = Achievement(d.id) achievement.trig(10, d.rank) if d.rank in TOP_RANKS: try: top = MongoArenaTopRanks.objects.get(id=d.rank) except DoesNotExist: top = MongoArenaTopRanks(id=d.rank) char_name = Char(d.id).cacheobj.name top.name = char_name top.save() logger.write("Reset Arena Week: Complete") logger.close()
def finish(self, actual_seconds=None, set_hang=True): if not self.hang_doing: raise SanguoException( errormsg.HANG_NOT_EXIST, self.char_id, "Hang Finish", "Hang Finish. But no hang exist" ) if not actual_seconds: actual_seconds = arrow.utcnow().timestamp - self.hang_doing.start if set_hang: self.hang.used += actual_seconds self.hang.save() self.hang_doing.finished = True self.hang_doing.actual_seconds = actual_seconds self.hang_doing.save() self.send_notify() attachment = Attachment(self.char_id) attachment.save_to_prize(1) actual_hours = actual_seconds / 3600 achievement = Achievement(self.char_id) achievement.trig(28, actual_hours)
def plundered(self, who, self_win): if not self.hang_doing: return if self_win: if self.hang_doing.plunder_win_times >= PLUNDER_DEFENSE_SUCCESS_MAX_TIMES: return self.hang_doing.plunder_win_times += 1 gold = PLUNDER_DEFENSE_SUCCESS_GOLD else: if self.hang_doing.plunder_lose_times >= PLUNDER_DEFENSE_FAILURE_MAX_TIMES: return self.hang_doing.plunder_lose_times += 1 gold = -PLUNDER_DEFENSE_FAILURE_GOLD l = MongoEmbededPlunderLog() l.name = who l.gold = gold if len(self.hang_doing.logs) >= 5: self.hang_doing.logs.pop(0) self.hang_doing.logs.append(l) self.hang_doing.save() achievement = Achievement(self.char_id) achievement.trig(33, 1)
def got_plundered(self, from_char_id, from_char_name, from_win, standard_drop): if from_win: tp = 1 else: tp = 2 gold = standard_drop['gold'] item_text = standard_drop_to_readable_text(standard_drop, sign='-') log = MongoEmbeddedHangLog() log.timestamp = arrow.utcnow().timestamp log.tp = tp log.who = from_char_name log.gold = gold log.item_text = item_text self.mongo_affairs.logs.append(log) if len(self.mongo_affairs.logs) > PLUNDER_LOG_MAX_AMOUNT: self.mongo_affairs.logs.pop(0) self.mongo_affairs.save() self.send_hang_notify() achievement = Achievement(self.char_id) achievement.trig(33, 1)
def _hero_add(char_id, hero_ids, hero_original_ids, send_notify, **kwargs): if send_notify: heros = [Hero.cache_obj(i) for i in hero_ids] add_hero_notify(char_id, heros) heros_dict = char_heros_dict(char_id) achievement = Achievement(char_id) achievement.trig(1, len(heros_dict), send_notify=send_notify) quality_one_heros_amount = 0 quality_two_heros_amount = 0 quality_three_heros_amount = 0 gender_female_heros_amount = 0 for h in heros_dict.values(): original_hero = HEROS[h.oid] if original_hero.quality == 1: quality_one_heros_amount += 1 if original_hero.quality == 2: quality_two_heros_amount += 1 if original_hero.quality == 3: quality_three_heros_amount += 1 if original_hero.gender == 2: gender_female_heros_amount += 1 achievement.trig(2, quality_one_heros_amount, send_notify=send_notify) achievement.trig(3, quality_two_heros_amount, send_notify=send_notify) achievement.trig(4, quality_three_heros_amount, send_notify=send_notify) achievement.trig(5, gender_female_heros_amount, send_notify=send_notify) achievement.trig(6, hero_original_ids, send_notify=send_notify)
def battle(self): need_sycee = 0 counter = Counter(self.char_id, 'arena') if counter.remained_value <= 0: counter = Counter(self.char_id, 'arena_buy') if counter.remained_value <= 0: char = Char(self.char_id).mc if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle", "arena no times. vip current: {0}, max {1}".format( char.vip, VIP_MAX_LEVEL)) raise SanguoException( errormsg.ARENA_NO_TIMES_FINAL, self.char_id, "Arena Battle", "arena no times. vip reach max level {0}".format( VIP_MAX_LEVEL)) else: need_sycee = ARENA_COST_SYCEE rival_id = self.choose_rival() if not rival_id: raise SanguoException(errormsg.ARENA_NO_RIVAL, self.char_id, "Arena Battle", "no rival.") if need_sycee: resource = Resource(self.char_id, "Arena Battle", "battle for no free times") resource.check_and_remove(sycee=-need_sycee) counter.incr() # set battle cd redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD) msg = protomsg.Battle() b = PVP(self.char_id, rival_id, msg) b.start() if msg.self_win: achievement = Achievement(self.char_id) achievement.trig(11, 1) self_score = self.score rival_arena = Arena(rival_id) rival_score = rival_arena.score new_score = calculate_score(self_score, rival_score, msg.self_win) self.set_score(new_score) t = Task(self.char_id) t.trig(2) self.send_notify(score=new_score) rival_arena.be_beaten(rival_score, self_score, not msg.self_win, self.char_id) return msg
def save_drop(self): stage_id = self.hang_doing.stage_id actual_seconds = self.hang_doing.actual_seconds times = actual_seconds / 15 stage = STAGES[stage_id] drop_exp = stage.normal_exp * times drop_gold = stage.normal_gold * times drop_gold = self._actual_gold(drop_gold) drop_ids = [int(i) for i in stage.normal_drop.split(',')] prepare_drop = get_drop(drop_ids, multi=times, gaussian=True) prepare_drop['exp'] += drop_exp prepare_drop['gold'] += drop_gold self.hang_doing.delete() self.hang_doing = None self.send_notify() prepare_drop = drop_after_stage_type(stage_id, prepare_drop) resource = Resource( self.char_id, "Hang Reward", "actual seconds = {0}, times = {1}".format(actual_seconds, times)) standard_drop = resource.add(**prepare_drop) achievement = Achievement(self.char_id) achievement.trig(29, drop_exp) return standard_drop_to_attachment_protomsg(standard_drop)
def step_up(self): to = self.equip.upgrade_to if not to: raise SanguoException( errormsg.EQUIPMENT_REACH_MAX_STEP, self.char_id, "Equipment Step Up", "Equipment {0} Can not step up".format(self.equip_id) ) step_up_need_gold = self.step_up_need_gold() stuff_needs = [] for x in self.equip.stuff_needs.split(','): _id, _amount = x.split(':') stuff_needs.append((int(_id), int(_amount))) resouce = Resource(self.char_id, "Equipment Step Up", "equipment {0}".format(self.equip_id)) with resouce.check(gold=-step_up_need_gold, stuffs=stuff_needs): self.oid = to self.equip = EQUIPMENTS[self.oid] self.mongo_item.equipments[str(self.equip_id)].oid = to add_gem_slots = self.equip.slots - len(self.mongo_item.equipments[str(self.equip_id)].gems) for i in range(add_gem_slots): self.mongo_item.equipments[str(self.equip_id)].gems.append(0) self.mongo_item.save() achievement = Achievement(self.char_id) achievement.trig(22, 1) if not self.equip.upgrade_to: achievement.trig(23, 1) return stuff_needs
def plunder(self): if not self.mongo_plunder.char_id: raise SanguoException(errormsg.PLUNDER_NO_RIVAL, self.char_id, "Plunder Battle", "no rival target") if self.mongo_plunder.current_times <= 0: raise SanguoException(errormsg.PLUNDER_NO_TIMES, self.char_id, "Plunder Battle", "no times") self.change_current_plunder_times(change_value=-1) rival_battle_heros = dill.loads( base64.b64decode(self.mongo_plunder.battle_heros)) msg = MsgBattle() pvp = PlunderBattle( self.char_id, self.mongo_plunder.char_id, msg, self.mongo_plunder.char_name, rival_battle_heros, ) pvp.start() t = Task(self.char_id) t.trig(3) to_char_id = self.mongo_plunder.char_id target_server_url = self.mongo_plunder.server_url if msg.self_win: standard_drop = self._get_plunder_reward( self.mongo_plunder.char_city_id, self.mongo_plunder.char_gold, self.mongo_plunder.char_hero_original_ids) self.clean_plunder_target() achievement = Achievement(self.char_id) achievement.trig(12, 1) PlunderLeaderboardWeekly.incr(self.char_id) TimesLogPlunder(self.char_id).inc() else: standard_drop = make_standard_drop_from_template() self.mongo_plunder.plunder_times += 1 self.mongo_plunder.save() self.send_notify() plunder_finished_signal.send( sender=None, from_char_id=self.char_id, from_char_name=Char(self.char_id).mc.name, to_char_id=to_char_id, from_win=msg.self_win, standard_drop=standard_drop, target_server_url=target_server_url, ) return (msg, standard_drop)
def checkin(self): if self.c.has_checked: raise SanguoException( errormsg.CHECKIN_ALREADY_CHECKIN, self.char_id, "CheckIn checkin", "already checkin", ) self.c.has_checked = True day = self.c.day if self.c.day == MAX_DAYS: self.c.day = 1 else: self.c.day += 1 self.c.save() resource = Resource(self.char_id, "Daily Checkin", 'checkin reward. day {0}'.format(day)) resource_add = CHECKIN_DATA[str(day)]['package'] resource_add = get_drop_from_raw_package(resource_add) standard_drop = resource.add(**resource_add) msg = CheckInResponse() msg.ret = 0 msg.reward.MergeFrom(standard_drop_to_attachment_protomsg(standard_drop)) achievement = Achievement(self.char_id) achievement.trig(34, 1) self.send_update_notify(day) return msg
def get_attachment(self, prize_id, param=0): # prizie_id == 1 挂机不在这里 if prize_id == 4: # 成就 from core.achievement import Achievement ach = Achievement(self.char_id) att_msg = ach.get_reward(param) if ach.has_prizes(): prize_id = None elif prize_id == 5: # 任务 from core.task import Task task = Task(self.char_id) att_msg = task.get_reward(param) if task.has_prizes(): prize_id = None elif prize_id == 6: # 官职每日登录 # from core.daily import OfficialDailyReward # od = OfficialDailyReward(self.char_id) # att_msg = od.get_reward() att_msg = None elif prize_id == 7: # 团队本 att_msg = None else: try: attachment = self.attachment.attachments[str(prize_id)] except KeyError: raise SanguoException( errormsg.ATTACHMENT_NOT_EXIST, self.char_id, "Attachment Get", "{0} not exist".format(prize_id) ) attachment = json.loads(attachment) resource = Resource(self.char_id, "Prize {0}".format(prize_id)) standard_drop = resource.add(**attachment) self.attachment.attachments.pop(str(prize_id)) self.attachment.save() att_msg = standard_drop_to_attachment_protomsg(standard_drop) # 删除此prize_id if prize_id: if prize_id in self.attachment.prize_ids: self.attachment.prize_ids.remove(prize_id) if str(prize_id) in self.attachment.attachments: self.attachment.attachments.pop(str(prize_id)) self.attachment.save() self.send_notify() return att_msg
def battle(self): counter = Counter(self.char_id, 'arena') try: # 免费次数 counter.incr() except CounterOverFlow: counter = Counter(self.char_id, 'arena_buy') try: # 花费元宝次数 counter.incr() except CounterOverFlow: char = Char(self.char_id).mc if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle", "arena no times. vip current: {0}, max {1}".format(char.vip, VIP_MAX_LEVEL) ) raise SanguoException( errormsg.ARENA_NO_TIMES_FINAL, self.char_id, "Arena Battle", "arena no times. vip reach max level {0}".format(VIP_MAX_LEVEL) ) else: resource = Resource(self.char_id, "Arena Battle", "battle for no free times") resource.check_and_remove(sycee=-ARENA_COST_SYCEE) rival_id = self.choose_rival() msg = protomsg.Battle() b = PVP(self.char_id, rival_id, msg) b.start() achievement = Achievement(self.char_id) if msg.self_win: score = ARENA_GET_SCORE_WHEN_WIN achievement.trig(11, 1) self.mongo_arena.continues_win += 1 else: score = ARENA_GET_SCORE_WHEN_LOST self.mongo_arena.continues_win = 0 self.mongo_arena.save() if score: self.mongo_day.score += score self.mongo_day.save() t = Task(self.char_id) t.trig(2) self.send_notify() return msg
def _char_level_up(char_id, new_level, **kwargs): _all_hero_changed(char_id) achievement = Achievement(char_id) achievement.trig(18, new_level) activity_stage = ActivityStage(char_id) activity_stage.check(new_level) ActivityStatic(char_id).trig(1001)
def get_attachment(self, prize_id, param=0): if prize_id == 1: # 挂机 from core.stage import Hang h = Hang(self.char_id) att_msg = h.save_drop() elif prize_id == 4: # 成就 from core.achievement import Achievement ach = Achievement(self.char_id) att_msg = ach.get_reward(param) elif prize_id == 5: # 任务 from core.task import Task task = Task(self.char_id) att_msg = task.get_reward(param) elif prize_id == 6: # 官职每日登录 from core.daily import OfficalDailyReward od = OfficalDailyReward(self.char_id) att_msg = od.get_reward() elif prize_id == 7: # 团队本 from core.stage import TeamBattle tb = TeamBattle(self.char_id) att_msg = tb.get_reward() else: try: attachment = self.attachment.attachments[str(prize_id)] except KeyError: raise SanguoException( errormsg.ATTACHMENT_NOT_EXIST, self.char_id, "Attachment Get", "{0} not exist".format(prize_id) ) attachment = json.loads(attachment) resource = Resource(self.char_id, "Prize {0}".format(prize_id)) standard_drop = resource.add(**attachment) self.attachment.attachments.pop(str(prize_id)) self.attachment.save() att_msg = standard_drop_to_attachment_protomsg(standard_drop) # 删除此prize_id if prize_id in self.attachment.prize_ids: self.attachment.prize_ids.remove(prize_id) if str(prize_id) in self.attachment.attachments: self.attachment.attachments.pop(str(prize_id)) self.attachment.save() self.send_notify() return att_msg
def battle(self): counter = Counter(self.char_id, 'arena') try: # 免费次数 counter.incr() except CounterOverFlow: counter = Counter(self.char_id, 'arena_buy') try: # 花费元宝次数 counter.incr() except CounterOverFlow: char = Char(self.char_id).mc if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle", "arena no times. vip current: {0}, max {1}".format( char.vip, VIP_MAX_LEVEL)) raise SanguoException( errormsg.ARENA_NO_TIMES_FINAL, self.char_id, "Arena Battle", "arena no times. vip reach max level {0}".format( VIP_MAX_LEVEL)) else: resource = Resource(self.char_id, "Arena Battle", "battle for no free times") resource.check_and_remove(sycee=-ARENA_COST_SYCEE) rival_id = self.choose_rival() msg = protomsg.Battle() b = PVP(self.char_id, rival_id, msg) b.start() achievement = Achievement(self.char_id) if msg.self_win: score = ARENA_GET_SCORE_WHEN_WIN achievement.trig(11, 1) self.mongo_arena.continues_win += 1 else: score = ARENA_GET_SCORE_WHEN_LOST self.mongo_arena.continues_win = 0 self.mongo_arena.save() if score: self.mongo_day.score += score self.mongo_day.save() t = Task(self.char_id) t.trig(2) self.send_notify() return msg
def someone_accept_me(self, from_id): self.mf.friends[str(from_id)] = FRIEND_OK self.mf.save() achievement = Achievement(self.char_id) achievement.trig(27, self.real_cur_amount) msg = protomsg.UpdateFriendNotify() self._msg_friend(msg.friend, from_id, FRIEND_OK) publish_to_char(self.char_id, pack_msg(msg))
def plunder(self): if not self.mongo_plunder.char_id: raise SanguoException(errormsg.PLUNDER_NO_RIVAL, self.char_id, "Plunder Battle", "no rival target") if self.mongo_plunder.current_times <= 0: raise SanguoException(errormsg.PLUNDER_NO_TIMES, self.char_id, "Plunder Battle", "no times") self.change_current_plunder_times(change_value=-1) rival_battle_heros = dill.loads(base64.b64decode(self.mongo_plunder.battle_heros)) msg = MsgBattle() pvp = PlunderBattle( self.char_id, self.mongo_plunder.char_id, msg, self.mongo_plunder.char_name, rival_battle_heros ) pvp.start() t = Task(self.char_id) t.trig(3) to_char_id = self.mongo_plunder.char_id target_server_url = self.mongo_plunder.server_url if msg.self_win: standard_drop = self._get_plunder_reward( self.mongo_plunder.char_city_id, self.mongo_plunder.char_gold, self.mongo_plunder.char_hero_original_ids ) self.clean_plunder_target() achievement = Achievement(self.char_id) achievement.trig(12, 1) PlunderLeaderboardWeekly.incr(self.char_id) else: standard_drop = make_standard_drop_from_template() self.mongo_plunder.plunder_times += 1 self.mongo_plunder.save() self.send_notify() plunder_finished_signal.send( sender=None, from_char_id=self.char_id, from_char_name=Char(self.char_id).mc.name, to_char_id=to_char_id, from_win=msg.self_win, standard_drop=standard_drop, target_server_url=target_server_url, ) return (msg, standard_drop)
def _get_plunder_reward(self, city_id, gold, hero_original_ids): def _get_prisoner(): prison = 0 heros = [hid for hid in hero_original_ids if hid] while heros: hid = random.choice(heros) heros.remove(hid) if hid in PRISONER_POOL: prison = hid break if random.randint(1, 100) <= PLUNDER_GET_PRISONER_PROB: return prison return 0 char = Char(self.char_id).mc vip_plus = VIP_FUNCTION[char.vip].plunder_addition standard_drop = make_standard_drop_from_template() standard_drop["gold"] = int(gold * (1 + vip_plus / 100.0)) # 战俘 got_hero_id = _get_prisoner() if got_hero_id: p = Prison(self.char_id) p.prisoner_add(got_hero_id, gold / 2) achievement = Achievement(self.char_id) achievement.trig(13, 1) # 掉落 city = BATTLES[city_id] if city.normal_drop: drop_ids = [int(i) for i in city.normal_drop.split(",")] drop_prob = max( PLUNDER_GET_DROPS_TIMES - (self.mongo_plunder.plunder_times - 1) * PLUNDER_DROP_DECREASE_FACTOR, PLUNDER_GET_DROPS_TIMES * PLUNDER_DROP_MIN_FACTOR, ) drop = get_drop(drop_ids, multi=int(drop_prob)) drop.pop("gold") standard_drop.update(drop) resource = Resource(self.char_id, "Plunder Reward") resource.add(**standard_drop) self.send_notify() if got_hero_id: standard_drop["heros"] = [(got_hero_id, 1)] return standard_drop
def get_reward(self, _id): try: this_task = TASKS[_id] except KeyError: raise SanguoException( errormsg.TASK_NOT_EXIST, self.char_id, "Task Get Reward", "Task {0} not exist".format(_id) ) if _id not in self.task.finished: raise SanguoException( errormsg.TASK_NOT_FINISH, self.char_id, "Task Get Reward", "Task {0} not finish".format(_id) ) if _id in self.task.complete: raise SanguoException( errormsg.TASK_ALREADY_GOT_REWARD, self.char_id, "Task Get Reward", "Task {0} already got reward".format(_id) ) sycee = this_task.sycee if this_task.sycee else 0 gold = this_task.gold if this_task.gold else 0 resource = Resource(self.char_id, "Task Reward", "task {0}".format(_id)) standard_drop = resource.add(gold=gold, sycee=sycee) if this_task.next_task: next_task = TASKS[this_task.next_task] if self.task.tasks[str(this_task.tp)] >= next_task.times: self.task.finished.append(this_task.next_task) index = self.task.doing.index(_id) self.task.doing.pop(index) self.task.doing.insert(index, this_task.next_task) self.task.finished.remove(_id) self.task.complete.append(_id) self.task.save() self.send_notify() if self.all_complete(): achievement = Achievement(self.char_id) achievement.trig(30, 1) return standard_drop_to_attachment_protomsg(standard_drop)
def release(self, _id): p = self._abandon(_id, action="Prisoner Release") got_gold = p.gold got_treasure = random.choice(PRISONER_RELEASE_GOT_TREASURE[HEROS[p.oid].quality]) resource = Resource(self.char_id, "Prisoner Release") standard_drop = resource.add(gold=got_gold, stuffs=[(got_treasure, 1)]) achievement = Achievement(self.char_id) achievement.trig(16, 1) return standard_drop_to_attachment_protomsg(standard_drop)
def _char_sycee_changed(char_id, now_value, cost_value, add_value, **kwargs): if cost_value: cost_value = abs(cost_value) achievement = Achievement(char_id) achievement.trig(31, cost_value) cslog = MongoCostSyceeLog() cslog.char_id = char_id cslog.sycee = cost_value cslog.cost_at = arrow.utcnow().timestamp cslog.save() ActivityStatic(char_id).trig(6001)
def kill(self, _id): p = self._abandon(_id, action="Prisoner Kill") soul_amount = random.randint(3, 10) souls = [(p.oid, soul_amount)] resource = Resource(self.char_id, "Prisoner Kill") standard_drop = resource.add(souls=souls) achievement = Achievement(self.char_id) achievement.trig(15, 1) return standard_drop_to_attachment_protomsg(standard_drop)
def gem_merge(self, _id, method): this_gem_amount = self.item.gems.get(str(_id), 0) if this_gem_amount == 0: raise SanguoException( errormsg.GEM_NOT_EXIST, self.char_id, "Gem Merge", "Gem {0} not exist".format(_id) ) elif this_gem_amount < 4: raise SanguoException( errormsg.GEM_NOT_ENOUGH, self.char_id, "Gem Merge", "Gem {0} not enough. {1} < 4".format(_id, this_gem_amount) ) to_id = GEMS[_id].merge_to if not to_id: raise SanguoException( errormsg.GEM_CAN_NOT_MERGE, self.char_id, "Gem Merge", "Gem {0} can not merge".format(_id) ) if method == MergeGemRequest.SIMPLE: # 单次合成 to_amount = 1 else: # 自动全部合成 to_amount, _ = divmod(this_gem_amount, 4) remove_amount = to_amount * 4 self.gem_remove(_id, remove_amount) self.gem_add([(to_id, to_amount)]) to_gem_obj = GEMS[to_id] achievement = Achievement(self.char_id) achievement.trig(25, to_amount) achievement.trig(26, to_gem_obj.level) tl = TimesLogGemMerge(self.char_id) for i in range(to_amount): tl.inc() return to_id, to_amount
def _char_level_up(char_id, new_level, **kwargs): heros_dict = char_heros_dict(char_id) achievement = Achievement(char_id) achievement.trig(18, new_level) heros = [] for hid in heros_dict.keys(): h = Hero(hid) h.save_cache() heros.append(h) update_hero_notify(char_id, heros) activity_stage = ActivityStage(char_id) activity_stage.check(new_level)
def get_reward(self, _id): try: this_task = TASKS[_id] except KeyError: raise SanguoException(errormsg.TASK_NOT_EXIST, self.char_id, "Task Get Reward", "Task {0} not exist".format(_id)) if _id not in self.task.finished: raise SanguoException(errormsg.TASK_NOT_FINISH, self.char_id, "Task Get Reward", "Task {0} not finish".format(_id)) if _id in self.task.complete: raise SanguoException(errormsg.TASK_ALREADY_GOT_REWARD, self.char_id, "Task Get Reward", "Task {0} already got reward".format(_id)) sycee = this_task.sycee if this_task.sycee else 0 gold = this_task.gold if this_task.gold else 0 resource = Resource(self.char_id, "Task Reward", "task {0}".format(_id)) standard_drop = resource.add(gold=gold, sycee=sycee) if this_task.next_task: next_task = TASKS[this_task.next_task] if self.task.tasks[str(this_task.tp)] >= next_task.times: self.task.finished.append(this_task.next_task) index = self.task.doing.index(_id) self.task.doing.pop(index) self.task.doing.insert(index, this_task.next_task) self.task.finished.remove(_id) self.task.complete.append(_id) self.task.save() self.send_notify() if self.all_complete(): achievement = Achievement(self.char_id) achievement.trig(30, 1) return standard_drop_to_attachment_protomsg(standard_drop)
def get_hang_reward(self, auto_start=True): """立即保存掉落,并且返回attachment消息""" # if not self.mongo_affairs.hang_city_id: # raise SanguoException( # errormsg.HANG_NOT_EXIST, # self.char_id, # "Get Hang Reward", # "hang not exist" # ) drops = self.get_drop() ac = ActivityEntry(self.char_id, 30004) if ac and ac.is_valid(): drops['gold'] *= 2 resource = Resource(self.char_id, "Hang Reward") standard_drop = resource.add(**drops) if auto_start: # 领取奖励后自动开始 self.start_hang(self.mongo_affairs.hang_city_id, get_reward=False) achievement = Achievement(self.char_id) achievement.trig(28, self.get_hang_obj().passed_time / 3600) achievement.trig(29, drops['exp']) return standard_drop_to_attachment_protomsg(standard_drop)
def pve_finish(char_id, stage_id, win, star, **kwargs): achievement = Achievement(char_id) if win: achievement.trig(7, stage_id) if star: achievement.trig(9, 1) t = Task(char_id) t.trig(1) else: achievement.trig(8, 1)
def login_notify(char_id): hero_objs = char_heros_obj(char_id) Char(char_id).send_notify() hero_notify(char_id, hero_objs) Item(char_id).send_notify() f = Formation(char_id) f.send_socket_notify() f.send_formation_notify() hang = Hang(char_id) hang.send_notify() Plunder(char_id).send_notify() p = Prison(char_id) p.send_prisoners_notify() Arena(char_id).send_notify() f = Friend(char_id) f.send_friends_notify() f.send_friends_amount_notify() m = Mail(char_id) m.send_mail_notify() CheckIn(char_id).send_notify() stage = Stage(char_id) stage.send_already_stage_notify() stage.send_new_stage_notify() stage_elite = EliteStage(char_id) stage_elite.send_notify() stage_elite.send_remained_times_notify() stage_activity = ActivityStage(char_id) stage_activity.send_notify() stage_activity.send_remained_times_notify() HeroPanel(char_id).send_notify() Task(char_id).send_notify() Achievement(char_id).send_notify() HeroSoul(char_id).send_notify() FunctionOpen(char_id).send_notify() Levy(char_id).send_notify() Attachment(char_id).send_notify()
def add_gem(self, index, gem_id): try: off_gem = self.mongo_item.equipments[str(self.equip_id)].gems[index] self.mongo_item.equipments[str(self.equip_id)].gems[index] = gem_id except IndexError: raise SanguoException( errormsg.EQUIPMENT_ADD_GEM_IN_NONE_SLOT, self.char_id, "Equipment Add Gem", "Equipment {0} gems IndexError. index {1}".format(self.equip_id, index) ) self.mongo_item.save() gem_amount = 0 for g in self.mongo_item.equipments[str(self.equip_id)].gems: if g != 0: gem_amount += 1 achievement = Achievement(self.char_id) achievement.trig(24, gem_amount) return off_gem
def add_gem(self, index, gem_id): try: off_gem = self.mongo_item.equipments[str( self.equip_id)].gems[index] self.mongo_item.equipments[str(self.equip_id)].gems[index] = gem_id except IndexError: raise SanguoException( errormsg.EQUIPMENT_ADD_GEM_IN_NONE_SLOT, self.char_id, "Equipment Add Gem", "Equipment {0} gems IndexError. index {1}".format( self.equip_id, index)) self.mongo_item.save() gem_amount = 0 for g in self.mongo_item.equipments[str(self.equip_id)].gems: if g != 0: gem_amount += 1 achievement = Achievement(self.char_id) achievement.trig(24, gem_amount) return off_gem
def get_hang_reward(self, auto_start=True): """立即保存掉落,并且返回attachment消息""" if not self.mongo_affairs.hang_city_id: raise SanguoException(errormsg.HANG_NOT_EXIST, self.char_id, "Get Hang Reward", "hang not exist") ho = self.get_hang_obj() battle_data = BATTLES[self.mongo_affairs.hang_city_id] percent = ho.passed_time / float(ho.max_time) * 100 for _pre, _add in HANG_REWARD_ADDITIONAL: if percent >= _pre: break else: _add = 1 char = Char(self.char_id) vip_level = char.mc.vip vip_add = VIP_FUNCTION[vip_level].hang_addition passed_time = int(ho.passed_time * _add * (1 + vip_add / 100.0)) reward_gold = passed_time / 15 * battle_data.normal_gold reward_gold = self.get_real_gold(reward_gold, self.mongo_affairs.logs) reward_exp = passed_time / 15 * battle_data.normal_exp if battle_data.normal_drop: # 模拟损失物品 drop_time = passed_time for log in self.mongo_affairs.logs: if log.tp == 1: drop_time -= PLUNDER_GET_DROPS_MINUTES * 60 drop_time_adjusted = max(int(passed_time * 0.25), drop_time) drops = get_drop( [int(i) for i in battle_data.normal_drop.split(',')], multi=drop_time_adjusted / 15) else: drops = make_standard_drop_from_template() drops['exp'] += reward_exp drops['gold'] += reward_gold resource = Resource(self.char_id, "Hang Reward") standard_drop = resource.add(**drops) if auto_start: # 领取奖励后自动开始 self.start_hang(self.mongo_affairs.hang_city_id, get_reward=False) achievement = Achievement(self.char_id) achievement.trig(28, ho.passed_time / 3600) achievement.trig(29, reward_exp) return standard_drop_to_attachment_protomsg(standard_drop)
def checkin(self): if self.c.has_checked: raise SanguoException( errormsg.CHECKIN_ALREADY_CHECKIN, self.char_id, "CheckIn checkin", "already checkin", ) self.c.has_checked = True day = self.c.day if self.c.day == MAX_DAYS: self.c.day = 1 else: self.c.day += 1 self.c.save() resource = Resource(self.char_id, "Daily Checkin", 'checkin reward. day {0}'.format(day)) resource_add = CHECKIN_DATA[str(day)]['package'] resource_add = get_drop_from_raw_package(resource_add) standard_drop = resource.add(**resource_add) msg = CheckInResponse() msg.ret = 0 msg.reward.MergeFrom( standard_drop_to_attachment_protomsg(standard_drop)) achievement = Achievement(self.char_id) achievement.trig(34, 1) self.send_update_notify(day) return msg
def reset(): amount = redis_client.zcard(REDIS_ARENA_KEY) logger = Logger("reset_arena_week.log") logger.write("Reset Arena Week: Start. chars amount: {0}".format(amount)) score_data = redis_client.zrevrange(REDIS_ARENA_KEY, 0, -1, withscores=True) data = [] for char_id, score in score_data: data.append((int(char_id), score, Char(int(char_id)).power)) data.sort(key=lambda item: (-item[1], -item[2])) # 发送奖励 for index, data in enumerate(data): rank = index + 1 char_id = data[0] achievement = Achievement(char_id) achievement.trig(10, rank) attachment = _get_reward_by_rank(data[1], rank) if not attachment: continue mail = Mail(char_id) mail.add(MAIL_ARENA_WEEK_REWARD_TITLE, MAIL_ARENA_WEEK_REWARD_CONTENT, attachment=attachment) logger.write("Reset Arena Week: Complete") logger.close()
def get_hang_reward(self, auto_start=True): """立即保存掉落,并且返回attachment消息""" # if not self.mongo_affairs.hang_city_id: # raise SanguoException( # errormsg.HANG_NOT_EXIST, # self.char_id, # "Get Hang Reward", # "hang not exist" # ) drops = self.get_drop() resource = Resource(self.char_id, "Hang Reward") standard_drop = resource.add(**drops) if auto_start: # 领取奖励后自动开始 self.start_hang(self.mongo_affairs.hang_city_id, get_reward=False) achievement = Achievement(self.char_id) achievement.trig(28, self.get_hang_obj().passed_time / 3600) achievement.trig(29, drops['exp']) return standard_drop_to_attachment_protomsg(standard_drop)
def pve_finish(char_id, stage_id, win, star, **kwargs): achievement = Achievement(char_id) if win: achievement.trig(7, stage_id) if star: achievement.trig(9, 1) t = Task(char_id) t.trig(1) else: achievement.trig(8, 1) # 城镇 # 自动开启第一个城镇,并且自动开始挂机 if win: if STAGES[stage_id].battle_end: affairs = Affairs(char_id) new_opened = affairs.open_city(STAGES[stage_id].battle) if STAGES[stage_id].battle == FIRST_CITY_ID and new_opened: affairs.start_hang(FIRST_CITY_ID)
def pve_finish(char_id, stage_id, win, star, **kwargs): achievement = Achievement(char_id) if win: achievement.trig(7, stage_id) if star: achievement.trig(9, 1) t = Task(char_id) t.trig(1) else: achievement.trig(8, 1) # 开启城镇 if win: if STAGES[stage_id].battle_open: affairs = Affairs(char_id) affairs.open_city(STAGES[stage_id].battle) # 判断活动 if win: ActivityStatic(char_id).trig(3001)
def gem_merge(self, _id, method): this_gem_amount = self.item.gems.get(str(_id), 0) if this_gem_amount == 0: raise SanguoException(errormsg.GEM_NOT_EXIST, self.char_id, "Gem Merge", "Gem {0} not exist".format(_id)) elif this_gem_amount < 4: raise SanguoException( errormsg.GEM_NOT_ENOUGH, self.char_id, "Gem Merge", "Gem {0} not enough. {1} < 4".format(_id, this_gem_amount)) to_id = GEMS[_id].merge_to if not to_id: raise SanguoException(errormsg.GEM_CAN_NOT_MERGE, self.char_id, "Gem Merge", "Gem {0} can not merge".format(_id)) if method == MergeGemRequest.SIMPLE: # 单次合成 to_amount = 1 else: # 自动全部合成 to_amount, _ = divmod(this_gem_amount, 4) remove_amount = to_amount * 4 self.gem_remove(_id, remove_amount) self.gem_add([(to_id, to_amount)]) to_gem_obj = GEMS[to_id] achievement = Achievement(self.char_id) achievement.trig(25, to_amount) achievement.trig(26, to_gem_obj.level) tl = TimesLogGemMerge(self.char_id) for i in range(to_amount): tl.inc() return to_id, to_amount
def gem_merge(self, _id): this_gem_amount = self.item.gems.get(str(_id), 0) if this_gem_amount == 0: raise SanguoException( errormsg.GEM_NOT_EXIST, self.char_id, "Gem Merge", "Gem {0} not exist".format(_id) ) elif this_gem_amount < 4: raise SanguoException( errormsg.GEM_NOT_ENOUGH, self.char_id, "Gem Merge", "Gem {0} not enough. {1} < 4".format(_id, this_gem_amount) ) to_id = GEMS[_id].merge_to if not to_id: raise SanguoException( errormsg.GEM_CAN_NOT_MERGE, self.char_id, "Gem Merge", "Gem {0} can not merge".format(_id) ) self.gem_remove(_id, 4) self.gem_add([(to_id, 1)]) to_gem_obj = GEMS[to_id] achievement = Achievement(self.char_id) achievement.trig(25, 1) achievement.trig(26, to_gem_obj.level) t = Task(self.char_id) t.trig(4)
def battle(self): need_sycee = 0 counter = Counter(self.char_id, 'arena') if counter.remained_value <= 0: counter = Counter(self.char_id, 'arena_buy') if counter.remained_value <= 0: char = Char(self.char_id).mc if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle", "arena no times. vip current: {0}, max {1}".format(char.vip, VIP_MAX_LEVEL) ) raise SanguoException( errormsg.ARENA_NO_TIMES_FINAL, self.char_id, "Arena Battle", "arena no times. vip reach max level {0}".format(VIP_MAX_LEVEL) ) else: need_sycee = ARENA_COST_SYCEE rival_id = self.choose_rival() if not rival_id: raise SanguoException( errormsg.ARENA_NO_RIVAL, self.char_id, "Arena Battle", "no rival." ) if need_sycee: resource = Resource(self.char_id, "Arena Battle", "battle for no free times") resource.check_and_remove(sycee=-need_sycee) counter.incr() # set battle cd redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD) msg = protomsg.Battle() b = PVP(self.char_id, rival_id, msg) b.start() t = Task(self.char_id) t.trig(2) drop = make_standard_drop_from_template() adding_score = 0 if msg.self_win: achievement = Achievement(self.char_id) achievement.trig(11, 1) # 只有打赢才设置积分 self_score = self.score rival_arena = Arena(rival_id) rival_score = rival_arena.score new_score = calculate_score(self_score, rival_score, msg.self_win) self.set_score(new_score) adding_score = new_score - self_score rival_arena.be_beaten(rival_score, self_score, not msg.self_win, self.char_id) TimesLogArenaWin(self.char_id).inc() ae = ActivityEntry(self.char_id, 50004) if ae and ae.is_valid(): drop = ae.get_additional_drop() Resource(self.char_id, "Arena Win").add(**drop) TimesLogArena(self.char_id).inc() ae = ActivityEntry(self.char_id, 40006) if ae: ae.trig() self.send_notify() drop['stuffs'].append((1001, adding_score)) return msg, drop
def _hero_step_up(char_id, hero_id, new_step, **kwargs): achievement = Achievement(char_id) achievement.trig(20, 1) if new_step == HERO_MAX_STEP: achievement.trig(21, 1)
def reset(signum): logger = Logger("reset_arena_week.log") logger.write("Start Arena Week") try: # 每周奖励 rank_data = get_rank_data(ARENA_WEEK_REWARD_LOWEST_RANK) rank_info_log = ["\n",] for index, data in enumerate(rank_data): rank = index + 1 char_id = data[0] score = data[1] _info_text = "Rank: {0}, Char: {1}, Score: {2}".format(rank, char_id, score) if score < ARENA_RANK_LINE: continue reward = _get_reward_by_rank(rank) if not reward: continue mail = Mail(char_id) mail.add( MAIL_ARENA_WEEK_REWARD_TITLE, MAIL_ARENA_WEEK_REWARD_CONTENT, attachment=json.dumps(reward)) _info_text = "{0}. Send Mail: True".format(_info_text) rank_info_log.append(_info_text) except: logger.error(traceback.format_exc()) else: logger.write("\n".join(rank_info_log)) logger.write("Done Arena Week") finally: logger.close() logger = Logger("reset_arena_week.log") logger.write("Start Arena Achievement") try: # 成就 rank_data = get_rank_data(None) for index, data in enumerate(rank_data): rank = index + 1 char_id = data[0] achievement = Achievement(char_id) achievement.trig(10, rank) except: logger.error(traceback.format_exc()) else: logger.write("Done Arena Achievement") finally: logger.close() logger = Logger("reset_arena_week.log") logger.write("Start Arena Activity 4001") try: # 开服比武奖励 _activity_anera_values = [] for _c in ACTIVITY_STATIC[4001].condition_objs: _activity_anera_values.append(_c.condition_value) LOWEST_RANK = max(_activity_anera_values) rank_data = get_rank_data(LOWEST_RANK) for index, data in enumerate(rank_data): char_id = data[0] ActivityStatic(char_id).trig(4001) except: logger.error(traceback.format_exc()) else: logger.write("Done Arena Activity 4001") finally: logger.close()
def battle(self): need_sycee = 0 counter = Counter(self.char_id, 'arena') if counter.remained_value <= 0: counter = Counter(self.char_id, 'arena_buy') if counter.remained_value <= 0: char = Char(self.char_id).mc if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle", "arena no times. vip current: {0}, max {1}".format(char.vip, VIP_MAX_LEVEL) ) raise SanguoException( errormsg.ARENA_NO_TIMES_FINAL, self.char_id, "Arena Battle", "arena no times. vip reach max level {0}".format(VIP_MAX_LEVEL) ) else: need_sycee = ARENA_COST_SYCEE rival_id = self.choose_rival() if not rival_id: raise SanguoException( errormsg.ARENA_NO_RIVAL, self.char_id, "Arena Battle", "no rival." ) if need_sycee: resource = Resource(self.char_id, "Arena Battle", "battle for no free times") resource.check_and_remove(sycee=-need_sycee) counter.incr() # set battle cd redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD) msg = protomsg.Battle() b = PVP(self.char_id, rival_id, msg) b.start() if msg.self_win: achievement = Achievement(self.char_id) achievement.trig(11, 1) self_score = self.score rival_arena = Arena(rival_id) rival_score = rival_arena.score new_score = calculate_score(self_score, rival_score, msg.self_win) self.set_score(new_score) t = Task(self.char_id) t.trig(2) self.send_notify(score=new_score) rival_arena.be_beaten(rival_score, self_score, not msg.self_win, self.char_id) return msg