Ejemplo n.º 1
0
    def refresh_event_id(self):
        if not self.open:
            return

        working_amount = self.get_working_slot_amount()
        try:
            i1, i2 = EVENT_INTERVAL[working_amount]
        except KeyError:
            return

        interval = random.randint(i1, i2)
        now = arrow.utcnow().timestamp
        if self.event_at + interval > now:
            return

        event_id = random.choice(ConfigTerritoryBuilding.get(self.id).levels[self.level].events)
        self.event_id = event_id
        self.event_at = now

        # NOTE 这里自己存盘
        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'buildings.{0}.event_id'.format(self.id): self.event_id,
                'buildings.{0}.event_at'.format(self.id): self.event_at,
            }}
        )
Ejemplo n.º 2
0
    def help(self, friend_id, building_id):
        friend_id = int(friend_id)
        if not FriendManager(self.server_id,
                             self.char_id).check_friend_exist(friend_id):
            raise GameException(
                ConfigErrorMessage.get_error_id("FRIEND_NOT_OK"))

        if not self.get_remained_help_times():
            raise GameException(
                ConfigErrorMessage.get_error_id(
                    "TERRITORY_NO_HELP_FRIEND_TIMES"))

        if not TerritoryFriend(self.server_id,
                               friend_id).get_remained_got_help_times():
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_NO_GOT_HELP_TIMES"))

        t = Territory(self.server_id, friend_id)
        building = t.get_building_object(building_id, slots_ids=[])

        event_id = building.event_id

        if not event_id:
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_BUILDING_NO_EVENT"))

        MongoTerritory.db(self.server_id).update_one(
            {'_id': friend_id},
            {'$set': {
                'buildings.{0}.event_id'.format(building_id): 0
            }})

        config = ConfigTerritoryEvent.get(event_id)
        if not config.npc:
            resource_classified = ResourceClassification.classify(
                config.reward_win)
            resource_classified.add(self.server_id,
                                    self.char_id,
                                    message="TerritoryFriend.help")

            # NOTE: 战斗要等到结算的时候再记录次数
            ValueLogTerritoryHelpFriendTimes(self.server_id,
                                             self.char_id).record()
            self.send_remained_times_notify()

            Territory(self.server_id,
                      friend_id).got_help(self.char_id, building_id,
                                          config.target_exp)

            return None, resource_classified

        npc_club = ConfigNPCFormation.get(config.npc)
        my_club = Club(self.server_id, self.char_id)

        f = Formation(self.server_id, self.char_id)
        match = ClubMatch(my_club, npc_club, 6, f.get_skill_sequence(), {})
        msg = match.start()
        msg.key = "{0}:{1}:{2}".format(friend_id, building_id, event_id)
        msg.map_name = GlobalConfig.value_string("MATCH_MAP_TERRITORY_FRIEND")
        return msg, None
Ejemplo n.º 3
0
    def refresh_event_id(self):
        if not self.open:
            return

        working_amount = self.get_working_slot_amount()
        try:
            i1, i2 = EVENT_INTERVAL[working_amount]
        except KeyError:
            return

        interval = random.randint(i1, i2)
        now = arrow.utcnow().timestamp
        if self.event_at + interval > now:
            return

        event_id = random.choice(
            ConfigTerritoryBuilding.get(self.id).levels[self.level].events)
        self.event_id = event_id
        self.event_at = now

        # NOTE 这里自己存盘
        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id}, {
            '$set': {
                'buildings.{0}.event_id'.format(self.id): self.event_id,
                'buildings.{0}.event_at'.format(self.id): self.event_at,
            }
        })
Ejemplo n.º 4
0
    def try_unlock_slot(self, send_notify=True):
        vip_level = VIP(self.server_id, self.char_id).level
        updater = {}

        bid_sids = []

        for building_id, config_building in ConfigTerritoryBuilding.INSTANCES.iteritems(
        ):
            for slot_id, config_slot in config_building.slots.iteritems():
                if str(slot_id) in self.doc['buildings'][str(
                        building_id)]['slots']:
                    # 已经开启了
                    continue

                # 条件满足一个就可以
                if (vip_level >= config_slot.need_vip_level) or (
                        self.doc['buildings'][str(building_id)]['level'] >=
                        config_slot.need_building_level):
                    slot_doc = MongoTerritory.document_slot()
                    self.doc['buildings'][str(building_id)]['slots'][str(
                        slot_id)] = slot_doc
                    updater['buildings.{0}.slots.{1}'.format(
                        building_id, slot_id)] = slot_doc

                    bid_sids.append((building_id, slot_id))

        if updater:
            MongoTerritory.db(self.server_id).update_one({'_id': self.char_id},
                                                         {'$set': updater})

        if send_notify:
            for bid, sid in bid_sids:
                self.send_notify(building_id=bid, slot_id=sid)
