Beispiel #1
0
    def add_station_exp(self, exp):
        self.doc['exp'] += exp

        old_level = self.doc['level']

        while True:
            config = ConfigBaseStationLevel.get(self.doc['level'])
            if self.doc['level'] == ConfigBaseStationLevel.MAX_LEVEL:
                if self.doc['exp'] > config.exp:
                    self.doc['exp'] = config.exp

                break

            if self.doc['exp'] < config.exp:
                break

            self.doc['level'] += 1
            self.doc['exp'] -= config.exp

        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'level': self.doc['level'],
                'exp': self.doc['exp'],
            }})

        self.send_station_notify()
        return self.doc['level'] - old_level
Beispiel #2
0
    def got_plundered(self, from_id, win_ways):
        config = ConfigPlunderIncome.get(win_ways)

        if win_ways > 0:
            revenge_item = (make_string_id(), from_id)

            self.doc['loss_percent'] += config.percent
            if self.doc['loss_percent'] > PLUNDER_MAX_LOST:
                self.doc['loss_percent'] = PLUNDER_MAX_LOST

            self.doc['revenge_list'].append(revenge_item)
            while len(self.doc['revenge_list']) > 20:
                self.doc['revenge_list'].pop(0)

            MongoPlunder.db(self.server_id).update_one({'_id': self.char_id}, {
                '$set': {
                    'loss_percent': self.doc['loss_percent'],
                    'revenge_list': self.doc['revenge_list'],
                }
            })

            self.send_revenge_notify()
            self.send_station_notify()

        config_station = ConfigBaseStationLevel.get(self.doc['product_level'])
        return config_station.get_product(config.percent)
Beispiel #3
0
    def add_station_exp(self, exp):
        self.doc['exp'] += exp

        old_level = self.doc['level']

        while True:
            config = ConfigBaseStationLevel.get(self.doc['level'])
            if self.doc['level'] == ConfigBaseStationLevel.MAX_LEVEL:
                if self.doc['exp'] > config.exp:
                    self.doc['exp'] = config.exp

                break

            if self.doc['exp'] < config.exp:
                break

            self.doc['level'] += 1
            self.doc['exp'] -= config.exp

        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'level': self.doc['level'],
                'exp': self.doc['exp'],
            }}
        )

        self.send_station_notify()
        return self.doc['level'] - old_level
Beispiel #4
0
    def buy_plunder_times(self):
        info = PlunderTimesBuyInfo(self.server_id, self.char_id)
        if not info.remained_buy_times:
            raise GameException(
                ConfigErrorMessage.get_error_id("PLUNDER_NO_BUY_TIMES"))

        cost = [
            (money_text_to_item_id('diamond'), info.buy_cost),
        ]

        rc = ResourceClassification.classify(cost)
        rc.check_exist(self.server_id, self.char_id)
        rc.remove(self.server_id,
                  self.char_id,
                  message="Plunder.buy_plunder_times")

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

        self.doc['plunder_remained_times'] += 1
        MongoPlunder.db(self.server_id).update_one({'_id': self.char_id}, {
            '$set': {
                'plunder_remained_times': self.doc['plunder_remained_times']
            }
        })
        self.send_plunder_times_notify()
Beispiel #5
0
    def got_plundered(self, from_id, win_ways):
        config = ConfigPlunderIncome.get(win_ways)

        if win_ways > 0:
            revenge_item = (make_string_id(), from_id)

            self.doc['loss_percent'] += config.percent
            if self.doc['loss_percent'] > PLUNDER_MAX_LOST:
                self.doc['loss_percent'] = PLUNDER_MAX_LOST

            self.doc['revenge_list'].append(revenge_item)
            while len(self.doc['revenge_list']) > 20:
                self.doc['revenge_list'].pop(0)

            MongoPlunder.db(self.server_id).update_one(
                {'_id': self.char_id},
                {'$set': {
                    'loss_percent': self.doc['loss_percent'],
                    'revenge_list': self.doc['revenge_list'],
                }}
            )

            self.send_revenge_notify()
            self.send_station_notify()

        config_station = ConfigBaseStationLevel.get(self.doc['product_level'])
        return config_station.get_product(config.percent)
