def add(self, staff_original_id, send_notify=True, trig_signal=True): if not ConfigStaffNew.get(staff_original_id): raise GameException(ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) unique_id = make_string_id() doc = MongoStaff.document_staff() doc['oid'] = staff_original_id MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': {'staffs.{0}'.format(unique_id): doc}}, ) if send_notify: self.send_notify(ids=[unique_id]) staff_new_add_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staffs_info=[(staff_original_id, unique_id), ], force_load_staffs=send_notify, ) if trig_signal: self.after_staffs_change_for_trig_signal() return unique_id
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 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 step_up(self): if self.step >= self.config.max_step: raise GameException(ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_STEP")) if self.level < self.config.steps[self.step].level_limit: raise GameException(ConfigErrorMessage.get_error_id("STAFF_LEVEL_NOT_ENOUGH")) using_items = self.config.steps[self.step].update_item_need resource_classified = ResourceClassification.classify(using_items) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove(self.server_id, self.char_id, message="Staff.step_up:{0}".format(self.oid)) self.step += 1 MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.step'.format(self.id): self.step }} ) # NOTE 升阶可能会导致 天赋技能 改变 # 不仅会影响自己,可能(如果在阵型中)也会影响到其他选手 # 所以这里不自己 calculate, 而是先让 club 重新 load staffs staff_step_up_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_step=self.step )
def step_up(self): if self.step >= self.config.max_step: raise GameException( ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_STEP")) if self.level < self.config.steps[self.step].level_limit: raise GameException( ConfigErrorMessage.get_error_id("STAFF_LEVEL_NOT_ENOUGH")) using_items = self.config.steps[self.step].update_item_need resource_classified = ResourceClassification.classify(using_items) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove(self.server_id, self.char_id, message="Staff.step_up:{0}".format( self.oid)) self.step += 1 MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.step'.format(self.id): self.step }}) # NOTE 升阶可能会导致 天赋技能 改变 # 不仅会影响自己,可能(如果在阵型中)也会影响到其他选手 # 所以这里不自己 calculate, 而是先让 club 重新 load staffs staff_step_up_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_step=self.step)
def add(self, staff_original_id, send_notify=True, trig_signal=True): if not ConfigStaffNew.get(staff_original_id): raise GameException( ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) unique_id = make_string_id() doc = MongoStaff.document_staff() doc['oid'] = staff_original_id MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}'.format(unique_id): doc }}, ) if send_notify: self.send_notify(ids=[unique_id]) staff_new_add_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staffs_info=[ (staff_original_id, unique_id), ], force_load_staffs=send_notify, ) if trig_signal: self.after_staffs_change_for_trig_signal() return unique_id
def __init__(self, server_id, char_id): self.server_id = server_id self.char_id = char_id if not MongoStaff.exist(self.server_id, self.char_id): doc = MongoStaff.document() doc['_id'] = self.char_id MongoStaff.db(server_id).insert_one(doc)
def equipment_change(self, bag_slot_id, tp): # 会影响的其他staff_id other_staff_id = "" if not bag_slot_id: # 卸下 bag_slot_id = "" else: # 更换 bag = Bag(self.server_id, self.char_id) slot = bag.get_slot(bag_slot_id) item_id = slot['item_id'] if get_item_type(item_id) != TYPE_EQUIPMENT: raise GameException( ConfigErrorMessage.get_error_id("INVALID_OPERATE")) equip_config = ConfigEquipmentNew.get(item_id) if equip_config.tp != tp: raise GameException( ConfigErrorMessage.get_error_id( "EQUIPMENT_TYPE_NOT_MATCH")) other_staff_id = StaffManger( self.server_id, self.char_id).find_staff_id_by_equipment_slot_id(bag_slot_id) if tp == EQUIP_MOUSE: key = 'equip_mouse' elif tp == EQUIP_KEYBOARD: key = 'equip_keyboard' elif tp == EQUIP_MONITOR: key = 'equip_monitor' elif tp == EQUIP_DECORATION: key = 'equip_decoration' elif tp == EQUIP_SPECIAL: key = 'equip_special' else: raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE")) setattr(self, key, bag_slot_id) MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.{1}'.format(self.id, key): bag_slot_id }}) self.calculate() self.make_cache() return other_staff_id
def add_exp_pool(self, exp): doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'exp_pool': 1}) new_exp = doc.get('exp_pool', 0) + exp if new_exp > MAX_INT: new_exp = MAX_INT MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'exp_pool': new_exp }}) self.send_notify(ids=[])
def test_upgrade_upgrading(self): skills = SkillManager(1, 1).get_staff_skills(self.staff_id) skill_id = random.choice(skills.keys()) MongoStaff.db(1).update_one( {'_id': 1}, {"$set": {'staffs.{0}.skills.{1}.end_at'.format(self.staff_id, skill_id): arrow.utcnow().timestamp + 10}} ) try: SkillManager(1, 1).upgrade(self.staff_id, int(skill_id)) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("SKILL_ALREADY_IN_UPGRADE") else: raise Exception('error')
def test_get_reward(self): TrainingExp(1, 1).start(1, int(self.staff_id)) TrainingExp(1, 1).speedup(1) TrainingExp(1, 1).get_reward(1) data = MongoStaff.db(1).find_one({'_id': 1}, {'staffs.{0}'.format(self.staff_id): 1}) assert data['staffs'][str(self.staff_id)]['exp'] > 0
def get_all_staff_object(self): """ :rtype: dict[str, Staff] """ doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) return {k: self.get_staff_object(k) for k, _ in doc['staffs'].iteritems()}
def send_notify(self, ids=None): if ids is None: projection = {'staffs': 1, 'exp_pool': 1} act = ACT_INIT else: if not ids: projection = {'staffs': 0} else: projection = {'staffs.{0}'.format(i): 1 for i in ids} projection['exp_pool'] = 1 act = ACT_UPDATE doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, projection) staffs = doc.get('staffs', {}) notify = StaffNotify() notify.act = act notify.exp_pool = doc.get('exp_pool', 0) for k, _ in staffs.iteritems(): notify_staff = notify.staffs.add() staff = self.get_staff_object(k) notify_staff.MergeFrom(staff.make_protomsg()) MessagePipe(self.char_id).put(msg=notify)
def equipment_change(self, bag_slot_id, tp): # 会影响的其他staff_id other_staff_id = "" if not bag_slot_id: # 卸下 bag_slot_id = "" else: # 更换 bag = Bag(self.server_id, self.char_id) slot = bag.get_slot(bag_slot_id) item_id = slot['item_id'] if get_item_type(item_id) != TYPE_EQUIPMENT: raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE")) equip_config = ConfigEquipmentNew.get(item_id) if equip_config.tp != tp: raise GameException(ConfigErrorMessage.get_error_id("EQUIPMENT_TYPE_NOT_MATCH")) other_staff_id = StaffManger(self.server_id, self.char_id).find_staff_id_by_equipment_slot_id(bag_slot_id) if tp == EQUIP_MOUSE: key = 'equip_mouse' elif tp == EQUIP_KEYBOARD: key = 'equip_keyboard' elif tp == EQUIP_MONITOR: key = 'equip_monitor' elif tp == EQUIP_DECORATION: key = 'equip_decoration' elif tp == EQUIP_SPECIAL: key = 'equip_special' else: raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE")) setattr(self, key, bag_slot_id) MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.{1}'.format(self.id, key): bag_slot_id }} ) self.calculate() self.make_cache() return other_staff_id
def reset(self): # 重置到初始状态 self.level = 1 self.step = 0 self.star = 0 self.level_exp = 0 self.star_exp = 0 MongoStaff.db(self.server_id).update_one({'_id': self.char_id}, { '$set': { 'staffs.{0}.level'.format(self.id): self.level, 'staffs.{0}.step'.format(self.id): self.step, 'staffs.{0}.star'.format(self.id): self.star, 'staffs.{0}.level_exp'.format(self.id): self.level_exp, 'staffs.{0}.star_exp'.format(self.id): self.star_exp, } })
def test_upgrade_already_max_level(self): skills = SkillManager(1, 1).get_staff_skills(self.staff_id) skill_id = random.choice(skills.keys()) config = ConfigSkill.get(int(skill_id)) MongoStaff.db(1).update_one( {'_id': 1}, {"$set": {'staffs.{0}.skills.{1}.level'.format(self.staff_id, skill_id): config.max_level}} ) try: SkillManager(1, 1).upgrade(self.staff_id, int(skill_id)) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("SKILL_ALREADY_MAX_LEVEL") else: raise Exception('error')
def remove(self, staff_id): if isinstance(staff_id, (list, tuple)): staff_ids = staff_id else: staff_ids = [staff_id] updater = {'staffs.{0}'.format(i): 1 for i in staff_ids} MongoStaff.db(self.server_id).update_one({'_id': self.char_id}, {'$unset': updater}) for i in staff_ids: Staff.clean_cache(i) notify = StaffRemoveNotify() notify.ids.extend(staff_ids) MessagePipe(self.char_id).put(msg=notify) self.after_staffs_change_for_trig_signal()
def level_up(self, exp_pool, up_level): from core.club import get_club_property max_level = min( ConfigStaffLevelNew.MAX_LEVEL, get_club_property(self.server_id, self.char_id, 'level') * 2) if self.level >= max_level: raise GameException( ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_LEVEL")) target_level = self.level + up_level if target_level > max_level: target_level = max_level old_level = self.level while self.level < target_level: config = ConfigStaffLevelNew.get(self.level) up_need_exp = config.exp - self.level_exp if exp_pool < up_need_exp: self.level_exp += exp_pool exp_pool = 0 break exp_pool -= up_need_exp self.level += 1 self.level_exp = 0 MongoStaff.db(self.server_id).update_one({'_id': self.char_id}, { '$set': { 'staffs.{0}.level'.format(self.id): self.level, 'staffs.{0}.level_exp'.format(self.id): self.level_exp } }) self.calculate() self.make_cache() staff_level_up_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_level=self.level) return exp_pool, self.level - old_level
def level_up(self, exp_pool, up_level): from core.club import get_club_property max_level = min(ConfigStaffLevelNew.MAX_LEVEL, get_club_property(self.server_id, self.char_id, 'level') * 2) if self.level >= max_level: raise GameException(ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_LEVEL")) target_level = self.level + up_level if target_level > max_level: target_level = max_level old_level = self.level while self.level < target_level: config = ConfigStaffLevelNew.get(self.level) up_need_exp = config.exp - self.level_exp if exp_pool < up_need_exp: self.level_exp += exp_pool exp_pool = 0 break exp_pool -= up_need_exp self.level += 1 self.level_exp = 0 MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.level'.format(self.id): self.level, 'staffs.{0}.level_exp'.format(self.id): self.level_exp }} ) self.calculate() self.make_cache() staff_level_up_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_level=self.level ) return exp_pool, self.level - old_level
def has_staff(self, ids): # unique id projection = {'staffs.{0}'.format(i): 1 for i in ids} doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, projection) if not doc: return False staffs = doc.get('staffs', {}) return len(ids) == len(staffs)
def reset(self): # 重置到初始状态 self.level = 1 self.step = 0 self.star = 0 self.level_exp = 0 self.star_exp = 0 MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.level'.format(self.id): self.level, 'staffs.{0}.step'.format(self.id): self.step, 'staffs.{0}.star'.format(self.id): self.star, 'staffs.{0}.level_exp'.format(self.id): self.level_exp, 'staffs.{0}.star_exp'.format(self.id): self.star_exp, }} )
def add_exp_pool(self, exp): doc = MongoStaff.db(self.server_id).find_one( {'_id': self.char_id}, {'exp_pool': 1} ) new_exp = doc.get('exp_pool', 0) + exp if new_exp > MAX_INT: new_exp = MAX_INT MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'exp_pool': new_exp }} ) self.send_notify(ids=[])
def remove(self, staff_id): if isinstance(staff_id, (list, tuple)): staff_ids = staff_id else: staff_ids = [staff_id] updater = {'staffs.{0}'.format(i): 1 for i in staff_ids} MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$unset': updater} ) for i in staff_ids: Staff.clean_cache(i) notify = StaffRemoveNotify() notify.ids.extend(staff_ids) MessagePipe(self.char_id).put(msg=notify) self.after_staffs_change_for_trig_signal()
def test_add(self): staff_ids = ConfigStaff.INSTANCES.keys() doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) add_id = 0 for staff_id in staff_ids: if str(staff_id) not in doc['staffs'].keys(): add_id = staff_id StaffManger(self.server_id, self.char_id).add(add_id) assert StaffManger(self.server_id, self.char_id).has_staff(add_id) is True
def get_all_staff_object(self): """ :rtype: dict[str, Staff] """ doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) return { k: self.get_staff_object(k) for k, _ in doc['staffs'].iteritems() }
def test_add_duplicate(self): doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) staff_id = int(random.choice(doc['staffs'].keys())) assert StaffManger(self.server_id, self.char_id).has_staff(staff_id) is True try: StaffManger(self.server_id, self.char_id).add(staff_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_ALREADY_HAVE") else: raise Exception("can not be here!")
def get_staffs_data(self, ids=None): """ :rtype: dict[str, dict] """ if ids: projection = {'staffs.{0}'.format(i): 1 for i in ids} else: projection = {'staffs': 1} doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, projection) return doc['staffs']
def test_start_shop_not_exist(self): staffs = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staff_id = 0 for i in range(1, 999): if str(i) not in staffs['staffs'].keys(): staff_id = i break try: TrainingShop(1, 1).start(1, staff_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST") else: raise Exception('Error')
def test_remove(self): staff_ids = ConfigStaff.INSTANCES.keys() doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}) staff_id = 0 for i in staff_ids: if str(i) not in doc['staffs'].keys(): staff_id = i break StaffManger(self.server_id, self.char_id).add(staff_id) assert StaffManger(self.server_id, self.char_id).has_staff(staff_id) is True StaffManger(self.server_id, self.char_id).remove(staff_id) assert StaffManger(self.server_id, self.char_id).has_staff(staff_id) is False
def test_start_shop_not_exist(self): staffs = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staff_id = 0 for i in range(1, 999): if str(i) not in staffs['staffs'].keys(): staff_id = i break try: TrainingShop(1, 1).start(1, staff_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id( "STAFF_NOT_EXIST") else: raise Exception('Error')
def batch_add(self, staffs): # [(oid, amount), ...] updater = {} unique_id_list = [] info = [] for oid, amount in staffs: if not ConfigStaffNew.get(oid): raise GameException(ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) for i in range(amount): unique_id = make_string_id() doc = MongoStaff.document_staff() doc['oid'] = oid unique_id_list.append(unique_id) updater['staffs.{0}'.format(unique_id)] = doc info.append((oid, unique_id)) MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': updater} ) self.send_notify(ids=unique_id_list) staff_new_add_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staffs_info=info, force_load_staffs=True, ) self.after_staffs_change_for_trig_signal()
def batch_add(self, staffs): # [(oid, amount), ...] updater = {} unique_id_list = [] info = [] for oid, amount in staffs: if not ConfigStaffNew.get(oid): raise GameException( ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST")) for i in range(amount): unique_id = make_string_id() doc = MongoStaff.document_staff() doc['oid'] = oid unique_id_list.append(unique_id) updater['staffs.{0}'.format(unique_id)] = doc info.append((oid, unique_id)) MongoStaff.db(self.server_id).update_one({'_id': self.char_id}, {'$set': updater}) self.send_notify(ids=unique_id_list) staff_new_add_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staffs_info=info, force_load_staffs=True, ) self.after_staffs_change_for_trig_signal()
def test_start_staff_not_exist(self): doc = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staffs = doc['staffs'] staff_id = 0 for i in range(1, 1000): if str(1) not in staffs.keys(): staff_id = i break try: TrainingProperty(1, 1).start(staff_id, get_one_available_training(1)) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST") else: raise Exception('error')
def test_check_staff_not_exist(self): staffs = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staff_ids = staffs['staffs'].keys() staff_id = 0 for i in range(1000, 1111): if str(i) not in staff_ids: staff_id = i break try: SkillManager(1, 1).check(staff_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_NOT_EXIST") else: raise Exception('Error')
def find_staff_id_by_equipment_slot_id(self, slot_id): # type: (str) -> str # 找slot_id在哪个角色身上 assert slot_id, "Invalid slot_id: {0}".format(slot_id) doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) for k, v in doc['staffs'].iteritems(): if v['equip_mouse'] == slot_id or \ v['equip_keyboard'] == slot_id or \ v['equip_monitor'] == slot_id or \ v['equip_decoration'] == slot_id or \ v.get('equip_special', '') == slot_id: return k return ""
def test_start_staff_not_exist(self): doc = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staffs = doc['staffs'] staff_id = 0 for i in range(1, 1000): if str(1) not in staffs.keys(): staff_id = i break try: TrainingProperty(1, 1).start(staff_id, get_one_available_training(1)) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id( "STAFF_NOT_EXIST") else: raise Exception('error')
def test_level_up(self): doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) s = ConfigStaff.get(int(random.choice(doc['staffs'].keys()))) level = 5 exp = 0 for i in range(1, level): exp += ConfigStaffLevel.get(i).exp[s.quality] exp += 1 assert StaffManger(self.server_id, self.char_id).get_staff_object(s.id).level == 1 assert StaffManger(self.server_id, self.char_id).get_staff_object(s.id).exp == 0 StaffManger(self.server_id, self.char_id).update(s.id, exp=exp) assert StaffManger(self.server_id, self.char_id).get_staff_object(s.id).level == level assert StaffManger(self.server_id, self.char_id).get_staff_object(s.id).exp == 1
def find_staff_id_by_equipment_slot_id(self, slot_id): # type: (str) -> str # 找slot_id在哪个角色身上 assert slot_id, "Invalid slot_id: {0}".format(slot_id) doc = MongoStaff.db(self.server_id).find_one( {'_id': self.char_id}, {'staffs': 1} ) for k, v in doc['staffs'].iteritems(): if v['equip_mouse'] == slot_id or \ v['equip_keyboard'] == slot_id or \ v['equip_monitor'] == slot_id or \ v['equip_decoration'] == slot_id or \ v.get('equip_special', '') == slot_id: return k return ""
def test_recruit_already_have(self): config = ConfigStaffRecruit.get(RECRUIT_ENUM_TO_CONFIG_ID[RECRUIT_NORMAL]) cost_type = 'gold' if config.cost_type == 1 else 'diamond' self.update(**{cost_type: config.cost_value}) StaffRecruit(self.server_id, self.char_id).refresh(RECRUIT_NORMAL) staff_ids = StaffRecruit(self.server_id, self.char_id).get_self_refreshed_staffs() doc = MongoStaff.db(self.server_id).find_one({'_id': 1}, {'staffs': 1}) test_id = 0 for staff_id in staff_ids: if str(staff_id) not in doc['staffs'].keys(): StaffManger(self.server_id, self.char_id).add(staff_id) test_id = staff_id break try: StaffRecruit(self.server_id, self.char_id).recruit(test_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_ALREADY_HAVE") else: raise Exception("can not be here!")
def test_recruit(self): config = ConfigStaffRecruit.get(RECRUIT_ENUM_TO_CONFIG_ID[RECRUIT_NORMAL]) cost_type = 'gold' if config.cost_type == 1 else 'diamond' self.update(**{cost_type: config.cost_value}) StaffRecruit(self.server_id, self.char_id).refresh(RECRUIT_NORMAL) staff_ids = StaffRecruit(self.server_id, self.char_id).get_self_refreshed_staffs() doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) test_id = 0 for staff_id in staff_ids: if str(staff_id) not in doc['staffs'].keys(): test_id = staff_id break assert StaffManger(self.server_id, self.char_id).has_staff(test_id) is False staff_cfg = ConfigStaff.get(test_id) tp = 'gold' if staff_cfg.buy_type == 1 else 'diamond' self.update(**{tp: staff_cfg.buy_cost}) StaffRecruit(self.server_id, self.char_id).recruit(test_id) assert StaffManger(self.server_id, self.char_id).has_staff(test_id) is True
def setup_class(cls): MongoCharacter.db(1).update_one( {'_id': 1}, {'$set': {'club.level': 10}} ) doc = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staff_ids = random.sample(ConfigStaff.INSTANCES.keys(), 15) for i in staff_ids: if str(i) not in doc['staffs']: StaffManger(1, 1).add(i) match_staff_ids = [] for k in doc['staffs'].keys(): match_staff_ids.append(int(k)) for staff_id in staff_ids: if staff_id not in match_staff_ids: match_staff_ids.append(staff_id) Club(1, 1).set_match_staffs(random.sample(match_staff_ids, 10)) LeagueGame.new(1)
def setup_class(cls): MongoCharacter.db(1).update_one({'_id': 1}, {'$set': { 'club.level': 10 }}) doc = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staff_ids = random.sample(ConfigStaff.INSTANCES.keys(), 15) for i in staff_ids: if str(i) not in doc['staffs']: StaffManger(1, 1).add(i) match_staff_ids = [] for k in doc['staffs'].keys(): match_staff_ids.append(int(k)) for staff_id in staff_ids: if staff_id not in match_staff_ids: match_staff_ids.append(staff_id) Club(1, 1).set_match_staffs(random.sample(match_staff_ids, 10)) LeagueGame.new(1)
def test_recruit_not_in_list(self): config = ConfigStaffRecruit.get(RECRUIT_ENUM_TO_CONFIG_ID[RECRUIT_NORMAL]) cost_type = 'gold' if config.cost_type == 1 else 'diamond' self.update(**{cost_type: config.cost_value}) StaffRecruit(self.server_id, self.char_id).refresh(RECRUIT_NORMAL) config_ids = ConfigStaff.INSTANCES.keys() staff_ids = StaffRecruit(self.server_id, self.char_id).get_self_refreshed_staffs() doc = MongoStaff.db(self.server_id).find_one({'_id': self.char_id}, {'staffs': 1}) has_staffs = [int(staff_id) for staff_id in doc['staffs'].keys()] staffs_set = set(staff_ids) | set(has_staffs) test_id = 0 for staff_id in config_ids: if staff_id not in staffs_set: test_id = staff_id break try: StaffRecruit(self.server_id, self.char_id).recruit(test_id) except GameException as e: assert e.error_id == ConfigErrorMessage.get_error_id("STAFF_RECRUIT_NOT_IN_LIST") else: raise Exception("can not be here!")
def setup(self): doc = MongoStaff.db(1).find_one({'_id': 1}, {'staffs': 1}) staffs = doc['staffs'] self.staff_id = int(random.choice(staffs.keys())) TrainingShop(1, 1)
def star_up(self, single): # single = True, 只升级一次 # single = False, 尽量升到下一星级 if self.star >= ConfigStaffStar.MAX_STAR: raise GameException( ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_STAR")) old_star = self.star def _make_single_up(): star_config = ConfigStaffStar.get(self.star) using_items = [(star_config.need_item_id, star_config.need_item_amount)] resource_classified = ResourceClassification.classify(using_items) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove(self.server_id, self.char_id, message="Staff.star_up:{0}".format( self.oid)) if random.randint(1, 100) <= 20: _exp = 6 is_crit = True else: _exp = random.randint(1, 3) is_crit = False exp = self.star_exp + _exp while True: if self.star == ConfigStaffStar.MAX_STAR: if exp >= ConfigStaffStar.get(self.star).exp: exp = ConfigStaffStar.get(self.star).exp - 1 break if exp < ConfigStaffStar.get(self.star).exp: break exp -= ConfigStaffStar.get(self.star).exp self.star += 1 self.star_exp = exp return is_crit, _exp, star_config.need_item_id, star_config.need_item_amount if single: crit, inc_exp, cost_item_id, cost_item_amount = _make_single_up() else: crit = False inc_exp = 0 cost_item_id = 0 cost_item_amount = 0 while self.star == old_star: try: _crit, _inc_exp, _cost_id, _cost_amount = _make_single_up() except GameException as e: if not inc_exp: # 说明一次都没成功 raise e else: break if _crit: crit = _crit inc_exp += _inc_exp cost_item_id = _cost_id cost_item_amount += _cost_amount MongoStaff.db(self.server_id).update_one({'_id': self.char_id}, { '$set': { 'staffs.{0}.star'.format(self.id): self.star, 'staffs.{0}.star_exp'.format(self.id): self.star_exp } }) if self.star != old_star: self.calculate() self.make_cache() star_changed = self.star > old_star if star_changed: staff_star_up_signal.send(sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_star=self.star) return star_changed, crit, inc_exp, cost_item_id, cost_item_amount
def star_up(self, single): # single = True, 只升级一次 # single = False, 尽量升到下一星级 if self.star >= ConfigStaffStar.MAX_STAR: raise GameException(ConfigErrorMessage.get_error_id("STAFF_ALREADY_MAX_STAR")) old_star = self.star def _make_single_up(): star_config = ConfigStaffStar.get(self.star) using_items = [(star_config.need_item_id, star_config.need_item_amount)] resource_classified = ResourceClassification.classify(using_items) resource_classified.check_exist(self.server_id, self.char_id) resource_classified.remove(self.server_id, self.char_id, message="Staff.star_up:{0}".format(self.oid)) if random.randint(1, 100) <= 20: _exp = 6 is_crit = True else: _exp = random.randint(1, 3) is_crit = False exp = self.star_exp + _exp while True: if self.star == ConfigStaffStar.MAX_STAR: if exp >= ConfigStaffStar.get(self.star).exp: exp = ConfigStaffStar.get(self.star).exp - 1 break if exp < ConfigStaffStar.get(self.star).exp: break exp -= ConfigStaffStar.get(self.star).exp self.star += 1 self.star_exp = exp return is_crit, _exp, star_config.need_item_id, star_config.need_item_amount if single: crit, inc_exp, cost_item_id, cost_item_amount = _make_single_up() else: crit = False inc_exp = 0 cost_item_id = 0 cost_item_amount = 0 while self.star == old_star: try: _crit, _inc_exp, _cost_id, _cost_amount = _make_single_up() except GameException as e: if not inc_exp: # 说明一次都没成功 raise e else: break if _crit: crit = _crit inc_exp += _inc_exp cost_item_id = _cost_id cost_item_amount += _cost_amount MongoStaff.db(self.server_id).update_one( {'_id': self.char_id}, {'$set': { 'staffs.{0}.star'.format(self.id): self.star, 'staffs.{0}.star_exp'.format(self.id): self.star_exp }} ) if self.star != old_star: self.calculate() self.make_cache() star_changed = self.star > old_star if star_changed: staff_star_up_signal.send( sender=None, server_id=self.server_id, char_id=self.char_id, staff_id=self.id, staff_oid=self.oid, new_star=self.star ) return star_changed, crit, inc_exp, cost_item_id, cost_item_amount