Ejemplo n.º 5
0
    def try_unlock_slot(self, send_notify=True):
        vip_level = VIP(self.server_id, self.char_id).level
        updater = {}

        bid_sids = []

        for building_id, config_building in ConfigTerritoryBuilding.INSTANCES.iteritems():
            for slot_id, config_slot in config_building.slots.iteritems():
                if str(slot_id) in self.doc['buildings'][str(building_id)]['slots']:
                    # 已经开启了
                    continue

                # 条件满足一个就可以
                if (vip_level >= config_slot.need_vip_level) or (
                            self.doc['buildings'][str(building_id)]['level'] >= config_slot.need_building_level):
                    slot_doc = MongoTerritory.document_slot()
                    self.doc['buildings'][str(building_id)]['slots'][str(slot_id)] = slot_doc
                    updater['buildings.{0}.slots.{1}'.format(building_id, slot_id)] = slot_doc

                    bid_sids.append((building_id, slot_id))

        if updater:
            MongoTerritory.db(self.server_id).update_one(
                {'_id': self.char_id},
                {'$set': updater}
            )

        if send_notify:
            for bid, sid in bid_sids:
                self.send_notify(building_id=bid, slot_id=sid)
Ejemplo n.º 6
0
    def training_get_reward(self, building_id, slot_id):
        building = self.get_building_object(building_id, slots_ids=[slot_id])
        try:
            slot = building.slots[slot_id]
        except KeyError:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if not slot.open:
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_SLOT_LOCK"))

        if not slot.finished:
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_SLOT_NOT_FINISH"))

        reward = slot.reward

        level_up = building.add_exp(reward['building_exp'])
        building.add_product(reward['product_amount'])

        empty_slot_doc = MongoTerritory.document_slot()

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp
        self.doc['buildings'][str(
            building_id)]['product_amount'] = building.product_amount
        self.doc['buildings'][str(building_id)]['slots'][str(
            slot_id)] = empty_slot_doc

        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id}, {
            '$set': {
                'buildings.{0}.level'.format(building_id):
                building.level,
                'buildings.{0}.exp'.format(building_id):
                building.exp,
                'buildings.{0}.product_amount'.format(building_id):
                building.product_amount,
                'buildings.{0}.slots.{1}'.format(building_id, slot_id):
                empty_slot_doc
            }
        })

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id, slot_id=slot_id)

        resource_classified = ResourceClassification.classify(reward['items'])
        resource_classified.add(
            self.server_id,
            self.char_id,
            message="Territory.training_get_reward:{0}".format(building_id))

        # 把建筑产出也要发回到客户端
        resource_classified.bag.append(
            (BUILDING_PRODUCT_ID_TABLE[building_id], reward['product_amount']))
        return resource_classified
Ejemplo n.º 7
0
    def add_work_card(self, amount):
        self.doc['work_card'] += amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'work_card': self.doc['work_card']
            }})

        self.send_notify(building_id=[], slot_id=[])
Ejemplo n.º 8
0
    def remove_work_card(self, amount):
        new_amount = self.check_work_card(amount)
        self.doc['work_card'] = new_amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'work_card': self.doc['work_card'],
            }})

        self.send_notify(building_id=[], slot_id=[])
Ejemplo n.º 9
0
    def add_work_card(self, amount):
        self.doc['work_card'] += amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'work_card': self.doc['work_card']
            }}
        )

        self.send_notify(building_id=[], slot_id=[])
Ejemplo n.º 10
0
    def remove_work_card(self, amount):
        new_amount = self.check_work_card(amount)
        self.doc['work_card'] = new_amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'work_card': self.doc['work_card'],
            }}
        )

        self.send_notify(building_id=[], slot_id=[])