Beispiel #6
0
    def plunder_report(self, key, win):
        try:
            way = int(key)
            assert way in [1, 2, 3]
        except:
            raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE"))

        way_index = way - 1
        result = 1 if win else 2

        target_id = self.doc['matching']['id']

        if not target_id:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        updater = {}
        self.doc['matching']['result'][way_index] = result
        updater['matching.result.{0}'.format(way_index)] = result

        if win:
            daily_key, info = self.get_daily_reward_info()
            win_ways = info.get('win_ways', 0)
            info['win_ways'] = win_ways + 1
            self.doc['daily_reward'] = {daily_key: info}
            updater['daily_reward'] = {daily_key: info}

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

        self.send_plunder_daily_reward_notify()
        self.send_result_notify()

        WinningPlunder(self.server_id, self.char_id).set(win)
Beispiel #7
0
    def try_initialize(self):
        if self.doc['active']:
            return

        ways = [
            self.get_way_object(1),
            self.get_way_object(2),
            self.get_way_object(3)
        ]
        club = Club(self.server_id, self.char_id)

        formation_staffs = club.formation_staffs
        """:type: list[core.staff.Staff]"""

        way_index = 0
        slot_id = 1

        for s in formation_staffs:
            ways[way_index].set_staff(slot_id, s.id)
            if s.unit:
                ways[way_index].set_unit(slot_id, s.unit.id)

            if way_index == 2:
                way_index = 0
                slot_id += 1
            else:
                way_index += 1

        self.doc['active'] = True
        MongoPlunder.db(self.server_id).update_one({'_id': self.char_id},
                                                   {'$set': {
                                                       'active': True
                                                   }})

        self.send_formation_notify()
Beispiel #8
0
    def plunder_report(self, key, win):
        try:
            way = int(key)
            assert way in [1, 2, 3]
        except:
            raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE"))

        way_index = way - 1
        result = 1 if win else 2

        target_id = self.doc['matching']['id']

        if not target_id:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        updater = {}
        self.doc['matching']['result'][way_index] = result
        updater['matching.result.{0}'.format(way_index)] = result

        if win:
            daily_key, info = self.get_daily_reward_info()
            win_ways = info.get('win_ways', 0)
            info['win_ways'] = win_ways + 1
            self.doc['daily_reward'] = {daily_key: info}
            updater['daily_reward'] = {daily_key: info}

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

        self.send_plunder_daily_reward_notify()
        self.send_result_notify()

        WinningPlunder(self.server_id, self.char_id).set(win)
Beispiel #9
0
    def try_initialize(self):
        if self.doc['active']:
            return

        ways = [self.get_way_object(1), self.get_way_object(2), self.get_way_object(3)]
        club = Club(self.server_id, self.char_id)

        formation_staffs = club.formation_staffs
        """:type: list[core.staff.Staff]"""

        way_index = 0
        slot_id = 1

        for s in formation_staffs:
            ways[way_index].set_staff(slot_id, s.id)
            if s.unit:
                ways[way_index].set_unit(slot_id, s.unit.id)

            if way_index == 2:
                way_index = 0
                slot_id += 1
            else:
                way_index += 1

        self.doc['active'] = True
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'active': True
            }}
        )

        self.send_formation_notify()
Beispiel #10
0
    def daily_reward_get(self, _id):
        config = ConfigPlunderDailyReward.get(_id)
        if not config:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        key, info = self.get_daily_reward_info()
        win_ways = info.get('win_ways', 0)
        got_list = info.get('got_list', [])

        if _id in got_list:
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_DAILY_REWARD_ALREADY_GOT"))

        if win_ways < _id:
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_DAILY_REWARD_NOT_ENOUGH"))

        rc = ResourceClassification.classify(config.reward)
        rc.add(self.server_id, self.char_id, message="Plunder.daily_reward_get:{0}".format(_id))

        got_list.append(_id)
        info['got_list'] = got_list

        self.doc['daily_reward'] = {key: info}
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'daily_reward': {key: info}
            }}
        )

        self.send_plunder_daily_reward_notify(info=info)
        return rc
