def equipment_change(self, staff_id, slot_id, tp): from core.formation import Formation if tp not in [EQUIP_MOUSE, EQUIP_KEYBOARD, EQUIP_MONITOR, EQUIP_DECORATION, EQUIP_SPECIAL]: raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE")) staff = self.get_staff_object(staff_id) if not staff: raise GameException(ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) other_staff_id = staff.equipment_change(slot_id, tp) if other_staff_id: # 把装备从 这个 staff 上撤下 self.get_staff_object(other_staff_id).equipment_change("", tp) changed = [staff_id] if other_staff_id: changed.append(other_staff_id) self.send_notify(ids=changed) in_formation_staff_ids = Formation(self.server_id, self.char_id).in_formation_staffs().keys() if staff.id in in_formation_staff_ids or other_staff_id in in_formation_staff_ids: self.after_staff_change(staff_id) task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.formation.Formation' )
def after_staffs_change_for_trig_signal(self): task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.staff.StaffManger' )
def after_units_change_for_trig_signal(self): task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.unit.UnitManager' )
def record(self, sub_id=None, value=1): doc = MongoTimesLog.document() doc['_id'] = make_string_id() doc['key'] = self.make_key(sub_id=sub_id) doc['timestamp'] = arrow.utcnow().timestamp doc['value'] = value MongoTimesLog.db(self.server_id).insert_one(doc) # task trig condition_name = 'core.value_log.{0}'.format(self.__class__.__name__) task_condition_trig_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, condition_name=condition_name)
def record(self, sub_id=None, value=1): doc = MongoTimesLog.document() doc['_id'] = make_string_id() doc['key'] = self.make_key(sub_id=sub_id) doc['timestamp'] = arrow.utcnow().timestamp doc['value'] = value MongoTimesLog.db(self.server_id).insert_one(doc) # task trig condition_name = 'core.value_log.{0}'.format(self.__class__.__name__) task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name=condition_name )
def add_log(self, gold=0, diamond=0, message=""): from apps.statistics.models import Statistics Statistics.objects.create( server_id=self.server_id, char_id=self.char_id, club_gold=gold, club_diamond=diamond, message=message ) self.send_notify() condition_name = 'core.statistics.{0}'.format(self.__class__.__name__) task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name=condition_name )
def equipment_change(self, staff_id, slot_id, tp): from core.formation import Formation if tp not in [ EQUIP_MOUSE, EQUIP_KEYBOARD, EQUIP_MONITOR, EQUIP_DECORATION, EQUIP_SPECIAL ]: raise GameException( ConfigErrorMessage.get_error_id("INVALID_OPERATE")) staff = self.get_staff_object(staff_id) if not staff: raise GameException( ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) other_staff_id = staff.equipment_change(slot_id, tp) if other_staff_id: # 把装备从 这个 staff 上撤下 self.get_staff_object(other_staff_id).equipment_change("", tp) changed = [staff_id] if other_staff_id: changed.append(other_staff_id) self.send_notify(ids=changed) in_formation_staff_ids = Formation( self.server_id, self.char_id).in_formation_staffs().keys() if staff.id in in_formation_staff_ids or other_staff_id in in_formation_staff_ids: self.after_staff_change(staff_id) task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.formation.Formation')
def report(self, key, win): rival_id = key my_rank = self.get_current_rank() my_max_rank = self.get_max_rank() doc = MongoArena.db(self.server_id).find_one( {'_id': str(self.char_id)}, { 'search_index': 1, 'continue_win': 1, }) ArenaRefreshCD(self.server_id, self.char_id).clean() ArenaMatchCD(self.server_id, self.char_id, rival_id).set(GlobalConfig.value("ARENA_MATCH_CD")) config_search = ConfigArenaSearchRange.get(doc['search_index']) if win: score_changed = config_search.score_win new_search_index = doc['search_index'] + 1 continue_win = doc.get('continue_win', 0) + 1 else: score_changed = -config_search.score_lose new_search_index = doc['search_index'] - 1 continue_win = 0 if new_search_index > ConfigArenaSearchRange.MAX_INDEX: new_search_index = ConfigArenaSearchRange.MAX_INDEX if new_search_index < 0: new_search_index = 0 MongoArena.db(self.server_id).update_one({'_id': str(self.char_id)}, { '$set': { 'search_index': new_search_index, 'rival': 0, 'continue_win': continue_win, }, '$inc': { 'match_times': 1, } }) ass = ArenaScore(self.server_id, self.char_id) score_changed = ass.add_score(score_changed) new_rank = ass.rank max_rank_changed = False if new_rank > my_max_rank: my_max_rank = new_rank MongoArena.db(self.server_id).update_one( {'_id': str(self.char_id)}, {'$set': { 'max_rank': new_rank }}) max_rank_changed = True rank_changed = new_rank - my_rank rival_rank = Arena(self.server_id, rival_id).get_current_rank() if win: ValueLogArenaWinTimes(self.server_id, self.char_id).record() config_reward = ConfigArenaMatchReward.get(1) else: config_reward = ConfigArenaMatchReward.get(2) ValueLogArenaHonorPoints( self.server_id, self.char_id).record(value=config_reward.honor) ValueLogArenaMatchTimes(self.server_id, self.char_id).record() drop = config_reward.get_drop() resource_classified = ResourceClassification.classify(drop) resource_classified.add(self.server_id, self.char_id, message="Arena.report:{0}".format(win)) self.send_honor_notify() self.send_notify() arena_match_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, target_id=rival_id, target_name=ArenaClub(self.server_id, rival_id).name, my_rank=new_rank, target_rank=rival_rank, win=win, continue_win=continue_win, ) if max_rank_changed: task_condition_trig_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.arena.Arena') WinningArena(self.server_id, self.char_id).set(win) return resource_classified, score_changed, -rank_changed, my_max_rank, new_rank, ass.score
def report(self, key, win): rival_id = key my_rank = self.get_current_rank() my_max_rank = self.get_max_rank() doc = MongoArena.db(self.server_id).find_one( {'_id': str(self.char_id)}, { 'search_index': 1, 'continue_win': 1, } ) ArenaRefreshCD(self.server_id, self.char_id).clean() ArenaMatchCD(self.server_id, self.char_id, rival_id).set(GlobalConfig.value("ARENA_MATCH_CD")) config_search = ConfigArenaSearchRange.get(doc['search_index']) if win: score_changed = config_search.score_win new_search_index = doc['search_index'] + 1 continue_win = doc.get('continue_win', 0) + 1 else: score_changed = -config_search.score_lose new_search_index = doc['search_index'] - 1 continue_win = 0 if new_search_index > ConfigArenaSearchRange.MAX_INDEX: new_search_index = ConfigArenaSearchRange.MAX_INDEX if new_search_index < 0: new_search_index = 0 MongoArena.db(self.server_id).update_one( {'_id': str(self.char_id)}, { '$set': { 'search_index': new_search_index, 'rival': 0, 'continue_win': continue_win, }, '$inc': { 'match_times': 1, } } ) ass = ArenaScore(self.server_id, self.char_id) score_changed = ass.add_score(score_changed) new_rank = ass.rank max_rank_changed = False if new_rank > my_max_rank: my_max_rank = new_rank MongoArena.db(self.server_id).update_one( {'_id': str(self.char_id)}, {'$set': {'max_rank': new_rank}} ) max_rank_changed = True rank_changed = new_rank - my_rank rival_rank = Arena(self.server_id, rival_id).get_current_rank() if win: ValueLogArenaWinTimes(self.server_id, self.char_id).record() config_reward = ConfigArenaMatchReward.get(1) else: config_reward = ConfigArenaMatchReward.get(2) ValueLogArenaHonorPoints(self.server_id, self.char_id).record(value=config_reward.honor) ValueLogArenaMatchTimes(self.server_id, self.char_id).record() drop = config_reward.get_drop() resource_classified = ResourceClassification.classify(drop) resource_classified.add(self.server_id, self.char_id, message="Arena.report:{0}".format(win)) self.send_honor_notify() self.send_notify() arena_match_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, target_id=rival_id, target_name=ArenaClub(self.server_id, rival_id).name, my_rank=new_rank, target_rank=rival_rank, win=win, continue_win=continue_win, ) if max_rank_changed: task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.arena.Arena' ) WinningArena(self.server_id, self.char_id).set(win) return resource_classified, score_changed, -rank_changed, my_max_rank, new_rank, ass.score
def after_staffs_change_for_trig_signal(self): task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.staff.StaffManger')
def after_units_change_for_trig_signal(self): task_condition_trig_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.unit.UnitManager')
def equipment_level_up(self, slot_id, times=1): from core.staff import StaffManger from core.formation import Formation from core.plunder import Plunder this_slot = self.doc['slots'][slot_id] item_id = this_slot['item_id'] if get_item_type(item_id) != TYPE_EQUIPMENT: raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE")) equip = Equipment.load_from_slot_data(this_slot) level = equip.level if equip.is_special: if level >= ConfigEquipmentSpecialLevel.MAX_LEVEL: raise GameException(ConfigErrorMessage.get_error_id("EQUIPMENT_SELF_REACH_MAX_LEVEL")) station_level_limit = Plunder(self.server_id, self.char_id).get_station_level() * 2 if level >= station_level_limit: raise GameException(ConfigErrorMessage.get_error_id("EQUIPMENT_SPECIAL_REACH_MAX_LEVEL")) max_level = min(ConfigEquipmentSpecialLevel.MAX_LEVEL, station_level_limit) else: config = ConfigEquipmentNew.get(item_id) if level >= config.max_level: raise GameException(ConfigErrorMessage.get_error_id("EQUIPMENT_SELF_REACH_MAX_LEVEL")) club_level_limit = get_club_property(self.server_id, self.char_id, 'level') * 2 if level >= club_level_limit: raise GameException(ConfigErrorMessage.get_error_id("EQUIPMENT_REACH_MAX_LEVEL")) max_level = min(config.max_level, club_level_limit) can_add_level = max_level - level if times > can_add_level: times = can_add_level def do_level_up(_add): _item_needs = equip.get_level_up_needs_items(_add) resource_classified = ResourceClassification.classify(_item_needs) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove(self.server_id, self.char_id, message="Bag.equipment_level_up:{0}".format(item_id)) old_level = level error_code = 0 for i in range(times, 0, -1): try: do_level_up(i) except GameException as e: error_code = e.error_id else: level += i break if level > old_level: self.doc['slots'][slot_id]['level'] = level MongoBag.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'slots.{0}.level'.format(slot_id): level }} ) ValueLogEquipmentLevelUpTimes(self.server_id, self.char_id).record(value=level - old_level) self.send_notify(slot_ids=[slot_id]) sm = StaffManger(self.server_id, self.char_id) staff_id = sm.find_staff_id_with_equip(slot_id) if staff_id: s_obj = sm.get_staff_object(staff_id) s_obj.calculate() s_obj.make_cache() sm.send_notify(ids=[staff_id]) fm = Formation(self.server_id, self.char_id) if staff_id in fm.in_formation_staffs(): Club(self.server_id, self.char_id).send_notify() task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.formation.Formation' ) return error_code, level != old_level, Equipment.load_from_slot_data(self.doc['slots'][slot_id])
def report(self, key, star): sweep_end_at = self.doc.get('sweep_end_at', 0) if sweep_end_at: raise GameException(ConfigErrorMessage.get_error_id("TOWER_IN_SWEEP_CANNOT_OPERATE")) if star not in [0, 1, 2, 3]: raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE")) level = int(key) config = ConfigTowerLevel.get(level) if not config: raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE")) update_levels = [level] self.doc['current_star'] += star updater = { 'current_star': self.doc['current_star'] } if star == 0: # 输了 # NOTE 坑 self.doc['levels'][str(level)] = -1 updater['levels.{0}'.format(level)] = -1 updater['turntable'] = {} all_list = [] goods = [] resource_classified = ResourceClassification.classify([]) else: self.doc['levels'][str(level)] = star updater['levels.{0}'.format(level)] = star # 开启下一关 if level < ConfigTowerLevel.MAX_LEVEL: next_level = level + 1 self.doc['levels'][str(next_level)] = 0 update_levels.append(next_level) updater['levels.{0}'.format(next_level)] = 0 # 记录连续最大三星 if star == 3 and level == self.doc['max_star_level'] + 1: self.doc['max_star_level'] = level updater['max_star_level'] = level # 记录最高层 if level > self.get_history_max_level(): self.doc['history_max_level'] = level updater['history_max_level'] = level # 转盘信息 turntable = config.get_turntable() if turntable: all_list = [] for _, v in turntable.iteritems(): all_list.extend(v) random.shuffle(all_list) turntable['all_list'] = all_list updater['turntable'] = turntable else: all_list = [] updater['turntable'] = {} # 是否有商品可购买 goods = config.get_sale_goods() if goods: self.doc['goods'].append([goods[0], 0]) self.doc['goods'].append([goods[1], 0]) updater['goods'] = self.doc['goods'] resource_classified = ResourceClassification.classify(config.get_star_reward(star)) resource_classified.add(self.server_id, self.char_id, message="Tower.report:{0},{1}".format(level, star)) ValueLogTowerWinTimes(self.server_id, self.char_id).record() self.set_today_max_star() total_star = self.get_total_current_star() if total_star > self.doc['history_max_star']: self.doc['history_max_star'] = total_star updater['history_max_star'] = total_star MongoTower.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': updater} ) self.send_notify(act=ACT_UPDATE, levels=update_levels) if goods: self.send_goods_notify() if 'history_max_level' in updater: task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.tower.Tower' ) return resource_classified, self.doc['current_star'], all_list, bool(goods)
def update(self, **kwargs): exp = kwargs.get('exp', 0) gold = kwargs.get('gold', 0) diamond = kwargs.get('diamond', 0) crystal = kwargs.get('crystal', 0) gas = kwargs.get('gas', 0) renown = kwargs.get('renown', 0) message = kwargs.get('message', "") self.gold += gold self.diamond += diamond self.exp += exp self.crystal += crystal self.gas += gas self.renown += renown if self.gold > MAX_INT: self.gold = MAX_INT if self.diamond > MAX_INT: self.diamond = MAX_INT if self.exp > MAX_INT: self.exp = MAX_INT if self.crystal > MAX_INT: self.crystal = MAX_INT if self.gas > MAX_INT: self.gas = MAX_INT if self.renown > MAX_INT: self.renown = MAX_INT if self.gold < 0: raise GameException(ConfigErrorMessage.get_error_id("GOLD_NOT_ENOUGH")) if self.diamond < 0: raise GameException(ConfigErrorMessage.get_error_id("DIAMOND_NOT_ENOUGH")) if self.crystal < 0: raise GameException(ConfigErrorMessage.get_error_id("CRYSTAL_NOT_ENOUGH")) if self.gas < 0: raise GameException(ConfigErrorMessage.get_error_id("GAS_NOT_ENOUGH")) if self.renown < 0: raise GameException(ConfigErrorMessage.get_error_id("RENOWN_NOT_ENOUGH")) # update level_changed = False while True: need_exp = ConfigClubLevel.get(self.level).exp if self.level >= ConfigClubLevel.MAX_LEVEL: if self.exp >= need_exp: self.exp = need_exp break if self.exp < need_exp: break self.exp -= need_exp self.level += 1 level_changed = True MongoCharacter.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'level': self.level, 'exp': self.exp, 'gold': self.gold, 'diamond': self.diamond, 'crystal': self.crystal, 'gas': self.gas, 'renown': self.renown }} ) if level_changed: club_level_up_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, new_level=self.level ) task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.club._ClubProperty' ) self.send_notify() if gold or diamond: FinanceStatistics(self.server_id, self.char_id).add_log( gold=gold, diamond=diamond, message=message )
def report(self, key, star): challenge_id = int(key) config = ConfigChallengeMatch.get(challenge_id) if not config: raise GameException(ConfigErrorMessage.get_error_id("CHALLENGE_NOT_EXIST")) ValueLogChallengeMatchTimes(self.server_id, self.char_id).record(sub_id=challenge_id) if star <= 0: self.send_challenge_notify(ids=[challenge_id]) return None ValueLogAllChallengeWinTimes(self.server_id, self.char_id).record() Energy(self.server_id, self.char_id).remove(config.energy) projection = { 'chapters': 1, 'challenge_star.{0}'.format(challenge_id): 1, 'challenge_drop.{0}'.format(challenge_id): 1, } for i in config.next: projection['challenge_star.{0}'.format(i)] = 1 doc = MongoChallenge.db(self.server_id).find_one( {'_id': self.char_id}, projection ) updated_chapter_ids = [] updated_challenge_ids = [] old_star = doc['challenge_star'][str(challenge_id)] if star > old_star: this_chapter_star = doc['chapters'].get(str(config.chapter), {}).get('star', 0) this_chapter_star += star - old_star MongoChallenge.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'challenge_star.{0}'.format(challenge_id): star, 'chapters.{0}.star'.format(config.chapter): this_chapter_star }} ) updated_chapter_ids.append(config.chapter) updated_challenge_ids.append(challenge_id) open_new_challenge = False # try open challenge, chapter for i in config.next: if str(i) in doc['challenge_star']: continue open_new_challenge = True updated_challenge_ids.append(i) MongoChallenge.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'challenge_star.{0}'.format(i): 0, }} ) chapter_id = ConfigChallengeMatch.get(i).chapter if str(chapter_id) in doc['chapters']: continue updated_chapter_ids.append(chapter_id) MongoChallenge.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'chapters.{0}'.format(chapter_id): { 'star': 0, 'rewards': [] } }} ) if updated_chapter_ids: self.send_chapter_notify(ids=updated_chapter_ids) if updated_challenge_ids: self.send_challenge_notify(ids=updated_challenge_ids) # drop drop_times = doc['challenge_drop'].get(str(challenge_id), {}) drop = config.get_drop(drop_times) resource_classified = ResourceClassification.classify(drop) resource_classified.add(self.server_id, self.char_id, message="Challenge.report:{0},{1}".format(key, star)) MongoChallenge.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'challenge_drop.{0}'.format(challenge_id): drop_times }} ) challenge_match_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, challenge_id=challenge_id, star=star ) if open_new_challenge: task_condition_trig_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, condition_name='core.challenge.Challenge' ) return resource_classified