Ejemplo n.º 11
0
    def help(self, friend_id, building_id):
        friend_id = int(friend_id)
        if not FriendManager(self.server_id, self.char_id).check_friend_exist(friend_id):
            raise GameException(ConfigErrorMessage.get_error_id("FRIEND_NOT_OK"))

        if not self.get_remained_help_times():
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_NO_HELP_FRIEND_TIMES"))

        if not TerritoryFriend(self.server_id, friend_id).get_remained_got_help_times():
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_NO_GOT_HELP_TIMES"))

        t = Territory(self.server_id, friend_id)
        building = t.get_building_object(building_id, slots_ids=[])

        event_id = building.event_id

        if not event_id:
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_BUILDING_NO_EVENT"))

        MongoTerritory.db(self.server_id).update_one(
            {'_id': friend_id},
            {'$set': {
                'buildings.{0}.event_id'.format(building_id): 0
            }}
        )

        config = ConfigTerritoryEvent.get(event_id)
        if not config.npc:
            resource_classified = ResourceClassification.classify(config.reward_win)
            resource_classified.add(self.server_id, self.char_id, message="TerritoryFriend.help")

            # NOTE: 战斗要等到结算的时候再记录次数
            ValueLogTerritoryHelpFriendTimes(self.server_id, self.char_id).record()
            self.send_remained_times_notify()

            Territory(self.server_id, friend_id).got_help(self.char_id, building_id, config.target_exp)

            return None, resource_classified

        npc_club = ConfigNPCFormation.get(config.npc)
        my_club = Club(self.server_id, self.char_id)

        f = Formation(self.server_id, self.char_id)
        match = ClubMatch(my_club, npc_club, 6, f.get_skill_sequence(), {})
        msg = match.start()
        msg.key = "{0}:{1}:{2}".format(friend_id, building_id, event_id)
        msg.map_name = GlobalConfig.value_string("MATCH_MAP_TERRITORY_FRIEND")
        return msg, None
Ejemplo n.º 12
0
    def building_auto_increase_product(self):
        updater = {}

        buildings = self.get_all_building_objects()
        for bid, b in buildings.iteritems():
            if b.auto_increase_product():
                self.doc['buildings'][str(
                    bid)]['product_amount'] = b.product_amount
                updater['buildings.{0}.product_amount'.format(
                    bid)] = b.product_amount

        if updater:
            MongoTerritory.db(self.server_id).update_one({'_id': self.char_id},
                                                         {'$set': updater})

            self.send_notify()
Ejemplo n.º 13
0
    def building_auto_increase_product(self):
        updater = {}

        buildings = self.get_all_building_objects()
        for bid, b in buildings.iteritems():
            if b.auto_increase_product():
                self.doc['buildings'][str(bid)]['product_amount'] = b.product_amount
                updater['buildings.{0}.product_amount'.format(bid)] = b.product_amount

        if updater:
            MongoTerritory.db(self.server_id).update_one(
                {'_id': self.char_id},
                {'$set': updater}
            )

            self.send_notify()
Ejemplo n.º 14
0
    def training_get_reward(self, building_id, slot_id):
        building = self.get_building_object(building_id, slots_ids=[slot_id])
        try:
            slot = building.slots[slot_id]
        except KeyError:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if not slot.open:
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_SLOT_LOCK"))

        if not slot.finished:
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_SLOT_NOT_FINISH"))

        reward = slot.reward

        level_up = building.add_exp(reward['building_exp'])
        building.add_product(reward['product_amount'])

        empty_slot_doc = MongoTerritory.document_slot()

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp
        self.doc['buildings'][str(building_id)]['product_amount'] = building.product_amount
        self.doc['buildings'][str(building_id)]['slots'][str(slot_id)] = empty_slot_doc

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'buildings.{0}.level'.format(building_id): building.level,
                'buildings.{0}.exp'.format(building_id): building.exp,
                'buildings.{0}.product_amount'.format(building_id): building.product_amount,
                'buildings.{0}.slots.{1}'.format(building_id, slot_id): empty_slot_doc
            }}
        )

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id, slot_id=slot_id)

        resource_classified = ResourceClassification.classify(reward['items'])
        resource_classified.add(self.server_id, self.char_id,
                                message="Territory.training_get_reward:{0}".format(building_id))

        # 把建筑产出也要发回到客户端
        resource_classified.bag.append((BUILDING_PRODUCT_ID_TABLE[building_id], reward['product_amount']))
        return resource_classified
Ejemplo n.º 15
0
    def add_product(self, items):
        # items: [(_id, amount),]
        updater = {}

        for _id, amount in items:
            building_id = TERRITORY_PRODUCT_BUILDING_TABLE[_id]

            building = self.get_building_object(building_id, slots_ids=[])
            building.add_product(amount)

            self.doc['buildings'][str(building_id)]['product_amount'] = building.product_amount
            updater['buildings.{0}.product_amount'.format(building_id)] = building.product_amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': updater}
        )

        self.send_notify()