Beispiel #11
0
    def auto_add_plunder_times(cls, server_id):
        char_ids = Club.get_recent_login_char_ids(server_id)
        char_ids = [i for i in char_ids]

        condition = {'$and': [
            {'_id': {'$in': char_ids}},
            {'plunder_remained_times': {'$lt': PLUNDER_TIMES_RECOVER_LIMIT}}
        ]}

        MongoPlunder.db(server_id).update_many(
            condition,
            {'$inc': {'plunder_remained_times': 1}}
        )
Beispiel #12
0
        def _make(doc):
            config = ConfigBaseStationLevel.get(doc['product_level'])
            items = config.get_product(100 - doc['loss_percent'])

            rc = ResourceClassification.classify(items)
            rc.add(server_id, doc['_id'], message="Plunder.make_product")

            MongoPlunder.db(server_id).update_one(
                {'_id': doc['_id']},
                {'$set': {
                    'product_level': doc['level'],
                    'loss_percent': 0,
                    # 'revenge_list': [],
                    'drop': rc.to_json(),
                }}
            )
Beispiel #13
0
    def make_product(cls, server_id):
        def _make(doc):
            config = ConfigBaseStationLevel.get(doc['product_level'])
            items = config.get_product(100 - doc['loss_percent'])

            rc = ResourceClassification.classify(items)
            rc.add(server_id, doc['_id'], message="Plunder.make_product")

            MongoPlunder.db(server_id).update_one(
                {'_id': doc['_id']},
                {
                    '$set': {
                        'product_level': doc['level'],
                        'loss_percent': 0,
                        # 'revenge_list': [],
                        'drop': rc.to_json(),
                    }
                })

        char_ids = Club.get_recent_login_char_ids(server_id)
        char_ids = [i for i in char_ids]
        docs = MongoPlunder.db(server_id).find({'_id': {
            '$in': char_ids
        }}, {
            'level': 1,
            'product_level': 1,
            'loss_percent': 1
        })

        for d in docs:
            _make(d)
Beispiel #14
0
    def make_product(cls, server_id):
        def _make(doc):
            config = ConfigBaseStationLevel.get(doc['product_level'])
            items = config.get_product(100 - doc['loss_percent'])

            rc = ResourceClassification.classify(items)
            rc.add(server_id, doc['_id'], message="Plunder.make_product")

            MongoPlunder.db(server_id).update_one(
                {'_id': doc['_id']},
                {'$set': {
                    'product_level': doc['level'],
                    'loss_percent': 0,
                    # 'revenge_list': [],
                    'drop': rc.to_json(),
                }}
            )

        char_ids = Club.get_recent_login_char_ids(server_id)
        char_ids = [i for i in char_ids]
        docs = MongoPlunder.db(server_id).find(
            {'_id': {'$in': char_ids}},
            {'level': 1, 'product_level': 1, 'loss_percent': 1}
        )

        for d in docs:
            _make(d)
Beispiel #15
0
    def get_station_product(self):
        drop = self.doc.get('drop', '')
        if not drop:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        rc = ResourceClassification.load_from_json(drop)
        rc.add(self.server_id, self.char_id, message="Plunder.get_station_product")
        self.doc['drop'] = ''
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'drop': ''
            }}
        )

        self.send_station_notify()
        return rc
Beispiel #16
0
        def _make(doc):
            config = ConfigBaseStationLevel.get(doc['product_level'])
            items = config.get_product(100 - doc['loss_percent'])

            rc = ResourceClassification.classify(items)
            rc.add(server_id, doc['_id'], message="Plunder.make_product")

            MongoPlunder.db(server_id).update_one(
                {'_id': doc['_id']},
                {
                    '$set': {
                        'product_level': doc['level'],
                        'loss_percent': 0,
                        # 'revenge_list': [],
                        'drop': rc.to_json(),
                    }
                })
