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 reset_one(self, _id): str_id = str(_id) if str_id not in self.stage.elites: raise SanguoException( errormsg.STAGE_ELITE_NOT_OPEN, self.char_id, "Elite Reset One", "reset a not opened stage {0}".format(_id) ) reset_times = self.stage.elites_buy.get(str(_id), 0) char = Char(self.char_id).mc can_reset_times = VIP_FUNCTION[char.vip].stage_elite_buy if reset_times >= can_reset_times: raise SanguoException( errormsg.STAGE_ELITE_RESET_FULL, self.char_id, "Elite Reset One", "reset {0}".format(_id) ) cost = self.get_reset_cost(_id) resource = Resource(self.char_id, "Elite Reset One", "reset {0}".format(_id)) with resource.check(sycee=-cost): self.stage.elites[str(_id)] = 0 self.stage.elites_buy[str(_id)] = reset_times + 1 self.stage.save() self.send_update_notify(_id)
def checkin(self): # 签到 from core.union.union import Union from core.union.battle import UnionBattle if not self.mongo_union_member.joined: raise SanguoException( errormsg.INVALID_OPERATE, self.char_id, "Union Checkin", "not join union" ) if self.mongo_union_member.checkin_times + 1 > self.checkin_total_amount: raise SanguoException( errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times" ) try: c = UNION_CHECKIN[self.mongo_union_member.checkin_times+1] except KeyError: raise SanguoException( errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times. UNION_CHECKIN KeyError: {0}".format(self.mongo_union_member.checkin_times+1) ) if c.cost_type == 1: needs = {'gold': -c.cost_value} else: needs = {'sycee': -c.cost_value} resources = Resource(self.char_id, "Union Checkin") with resources.check(**needs): self.mongo_union_member.checkin_times += 1 self.mongo_union_member.last_checkin_timestamp = arrow.utcnow().timestamp self.add_coin(c.got_coin, send_notify=False) self.add_contribute_points(c.got_contributes, send_notify=False) self.mongo_union_member.save() self.send_personal_notify() Union(self.char_id).add_contribute_points(c.got_contributes) UnionBattle(self.char_id).send_notify() doc = MongoUnion._get_collection().find_one( {'_id': self.mongo_union_member.joined}, {'owner': 1} ) owner = doc['owner'] UnionBattle(owner).send_notify() drop = make_standard_drop_from_template() drop['union_coin'] = c.got_coin drop['union_contribute_points'] = c.got_contributes return standard_drop_to_attachment_protomsg(drop)
def levy(self): char = Char(self.char_id).mc if self.counter.remained_value <= 0: if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.LEVY_NO_TIMES, self.char_id, "Levy", "no times. but can get additional times by increase vip level. current: {0}, max: {1}".format(char.vip, VIP_MAX_LEVEL) ) raise SanguoException( errormsg.LEVY_NO_TIMES_FINAL, self.char_id, "Levy", "no times. vip reach the max level {0}".format(VIP_MAX_LEVEL) ) resource = Resource(self.char_id, "Levy") cost_cyess = self.get_cost_sycee() with resource.check(sycee=-cost_cyess): drop = self.get_levy_drop() self.counter.incr() standard_drop = resource.add(**drop) t = Task(self.char_id) t.trig(4) self.send_notify() return standard_drop
def create(self,name): if len(name) > UNION_NAME_MAX_LENGTH: raise SanguoException( errormsg.UNION_NAME_TOO_LONG, self.char_id, "Union Create", "name too long: {0}".format(name.encode('utf-8')) ) if MongoUnion.objects.filter(name=name).count() > 0: raise SanguoException( errormsg.UNION_NAME_ALREADY_EXIST, self.char_id, "Union Create", "name already exist: {0}".format(name.encode('utf-8')) ) resource = Resource(self.char_id, "Union Create") with resource.check(sycee=-UNION_CREATE_NEEDS_SYCEE): new_id = id_generator('union')[0] mu = MongoUnion(id=new_id) mu.owner = self.char_id mu.name = name mu.bulletin = UNION_DEFAULT_DES mu.level = 1 mu.contribute_points = 0 mu.save() UnionMember(self.char_id).join_union(new_id) Union(self.char_id, new_id).send_notify() UnionBattle(self.char_id).send_notify()
def levy(self): char = Char(self.char_id).mc if self.counter.remained_value <= 0: if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.LEVY_NO_TIMES, self.char_id, "Levy", "no times. but can get additional times by increase vip level. current: {0}, max: {1}" .format(char.vip, VIP_MAX_LEVEL)) raise SanguoException( errormsg.LEVY_NO_TIMES_FINAL, self.char_id, "Levy", "no times. vip reach the max level {0}".format(VIP_MAX_LEVEL)) resource = Resource(self.char_id, "Levy") cost_cyess = self.get_cost_sycee() with resource.check(sycee=-cost_cyess): drop = self.get_levy_drop() self.counter.incr() standard_drop = resource.add(**drop) t = Task(self.char_id) t.trig(4) self.send_notify() return standard_drop
def levy(self): char = Char(self.char_id).mc if self.counter.remained_value <= 0: if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.LEVY_NO_TIMES, self.char_id, "Levy", "no times. but can get additional times by increase vip level. current: {0}, max: {1}" .format(char.vip, VIP_MAX_LEVEL)) raise SanguoException( errormsg.LEVY_NO_TIMES_FINAL, self.char_id, "Levy", "no times. vip reach the max level {0}".format(VIP_MAX_LEVEL)) resource = Resource(self.char_id, "Levy") cost_cyess = self.get_cost_sycee() with resource.check(sycee=-cost_cyess): got_gold = LEVY_GOT_GOLD_FUNCTION(char.level) prob = random.randint(1, 100) for k, v in LEVY_CRIT_PROB_TABLE: if prob <= k: break got_gold *= v self.counter.incr() resource.add(gold=got_gold) self.send_notify()
def prisoner_get(self, _id, treasures): str_id = str(_id) if str_id not in self.p.prisoners: raise SanguoException(errormsg.PRISONER_NOT_EXIST, self.char_id, "Prisoner Get", "{0} not exist".format(_id)) if not self.p.prisoners[str_id].active: raise SanguoException(errormsg.PRISONER_NOT_ACTIVE, self.char_id, "Prisoner Get", "{0} not active".format(_id)) treasures_prob = 0 for tid in treasures: try: treasures_prob += TREASURES[tid].value except KeyError: raise SanguoException(errormsg.STUFF_NOT_EXIST, self.char_id, "Prisoner Get", "treasure {0} not exist".format(tid)) using_stuffs = [(tid, 1) for tid in treasures] resource = Resource(self.char_id, "Prisoner Get") with resource.check(stuffs=using_stuffs): got = False prob = self.p.prisoners[str_id].prob + treasures_prob char = Char(self.char_id).mc vip_plus = VIP_FUNCTION[char.vip].prisoner_get prob += vip_plus if prob >= random.randint(1, 100): # got it save_hero(self.char_id, self.p.prisoners[str_id].oid) got = True self.p.prisoners.pop(str_id) msg = protomsg.RemovePrisonerNotify() msg.ids.append(_id) publish_to_char(self.char_id, pack_msg(msg)) else: self.p.prisoners[str_id].active = False msg = protomsg.UpdatePrisonerNotify() p = msg.prisoner.add() p_obj = self.p.prisoners[str_id] self._fill_up_prisoner_msg(p, _id, p_obj.oid, p_obj.prob, p_obj.active) publish_to_char(self.char_id, pack_msg(msg)) self.p.save() t = Task(self.char_id) t.trig(5) return got
def refresh(self): if self.all_opended(): # 所有卡都翻完了。直接刷新 self.panel = self.make_new_panel() self.send_notify() return resouce = Resource(self.char_id, "HeroPanel Refresh") with resouce.check(sycee=-GET_HERO_FORCE_REFRESH_COST): self.panel = self.make_new_panel() self.send_notify()
def strength(self, _id, method): # method: 1 - free ,2 - gold, 3 - sycee try: h = self.mongo_horse.horses[str(_id)] except KeyError: raise SanguoException( errormsg.HORSE_NOT_EXIST, self.char_id, "Horse Strength", "horse {0} not exist".format(_id) ) hobj = OneHorse(_id, h.oid, h.attack, h.defense, h.hp) try: HorseFreeTimesManager(self.char_id).incr() except CounterOverFlow: # 已经没有免费次数了 if method == 1: # 但还要免费强化,引发异常 raise SanguoException( errormsg.HORSE_STRENGTH_NO_FREE_TIMES, self.char_id, "Horse Strength", "strength {0} using free times. but no free times".format(_id) ) # 这个时候就要根据method来确定是否using_sycee和 resource_needs了 if method == 2: using_sycee = False resource_needs = {'gold': -hobj.strength_cost_gold} else: using_sycee = True resource_needs = {'sycee': -hobj.strength_cost_sycee} else: # 还有免费次数,直接按照免费来搞 using_sycee = False resource_needs = {} if resource_needs: resource = Resource(self.char_id, "Horse Strength", "strength {0} with method {1}".format(_id, method)) with resource.check(**resource_needs): new_hobj = hobj.strength(using_sycee) else: new_hobj = hobj.strength(using_sycee) self.mongo_horse.strengthed_horse = {str(_id): new_hobj.to_mongo_record()} self.mongo_horse.save() return new_hobj
def strength(self, _id, method): # method: 1 - free ,2 - gold, 3 - sycee try: h = self.mongo_horse.horses[str(_id)] except KeyError: raise SanguoException(errormsg.HORSE_NOT_EXIST, self.char_id, "Horse Strength", "horse {0} not exist".format(_id)) hobj = OneHorse(_id, h.oid, h.attack, h.defense, h.hp) try: HorseFreeTimesManager(self.char_id).incr() except CounterOverFlow: # 已经没有免费次数了 if method == 1: # 但还要免费强化,引发异常 raise SanguoException( errormsg.HORSE_STRENGTH_NO_FREE_TIMES, self.char_id, "Horse Strength", "strength {0} using free times. but no free times".format( _id)) # 这个时候就要根据method来确定是否using_sycee和 resource_needs了 if method == 2: using_sycee = False resource_needs = {'gold': -hobj.strength_cost_gold} else: using_sycee = True resource_needs = {'sycee': -hobj.strength_cost_sycee} else: # 还有免费次数,直接按照免费来搞 using_sycee = False resource_needs = {} if resource_needs: resource = Resource( self.char_id, "Horse Strength", "strength {0} with method {1}".format(_id, method)) with resource.check(**resource_needs): new_hobj = hobj.strength(using_sycee) else: new_hobj = hobj.strength(using_sycee) self.mongo_horse.strengthed_horse = { str(_id): new_hobj.to_mongo_record() } self.mongo_horse.save() return new_hobj
def checkin(self): # 签到 from core.union.union import Union from core.union.battle import UnionBattle if not self.mongo_union_member.joined: raise SanguoException(errormsg.INVALID_OPERATE, self.char_id, "Union Checkin", "not join union") if self.mongo_union_member.checkin_times + 1 > self.checkin_total_amount: raise SanguoException(errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times") try: c = UNION_CHECKIN[self.mongo_union_member.checkin_times + 1] except KeyError: raise SanguoException( errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times. UNION_CHECKIN KeyError: {0}".format( self.mongo_union_member.checkin_times + 1)) if c.cost_type == 1: needs = {'gold': -c.cost_value} else: needs = {'sycee': -c.cost_value} resources = Resource(self.char_id, "Union Checkin") with resources.check(**needs): self.mongo_union_member.checkin_times += 1 self.mongo_union_member.last_checkin_timestamp = arrow.utcnow( ).timestamp self.add_coin(c.got_coin, send_notify=False) self.add_contribute_points(c.got_contributes, send_notify=False) self.mongo_union_member.save() self.send_personal_notify() Union(self.char_id).add_contribute_points(c.got_contributes) UnionBattle(self.char_id).send_notify() doc = MongoUnion._get_collection().find_one( {'_id': self.mongo_union_member.joined}, {'owner': 1}) owner = doc['owner'] UnionBattle(owner).send_notify() drop = make_standard_drop_from_template() drop['union_coin'] = c.got_coin drop['union_contribute_points'] = c.got_contributes return standard_drop_to_attachment_protomsg(drop)
def reset_total(self): counter = Counter(self.char_id, 'stage_elite_buy_total') if counter.remained_value <= 0: raise SanguoException(errormsg.STAGE_ELITE_RESET_TOTAL_FULL, self.char_id, "Elite Reset Total", "reset total") cost = self.get_total_reset_cost() resource = Resource(self.char_id, "Elite Reset Total", "") with resource.check(sycee=-cost): counter = Counter(self.char_id, 'stage_elite') counter.reset() self.send_remained_times_notify()
def reset_total(self): counter = Counter(self.char_id, 'stage_elite_buy_total') if counter.remained_value <= 0: raise SanguoException( errormsg.STAGE_ELITE_RESET_TOTAL_FULL, self.char_id, "Elite Reset Total", "reset total" ) cost = self.get_total_reset_cost() resource = Resource(self.char_id, "Elite Reset Total", "") with resource.check(sycee=-cost): counter = Counter(self.char_id, 'stage_elite') counter.reset() self.send_remained_times_notify()
def refresh(self): if self.all_opended(): # 所有卡都翻完了。直接刷新 self.panel = self.make_new_panel() self.send_notify() return if self.refresh_seconds > 0: # 免费刷新还在冷却,只能用元宝刷新,不重置免费刷新时间 resouce = Resource(self.char_id, "HeroPanel Refresh") with resouce.check(sycee=-GET_HERO_FORCE_REFRESH_COST): self.panel = self.make_new_panel(reset_time=False) self.send_notify() return # 可以用免费刷新 self.panel = self.make_new_panel() self.send_notify()
def checkin(self): if not self.mongo_union_member.joined: raise SanguoException( errormsg.INVALID_OPERATE, self.char_id, "Union Checkin", "not join union" ) if self.mongo_union_member.checkin_times + 1 > self.checkin_total_amount: raise SanguoException( errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times" ) try: c = UNION_CHECKIN[self.mongo_union_member.checkin_times+1] except KeyError: raise SanguoException( errormsg.UNION_CHECKIN_REACH_MAX_TIMES, self.char_id, "Union Checkin", "reached max times. UNION_CHECKIN KeyError: {0}".format(self.mongo_union_member.checkin_times+1) ) if c.cost_type == 1: needs = {'gold': -c.cost_value} else: needs = {'sycee': -c.cost_value} resources = Resource(self.char_id, "Union Checkin") with resources.check(**needs): self.mongo_union_member.checkin_times += 1 self.mongo_union_member.last_checkin_timestamp = arrow.utcnow().timestamp self.add_coin(c.got_coin, send_notify=False) self.add_contribute_points(c.got_contributes, send_notify=False) self.mongo_union_member.save() self.send_personal_notify() Union(self.char_id, self.mongo_union_member.joined).add_contribute_points(c.got_contributes)
def levy(self): char = Char(self.char_id).mc if self.counter.remained_value <= 0: if char.vip < VIP_MAX_LEVEL: raise SanguoException( errormsg.LEVY_NO_TIMES, self.char_id, "Levy", "no times. but can get additional times by increase vip level. current: {0}, max: {1}".format(char.vip, VIP_MAX_LEVEL) ) raise SanguoException( errormsg.LEVY_NO_TIMES_FINAL, self.char_id, "Levy", "no times. vip reach the max level {0}".format(VIP_MAX_LEVEL) ) resource = Resource(self.char_id, "Levy") cost_cyess = self.get_cost_sycee() with resource.check(sycee=-cost_cyess): got_gold = LEVY_GOT_GOLD_FUNCTION(char.level) prob = random.randint(1, 100) for k, v in LEVY_CRIT_PROB_TABLE: if prob <= k: break got_gold *= v self.counter.incr() resource.add(gold=got_gold) t = Task(self.char_id) t.trig(4) self.send_notify() return got_gold
def buy(self, _id, amount): if amount < 1: raise SanguoException( errormsg.STORE_INVALID_BUY_AMOUNT, self.char_id, "Store Buy", "invalid amount {0}".format(amount) ) store = self.get_new_store() try: this_goods = store[_id] except KeyError: raise SanguoException( errormsg.STORE_GOODS_NOT_EXIST, self.char_id, "Store Buy", "{0} not exist".format(_id) ) char = Char(self.char_id) mc = char.mc # check vip if this_goods['vip_condition'] > mc.vip: raise SanguoException( errormsg.STORE_GOODS_VIP_CONDITION, self.char_id, "Store Buy", "{0} has vip condition {1}. greater than char vip {2}".format(_id, this_goods['vip_condition'], mc.vip) ) # check level if this_goods['level_condition'] > mc.level: raise SanguoException( errormsg.STORE_GOODS_LEVEL_CONDITION, self.char_id, "Store Buy", "{0} has level condition {1}. greater than char level {2}".format(_id, this_goods['level_condition'], mc.level) ) # check total amount if this_goods['has_total_amount']: if this_goods['total_amount_run_time'] < amount: raise SanguoException( errormsg.STORE_GOODS_AMOUNT_NOT_ENOUGH, self.char_id, "Store Buy", "{0} amount not enough. remained {1}, buy amount {2}".format(_id, this_goods['total_amount_run_time'], amount) ) # check limit if this_goods['has_limit_amount']: remained_amount = self.get_limit_remained_amount(_id, this_goods['limit_amount']) if remained_amount < amount: raise SanguoException( errormsg.STORE_GOODS_CHAR_LIMIT, self.char_id, "Store Buy", "{0} reach limit {1}".format(_id, this_goods['limit_amount']) ) # check gold or sycee wealth_needs = this_goods['sell_price'] * amount if this_goods['sell_type'] == 1: resource_need = {'gold': -wealth_needs} else: resource_need = {'sycee': -wealth_needs} resource = Resource(self.char_id, "Store Buy", 'buy {0}, amount: {1}'.format(_id, amount)) with resource.check(**resource_need): # 本地server检查完毕,然后通过API通知HUB购买。 # 对于有total amount限制的物品,HUB可能返回错误 data = { 'char_id': self.char_id, 'goods_id': _id, 'goods_amount': amount, } try: res = api_store_buy(data) except APIFailure: raise SanguoException( errormsg.SERVER_FAULT, self.char_id, "Store", "APIFailure. api_store_buy" ) if res['ret'] != 0: raise SanguoException( res['ret'], self.char_id, "Store Buy", "api failure" ) # ALL OK # 开始操作 if this_goods['has_limit_amount']: # 有每人限量的记录到每人的购买记录中 self.mc_limit.limits[str(_id)] = self.mc_limit.limits.get(str(_id), 0) + amount self.mc_limit.save() # 更新store if this_goods['has_total_amount']: store[_id]['total_amount_run_time'] = res['data']['total_amount_run_time'] # 给东西 resource_add = {} if this_goods['item_tp'] == 1: resource_add['heros'] = [(this_goods['item_id'], amount)] elif this_goods['item_tp'] == 2: resource_add['equipments'] = [(this_goods['item_id'], 1, amount)] elif this_goods['item_tp'] == 3: resource_add['gems'] = [(this_goods['item_id'], amount)] else: resource_add['stuffs'] = [(this_goods['item_id'], amount)] resource.add(**resource_add) self.send_notify(store=store)
def buy(self, _id, amount): store = self.get_new_store() try: this_goods = store[_id] except KeyError: raise SanguoException(errormsg.STORE_GOODS_NOT_EXIST, self.char_id, "Store Buy", "{0} not exist".format(_id)) char = Char(self.char_id) mc = char.mc # TODO check vip # if this_goods['vip_condition'] > mc.vip: # raise InvalidOperate("Store Buy. Char {0} Try to buy {1}. But vip test not passed. {2} < {3}".format( # self.char_id, _id, mc.vip, this_goods['vip_condition'] # )) # check level if this_goods['level_condition'] > mc.level: raise SanguoException( errormsg.STORE_GOODS_LEVEL_CONDITION, self.char_id, "Store Buy", "{0} has level condition {1}. greater than char level {2}". format(_id, this_goods['level_condition'], mc.level)) # check total amount if this_goods['has_total_amount']: if this_goods['total_amount_run_time'] < amount: raise SanguoException( errormsg.STORE_GOODS_AMOUNT_NOT_ENOUGH, self.char_id, "Store Buy", "{0} amount not enough. remained {1}, buy amount {2}". format(_id, this_goods['total_amount_run_time'], amount)) # check limit if this_goods['has_limit_amount']: remained_amount = self.get_limit_remained_amount( _id, this_goods['limit_amount']) if remained_amount < amount: raise SanguoException( errormsg.STORE_GOODS_CHAR_LIMIT, self.char_id, "Store Buy", "{0} reach limit {1}".format(_id, this_goods['limit_amount'])) # check gold or sycee wealth_needs = this_goods['sell_price'] * amount resource = Resource(self.char_id, "Store Buy", 'buy {0}, amount: {1}'.format(_id, amount)) if this_goods['sell_type'] == 1: resource_need = {'gold': -wealth_needs} else: resource_need = {'sycee': -wealth_needs} if this_goods['sell_type'] == 1: if mc.gold < wealth_needs: raise SanguoException( errormsg.GOLD_NOT_ENOUGH, self.char_id, "Store Buy", "gold not enough", ) else: if mc.sycee < wealth_needs: raise SanguoException(errormsg.SYCEE_NOT_ENOUGH, self.char_id, "Store Buy", "sycee not enough") with resource.check(**resource_need): # 本地server检查完毕,然后通过API通知HUB购买。 # 对于有total amount限制的物品,HUB可能返回错误 data = { 'char_id': self.char_id, 'goods_id': _id, 'goods_amount': amount, } try: res = api_store_buy(data) except APIFailure: raise SanguoException(errormsg.SERVER_FAULT, self.char_id, "Store", "APIFailure. api_store_buy") if res['ret'] != 0: raise SanguoException(res['ret'], self.char_id, "Store Buy", "api failure") # ALL OK # 开始操作 if this_goods['has_limit_amount']: # 有每人限量的记录到每人的购买记录中 self.mc_limit.limits[str(_id)] = self.mc_limit.limits.get( str(_id), 0) + amount self.mc_limit.save() # 更新store if this_goods['has_total_amount']: store[_id]['total_amount_run_time'] = res['data'][ 'total_amount_run_time'] # 给东西 resource_add = {} if this_goods['item_tp'] == 1: resource_add['heros'] = [(this_goods['item_id'], amount)] elif this_goods['item_tp'] == 2: resource_add['equipments'] = [this_goods['item_id']] * amount elif this_goods['item_tp'] == 3: resource_add['gems'] = [(this_goods['item_id'], amount)] else: resource_add['stuffs'] = [(this_goods['item_id'], amount)] resource.add(**resource_add) self.send_notify(store=store)
def level_up(self, quick=False): def _up(): if self.level >= EQUIP_MAX_LEVEL: raise SanguoException( errormsg.EQUIPMENT_REACH_MAX_LEVEL, self.char_id, "Equipment Level Up", "Equipment {0} has already touch max level {1}".format(self.equip.id, EQUIP_MAX_LEVEL)) if self.level >= char_level: raise SanguoException( errormsg.EQUIPMENT_REACH_CHAR_LEVEL, self.char_id, "Equipment Level Up", "Equipment {0} level {1} >= char level {2}".format(self.equip.id, self.level, char_level) ) gold_needs = self.level_up_need_gold() if cache_char.gold < gold_needs: raise SanguoException( errormsg.GOLD_NOT_ENOUGH, self.char_id, "Equipment Level Up", "Gold Not Enough" ) cache_char.gold -= gold_needs prob = random.randint(1, 100) for p, l in LEVEL_UP_PROBS: if prob <= p: actual_level_up = l break self.mongo_item.equipments[str(self.equip_id)].level += actual_level_up self.level += actual_level_up return gold_needs char = Char(self.char_id) cache_char = char.mc char_level = cache_char.level LEVEL_UP_PROBS = ( (30, 1), (80, 2), (100, 3) ) all_gold_needs = 0 equip_msgs = [] if quick: quick_times = 0 while True: try: all_gold_needs += _up() except SanguoException: if quick_times == 0: raise else: break else: quick_times += 1 msg = protomsg.Equip() self._msg_equip(msg, self.equip_id, self.mongo_item.equipments[str(self.equip_id)], self) equip_msgs.append(msg) else: all_gold_needs += _up() msg = protomsg.Equip() self._msg_equip(msg, self.equip_id, self.mongo_item.equipments[str(self.equip_id)], self) equip_msgs.append(msg) resource = Resource(self.char_id, "Equipment Level Up", "equipment {0}".format(self.equip_id)) with resource.check(gold=-all_gold_needs): self.mongo_item.save() Task(self.char_id).trig(8) return equip_msgs
def prisoner_get(self, _id, treasures): str_id = str(_id) if str_id not in self.p.prisoners: raise SanguoException( errormsg.PRISONER_NOT_EXIST, self.char_id, "Prisoner Get", "{0} not exist".format(_id) ) if not self.p.prisoners[str_id].active: raise SanguoException( errormsg.PRISONER_NOT_ACTIVE, self.char_id, "Prisoner Get", "{0} not active".format(_id) ) treasures_prob = 0 for tid in treasures: try: treasures_prob += TREASURES[tid].value except KeyError: raise SanguoException( errormsg.STUFF_NOT_EXIST, self.char_id, "Prisoner Get", "treasure {0} not exist".format(tid) ) def _get(): got = False prob = self.p.prisoners[str_id].prob + treasures_prob ae = ActivityEntry(self.char_id, 40008) if ae and ae.is_ok(): prob += 80 ae = ActivityEntry(self.char_id, 50005) if ae and ae.is_valid(): _vip = ae.get_current_value(self.char_id) if _vip == 4: prob += 30 elif _vip == 5: prob += 50 elif _vip == 6: prob += 60 elif _vip >= 7: prob += 100 if prob >= random.randint(1, 100): # got it save_hero(self.char_id, self.p.prisoners[str_id].oid) got = True self.p.prisoners.pop(str_id) msg = protomsg.RemovePrisonerNotify() msg.ids.append(_id) publish_to_char(self.char_id, pack_msg(msg)) else: self.p.prisoners[str_id].active = False msg = protomsg.UpdatePrisonerNotify() p = msg.prisoner.add() p_obj = self.p.prisoners[str_id] self._fill_up_prisoner_msg(p, _id, p_obj.oid, p_obj.prob, p_obj.active) publish_to_char(self.char_id, pack_msg(msg)) self.p.save() return got using_stuffs = [(tid, 1) for tid in treasures] if using_stuffs: resource = Resource(self.char_id, "Prisoner Get") with resource.check(stuffs=using_stuffs): got = _get() else: got = _get() t = Task(self.char_id) t.trig(5) if got: achievement = Achievement(self.char_id) achievement.trig(14, 1) TimesLogPrisonGetSuccess(self.char_id).inc() return got
def level_up(self, quick=False): def _up(): if self.level >= EQUIP_MAX_LEVEL: raise SanguoException( errormsg.EQUIPMENT_REACH_MAX_LEVEL, self.char_id, "Equipment Level Up", "Equipment {0} has already touch max level {1}".format( self.equip.id, EQUIP_MAX_LEVEL)) if self.level >= char_level: raise SanguoException( errormsg.EQUIPMENT_REACH_CHAR_LEVEL, self.char_id, "Equipment Level Up", "Equipment {0} level {1} >= char level {2}".format( self.equip.id, self.level, char_level)) gold_needs = self.level_up_need_gold() if cache_char.gold < gold_needs: raise SanguoException(errormsg.GOLD_NOT_ENOUGH, self.char_id, "Equipment Level Up", "Gold Not Enough") cache_char.gold -= gold_needs prob = random.randint(1, 100) for p, l in LEVEL_UP_PROBS: if prob <= p: actual_level_up = l break self.mongo_item.equipments[str( self.equip_id)].level += actual_level_up self.level += actual_level_up return gold_needs char = Char(self.char_id) cache_char = char.mc char_level = cache_char.level LEVEL_UP_PROBS = ((30, 1), (80, 2), (100, 3)) all_gold_needs = 0 equip_msgs = [] if quick: quick_times = 0 while True: try: all_gold_needs += _up() except SanguoException: if quick_times == 0: raise else: break else: quick_times += 1 msg = protomsg.Equip() self._msg_equip( msg, self.equip_id, self.mongo_item.equipments[str(self.equip_id)], self) equip_msgs.append(msg) else: all_gold_needs += _up() msg = protomsg.Equip() self._msg_equip(msg, self.equip_id, self.mongo_item.equipments[str(self.equip_id)], self) equip_msgs.append(msg) resource = Resource(self.char_id, "Equipment Level Up", "equipment {0}".format(self.equip_id)) with resource.check(gold=-all_gold_needs): self.mongo_item.save() Task(self.char_id).trig(8) return equip_msgs
def open(self, _id): if str(_id) not in self.panel.panel: raise SanguoException( errormsg.HEROPANEL_SOCKET_NOT_EXIST, self.char_id, "HeroPanel Open", "HeroPanel Socket {0} not exist".format(_id) ) if self.panel.panel[str(_id)].opened: raise SanguoException( errormsg.HEROPANEL_SOCKET_ALREADY_OPENED, self.char_id, "HeroPanel Open", "HeroPanel Socket {0} already opended".format(_id) ) none_opended_heros = self.none_opened_heros() if not none_opended_heros: raise SanguoException( errormsg.HEROPANEL_ALL_OPENED, self.char_id, "HeroPanel Open", "all opened." ) none_opened_good_hero = None none_opened_other_heros = [] for k, v in none_opended_heros: if v.good: none_opened_good_hero = (k, v) continue none_opened_other_heros.append((k, v)) counter = Counter(self.char_id, 'gethero') try: counter.incr() using_sycee = 0 except CounterOverFlow: # 没有免费次数了,需要用元宝 using_sycee = GET_HERO_COST resource = Resource(self.char_id, "HeroPanel Open") with resource.check(sycee=-using_sycee): if none_opened_good_hero: # 还没有取到甲卡 prob = GET_HERO_QUALITY_ONE_PROB[self.open_times + 1] if random.randint(1, 100) <= prob: # 取得甲卡 socket_id, hero = none_opened_good_hero else: socket_id, hero = random.choice(none_opened_other_heros) else: socket_id, hero = random.choice(none_opened_other_heros) self.panel.panel[str(_id)], self.panel.panel[socket_id] = self.panel.panel[socket_id], self.panel.panel[str(_id)] self.panel.panel[str(_id)].opened = True self.panel.save() save_hero(self.char_id, hero.oid) self.send_notify() return hero.oid
def open(self, _id): if str(_id) not in self.panel.panel: raise SanguoException( errormsg.HEROPANEL_SOCKET_NOT_EXIST, self.char_id, "HeroPanel Open", "HeroPanel Socket {0} not exist".format(_id) ) if self.panel.panel[str(_id)].opened: # raise SanguoException( # errormsg.HEROPANEL_SOCKET_ALREADY_OPENED, # self.char_id, # "HeroPanel Open", # "HeroPanel Socket {0} already opended".format(_id) # ) return None if self.all_opended(): raise SanguoException( errormsg.HEROPANEL_ALL_OPENED, self.char_id, "HeroPanel Open", "all opened." ) none_opened_heros = self.none_opened_heros() none_opened_other_heros = [(socket_id, h) for socket_id, h in none_opened_heros if not h.good] def _random_get(): if none_opened_other_heros: return random.choice(none_opened_other_heros) return random.choice(none_opened_heros) using_sycee = self.get_hero_cost(incr=True) resource = Resource(self.char_id, "HeroPanel Open") with resource.check(sycee=-using_sycee): if not self.has_got_good_hero(): # 还没有取到甲卡 if self.panel.refresh_times == 0: # 新角色第一次抽卡,给好卡 prob = 100 else: prob = GET_HERO_QUALITY_ONE_PROB[self.open_times + 1] if random.randint(1, 100) <= prob: # 取得甲卡 for k, v in none_opened_heros: if v.good: socket_id, hero = k, v break else: socket_id, hero = _random_get() else: socket_id, hero = _random_get() else: socket_id, hero = _random_get() self.panel.panel[str(_id)], self.panel.panel[socket_id] = self.panel.panel[socket_id], self.panel.panel[str(_id)] self.panel.panel[str(_id)].opened = True self.panel.save() save_hero(self.char_id, hero.oid) self.send_notify() Task(self.char_id).trig(7) return hero.oid
def prisoner_get(self, _id, treasures): str_id = str(_id) if str_id not in self.p.prisoners: raise SanguoException( errormsg.PRISONER_NOT_EXIST, self.char_id, "Prisoner Get", "{0} not exist".format(_id) ) if not self.p.prisoners[str_id].active: raise SanguoException( errormsg.PRISONER_NOT_ACTIVE, self.char_id, "Prisoner Get", "{0} not active".format(_id) ) treasures_prob = 0 for tid in treasures: try: treasures_prob += TREASURES[tid].value except KeyError: raise SanguoException( errormsg.STUFF_NOT_EXIST, self.char_id, "Prisoner Get", "treasure {0} not exist".format(tid) ) using_stuffs = [(tid, 1) for tid in treasures] resource = Resource(self.char_id, "Prisoner Get") with resource.check(stuffs=using_stuffs): got = False prob = self.p.prisoners[str_id].prob + treasures_prob char = Char(self.char_id).mc vip_plus = VIP_FUNCTION[char.vip].prisoner_get prob += vip_plus if prob >= random.randint(1, 100): # got it save_hero(self.char_id, self.p.prisoners[str_id].oid) got = True self.p.prisoners.pop(str_id) msg = protomsg.RemovePrisonerNotify() msg.ids.append(_id) publish_to_char(self.char_id, pack_msg(msg)) else: self.p.prisoners[str_id].active = False msg = protomsg.UpdatePrisonerNotify() p = msg.prisoner.add() p_obj = self.p.prisoners[str_id] self._fill_up_prisoner_msg(p, _id, p_obj.oid, p_obj.prob, p_obj.active) publish_to_char(self.char_id, pack_msg(msg)) self.p.save() t = Task(self.char_id) t.trig(5) return got
def open(self, _id): if str(_id) not in self.panel.panel: raise SanguoException(errormsg.HEROPANEL_SOCKET_NOT_EXIST, self.char_id, "HeroPanel Open", "HeroPanel Socket {0} not exist".format(_id)) if self.panel.panel[str(_id)].opened: # raise SanguoException( # errormsg.HEROPANEL_SOCKET_ALREADY_OPENED, # self.char_id, # "HeroPanel Open", # "HeroPanel Socket {0} already opended".format(_id) # ) return None if self.all_opended(): raise SanguoException(errormsg.HEROPANEL_ALL_OPENED, self.char_id, "HeroPanel Open", "all opened.") none_opened_heros = self.none_opened_heros() none_opened_other_heros = [(socket_id, h) for socket_id, h in none_opened_heros if not h.good] def _random_get(): if none_opened_other_heros: return random.choice(none_opened_other_heros) return random.choice(none_opened_heros) using_sycee = self.get_hero_cost(incr=True) resource = Resource(self.char_id, "HeroPanel Open") with resource.check(sycee=-using_sycee): if not self.has_got_good_hero(): # 还没有取到甲卡 if self.panel.refresh_times == 0: # 新角色第一次抽卡,给好卡 prob = 100 else: prob = GET_HERO_QUALITY_ONE_PROB[self.open_times + 1] if random.randint(1, 100) <= prob: # 取得甲卡 for k, v in none_opened_heros: if v.good: socket_id, hero = k, v break else: socket_id, hero = _random_get() else: socket_id, hero = _random_get() else: socket_id, hero = _random_get() self.panel.panel[str(_id)], self.panel.panel[ socket_id] = self.panel.panel[socket_id], self.panel.panel[str( _id)] self.panel.panel[str(_id)].opened = True self.panel.save() save_hero(self.char_id, hero.oid) self.send_notify() Task(self.char_id).trig(7) return hero.oid