예제 #1
0
    def __init__(self, char_id):
        self.char_id = char_id

        try:
            self.mongo_horse = MongoHorse.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_horse = MongoHorse(id=self.char_id)
            self.mongo_horse.horses = {}
            self.mongo_horse.strengthed_horse = {}
            self.mongo_horse.save()
예제 #2
0
    def __init__(self, char_id):
        self.char_id = char_id

        try:
            self.mongo_horse = MongoHorse.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_horse = MongoHorse(id=self.char_id)
            self.mongo_horse.horses = {}
            self.mongo_horse.strengthed_horse = {}
            self.mongo_horse.save()
예제 #3
0
class Horse(object):
    def __init__(self, char_id):
        self.char_id = char_id

        try:
            self.mongo_horse = MongoHorse.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_horse = MongoHorse(id=self.char_id)
            self.mongo_horse.horses = {}
            self.mongo_horse.strengthed_horse = {}
            self.mongo_horse.save()

    def has_horse(self, horse_id):
        return str(horse_id) in self.mongo_horse.horses

    def add(self, oid):
        assert oid in HORSE

        embedded_horse = MongoEmbeddedHorse()
        embedded_horse.oid = oid
        embedded_horse.attack = 0
        embedded_horse.defense = 0
        embedded_horse.hp = 0

        new_id = id_generator('equipment')[0]

        self.mongo_horse.horses[str(new_id)] = embedded_horse
        self.mongo_horse.save()

        hobj = OneHorse(
            new_id,
            embedded_horse.oid,
            embedded_horse.attack,
            embedded_horse.defense,
            embedded_horse.hp
        )

        msg = HorsesAddNotify()
        msg_h = msg.horses.add()
        msg_h.MergeFrom(hobj.make_msg())
        publish_to_char(self.char_id, pack_msg(msg))

    def remove(self, _id):
        self.mongo_horse.horses.pop(str(_id))
        self.mongo_horse.save()

        msg = HorsesRemoveNotify()
        msg.ids.extend([_id])
        publish_to_char(self.char_id, pack_msg(msg))


    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 sell(self, _id):
        self.check_sell(_id)

        h = self.mongo_horse.horses[str(_id)]
        got_gold = HORSE[h.oid].sell_gold

        resource = Resource(self.char_id, "Horse Sell", "sell horse {0}".format(_id))
        resource.add(gold=got_gold)

        self.remove(_id)



    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_confirm(self, cancel=False):
        try:
            _id, h = self.mongo_horse.strengthed_horse.items()[0]
        except IndexError:
            raise SanguoException(
                errormsg.INVALID_OPERATE,
                self.char_id,
                "Horse Strength Confirm",
                "no data for confirm"
            )

        self.mongo_horse.strengthed_horse = {}
        self.mongo_horse.save()

        if not cancel:
            self.mongo_horse.horses[str(_id)] = h
            self.mongo_horse.save()
            self.send_update_notify(_id, h)


    def evolution(self, horse_id, horse_soul_id):
        from core.item import Item

        try:
            h = self.mongo_horse.horses[str(horse_id)]
        except KeyError:
            raise SanguoException(
                errormsg.HORSE_NOT_EXIST,
                self.char_id,
                "Hero Evolution",
                "horse {0} not exist".format(horse_id)
            )

        item = Item(self.char_id)
        if not item.has_stuff(horse_soul_id):
            raise SanguoException(
                errormsg.STUFF_NOT_EXIST,
                self.char_id,
                "Hero Evolution",
                "horse soul {0} not exist".format(horse_soul_id)
            )

        stuff = STUFFS[horse_soul_id]
        if stuff.tp != 4:
            raise SanguoException(
                errormsg.INVALID_OPERATE,
                self.char_id,
                "Hero Evolution",
                "stuff {0} tp {1} != 4".format(horse_soul_id, stuff.tp)
            )

        if stuff.value not in HORSE:
            raise SanguoException(
                errormsg.INVALID_OPERATE,
                self.char_id,
                "Hero Evolution",
                "stuff value not in HORSE".format(stuff.value)
            )

        item.stuff_remove(horse_soul_id, 1)

        h.oid = stuff.value
        self.mongo_horse.save()
        self.send_update_notify(horse_id, h)


    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 send_notify(self):
        msg = HorsesNotify()
        for k, v in self.mongo_horse.horses.iteritems():
            hobj = OneHorse(int(k), v.oid, v.attack, v.defense, v.hp)
            msg_h = msg.horses.add()
            msg_h.MergeFrom(hobj.make_msg())

        publish_to_char(self.char_id, pack_msg(msg))
