Exemplo n.º 1
0
    def battle(self, _id):
        try:
            self.this_stage = STAGE_ACTIVITY[_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ACTIVITY_NOT_EXIST,
                                  self.char_id, "StageActivity Battle",
                                  "StageActivity {0} not exist".format(_id))

        if _id not in self.stage.activities:
            raise SanguoException(errormsg.STAGE_ACTIVITY_NOT_OPEN,
                                  self.char_id, "StageActivity Battle",
                                  "StageActivity {0} not open".format(_id))

        counter = ActivityStageCount(self.char_id)
        counter.make_func_name(self.this_stage.tp)

        if counter.remained_value <= 0:
            raise SanguoException(
                errormsg.STAGE_ACTIVITY_TOTAL_NO_TIMES, self.char_id,
                "StageActivity Battle",
                "StageActivity no total times. battle {0}".format(_id))

        battle_msg = protomsg.Battle()
        b = ActivityPVE(self.char_id, _id, battle_msg)
        b.start()

        if battle_msg.self_win:
            counter.incr()
            self.send_remained_times_notify()

            Task(self.char_id).trig(9)

        return battle_msg
Exemplo n.º 2
0
    def battle(self):
        need_sycee = 0

        counter = Counter(self.char_id, 'arena')
        if counter.remained_value <= 0:
            counter = Counter(self.char_id, 'arena_buy')
            if counter.remained_value <= 0:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(
                            char.vip, VIP_MAX_LEVEL))
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL, self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(
                        VIP_MAX_LEVEL))
            else:
                need_sycee = ARENA_COST_SYCEE

        rival_id = self.choose_rival()
        if not rival_id:
            raise SanguoException(errormsg.ARENA_NO_RIVAL, self.char_id,
                                  "Arena Battle", "no rival.")

        if need_sycee:
            resource = Resource(self.char_id, "Arena Battle",
                                "battle for no free times")
            resource.check_and_remove(sycee=-need_sycee)

        counter.incr()

        # set battle cd
        redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD)

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        if msg.self_win:
            achievement = Achievement(self.char_id)
            achievement.trig(11, 1)

        self_score = self.score
        rival_arena = Arena(rival_id)
        rival_score = rival_arena.score

        new_score = calculate_score(self_score, rival_score, msg.self_win)
        self.set_score(new_score)

        t = Task(self.char_id)
        t.trig(2)

        self.send_notify(score=new_score)

        rival_arena.be_beaten(rival_score, self_score, not msg.self_win,
                              self.char_id)

        return msg
Exemplo n.º 3
0
    def battle(self, _id):
        str_id = str(_id)

        try:
            self.this_stage = STAGE_ELITE[_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ELITE_NOT_EXIST, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} not exist".format(_id))

        try:
            times = self.stage.elites[str_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ELITE_NOT_OPEN, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} not open".format(_id))

        if str(self.this_stage.open_condition) not in self.stage.stages:
            if self.this_stage.previous and str(
                    self.this_stage.previous) not in self.stage.elites:
                raise SanguoException(
                    errormsg.STAGE_ELITE_NOT_OPEN, self.char_id,
                    "StageElite Battle",
                    "StageElite {0} not open. XXX check source core/stage/EliteStage Open"
                    .format(_id))

        if times >= self.this_stage.times:
            raise SanguoException(errormsg.STAGE_ELITE_NO_TIMES, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} no times".format(_id))

        counter = Counter(self.char_id, 'stage_elite')
        if counter.remained_value <= 0:
            raise SanguoException(errormsg.STAGE_ELITE_TOTAL_NO_TIMES,
                                  self.char_id, "StageElite Battle",
                                  "stageElite no total times.")

        battle_msg = protomsg.Battle()
        b = ElitePVE(self.char_id, _id, battle_msg)
        b.start()

        if battle_msg.self_win:
            self.stage.elites[str_id] += 1
            if self.this_stage.next:

                if str(self.this_stage.next) not in self.stage.elites:
                    self.stage.elites[str(self.this_stage.next)] = 0
                    self.send_new_notify(self.this_stage.next)
            self.stage.save()

            self.send_update_notify(_id)

            counter.incr()
            self.send_remained_times_notify()
            self.enable_next_elite_stage(_id)

        return battle_msg