Beispiel #17
0
    def get_station_product(self):
        drop = self.doc.get('drop', '')
        if not drop:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        rc = ResourceClassification.load_from_json(drop)
        rc.add(self.server_id,
               self.char_id,
               message="Plunder.get_station_product")
        self.doc['drop'] = ''
        MongoPlunder.db(self.server_id).update_one({'_id': self.char_id},
                                                   {'$set': {
                                                       'drop': ''
                                                   }})

        self.send_station_notify()
        return rc
Beispiel #18
0
    def spy(self, target_id):
        index = self.find_search_target_index_by_target_id(target_id)
        if self.doc['search'][index]['spied']:
            return

        cost = [(money_text_to_item_id('diamond'), 5), ]

        rc = ResourceClassification.classify(cost)
        rc.check_exist(self.server_id, self.char_id)
        rc.remove(self.server_id, self.char_id, message="Plunder.spy")

        self.doc['search'][index]['spied'] = True
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'search.{0}.spied'.format(index): True
            }}
        )

        self.send_search_notify()
Beispiel #19
0
    def auto_add_plunder_times(cls, server_id):
        char_ids = Club.get_recent_login_char_ids(server_id)
        char_ids = [i for i in char_ids]

        condition = {
            '$and': [{
                '_id': {
                    '$in': char_ids
                }
            }, {
                'plunder_remained_times': {
                    '$lt': PLUNDER_TIMES_RECOVER_LIMIT
                }
            }]
        }

        MongoPlunder.db(server_id).update_many(
            condition, {'$inc': {
                'plunder_remained_times': 1
            }})
Beispiel #20
0
    def spy(self, target_id):
        index = self.find_search_target_index_by_target_id(target_id)
        if self.doc['search'][index]['spied']:
            return

        cost = [
            (money_text_to_item_id('diamond'), 5),
        ]

        rc = ResourceClassification.classify(cost)
        rc.check_exist(self.server_id, self.char_id)
        rc.remove(self.server_id, self.char_id, message="Plunder.spy")

        self.doc['search'][index]['spied'] = True
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'search.{0}.spied'.format(index): True
            }})

        self.send_search_notify()
Beispiel #21
0
    def buy_plunder_times(self):
        info = PlunderTimesBuyInfo(self.server_id, self.char_id)
        if not info.remained_buy_times:
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_NO_BUY_TIMES"))

        cost = [(money_text_to_item_id('diamond'), info.buy_cost), ]

        rc = ResourceClassification.classify(cost)
        rc.check_exist(self.server_id, self.char_id)
        rc.remove(self.server_id, self.char_id, message="Plunder.buy_plunder_times")

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

        self.doc['plunder_remained_times'] += 1
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {
                'plunder_remained_times': self.doc['plunder_remained_times']
            }}
        )
        self.send_plunder_times_notify()
Beispiel #22
0
    def daily_reward_get(self, _id):
        config = ConfigPlunderDailyReward.get(_id)
        if not config:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        key, info = self.get_daily_reward_info()
        win_ways = info.get('win_ways', 0)
        got_list = info.get('got_list', [])

        if _id in got_list:
            raise GameException(
                ConfigErrorMessage.get_error_id(
                    "PLUNDER_DAILY_REWARD_ALREADY_GOT"))

        if win_ways < _id:
            raise GameException(
                ConfigErrorMessage.get_error_id(
                    "PLUNDER_DAILY_REWARD_NOT_ENOUGH"))

        rc = ResourceClassification.classify(config.reward)
        rc.add(self.server_id,
               self.char_id,
               message="Plunder.daily_reward_get:{0}".format(_id))

        got_list.append(_id)
        info['got_list'] = got_list

        self.doc['daily_reward'] = {key: info}
        MongoPlunder.db(self.server_id).update_one(
            {'_id': self.char_id}, {'$set': {
                'daily_reward': {
                    key: info
                }
            }})

        self.send_plunder_daily_reward_notify(info=info)
        return rc