예제 #4
0
class Horse(object):
    def __init__(self, char_id):
        self.char_id = char_id

        try:
            self.mongo_horse = MongoHorse.objects.get(id=self.char_id)
        except DoesNotExist:
            self.mongo_horse = MongoHorse(id=self.char_id)
            self.mongo_horse.horses = {}
            self.mongo_horse.strengthed_horse = {}
            self.mongo_horse.save()

    def has_horse(self, horse_id):
        return str(horse_id) in self.mongo_horse.horses

    def add(self, oid):
        assert oid in HORSE

        embedded_horse = MongoEmbeddedHorse()
        embedded_horse.oid = oid
        embedded_horse.attack = 0
        embedded_horse.defense = 0
        embedded_horse.hp = 0

        new_id = id_generator('equipment')[0]

        self.mongo_horse.horses[str(new_id)] = embedded_horse
        self.mongo_horse.save()

        hobj = OneHorse(new_id, embedded_horse.oid, embedded_horse.attack,
                        embedded_horse.defense, embedded_horse.hp)

        msg = HorsesAddNotify()
        msg_h = msg.horses.add()
        msg_h.MergeFrom(hobj.make_msg())
        publish_to_char(self.char_id, pack_msg(msg))

    def remove(self, _id):
        self.mongo_horse.horses.pop(str(_id))
        self.mongo_horse.save()

        msg = HorsesRemoveNotify()
        msg.ids.extend([_id])
        publish_to_char(self.char_id, pack_msg(msg))

    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 sell(self, _id):
        self.check_sell(_id)

        h = self.mongo_horse.horses[str(_id)]
        got_gold = HORSE[h.oid].sell_gold

        resource = Resource(self.char_id, "Horse Sell",
                            "sell horse {0}".format(_id))
        resource.add(gold=got_gold)

        self.remove(_id)

    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_confirm(self, cancel=False):
        try:
            _id, h = self.mongo_horse.strengthed_horse.items()[0]
        except IndexError:
            raise SanguoException(errormsg.INVALID_OPERATE, self.char_id,
                                  "Horse Strength Confirm",
                                  "no data for confirm")

        self.mongo_horse.strengthed_horse = {}
        self.mongo_horse.save()

        if not cancel:
            self.mongo_horse.horses[str(_id)] = h
            self.mongo_horse.save()
            self.send_update_notify(_id, h)

    def evolution(self, horse_id, horse_soul_id):
        from core.item import Item

        try:
            h = self.mongo_horse.horses[str(horse_id)]
        except KeyError:
            raise SanguoException(errormsg.HORSE_NOT_EXIST, self.char_id,
                                  "Hero Evolution",
                                  "horse {0} not exist".format(horse_id))

        item = Item(self.char_id)
        if not item.has_stuff(horse_soul_id):
            raise SanguoException(
                errormsg.STUFF_NOT_EXIST, self.char_id, "Hero Evolution",
                "horse soul {0} not exist".format(horse_soul_id))

        stuff = STUFFS[horse_soul_id]
        if stuff.tp != 4:
            raise SanguoException(
                errormsg.INVALID_OPERATE, self.char_id, "Hero Evolution",
                "stuff {0} tp {1} != 4".format(horse_soul_id, stuff.tp))

        if stuff.value not in HORSE:
            raise SanguoException(
                errormsg.INVALID_OPERATE, self.char_id, "Hero Evolution",
                "stuff value not in HORSE".format(stuff.value))

        item.stuff_remove(horse_soul_id, 1)

        h.oid = stuff.value
        self.mongo_horse.save()
        self.send_update_notify(horse_id, h)

    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 send_notify(self):
        msg = HorsesNotify()
        for k, v in self.mongo_horse.horses.iteritems():
            hobj = OneHorse(int(k), v.oid, v.attack, v.defense, v.hp)
            msg_h = msg.horses.add()
            msg_h.MergeFrom(hobj.make_msg())

        publish_to_char(self.char_id, pack_msg(msg))