Exemplo n.º 4
0
    def battle(self):
        counter = Counter(self.char_id, 'arena')
        try:
            # 免费次数
            counter.incr()
        except CounterOverFlow:
            counter = Counter(self.char_id, 'arena_buy')

            try:
                # 花费元宝次数
                counter.incr()
            except CounterOverFlow:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(
                            char.vip, VIP_MAX_LEVEL))
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL, self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(
                        VIP_MAX_LEVEL))

            else:
                resource = Resource(self.char_id, "Arena Battle",
                                    "battle for no free times")
                resource.check_and_remove(sycee=-ARENA_COST_SYCEE)

        rival_id = self.choose_rival()

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        achievement = Achievement(self.char_id)

        if msg.self_win:
            score = ARENA_GET_SCORE_WHEN_WIN
            achievement.trig(11, 1)
            self.mongo_arena.continues_win += 1
        else:
            score = ARENA_GET_SCORE_WHEN_LOST
            self.mongo_arena.continues_win = 0

        self.mongo_arena.save()

        if score:
            self.mongo_day.score += score
            self.mongo_day.save()

        t = Task(self.char_id)
        t.trig(2)

        self.send_notify()
        return msg
Exemplo n.º 5
0
    def battle(self, boss_id):
        try:
            this_boss = self.mongo_boss.opened[str(boss_id)]
        except KeyError:
            raise SanguoException(
                errormsg.UNION_BOSS_NOT_STARTED,
                self.char_id,
                "UnionBoss Battle",
                "boss not started {0}".format(boss_id)
            )

        if this_boss.hp <= 0:
            raise SanguoException(
                errormsg.UNION_BOSS_DEAD,
                self.char_id,
                "UnionBoss Battle",
                "boss dead {0}".format(boss_id)
            )

        # TODO boss times limit


        msg = protomsg.Battle()
        battle = UnionBossBattle(self.char_id, boss_id, msg, this_boss.hp)
        remained_hp = battle.start()

        if remained_hp == 0:
            killer = self.char_id
        else:
            killer = 0

        this_boss.hp = remained_hp
        this_boss.killer = killer

        eubl = MongoEmbeddedUnionBossLog()
        eubl.char_id = self.char_id
        eubl.damage = battle.get_total_damage()

        this_boss.logs.append(eubl)
        self.mongo_boss.save()

        self.incr_battle_times()
        self.after_battle(eubl.damage)

        if killer:
            self.boss_has_been_killed(boss_id)

        return msg
Exemplo n.º 6
0
    def battle(self, boss_id):
        try:
            with Lock('union_boss_battle').lock():
                self.load_data()
                try:
                    this_boss = self.mongo_boss.opened[str(boss_id)]
                except KeyError:
                    raise SanguoException(
                        errormsg.UNION_BOSS_NOT_STARTED, self.char_id,
                        "UnionBoss Battle",
                        "boss not started {0}".format(boss_id))

                if this_boss.hp <= 0:
                    raise SanguoException(errormsg.UNION_BOSS_DEAD,
                                          self.char_id, "UnionBoss Battle",
                                          "boss dead {0}".format(boss_id))

                if self.cur_times >= self.max_times:
                    raise SanguoException(errormsg.UNION_BOSS_NO_TIMES,
                                          self.char_id, "UnionBoss Battle",
                                          "no times")

                msg = protomsg.Battle()

                battle = UnionBossBattle(self.char_id, boss_id, msg,
                                         this_boss.hp)
                remained_hp = battle.start()
                damage = battle.get_total_damage()

                this_boss.hp = remained_hp
                if remained_hp <= 0:
                    this_boss.killer = self.char_id

                self.save_battle_log(this_boss, damage)
                self.incr_battle_times()
                drop_msg = self.after_battle(boss_id, damage, remained_hp <= 0)

                if remained_hp <= 0:
                    self.boss_has_been_killed(boss_id)

                self.mongo_boss.save()
                return msg, drop_msg
        except LockTimeOut:
            raise SanguoException(errormsg.UNION_BOSS_LOCKED, self.char_id,
                                  "UnionBoss Battle",
                                  "boss locked {0}".format(boss_id))
