def start(self, dungeon_id, formation_slots=None): grade_conf = ConfigDungeonGrade.get(dungeon_id) if not grade_conf: raise GameException(ConfigErrorMessage.get_error_id("DUNGEON_NOT_EXIST")) map_name = ConfigDungeon.get(grade_conf.belong).map_name club_level = get_club_property(self.server_id, self.char_id, 'level') if grade_conf.need_level > club_level: raise GameException(ConfigErrorMessage.get_error_id("DUNGEON_CLUB_LEVEL_NOT_ENOUGH")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) Energy(self.server_id, self.char_id).check(ConfigDungeon.get(grade_conf.belong).cost) ri = TimesInfo(self.server_id, self.char_id, grade_conf.belong) if not ri.remained_match_times: # 购买 self.buy_times(grade_conf.belong) club_one = Club(self.server_id, self.char_id) club_two = ConfigNPCFormation.get(grade_conf.npc) msg = ClubMatch(club_one, club_two, 6, f.get_skill_sequence(), {}).start() msg.key = str(dungeon_id) msg.map_name = map_name return msg
def equip_sell(self, ids): if not isinstance(ids, (set, list, tuple)): ids = [ids] f = Formation(self.char_id) ids = set(ids) for _id in ids: if not self.has_equip(_id): raise SanguoException( errormsg.EQUIPMENT_NOT_EXIST, self.char_id, "Equipment Sell", "Equipment {0} NOT exist".format(_id) ) if f.find_socket_by_equip(_id): raise SanguoException( errormsg.EQUIPMENT_CANNOT_SELL_FORMATION, self.char_id, "Equipment Sell", "Equipment {0} in Formation, Can not sell".format(_id) ) gold = 0 for _id in ids: e = Equipment(self.char_id, _id, self.item) gold += e.sell_gold() resource = Resource(self.char_id, "Equipment Sell", "equipments {0}".format(ids)) resource.check_and_remove(equipments=list(ids)) resource.add(gold=gold)
def trig(self, char_level, stage_id=None): try: s = MongoStage.objects.get(id=self.char_id) passed_stages = s.stages.keys() except DoesNotExist: passed_stages = [] passed_stages.append('0') if stage_id: passed_stages.append(str(stage_id)) opened_funcs = [] for func_id in self.mf.freeze[:]: try: this_func = FUNCTION_DEFINE[func_id] except KeyError: self.mf.freeze.remove(func_id) continue if char_level >= this_func.char_level and str( this_func.stage_id) in passed_stages: # OPEN self.mf.freeze.remove(func_id) opened_funcs.append(func_id) self.mf.save() f = Formation(self.char_id) for of in opened_funcs[:]: if of in FUNC_SOCKET_AMOUNT_TABLE: opened = f.open_socket(FUNC_SOCKET_AMOUNT_TABLE[of]) if not opened: opened_funcs.remove(of) return opened_funcs
def start(self, dungeon_id, formation_slots=None): grade_conf = ConfigDungeonGrade.get(dungeon_id) if not grade_conf: raise GameException( ConfigErrorMessage.get_error_id("DUNGEON_NOT_EXIST")) map_name = ConfigDungeon.get(grade_conf.belong).map_name club_level = get_club_property(self.server_id, self.char_id, 'level') if grade_conf.need_level > club_level: raise GameException( ConfigErrorMessage.get_error_id( "DUNGEON_CLUB_LEVEL_NOT_ENOUGH")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) Energy(self.server_id, self.char_id).check(ConfigDungeon.get(grade_conf.belong).cost) ri = TimesInfo(self.server_id, self.char_id, grade_conf.belong) if not ri.remained_match_times: # 购买 self.buy_times(grade_conf.belong) club_one = Club(self.server_id, self.char_id) club_two = ConfigNPCFormation.get(grade_conf.npc) msg = ClubMatch(club_one, club_two, 6, f.get_skill_sequence(), {}).start() msg.key = str(dungeon_id) msg.map_name = map_name return msg
def help(self, friend_id, building_id): friend_id = int(friend_id) if not FriendManager(self.server_id, self.char_id).check_friend_exist(friend_id): raise GameException( ConfigErrorMessage.get_error_id("FRIEND_NOT_OK")) if not self.get_remained_help_times(): raise GameException( ConfigErrorMessage.get_error_id( "TERRITORY_NO_HELP_FRIEND_TIMES")) if not TerritoryFriend(self.server_id, friend_id).get_remained_got_help_times(): raise GameException( ConfigErrorMessage.get_error_id("TERRITORY_NO_GOT_HELP_TIMES")) t = Territory(self.server_id, friend_id) building = t.get_building_object(building_id, slots_ids=[]) event_id = building.event_id if not event_id: raise GameException( ConfigErrorMessage.get_error_id("TERRITORY_BUILDING_NO_EVENT")) MongoTerritory.db(self.server_id).update_one( {'_id': friend_id}, {'$set': { 'buildings.{0}.event_id'.format(building_id): 0 }}) config = ConfigTerritoryEvent.get(event_id) if not config.npc: resource_classified = ResourceClassification.classify( config.reward_win) resource_classified.add(self.server_id, self.char_id, message="TerritoryFriend.help") # NOTE: 战斗要等到结算的时候再记录次数 ValueLogTerritoryHelpFriendTimes(self.server_id, self.char_id).record() self.send_remained_times_notify() Territory(self.server_id, friend_id).got_help(self.char_id, building_id, config.target_exp) return None, resource_classified npc_club = ConfigNPCFormation.get(config.npc) my_club = Club(self.server_id, self.char_id) f = Formation(self.server_id, self.char_id) match = ClubMatch(my_club, npc_club, 6, f.get_skill_sequence(), {}) msg = match.start() msg.key = "{0}:{1}:{2}".format(friend_id, building_id, event_id) msg.map_name = GlobalConfig.value_string("MATCH_MAP_TERRITORY_FRIEND") return msg, None
def break_hero(char_id, _id): # 主动将武将转为卡魂 try: h = char_heros_dict(char_id)[_id] except KeyError: raise SanguoException(errormsg.HERO_NOT_EXSIT, char_id, "Hero Break", "hero {0} not exist".format(_id)) formation = Formation(char_id) if _id in formation.in_formation_hero_ids(): raise SanguoException( errormsg.HERO_CANNOT_BREAK_IN_FORMATION, char_id, "Hero Break", "hero {0} in formation, cannot break".format(_id)) oid = h.oid quality = HEROS[oid].quality souls_amount = SAVE_HERO_TO_SOUL_TABLE[quality] MongoHero.objects.get(id=_id).delete() hero_del_signal.send(sender=None, char_id=char_id, hero_id=_id, hero_oid=oid) HeroSoul(char_id).add_soul([(oid, souls_amount)])
def trig(self, char_level, stage_id=None): try: s = MongoStage.objects.get(id=self.char_id) passed_stages = s.stages.keys() except DoesNotExist: passed_stages = [] passed_stages.append('0') if stage_id: passed_stages.append(str(stage_id)) opened_funcs = [] for func_id in self.mf.freeze[:]: try: this_func = FUNCTION_DEFINE[func_id] except KeyError: self.mf.freeze.remove(func_id) continue if char_level >= this_func.char_level and str(this_func.stage_id) in passed_stages: # OPEN self.mf.freeze.remove(func_id) opened_funcs.append(func_id) self.mf.save() f = Formation(self.char_id) for of in opened_funcs[:]: if of in FUNC_SOCKET_AMOUNT_TABLE: opened = f.open_socket(FUNC_SOCKET_AMOUNT_TABLE[of]) if not opened: opened_funcs.remove(of) return opened_funcs
def match(self, friend_id): friend_id = int(friend_id) if not self.check_friend_exist(friend_id): raise GameException(ConfigErrorMessage.get_error_id("FRIEND_NOT_OK")) club_one = Club(self.server_id, self.char_id) club_two = Club(self.server_id, friend_id) f_one = Formation(self.server_id, self.char_id) f_two = Formation(self.server_id, self.char_id) match = ClubMatch(club_one, club_two, 6, f_one.get_skill_sequence(), f_two.get_skill_sequence()) msg = match.start() msg.key = "" msg.map_name = GlobalConfig.value_string("MATCH_MAP_FRIEND") friend_match_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, target_id=friend_id, win=msg.club_one_win ) return msg
def create_character_infomation_message(char_id): from core.character import Char from core.formation import Formation from core.hero import char_heros_dict msg = CharacterInfomation() char = Char(char_id) heros = char_heros_dict(char_id) f = Formation(char_id) msg.id = char.mc.id msg.name = char.mc.name msg.level = char.mc.level msg.vip = char.mc.vip msg.power = char.power msg.leader = f.get_leader_oid() for hid in f.in_formation_hero_ids(): msg_hero = msg.formation.add() if hid == 0: msg_hero.id = 0 msg_hero.oid = 0 msg_hero.step = 0 else: h = heros[hid] msg_hero.id = h.id msg_hero.oid = h.oid msg_hero.step = h.step return msg
def break_hero(char_id, _id): # 主动将武将转为卡魂 try: h = char_heros_dict(char_id)[_id] except KeyError: raise SanguoException( errormsg.HERO_NOT_EXSIT, char_id, "Hero Break", "hero {0} not exist".format(_id) ) formation = Formation(char_id) if _id in formation.in_formation_hero_ids(): raise SanguoException( errormsg.HERO_CANNOT_BREAK_IN_FORMATION, char_id, "Hero Break", "hero {0} in formation, cannot break".format(_id) ) oid = h.oid quality = HEROS[oid].quality souls_amount = SAVE_HERO_TO_SOUL_TABLE[quality] MongoHero.objects.get(id=_id).delete() hero_del_signal.send( sender=None, char_id=char_id, hero_id=_id, hero_oid=oid ) HeroSoul(char_id).add_soul([(oid, souls_amount)])
def start(self, challenge_id, formation_slots=None): config = ConfigChallengeMatch.get(challenge_id) if not config: raise GameException(ConfigErrorMessage.get_error_id("CHALLENGE_NOT_EXIST")) if config.condition_challenge: doc = MongoChallenge.db(self.server_id).find_one( {'_id': self.char_id}, {'challenge_star.{0}'.format(config.condition_challenge): 1} ) if str(config.condition_challenge) not in doc['challenge_star']: raise GameException(ConfigErrorMessage.get_error_id("CHALLENGE_NOT_OPEN")) rt = RemainedTimes(self.server_id, self.char_id, challenge_id) if not rt.remained_match_times: raise GameException(ConfigErrorMessage.get_error_id("CHALLENGE_WITHOUT_TIMES")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) Energy(self.server_id, self.char_id).check(config.energy) club_one = Club(self.server_id, self.char_id) club_two = ChallengeNPCClub(challenge_id) match = ClubMatch(club_one, club_two, 6, f.get_skill_sequence(), {}) msg = match.start() msg.key = str(challenge_id) msg.map_name = config.map_name return msg
def load_formation_staffs(self): # NOTE: 这段代码其实是从 Club.force_load_staffs 抄来的 from core.formation import Formation from core.unit import UnitManager from core.talent import TalentManager from core.collection import Collection from core.party import Party from core.inspire import Inspire self.formation_staffs = [] """:type: list[core.staff.Staff]""" sm = StaffManger(self.server_id, self.char_id) fm = Formation(self.server_id, self.char_id) um = UnitManager(self.server_id, self.char_id) ins = Inspire(self.server_id, self.char_id) staffs = sm.get_staffs_data() in_formation_staffs = self.in_formation_staffs() for k, v in in_formation_staffs.iteritems(): obj = Staff(self.server_id, self.char_id, k, staffs[k]) self.formation_staffs.append(obj) obj.policy = in_formation_staffs[k]['policy'] obj.formation_position = in_formation_staffs[k]['position'] unit_id = in_formation_staffs[k]['unit_id'] if unit_id: obj.set_unit(um.get_unit_object(unit_id)) club = Club(self.server_id, self.char_id, load_staffs=False) club.formation_staffs = self.formation_staffs working_staff_oids = self.working_staff_oids() for obj in self.formation_staffs: obj.check_qianban(working_staff_oids) obj.add_self_talent_effect(club) talent_effects_1 = TalentManager(self.server_id, self.char_id).get_talent_effects() talent_effects_2 = Collection(self.server_id, self.char_id).get_talent_effects() talent_effects_3 = fm.get_talent_effects() talent_effects_4 = Party(self.server_id, self.char_id).get_talent_effects() club.add_talent_effects(talent_effects_1) club.add_talent_effects(talent_effects_2) club.add_talent_effects(talent_effects_3) club.add_talent_effects(talent_effects_4) config_inspire_level_addition, config_inspire_step_addition = ins.get_addition_config( ) for obj in self.formation_staffs: obj.config_inspire_level_addition = config_inspire_level_addition obj.config_inspire_step_addition = config_inspire_step_addition obj.calculate()
def _equip_changed(char_id, equip_obj, **kwargs): equip_id = equip_obj.equip_id f = Formation(char_id) socket = f.find_socket_by_equip(equip_id) if socket: socket_changed_signal.send(sender=None, socket_obj=socket)
def set_formation(request): req = request._proto char_id = request._char_id f = Formation(char_id) socket_ids = [int(s) for s in req.socket_ids] f.save_formation(socket_ids)
def create(cls, server_id, char_id, club_name, club_flag): from core.staff import StaffManger from core.formation import Formation from core.mail import MailManager from apps.config.models import Mail as ModelMail doc = MongoCharacter.document() doc['_id'] = char_id doc['create_at'] = arrow.utcnow().timestamp doc['name'] = club_name doc['flag'] = club_flag doc['gold'] = CHAR_INIT_GOLD doc['diamond'] = CHAR_INIT_DIAMOND doc['crystal'] = CHAR_INIT_CRYSTAL doc['gas'] = CHAR_INIT_GAS sm = StaffManger(server_id, char_id) formation_init_data = [] for staff_id, unit_id in CHAR_INIT_STAFFS: uid = sm.add(staff_id, send_notify=False, trig_signal=False) formation_init_data.append((uid, unit_id)) fm = Formation(server_id, char_id) fm.initialize(formation_init_data) MongoCharacter.db(server_id).insert_one(doc) # add welfare mail start_time = get_start_time_of_today() condition = Q(send_at__gte=start_time.format("YYYY-MM-DD HH:mm:ssZ")) &\ Q(send_at__lte=arrow.utcnow().format("YYYY-MM-DD HH:mm:ssZ")) mails = ModelMail.objects.filter(condition) m = MailManager(server_id, char_id) for m_obj in mails: if not m_obj.welfare: continue ok = False if m_obj.condition_type == 1: ok = True elif m_obj.condition_type == 2 and server_id in m_obj.get_parsed_condition_value(): ok = True elif m_obj.condition_type == 3 and server_id not in m_obj.get_parsed_condition_value(): ok = True if not ok: continue if m_obj.items: rc = ResourceClassification.classify(m_obj.get_parsed_items()) attachment = rc.to_json() else: attachment = "" m.add(m_obj.title, m_obj.content, attachment=attachment, send_notify=False)
def _add_equip_attrs(self): from core.item import Equipment f = Formation(self.char_id) socket = f.find_socket_by_hero(self.id) if not socket: return # 先把装备数值加到人物上 equipments = [] for x in ['weapon', 'armor', 'jewelry']: equip_id = getattr(socket, x) if equip_id: equip = Equipment(self.char_id, equip_id) self.attack += equip.attack self.defense += equip.defense self.hp += equip.hp equipments.append(equip) # 然后加成人物的专属装备 additions = {} special_equipments = self.model_hero.special_equipments if special_equipments: for equip in equipments: _cls = equip.equip.cls if _cls not in special_equipments: continue _tp = equip.equip.tp additions[_tp] = additions.get(_tp, 0) + special_equipments[_cls] for _tp, _add_percent in additions.items(): if _tp == 1: # attack self.attack *= (1 + _add_percent / 100.0) elif _tp == 2: # defense self.defense *= (1 + _add_percent / 100.0) else: # hp self.hp *= (1 + _add_percent / 100.0) self.hp = int(self.hp) # 最后再把宝石加上 for equip in equipments: for k, v in equip.gem_attributes.iteritems(): value = getattr(self, k) setattr(self, k, value + v) # 马 if socket.horse: horse = Horse(self.char_id).mongo_horse.horses[str(socket.horse)] self.attack += horse.attack self.defense += horse.defense self.hp += horse.hp self.crit += int(HORSE[horse.oid].crit / 10)
def power(self): f = Formation(self.id) hero_ids = f.in_formation_hero_ids() p = 0 for hid in hero_ids: if hid == 0: continue h = Hero.cache_obj(hid) p += h.power return p
def send_update_notify(self, _id, h): hobj = OneHorse(int(_id), h.oid, h.attack, h.defense, h.hp) msg = HorsesUpdateNotify() msg.horse.MergeFrom(hobj.make_msg()) publish_to_char(self.char_id, pack_msg(msg)) f = Formation(self.char_id) socket = f.find_socket_by_horse(int(_id)) if socket: socket_changed_signal.send(sender=None, socket_obj=socket)
def load_formation_staffs(self): # NOTE: 这段代码其实是从 Club.force_load_staffs 抄来的 from core.formation import Formation from core.unit import UnitManager from core.talent import TalentManager from core.collection import Collection from core.party import Party from core.inspire import Inspire self.formation_staffs = [] """:type: list[core.staff.Staff]""" sm = StaffManger(self.server_id, self.char_id) fm = Formation(self.server_id, self.char_id) um = UnitManager(self.server_id, self.char_id) ins = Inspire(self.server_id, self.char_id) staffs = sm.get_staffs_data() in_formation_staffs = self.in_formation_staffs() for k, v in in_formation_staffs.iteritems(): obj = Staff(self.server_id, self.char_id, k, staffs[k]) self.formation_staffs.append(obj) obj.policy = in_formation_staffs[k]['policy'] obj.formation_position = in_formation_staffs[k]['position'] unit_id = in_formation_staffs[k]['unit_id'] if unit_id: obj.set_unit(um.get_unit_object(unit_id)) club = Club(self.server_id, self.char_id, load_staffs=False) club.formation_staffs = self.formation_staffs working_staff_oids = self.working_staff_oids() for obj in self.formation_staffs: obj.check_qianban(working_staff_oids) obj.add_self_talent_effect(club) talent_effects_1 = TalentManager(self.server_id, self.char_id).get_talent_effects() talent_effects_2 = Collection(self.server_id, self.char_id).get_talent_effects() talent_effects_3 = fm.get_talent_effects() talent_effects_4 = Party(self.server_id, self.char_id).get_talent_effects() club.add_talent_effects(talent_effects_1) club.add_talent_effects(talent_effects_2) club.add_talent_effects(talent_effects_3) club.add_talent_effects(talent_effects_4) config_inspire_level_addition, config_inspire_step_addition = ins.get_addition_config() for obj in self.formation_staffs: obj.config_inspire_level_addition = config_inspire_level_addition obj.config_inspire_step_addition = config_inspire_step_addition obj.calculate()
def load_units(self): from core.formation import Formation units = {} """:type: dict[int, Unit]""" doc = MongoUnit.db(self.server_id).find_one({'_id': self.char_id}) for _id, _data in doc['units'].iteritems(): u = Unit(self.server_id, self.char_id, int(_id), _data) u.calculate() u.make_cache() units[int(_id)] = u race = { 1: {'level': 0, 'step': 0}, 2: {'level': 0, 'step': 0}, 3: {'level': 0, 'step': 0}, } for _, u in units.iteritems(): race[u.config.race]['level'] += u.level race[u.config.race]['step'] += u.step additions = { 1: UnitAddition(), 2: UnitAddition(), 3: UnitAddition(), } for k, v in additions.iteritems(): _level_addition = ConfigUnitAddition.get_level_addition(k, race[k]['level']) _step_addition = ConfigUnitAddition.get_step_addition(k, race[k]['step']) if _level_addition: for attr in UnitAddition.__slots__: v.add_property(attr, getattr(_level_addition, attr)) if _step_addition: for attr in UnitAddition.__slots__: v.add_property(attr, getattr(_step_addition, attr)) # 只作用于上阵兵种 in_format_staffs = Formation(self.server_id, self.char_id).in_formation_staffs() for k, v in in_format_staffs.iteritems(): if not v['unit_id']: continue u = units[v['unit_id']] _add = additions[u.config.race] for attr in UnitAddition.__slots__: _old_value = getattr(u, attr) _new_value = _old_value + getattr(_add, attr) setattr(u, attr, _new_value) u.make_cache()
def load_staffs(self): from core.staff import StaffManger from core.formation import Formation self.formation_staffs = [] sm = StaffManger(self.server_id, self.char_id) fm = Formation(self.server_id, self.char_id) for k in fm.in_formation_staffs(): self.formation_staffs.append(sm.get_staff_object(k))
def check_sell(self, horse_id): if not self.has_horse(horse_id): raise SanguoException(errormsg.HORSE_NOT_EXIST, self.char_id, "Horse Check Sell", "horse {0} not exist".format(horse_id)) f = Formation(self.char_id) if f.find_socket_by_horse(horse_id): raise SanguoException( errormsg.HORSE_CAN_NOT_SELL_IN_FORMATION, self.char_id, "Horse Check Sell", "horse {0} in formation, can not sell".format(horse_id))
def set_unit(request): server_id = request._game_session.server_id char_id = request._game_session.char_id slot_id = request._proto.slot_id unit_id = request._proto.unit_id f = Formation(server_id, char_id) f.set_unit(slot_id, unit_id) response = FormationSetUnitResponse() response.ret = 0 return ProtobufResponse(response)
def set_staff(request): server_id = request._game_session.server_id char_id = request._game_session.char_id slot_id = request._proto.slot_id staff_id = request._proto.staff_id f = Formation(server_id, char_id) f.set_staff(slot_id, staff_id) response = FormationSetStaffResponse() response.ret = 0 return ProtobufResponse(response)
def send_update_notify(self, _id, h): hobj = OneHorse(int(_id), h.oid, h.attack, h.defense, h.hp) msg = HorsesUpdateNotify() msg.horse.MergeFrom(hobj.make_msg()) publish_to_char(self.char_id, pack_msg(msg)) f = Formation(self.char_id) socket = f.find_socket_by_horse(int(_id)) if socket: socket_changed_signal.send( sender=None, socket_obj=socket )
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 help(self, friend_id, building_id): friend_id = int(friend_id) if not FriendManager(self.server_id, self.char_id).check_friend_exist(friend_id): raise GameException(ConfigErrorMessage.get_error_id("FRIEND_NOT_OK")) if not self.get_remained_help_times(): raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_NO_HELP_FRIEND_TIMES")) if not TerritoryFriend(self.server_id, friend_id).get_remained_got_help_times(): raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_NO_GOT_HELP_TIMES")) t = Territory(self.server_id, friend_id) building = t.get_building_object(building_id, slots_ids=[]) event_id = building.event_id if not event_id: raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_BUILDING_NO_EVENT")) MongoTerritory.db(self.server_id).update_one( {'_id': friend_id}, {'$set': { 'buildings.{0}.event_id'.format(building_id): 0 }} ) config = ConfigTerritoryEvent.get(event_id) if not config.npc: resource_classified = ResourceClassification.classify(config.reward_win) resource_classified.add(self.server_id, self.char_id, message="TerritoryFriend.help") # NOTE: 战斗要等到结算的时候再记录次数 ValueLogTerritoryHelpFriendTimes(self.server_id, self.char_id).record() self.send_remained_times_notify() Territory(self.server_id, friend_id).got_help(self.char_id, building_id, config.target_exp) return None, resource_classified npc_club = ConfigNPCFormation.get(config.npc) my_club = Club(self.server_id, self.char_id) f = Formation(self.server_id, self.char_id) match = ClubMatch(my_club, npc_club, 6, f.get_skill_sequence(), {}) msg = match.start() msg.key = "{0}:{1}:{2}".format(friend_id, building_id, event_id) msg.map_name = GlobalConfig.value_string("MATCH_MAP_TERRITORY_FRIEND") return msg, None
def level_up(self, staff_id, up_level): from core.formation import Formation staff = self.get_staff_object(staff_id) if not staff: raise GameException( ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'exp_pool': 1}) exp_pool = doc.get('exp_pool', 0) remained_exp_pool, _level_up_amount = staff.level_up( exp_pool, up_level) MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'exp_pool': remained_exp_pool }}) self.send_notify(ids=[staff_id]) if _level_up_amount: ValueLogStaffLevelUpTimes( self.server_id, self.char_id).record(value=_level_up_amount) in_formation_staff_ids = Formation( self.server_id, self.char_id).in_formation_staffs().keys() if staff.id in in_formation_staff_ids: self.after_staff_change(staff_id) self.after_staffs_change_for_trig_signal()
def equip_check_sell(self, ids): if not isinstance(ids, (set, list, tuple)): ids = [ids] f = Formation(self.char_id) ids = set(ids) for _id in ids: if not self.has_equip(_id): raise SanguoException(errormsg.EQUIPMENT_NOT_EXIST, self.char_id, "Equipment Check Sell", "Equipment {0} NOT exist".format(_id)) if f.find_socket_by_equip(_id): raise SanguoException( errormsg.EQUIPMENT_CANNOT_SELL_FORMATION, self.char_id, "Equipment Check Sell", "Equipment {0} in Formation, Can not sell".format(_id))
def check_sell(self, horse_id): if not self.has_horse(horse_id): raise SanguoException( errormsg.HORSE_NOT_EXIST, self.char_id, "Horse Check Sell", "horse {0} not exist".format(horse_id) ) f = Formation(self.char_id) if f.find_socket_by_horse(horse_id): raise SanguoException( errormsg.HORSE_CAN_NOT_SELL_IN_FORMATION, self.char_id, "Horse Check Sell", "horse {0} in formation, can not sell".format(horse_id) )
def challenge_match_handler(server_id, char_id, challenge_id, star, **kwargs): TaskMain(server_id, char_id).trig(challenge_id) if star > 0: Formation(server_id, char_id).try_open_slots() Inspire(server_id, char_id).try_open_slots() TaskDaily(server_id, char_id).try_open() Plunder(server_id, char_id).try_initialize() ActivityChallenge(server_id, char_id).trig_notify(challenge_id)
def destroy(self, staff_id, tp): from core.club import Club from core.formation import Formation self._destroy_check(staff_id) staff = self.get_staff_object(staff_id) if not staff: raise GameException( ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) if tp == 0: # 普通分解 items = staff.get_cost_items(70) crystal = ConfigStaffNew.get(staff.oid).crystal items.append((money_text_to_item_id('crystal'), crystal)) else: if staff.is_initial_state(): raise GameException( ConfigErrorMessage.get_error_id( "STAFF_CANNOT_DESTROY_INITIAL_STATE")) # TODO diamond count resource_classified = ResourceClassification.classify([ (money_text_to_item_id('diamond'), 50) ]) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove( self.server_id, self.char_id, message="StaffManger.destroy:{0},{1}".format(staff.oid, tp)) items = staff.get_cost_items(100) resource_classified = ResourceClassification.classify(items) resource_classified.add(self.server_id, self.char_id, message="StaffManger.destroy:{0},{1}".format( staff.oid, tp)) if tp == 0: self.remove(staff_id) else: staff.reset() in_formation_staff_ids = Formation( self.server_id, self.char_id).in_formation_staffs().keys() if staff.id in in_formation_staff_ids: Club(self.server_id, self.char_id, load_staffs=False).force_load_staffs(send_notify=True) else: staff.calculate() staff.make_cache() self.send_notify(ids=[staff_id]) self.after_staffs_change_for_trig_signal() return resource_classified
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 use(request): server_id = request._game_session.server_id char_id = request._game_session.char_id fid = request._proto.id Formation(server_id, char_id).use_formation(fid) response = FormationUseResponse() response.ret = 0 return ProtobufResponse(response)
def level_up(request): server_id = request._game_session.server_id char_id = request._game_session.char_id fid = request._proto.id Formation(server_id, char_id).levelup_formation(fid) response = FormationLevelUpResponse() response.ret = 0 return ProtobufResponse(response)
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 match(self, friend_id): friend_id = int(friend_id) if not self.check_friend_exist(friend_id): raise GameException( ConfigErrorMessage.get_error_id("FRIEND_NOT_OK")) club_one = Club(self.server_id, self.char_id) club_two = Club(self.server_id, friend_id) f_one = Formation(self.server_id, self.char_id) f_two = Formation(self.server_id, self.char_id) match = ClubMatch(club_one, club_two, 6, f_one.get_skill_sequence(), f_two.get_skill_sequence()) msg = match.start() msg.key = "" msg.map_name = GlobalConfig.value_string("MATCH_MAP_FRIEND") friend_match_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, target_id=friend_id, win=msg.club_one_win) return msg
def after_change(self, uid): from core.formation import Formation from core.plunder import Plunder from core.championship import Championship self.load_units() unit = self.get_unit_object(uid) all_units = self.get_units_data() all_units_object = [ self.get_unit_object(k) for k, _ in all_units.iteritems() ] changed_units = {} for u in all_units_object: if u.config.race == unit.config.race: changed_units[u.id] = u self.send_notify_with_unit_objs(changed_units.values()) fm = Formation(self.server_id, self.char_id) _changed = False for k, v in fm.in_formation_staffs().iteritems(): if v['unit_id'] in changed_units: _changed = True break if _changed: club = Club(self.server_id, self.char_id, load_staffs=False) club.force_load_staffs(send_notify=True) _p = Plunder(self.server_id, self.char_id) if _p.find_way_id_by_unit_id(uid): _p.send_formation_notify() _c = Championship(self.server_id, self.char_id) if _c.find_way_id_by_unit_id(uid): _c.send_formation_notify()
def match_start(self, key, formation_slots=None): try: friend_id, building_id, event_id = key.split(':') friend_id = int(friend_id) building_id = int(building_id) event_id = int(event_id) except: raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) config = ConfigTerritoryEvent.get(event_id) npc_club = ConfigNPCFormation.get(config.npc) my_club = Club(self.server_id, self.char_id) match = ClubMatch(my_club, npc_club, 6, f.get_skill_sequence(), {}) msg = match.start() msg.key = "{0}:{1}:{2}".format(friend_id, building_id, event_id) msg.map_name = GlobalConfig.value_string("MATCH_MAP_TERRITORY_FRIEND") return msg
def __init__(self, server_id, char_id): from core.formation import Formation from core.plunder import Plunder from core.inspire import Inspire from core.championship import Championship self.server_id = server_id self.char_id = char_id self.formation = Formation(server_id, char_id) p = Plunder(server_id, char_id) self.plunder_formation_1 = p.get_way_object(1) self.plunder_formation_2 = p.get_way_object(2) self.plunder_formation_3 = p.get_way_object(3) c = Championship(server_id, char_id) self.championship_formation_1 = c.get_way_object(1) self.championship_formation_2 = c.get_way_object(2) self.championship_formation_3 = c.get_way_object(3) self.inspire = Inspire(server_id, char_id)
def equip_check_sell(self, ids): if not isinstance(ids, (set, list, tuple)): ids = [ids] f = Formation(self.char_id) ids = set(ids) for _id in ids: if not self.has_equip(_id): raise SanguoException( errormsg.EQUIPMENT_NOT_EXIST, self.char_id, "Equipment Check Sell", "Equipment {0} NOT exist".format(_id) ) if f.find_socket_by_equip(_id): raise SanguoException( errormsg.EQUIPMENT_CANNOT_SELL_FORMATION, self.char_id, "Equipment Check Sell", "Equipment {0} in Formation, Can not sell".format(_id) )
def after_change(self, uid): from core.formation import Formation from core.plunder import Plunder from core.championship import Championship self.load_units() unit = self.get_unit_object(uid) all_units = self.get_units_data() all_units_object = [self.get_unit_object(k) for k, _ in all_units.iteritems()] changed_units = {} for u in all_units_object: if u.config.race == unit.config.race: changed_units[u.id] = u self.send_notify_with_unit_objs(changed_units.values()) fm = Formation(self.server_id, self.char_id) _changed = False for k, v in fm.in_formation_staffs().iteritems(): if v['unit_id'] in changed_units: _changed = True break if _changed: club = Club(self.server_id, self.char_id, load_staffs=False) club.force_load_staffs(send_notify=True) _p = Plunder(self.server_id, self.char_id) if _p.find_way_id_by_unit_id(uid): _p.send_formation_notify() _c = Championship(self.server_id, self.char_id) if _c.find_way_id_by_unit_id(uid): _c.send_formation_notify()
def skill_sequence_set_staff(request): server_id = request._game_session.server_id char_id = request._game_session.char_id seq_id = request._proto.seq_id index = request._proto.index staff_id = request._proto.staff_id Formation(server_id, char_id).skill_sequence_set_staff(seq_id, index, staff_id) response = SkillSequenceSetStaffResponse() response.ret = 0 return ProtobufResponse(response)
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 match(self, formation_slots=None): 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")) level = self.get_current_level() if not level: raise GameException(ConfigErrorMessage.get_error_id("TOWER_ALREADY_ALL_PASSED")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) config = ConfigTowerLevel.get(level) club_one = Club(self.server_id, self.char_id) if config.talent_id: club_one.add_talent_effects([config.talent_id]) club_two = config.make_club() if not club_one.formation_staffs: club_one.load_staffs() if not club_two.formation_staffs: club_two.load_staffs() club_one.add_tower_temporary_talent_effects() club_one.make_temporary_staff_calculate() club_two.add_tower_temporary_talent_effects() club_two.make_temporary_staff_calculate() club_match = ClubMatch(club_one, club_two, 6, f.get_skill_sequence(), {}) msg = club_match.start() msg.key = str(level) msg.map_name = config.map_name return msg
def match(self, formation_slots=None): doc = MongoArena.db(self.server_id).find_one( {'_id': str(self.char_id)}, {'rival': 1} ) rival_id = doc['rival'] if not rival_id: raise GameException(ConfigErrorMessage.get_error_id("ARENA_MATCH_NO_RIVAL")) f = Formation(self.server_id, self.char_id) if formation_slots: f.sync_slots(formation_slots) self.check_and_buy_times() club_one = Club(self.server_id, self.char_id) club_two = ArenaClub(self.server_id, rival_id) club_match = ClubMatch(club_one, club_two, 6, f.get_skill_sequence(), {}) msg = club_match.start() msg.key = rival_id msg.map_name = GlobalConfig.value_string("MATCH_MAP_ARENA") return msg