Ejemplo n.º 16
0
    def add_product(self, items):
        # items: [(_id, amount),]
        updater = {}

        for _id, amount in items:
            building_id = TERRITORY_PRODUCT_BUILDING_TABLE[_id]

            building = self.get_building_object(building_id, slots_ids=[])
            building.add_product(amount)

            self.doc['buildings'][str(
                building_id)]['product_amount'] = building.product_amount
            updater['buildings.{0}.product_amount'.format(
                building_id)] = building.product_amount

        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id},
                                                     {'$set': updater})

        self.send_notify()
Ejemplo n.º 17
0
    def inspire_building(self, building_id):
        building = self.get_building_object(building_id, slots_ids=[])
        drop, level_up = building.make_inspire()

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp

        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id}, {
            '$set': {
                'buildings.{0}.level'.format(building_id): building.level,
                'buildings.{0}.exp'.format(building_id): building.exp,
            }
        })

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id)
        return drop
Ejemplo n.º 18
0
    def add_building_exp(self, building_id, exp, help_from_char_id=None):
        building = self.get_building_object(building_id)
        level_up = building.add_exp(exp)

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp

        operation = {
            '$set': {
                'buildings.{0}.level'.format(building_id): building.level,
                'buildings.{0}.exp'.format(building_id): building.exp,
            }
        }

        if help_from_char_id:
            slots = []
            for _, s in building.slots.iteritems():
                if s.open and not s.finished:
                    slots.append(s)

            if slots:
                slot = random.choice(slots)

                log = (
                    4,
                    (get_club_property(self.server_id, help_from_char_id,
                                       'name'), str(exp)),
                    0,
                )
                operation['$push'] = {
                    'buildings.{0}.slots.{1}.report'.format(
                        building_id, slot.id):
                    log
                }

        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id},
                                                     operation)

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id)
Ejemplo n.º 19
0
    def remove_product(self, items):
        # items: [(_id, amount),]
        updater = {}

        for _id, amount in items:
            building_id = TERRITORY_PRODUCT_BUILDING_TABLE[_id]

            new_amount = self.doc['buildings'][str(building_id)]['product_amount'] - amount
            if new_amount < 0:
                new_amount = 0

            self.doc['buildings'][str(building_id)]['product_amount'] = new_amount
            updater['buildings.{0}.product_amount'.format(building_id)] = new_amount

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': updater}
        )

        self.send_notify()
Ejemplo n.º 20
0
    def auto_increase_product(cls, server_id):
        level_limit = GlobalConfig.value("TERRITORY_BUILDING_AUTO_INCREASE_LEVEL")
        level_condition = {'level': {'$gte': level_limit}}

        char_ids = Club.get_recent_login_char_ids(server_id, other_conditions=[level_condition])
        char_ids = [i for i in char_ids]

        docs = MongoTerritory.db(server_id).find({'_id': {'$in': char_ids}})
        for doc in docs:
            t = Territory(server_id, doc['_id'], doc)
            t.building_auto_increase_product()
Ejemplo n.º 21
0
    def __init__(self, server_id, char_id, doc=None):
        self.server_id = server_id
        self.char_id = char_id

        if doc:
            self.doc = doc
        else:
            self.doc = MongoTerritory.db(self.server_id).find_one({'_id': self.char_id})
            if not self.doc:
                self.doc = MongoTerritory.document()
                self.doc['_id'] = self.char_id
                self.doc['work_card'] = INIT_WORK_CARD

                for i in BUILDING_PRODUCT_ID_TABLE:
                    building_doc = MongoTerritory.document_building()
                    self.doc['buildings'][str(i)] = building_doc

                MongoTerritory.db(self.server_id).insert_one(self.doc)

        self.try_unlock_slot(send_notify=False)
Ejemplo n.º 22
0
    def inspire_building(self, building_id):
        building = self.get_building_object(building_id, slots_ids=[])
        drop, level_up = building.make_inspire()

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'buildings.{0}.level'.format(building_id): building.level,
                'buildings.{0}.exp'.format(building_id): building.exp,
            }}
        )

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id)
        return drop