Exemplo n.º 7
0
    def battle(self):
        need_sycee = 0

        counter = Counter(self.char_id, 'arena')
        if counter.remained_value <= 0:
            counter = Counter(self.char_id, 'arena_buy')
            if counter.remained_value <= 0:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES,
                        self.char_id,
                        "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(char.vip, VIP_MAX_LEVEL)
                    )
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL,
                    self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(VIP_MAX_LEVEL)
                )
            else:
                need_sycee = ARENA_COST_SYCEE

        rival_id = self.choose_rival()
        if not rival_id:
            raise SanguoException(
                errormsg.ARENA_NO_RIVAL,
                self.char_id,
                "Arena Battle",
                "no rival."
            )

        if need_sycee:
            resource = Resource(self.char_id, "Arena Battle", "battle for no free times")
            resource.check_and_remove(sycee=-need_sycee)

        counter.incr()

        # set battle cd
        redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD)

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        t = Task(self.char_id)
        t.trig(2)

        drop = make_standard_drop_from_template()
        adding_score = 0
        if msg.self_win:
            achievement = Achievement(self.char_id)
            achievement.trig(11, 1)

            # 只有打赢才设置积分
            self_score = self.score
            rival_arena = Arena(rival_id)
            rival_score = rival_arena.score

            new_score = calculate_score(self_score, rival_score, msg.self_win)
            self.set_score(new_score)
            adding_score = new_score - self_score

            rival_arena.be_beaten(rival_score, self_score, not msg.self_win, self.char_id)

            TimesLogArenaWin(self.char_id).inc()

            ae = ActivityEntry(self.char_id, 50004)
            if ae and ae.is_valid():
                drop = ae.get_additional_drop()
                Resource(self.char_id, "Arena Win").add(**drop)

        TimesLogArena(self.char_id).inc()
        ae = ActivityEntry(self.char_id, 40006)
        if ae:
            ae.trig()

        self.send_notify()
        drop['stuffs'].append((1001, adding_score))
        return msg, drop
Exemplo n.º 8
0
    def battle(self, stage_id):
        try:
            this_stage = STAGES[stage_id]
        except KeyError:
            raise SanguoException(
                errormsg.STAGE_NOT_EXIST,
                self.char_id,
                "Stage Battle",
                "Stage {0} not exist".format(stage_id)
            )

        char = Char(self.char_id)
        char_level = char.mc.level
        if char_level < this_stage.level_limit:
            raise SanguoException(
                errormsg.STAGE_LEVEL_GREATER_THAN_CHAR_LEVEL,
                self.char_id,
                "Stage Battle",
                "Stage {0} level limit {1} > char level {2}".format(stage_id, this_stage.level_limit, char_level)
            )

        open_condition = this_stage.open_condition
        if open_condition and str(open_condition) not in self.stage.stages:
            raise SanguoException(
                errormsg.STAGE_NOT_OPEN,
                self.char_id,
                "Stage Battle",
                "Stage {0} not open. condition is {1}".format(stage_id, open_condition)
            )

        if str(stage_id) not in self.stage.stages:
            self.first = True

        battle_msg = protomsg.Battle()
        b = PVE(self.char_id, stage_id, battle_msg)
        b.start()

        star = False
        if battle_msg.first_ground.self_win and battle_msg.second_ground.self_win and battle_msg.third_ground.self_win:
            star = True
            if not self.stage.stages.get(str(stage_id), False):
                self.first_star = True

            if stage_id > self.stage.max_star_stage:
                self.stage.max_star_stage = stage_id

        self.star = star

        if battle_msg.self_win:
            # 当前关卡星设置
            old_star = self.stage.stages.get(str(stage_id), False)
            if not old_star:
                self.stage.stages[str(stage_id)] = star

            # 设置新关卡
            stage_new = getattr(this_stage, 'next', None)
            if stage_new:
                if str(stage_new) not in self.stage.stages:
                    if self.stage.stage_new != stage_new:
                        self.stage.stage_new = stage_new
                        self.send_new_stage_notify()

            # 发送通知
            msg = protomsg.CurrentStageNotify()
            opened_func = FunctionOpen(self.char_id).trig_by_stage_id(stage_id)
            self._msg_stage(msg.stage, stage_id, old_star or star)
            msg.funcs.extend(opened_func)
            publish_to_char(self.char_id, pack_msg(msg))

        self.stage.save()

        if battle_msg.self_win:
            # 开启精英关卡
            elite = EliteStage(self.char_id)
            elite.enable_by_condition_id(stage_id)


        pve_finished_signal.send(
            sender=None,
            char_id=self.char_id,
            stage_id=stage_id,
            win=battle_msg.self_win,
            star=star,
        )

        return battle_msg