Beispiel #23
0
    def __init__(self, server_id, char_id):
        self.server_id = server_id
        self.char_id = char_id

        self.is_active = Challenge(server_id, char_id).is_challenge_id_passed(
            GlobalConfig.value("PLUNDER_ACTIVE_CHALLENGE_ID"))

        self.doc = MongoPlunder.db(self.server_id).find_one(
            {'_id': self.char_id})
        if not self.doc:
            self.doc = MongoPlunder.document()
            self.doc['_id'] = self.char_id
            self.doc['plunder_remained_times'] = PLUNDER_TIMES_INIT_TIMES
            MongoPlunder.db(self.server_id).insert_one(self.doc)

        _, today_daily_reward_info = self.get_daily_reward_info()
        if not today_daily_reward_info:
            # 可以清理数据
            self.doc['daily_reward'] = {}
            MongoPlunder.db(self.server_id).update_one(
                {'_id': self.char_id}, {'$set': {
                    'daily_reward': {}
                }})
Beispiel #24
0
    def __init__(self, server_id, char_id):
        self.server_id = server_id
        self.char_id = char_id

        self.is_active = Challenge(server_id, char_id).is_challenge_id_passed(
            GlobalConfig.value("PLUNDER_ACTIVE_CHALLENGE_ID"))

        self.doc = MongoPlunder.db(self.server_id).find_one({'_id': self.char_id})
        if not self.doc:
            self.doc = MongoPlunder.document()
            self.doc['_id'] = self.char_id
            self.doc['plunder_remained_times'] = PLUNDER_TIMES_INIT_TIMES
            MongoPlunder.db(self.server_id).insert_one(self.doc)

        _, today_daily_reward_info = self.get_daily_reward_info()
        if not today_daily_reward_info:
            # 可以清理数据
            self.doc['daily_reward'] = {}
            MongoPlunder.db(self.server_id).update_one(
                {'_id': self.char_id},
                {'$set': {
                    'daily_reward': {}
                }}
            )