Ejemplo n.º 23
0
    def add_building_exp(self, building_id, exp, help_from_char_id=None):
        building = self.get_building_object(building_id)
        level_up = building.add_exp(exp)

        self.doc['buildings'][str(building_id)]['level'] = building.level
        self.doc['buildings'][str(building_id)]['exp'] = building.exp

        operation = {
            '$set': {
                'buildings.{0}.level'.format(building_id): building.level,
                'buildings.{0}.exp'.format(building_id): building.exp,
            }
        }

        if help_from_char_id:
            slots = []
            for _, s in building.slots.iteritems():
                if s.open and not s.finished:
                    slots.append(s)

            if slots:
                slot = random.choice(slots)

                log = (
                    4,
                    (get_club_property(self.server_id, help_from_char_id, 'name'), str(exp)),
                    0,
                )
                operation['$push'] = {
                    'buildings.{0}.slots.{1}.report'.format(building_id, slot.id): log
                }

        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            operation
        )

        if level_up:
            self.try_unlock_slot()

        self.send_notify(building_id=building_id)
Ejemplo n.º 24
0
    def __init__(self, server_id, char_id, doc=None):
        self.server_id = server_id
        self.char_id = char_id

        if doc:
            self.doc = doc
        else:
            self.doc = MongoTerritory.db(self.server_id).find_one(
                {'_id': self.char_id})
            if not self.doc:
                self.doc = MongoTerritory.document()
                self.doc['_id'] = self.char_id
                self.doc['work_card'] = INIT_WORK_CARD

                for i in BUILDING_PRODUCT_ID_TABLE:
                    building_doc = MongoTerritory.document_building()
                    self.doc['buildings'][str(i)] = building_doc

                MongoTerritory.db(self.server_id).insert_one(self.doc)

        self.try_unlock_slot(send_notify=False)
Ejemplo n.º 25
0
    def remove_product(self, items):
        # items: [(_id, amount),]
        updater = {}

        for _id, amount in items:
            building_id = TERRITORY_PRODUCT_BUILDING_TABLE[_id]

            new_amount = self.doc['buildings'][str(
                building_id)]['product_amount'] - amount
            if new_amount < 0:
                new_amount = 0

            self.doc['buildings'][str(
                building_id)]['product_amount'] = new_amount
            updater['buildings.{0}.product_amount'.format(
                building_id)] = new_amount

        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id},
                                                     {'$set': updater})

        self.send_notify()
Ejemplo n.º 26
0
    def auto_increase_product(cls, server_id):
        level_limit = GlobalConfig.value(
            "TERRITORY_BUILDING_AUTO_INCREASE_LEVEL")
        level_condition = {'level': {'$gte': level_limit}}

        char_ids = Club.get_recent_login_char_ids(
            server_id, other_conditions=[level_condition])
        char_ids = [i for i in char_ids]

        docs = MongoTerritory.db(server_id).find({'_id': {'$in': char_ids}})
        for doc in docs:
            t = Territory(server_id, doc['_id'], doc)
            t.building_auto_increase_product()
Ejemplo n.º 27
0
    def training_star(self, building_id, slot_id, staff_id, hour):
        if hour not in TRAINING_HOURS:
            raise GameException(ConfigErrorMessage.get_error_id('BAD_MESSAGE'))

        sm = StaffManger(self.server_id, self.char_id)
        sm.check_staff([staff_id])

        building = self.get_building_object(building_id, slots_ids=[slot_id])
        try:
            slot = building.slots[slot_id]
        except KeyError:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if not slot.open:
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_SLOT_LOCK"))

        if slot.staff_id:
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_SLOT_HAS_STAFF"))

        if self.is_staff_training_check_by_oid(sm.get_staff_object(staff_id).oid):
            raise GameException(ConfigErrorMessage.get_error_id("TERRITORY_STAFF_IS_TRAINING"))

        config_slot = ConfigTerritoryBuilding.get(building_id).slots[slot_id]
        cost_amount = config_slot.get_cost_amount(building.level, TRAINING_HOURS.index(hour))

        new_amount = self.check_work_card(cost_amount)
        self.doc['work_card'] = new_amount

        slot.staff_id = staff_id
        slot.hour = hour

        start_at = arrow.utcnow().timestamp

        slot_doc = MongoTerritory.document_slot()
        slot_doc['staff_id'] = staff_id
        slot_doc['start_at'] = start_at
        slot_doc['hour'] = hour

        building_exp, product_amount = slot.get_building_reward()
        reward = {
            'building_exp': building_exp,
            'product_amount': product_amount,
            'items': []
        }

        ri = ResultItems()

        # 固定奖励
        report = [
            (
                1,
                [str(building_exp), ConfigItemNew.get(slot.product_id).name,
                 str(product_amount)],
                0,
            ),
        ]

        # 选手特产
        config_staff_product = ConfigTerritoryStaffProduct.get(sm.get_staff_object(staff_id).oid)
        if config_staff_product:
            _id, _amount = config_staff_product.get_product(TRAINING_HOURS.index(hour))
            if _amount:
                report.append((
                    2,
                    [ConfigItemNew.get(_id).name, str(_amount)],
                    0,
                ))

                ri.add(_id, _amount)

        # 概率奖励
        end_at = start_at + hour * 3600
        extra_at = start_at + 3600
        while extra_at < end_at:
            _id, _amount = ConfigTerritoryBuilding.get(building_id).slots[slot_id].get_extra_product(1)

            report.append((
                3,
                [ConfigItemNew.get(_id).name, str(_amount)],
                extra_at
            ))

            ri.add(_id, _amount)
            extra_at += 3600

        reward['items'] = ri.items.items()

        slot_doc['report'] = report
        slot_doc['reward'] = reward

        self.doc['buildings'][str(building_id)]['slots'][str(slot_id)] = slot_doc
        MongoTerritory.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'work_card': self.doc['work_card'],
                'buildings.{0}.slots.{1}'.format(building_id, slot_id): slot_doc
            }}
        )

        ValueLogTerritoryTrainingTimes(self.server_id, self.char_id).record()

        self.send_notify(building_id=building_id, slot_id=slot_id)
