def ensure_test_data(): global _test_data_created if _test_data_created: return _test_data_created = True ensure_test_tables() log('creating test data') already_exists = (Player.select().where( Player.cfn_id == sample_player_id).exists()) if already_exists: log('existing test data found') return create_test_player( cfn_id=sample_player_id, name='TestPlayer', subscribed=True, ) Rank.create( id=1, created_at=datetime.utcnow(), player_cfn=sample_player_id, league_points=100, placement=1, favorite_character=1, )
def subscribe_to_new_player(player_model): existing = (Player.select().where(Player.cfn_id == player_model.cfn_id)) if existing: (Player.update( subscribed=1, name=player_model.name, region=player_model.region, platform=player_model.platform, ).where(Player.cfn_id == player_model.cfn_id).execute()) else: bulk_insert(Player, [{ 'cfn_id': player_model.cfn_id, 'name': player_model.name, 'region': player_model.region, 'platform': player_model.platform, 'subscribed': 1, }])
def set_player_updated_at(pids): log("setting players as updated") if not pids: log('no players to update!') return now_str = _get_now_str() (Player.update( updated_at=now_str).where(Player.cfn_id << list(pids)).execute())
def test_player_combine(self): player = Player(cfn_id=self.player_id) player_data = update_player_with_matches(player, self.matches) dupe_pd = player_data dupe_pd = dupe_pd.combine(dupe_pd) dupe_pd = dupe_pd.combine(dupe_pd) dupe_pd.calculate_stats() self.assertEqual(376, dupe_pd.game_count)
def load_subscribed_player_ids( subset_ids=None, batch_size=None, ): query = (Player.select().where(Player.subscribed == 1)) if subset_ids: query = (query.where(Player.cfn_id << list(subset_ids))) if batch_size: query = (query.order_by(Player.updated_at).limit(batch_size)) return set([p.cfn_id for p in query])
def create_test_player(cfn_id=None, name=None, subscribed=False): global _id_counter _id_counter += 1 cfn_id = cfn_id or _id_counter name = name or 'Player #%s' % _id_counter subscribed = 1 if subscribed else 0 return Player.create( cfn_id=cfn_id, name=name, subscribed=subscribed, )
def save_ranking_results(ranking): player_source = [{ 'cfn_id': int(rank['pidx']), 'name': rank['userid'], 'subscribed': 0, 'region': rank['region'], 'platform': rank['accountsource'], } for rank in ranking] existing_pids = _insert_missing_players(player_source) rank_source = [{ 'player_cfn': int(rank['pidx']), 'league_points': int(rank['lp']), 'placement': int(rank['rownum']), 'favorite_character': int(rank['favchar']), } for rank in ranking] bulk_insert(Rank, rank_source) player_ids = [r['player_cfn'] for r in rank_source] set_subscribed(player_ids) player_update = [{ 'cfn_id': int(rank['pidx']), 'region': rank['region'], 'platform': rank['accountsource'], } for rank in ranking if int(rank['pidx']) in existing_pids] regions = defaultdict(set) platforms = defaultdict(set) for pu in player_update: regions[pu['region']].add(pu['cfn_id']) platforms[pu['platform']].add(pu['cfn_id']) for region, pids in regions.items(): (Player.update( region=region).where((Player.region >> None) & (Player.cfn_id << list(pids))).execute()) for platform, pids in platforms.items(): (Player.update(platform=platform).where((Player.platform >> None) & ( Player.cfn_id << list(pids))).execute())
def test_empty_ok(self): player = Player(cfn_id=self.player_id) vm_cache = PlayerViewModelCache() with mock.patch('py.src.view_model.get_player_by_name') as m: m.return_value = player player_vm = vm_cache.get_by_name(player.name) self.assertEqual(2728664320, player_vm.data.player_id) self.assertEqual(0, player_vm.data.game_count) self.assertEqual(0, player_vm.data.game_wins) self.assertEqual(None, player_vm.league_points) self.assertEqual(None, player_vm.player.region) self.assertEqual(None, player_vm.player.platform) self.assertEqual(None, player_vm.data.most_used_character) self.assertEqual(None, player_vm.latest_match_date)
def test_individual_player(self): player = Player(cfn_id=self.player_id) update_player_with_matches(player, self.matches) vm_cache = PlayerViewModelCache() with mock.patch('py.src.view_model.get_player_by_name') as m: m.return_value = player player_vm = vm_cache.get_by_name(player.name) self.assertEqual(2728664320, player_vm.data.player_id) self.assertEqual(94, player_vm.data.game_count) self.assertEqual(62, player_vm.data.game_wins) self.assertEqual(2157, player_vm.league_points) self.assertEqual('USA', player_vm.player.region) self.assertEqual('steam', player_vm.player.platform) self.assertEqual(14, player_vm.data.most_used_character) self.assertEqual('2016-03-06', player_vm.latest_match_date)
def get_latest_player_updated_at(): return (Player.select().order_by( Player.updated_at.desc()).get()).updated_at
def test_to_dict(self): player = Player(cfn_id=self.player_id) player_data = update_player_with_matches(player, self.matches) dict1 = player_data.to_dict() dict2 = IndividualPlayerData(player_dict=dict1).to_dict() self.assertEqual(dict1, dict2)
def ensure_test_tables(): ensure_testing() if not Player.table_exists(): log('creating test tables') DATABASE.create_tables([Player, Match, Rank])
def process_matches(self, player_id, matches): with mock.patch('py.src.match.model.cache.load_player_by_id') as m_lp: mock_player = Player() mock_player.cfn_id = player_id m_lp.return_value = mock_player return self.cache.process_matches(player_id, matches)
def load_player_by_name(name): try: return (Player.get(Player.name == name, Player.subscribed == 1)) except Player.DoesNotExist: raise PlayerDoesNotExist()
def get_subscribed_player_count(): return (Player.select().where(Player.subscribed == 1).count())
def load_player_by_id(player_id): try: return (Player.get(Player.cfn_id == player_id)) except Player.DoesNotExist: raise PlayerDoesNotExist()
def set_subscribed(pids): log("updating affected players to subscribed") if not pids: return (Player.update(subscribed=1).where(Player.subscribed == 0, Player.cfn_id << list(pids)).execute())
def load_player_by_approx_name(name): try: return (Player.get(LowerStr(Player.name) == name.lower())) except Player.DoesNotExist: raise PlayerDoesNotExist()
def load_player_ticks(): return list( Player.select(Player.cfn_id, Player.match_latest_ticks).where(Player.subscribed == 1))
def load_subscribed_player_names(): daos = (Player.select(Player.name).where(Player.subscribed == 1)) return set([p.name for p in daos])