Beispiel #25
0
    def get_reward(self):
        target_id = self.doc['matching']['id']
        tp = self.doc['matching']['tp']
        result = self.doc['matching']['result']

        if not target_id:
            raise GameException(
                ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if 0 in result:
            raise GameException(
                ConfigErrorMessage.get_error_id(
                    "PLUNDER_MATCH_NOT_FINISH_CANNOT_GET_REWARD"))

        self.doc['matching'] = {
            'id': 0,
            'tp': 0,
            'result': [0, 0, 0],
        }

        updater = {'matching': self.doc['matching']}

        # NOTE: real target id
        plunder_got = []
        win_ways = sum([i for i in result if i == 1])

        config = ConfigPlunderIncome.get(win_ways)
        plunder_got.append((STATION_EXP_ID, config.exp))

        if tp == PLUNDER_TYPE_PLUNDER:
            real_id = target_id
            search_index = self.find_search_target_index_by_target_id(
                target_id)

            data = self.doc['search'][search_index]
            self.search(check_cd=False, replace_search_index=search_index)

            if is_npc(real_id):
                target_plunder = PlunderNPC(data['id'], data['name'],
                                            data['station_level'],
                                            data['ways_npc'])
            else:
                target_plunder = Plunder(self.server_id, real_id)
                plunder_got.extend(config.get_extra_income())

            _got = target_plunder.got_plundered(self.char_id, win_ways)
            plunder_got.extend(_got)

            ValueLogPlunderTimes(self.server_id, self.char_id).record()
            self.doc['plunder_remained_times'] -= 1
            updater['plunder_remained_times'] = self.doc[
                'plunder_remained_times']

        else:
            revenge_index = self.find_revenge_target_index_by_target_id(
                target_id)
            _, real_id = self.doc['revenge_list'].pop(revenge_index)
            updater['revenge_list'] = self.doc['revenge_list']

            ValueLogPlunderRevengeTimes(self.server_id, self.char_id).record()
            self.send_revenge_notify()

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

        self.send_result_notify()
        self.send_plunder_times_notify()

        rc = ResourceClassification.classify(plunder_got)
        rc.add(self.server_id, self.char_id, message="Plunder.get_reward")
        return result, rc
Beispiel #26
0
    def search(self, check_cd=True, replace_search_index=None, send_notify=True):
        if check_cd and self.get_search_cd():
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_SEARCH_IN_CD"))

        def _query_real(_level_low, _level_high):
            _skip_char_ids = [_s['id'] for _s in self.doc['search']]
            _skip_char_ids.append(self.char_id)

            _condition = {'$and': [
                {'_id': {'$nin': _skip_char_ids}},
                {'level': {'$gte': _level_low}},
                {'level': {'$lte': _level_high}}
            ]}

            _docs = MongoCharacter.db(self.server_id).find(_condition, {'_id': 1})
            _ids = []
            for _doc in _docs:
                _ids.append(_doc['_id'])

            return _ids

        self_club_level = get_club_property(self.server_id, self.char_id, 'level')
        level_low = self_club_level - GlobalConfig.value("PLUNDER_SEARCH_LEVEL_RANGE_LOW")
        level_high = self_club_level + GlobalConfig.value("PLUNDER_SEARCH_LEVEL_RANGE_HIGH")

        real_ids = _query_real(level_low, level_high)
        # filter by active and loss_percent
        condition = {'$and': [
            {'_id': {'$in': real_ids}},
            {'active': True},
            {'loss_percent': {'$lt': PLUNDER_MAX_LOST}}
        ]}

        docs = MongoPlunder.db(self.server_id).find(condition, {'_id': 1})
        result_ids = []
        for doc in docs:
            if PlunderMatchCD(self.server_id, self.char_id, doc['_id']).get_cd_seconds():
                continue

            result_ids.append(doc['_id'])

        random.shuffle(result_ids)

        search_docs = []
        for i in result_ids:
            search_docs.append({'id': i, 'spied': False})
            if len(search_docs) == 2:
                break

        need_npc_amount = 2 - len(search_docs)
        if need_npc_amount:
            for i in range(need_npc_amount):
                config_plunder_npc = ConfigPlunderNPC.get_by_level(self_club_level)
                npc_doc = config_plunder_npc.to_doc(self.doc['level'])

                search_docs.append(npc_doc)

        if replace_search_index:
            self.doc['search'][replace_search_index] = search_docs[0]
            updater = {
                'search.{0}'.format(replace_search_index): self.doc['search'][replace_search_index]
            }
        else:
            self.doc['search'] = search_docs
            updater = {'search': search_docs}

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

        if check_cd:
            self.set_search_cd()

        if send_notify:
            self.send_search_notify()
Beispiel #27
0
    def plunder_start(self, _id, tp, formation_slots=None, win=None):
        if tp not in [PLUNDER_TYPE_PLUNDER, PLUNDER_TYPE_REVENGE]:
            raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE"))

        for i in [0, 1, 2]:
            if self.doc['matching']['result'][i] == 0:
                way = i + 1
                break
        else:
            # 都打完了
            raise GameException(
                ConfigErrorMessage.get_error_id("PLUNDER_MATCH_ALL_FINISHED"))

        target_id = self.doc['matching']['id']
        if not target_id:
            if tp == PLUNDER_TYPE_PLUNDER:
                _index = self.find_search_target_index_by_target_id(_id)
                target_id = self.doc['search'][_index]['id']
            else:
                _index = self.find_revenge_target_index_by_target_id(_id)
                target_id = self.doc['revenge_list'][_index][0]
        else:
            # 要保证target_id 一样
            if str(target_id) != _id:
                raise GameException(
                    ConfigErrorMessage.get_error_id(
                        "PLUNDER_TARGET_ID_NOT_SAME"))

        updater = {}
        if not self.doc['matching']['id']:
            self.doc['matching']['id'] = target_id
            self.doc['matching']['tp'] = tp
            updater['matching.id'] = target_id
            updater['matching.tp'] = tp

        if way == 1:
            # 开始的第一路,这时候要判断次数
            if tp == PLUNDER_TYPE_PLUNDER:
                if not self.doc['plunder_remained_times']:
                    self.buy_plunder_times()

                PlunderMatchCD(self.server_id, self.char_id, target_id).set(
                    GlobalConfig.value("PLUNDER_MATCH_CD"))

            else:
                if not self.get_revenge_remained_times():
                    raise GameException(
                        ConfigErrorMessage.get_error_id(
                            "PLUNDER_REVENGE_NO_TIMES"))

                self.send_revenge_notify()

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

        if win is not None:
            self.plunder_report(way, win)
            return None

        my_way = self.get_way_object(way)
        if formation_slots:
            my_way.sync_slots(formation_slots)
            my_way.load_formation_staffs()
            self.send_formation_notify()

        my_club = Club(self.server_id, self.char_id, load_staffs=False)
        my_club.formation_staffs = my_way.formation_staffs

        match = ClubMatch(my_club, None, 3, my_way.get_skill_sequence(), {})
        msg = match.start(auto_load_staffs=False)
        msg.key = str(way)
        msg.map_name = GlobalConfig.value_string("MATCH_MAP_PLUNDER")

        return msg
Beispiel #28
0
    def search(self,
               check_cd=True,
               replace_search_index=None,
               send_notify=True):
        if check_cd and self.get_search_cd():
            raise GameException(
                ConfigErrorMessage.get_error_id("PLUNDER_SEARCH_IN_CD"))

        def _query_real(_level_low, _level_high):
            _skip_char_ids = [_s['id'] for _s in self.doc['search']]
            _skip_char_ids.append(self.char_id)

            _condition = {
                '$and': [{
                    '_id': {
                        '$nin': _skip_char_ids
                    }
                }, {
                    'level': {
                        '$gte': _level_low
                    }
                }, {
                    'level': {
                        '$lte': _level_high
                    }
                }]
            }

            _docs = MongoCharacter.db(self.server_id).find(
                _condition, {'_id': 1})
            _ids = []
            for _doc in _docs:
                _ids.append(_doc['_id'])

            return _ids

        self_club_level = get_club_property(self.server_id, self.char_id,
                                            'level')
        level_low = self_club_level - GlobalConfig.value(
            "PLUNDER_SEARCH_LEVEL_RANGE_LOW")
        level_high = self_club_level + GlobalConfig.value(
            "PLUNDER_SEARCH_LEVEL_RANGE_HIGH")

        real_ids = _query_real(level_low, level_high)
        # filter by active and loss_percent
        condition = {
            '$and': [{
                '_id': {
                    '$in': real_ids
                }
            }, {
                'active': True
            }, {
                'loss_percent': {
                    '$lt': PLUNDER_MAX_LOST
                }
            }]
        }

        docs = MongoPlunder.db(self.server_id).find(condition, {'_id': 1})
        result_ids = []
        for doc in docs:
            if PlunderMatchCD(self.server_id, self.char_id,
                              doc['_id']).get_cd_seconds():
                continue

            result_ids.append(doc['_id'])

        random.shuffle(result_ids)

        search_docs = []
        for i in result_ids:
            search_docs.append({'id': i, 'spied': False})
            if len(search_docs) == 2:
                break

        need_npc_amount = 2 - len(search_docs)
        if need_npc_amount:
            for i in range(need_npc_amount):
                config_plunder_npc = ConfigPlunderNPC.get_by_level(
                    self_club_level)
                npc_doc = config_plunder_npc.to_doc(self.doc['level'])

                search_docs.append(npc_doc)

        if replace_search_index:
            self.doc['search'][replace_search_index] = search_docs[0]
            updater = {
                'search.{0}'.format(replace_search_index):
                self.doc['search'][replace_search_index]
            }
        else:
            self.doc['search'] = search_docs
            updater = {'search': search_docs}

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

        if check_cd:
            self.set_search_cd()

        if send_notify:
            self.send_search_notify()
Beispiel #29
0
    def plunder_start(self, _id, tp, formation_slots=None, win=None):
        if tp not in [PLUNDER_TYPE_PLUNDER, PLUNDER_TYPE_REVENGE]:
            raise GameException(ConfigErrorMessage.get_error_id("BAD_MESSAGE"))

        for i in [0, 1, 2]:
            if self.doc['matching']['result'][i] == 0:
                way = i + 1
                break
        else:
            # 都打完了
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_MATCH_ALL_FINISHED"))

        target_id = self.doc['matching']['id']
        if not target_id:
            if tp == PLUNDER_TYPE_PLUNDER:
                _index = self.find_search_target_index_by_target_id(_id)
                target_id = self.doc['search'][_index]['id']
            else:
                _index = self.find_revenge_target_index_by_target_id(_id)
                target_id = self.doc['revenge_list'][_index][0]
        else:
            # 要保证target_id 一样
            if str(target_id) != _id:
                raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_TARGET_ID_NOT_SAME"))

        updater = {}
        if not self.doc['matching']['id']:
            self.doc['matching']['id'] = target_id
            self.doc['matching']['tp'] = tp
            updater['matching.id'] = target_id
            updater['matching.tp'] = tp

        if way == 1:
            # 开始的第一路,这时候要判断次数
            if tp == PLUNDER_TYPE_PLUNDER:
                if not self.doc['plunder_remained_times']:
                    self.buy_plunder_times()

                PlunderMatchCD(self.server_id, self.char_id, target_id).set(GlobalConfig.value("PLUNDER_MATCH_CD"))

            else:
                if not self.get_revenge_remained_times():
                    raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_REVENGE_NO_TIMES"))

                self.send_revenge_notify()

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

        if win is not None:
            self.plunder_report(way, win)
            return None

        my_way = self.get_way_object(way)
        if formation_slots:
            my_way.sync_slots(formation_slots)
            my_way.load_formation_staffs()
            self.send_formation_notify()

        my_club = Club(self.server_id, self.char_id, load_staffs=False)
        my_club.formation_staffs = my_way.formation_staffs

        match = ClubMatch(my_club, None, 3, my_way.get_skill_sequence(), {})
        msg = match.start(auto_load_staffs=False)
        msg.key = str(way)
        msg.map_name = GlobalConfig.value_string("MATCH_MAP_PLUNDER")

        return msg
Beispiel #30
0
    def get_reward(self):
        target_id = self.doc['matching']['id']
        tp = self.doc['matching']['tp']
        result = self.doc['matching']['result']

        if not target_id:
            raise GameException(ConfigErrorMessage.get_error_id("INVALID_OPERATE"))

        if 0 in result:
            raise GameException(ConfigErrorMessage.get_error_id("PLUNDER_MATCH_NOT_FINISH_CANNOT_GET_REWARD"))

        self.doc['matching'] = {
            'id': 0,
            'tp': 0,
            'result': [0, 0, 0],
        }

        updater = {
            'matching': self.doc['matching']
        }

        # NOTE: real target id
        plunder_got = []
        win_ways = sum([i for i in result if i == 1])

        config = ConfigPlunderIncome.get(win_ways)
        plunder_got.append((STATION_EXP_ID, config.exp))

        if tp == PLUNDER_TYPE_PLUNDER:
            real_id = target_id
            search_index = self.find_search_target_index_by_target_id(target_id)

            data = self.doc['search'][search_index]
            self.search(check_cd=False, replace_search_index=search_index)

            if is_npc(real_id):
                target_plunder = PlunderNPC(data['id'], data['name'], data['station_level'], data['ways_npc'])
            else:
                target_plunder = Plunder(self.server_id, real_id)
                plunder_got.extend(config.get_extra_income())

            _got = target_plunder.got_plundered(self.char_id, win_ways)
            plunder_got.extend(_got)

            ValueLogPlunderTimes(self.server_id, self.char_id).record()
            self.doc['plunder_remained_times'] -= 1
            updater['plunder_remained_times'] = self.doc['plunder_remained_times']

        else:
            revenge_index = self.find_revenge_target_index_by_target_id(target_id)
            _, real_id = self.doc['revenge_list'].pop(revenge_index)
            updater['revenge_list'] = self.doc['revenge_list']

            ValueLogPlunderRevengeTimes(self.server_id, self.char_id).record()
            self.send_revenge_notify()

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

        self.send_result_notify()
        self.send_plunder_times_notify()

        rc = ResourceClassification.classify(plunder_got)
        rc.add(self.server_id, self.char_id, message="Plunder.get_reward")
        return result, rc