Ejemplo n.º 28
0
    def training_star(self, building_id, slot_id, staff_id, hour):
        if hour not in TRAINING_HOURS:
            raise GameException(ConfigErrorMessage.get_error_id('BAD_MESSAGE'))

        sm = StaffManger(self.server_id, self.char_id)
        sm.check_staff([staff_id])

        building = self.get_building_object(building_id, slots_ids=[slot_id])
        try:
            slot = building.slots[slot_id]
        except KeyError:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if not slot.open:
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_SLOT_LOCK"))

        if slot.staff_id:
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_SLOT_HAS_STAFF"))

        if self.is_staff_training_check_by_oid(
                sm.get_staff_object(staff_id).oid):
            raise GameException(
                ConfigErrorMessage.get_error_id("TERRITORY_STAFF_IS_TRAINING"))

        config_slot = ConfigTerritoryBuilding.get(building_id).slots[slot_id]
        cost_amount = config_slot.get_cost_amount(building.level,
                                                  TRAINING_HOURS.index(hour))

        new_amount = self.check_work_card(cost_amount)
        self.doc['work_card'] = new_amount

        slot.staff_id = staff_id
        slot.hour = hour

        start_at = arrow.utcnow().timestamp

        slot_doc = MongoTerritory.document_slot()
        slot_doc['staff_id'] = staff_id
        slot_doc['start_at'] = start_at
        slot_doc['hour'] = hour

        building_exp, product_amount = slot.get_building_reward()
        reward = {
            'building_exp': building_exp,
            'product_amount': product_amount,
            'items': []
        }

        ri = ResultItems()

        # 固定奖励
        report = [
            (
                1,
                [
                    str(building_exp),
                    ConfigItemNew.get(slot.product_id).name,
                    str(product_amount)
                ],
                0,
            ),
        ]

        # 选手特产
        config_staff_product = ConfigTerritoryStaffProduct.get(
            sm.get_staff_object(staff_id).oid)
        if config_staff_product:
            _id, _amount = config_staff_product.get_product(
                TRAINING_HOURS.index(hour))
            if _amount:
                report.append((
                    2,
                    [ConfigItemNew.get(_id).name,
                     str(_amount)],
                    0,
                ))

                ri.add(_id, _amount)

        # 概率奖励
        end_at = start_at + hour * 3600
        extra_at = start_at + 3600
        while extra_at < end_at:
            _id, _amount = ConfigTerritoryBuilding.get(
                building_id).slots[slot_id].get_extra_product(1)

            report.append((3, [ConfigItemNew.get(_id).name,
                               str(_amount)], extra_at))

            ri.add(_id, _amount)
            extra_at += 3600

        reward['items'] = ri.items.items()

        slot_doc['report'] = report
        slot_doc['reward'] = reward

        self.doc['buildings'][str(building_id)]['slots'][str(
            slot_id)] = slot_doc
        MongoTerritory.db(self.server_id).update_one({'_id': self.char_id}, {
            '$set': {
                'work_card': self.doc['work_card'],
                'buildings.{0}.slots.{1}'.format(building_id, slot_id):
                slot_doc
            }
        })

        ValueLogTerritoryTrainingTimes(self.server_id, self.char_id).record()

        self.send_notify(building_id=building_id, slot_id=slot_id)