def setUp(self): self.ranking_id = ObjectId() self.region = 'norcal' self.time = datetime.now() self.tournaments = [ObjectId(), ObjectId()] self.ranking_entry_1 = RankingEntry(rank=1, player=ObjectId(), rating=20.5) self.ranking_entry_2 = RankingEntry(rank=2, player=ObjectId(), rating=19.3) self.rankings = [self.ranking_entry_1, self.ranking_entry_2] self.ranking = Ranking(id=self.ranking_id, region=self.region, time=self.time, tournaments=self.tournaments, ranking=self.rankings) self.ranking_json_dict = { '_id': self.ranking_id, 'region': self.region, 'time': self.time, 'tournaments': self.tournaments, 'ranking': [r.dump(context='db') for r in self.rankings] }
def post(self): parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('name') parser.add_argument('source') data = parser.parse_args() if Ranking.get_or_none(Ranking.name == data['name']) is not None: return {'message': f"Ranking with such name already exists"}, 409 ranking = Ranking.create(user=current_user.id, name=data['name'], datasource=data['source']) return jsonify(message=f'Ranking {ranking.name} was created, id={ranking.id}')
class TestRanking(unittest.TestCase): def setUp(self): self.ranking_id = ObjectId() self.region = 'norcal' self.time = datetime.now() self.tournaments = [ObjectId(), ObjectId()] self.ranking_entry_1 = RankingEntry( rank=1, player=ObjectId(), rating=20.5) self.ranking_entry_2 = RankingEntry( rank=2, player=ObjectId(), rating=19.3) self.rankings = [self.ranking_entry_1, self.ranking_entry_2] self.ranking = Ranking( id=self.ranking_id, region=self.region, time=self.time, tournaments=self.tournaments, ranking=self.rankings) self.ranking_json_dict = { '_id': self.ranking_id, 'region': self.region, 'time': self.time, 'tournaments': self.tournaments, 'ranking': [r.dump(context='db') for r in self.rankings] } def test_dump(self): self.assertEqual(self.ranking.dump( context='db'), self.ranking_json_dict) def test_load(self): ranking = Ranking.load(self.ranking_json_dict, context='db') self.assertEqual(ranking.id, self.ranking.id) self.assertEqual(ranking.region, self.ranking.region) self.assertEqual(ranking.time, self.ranking.time) self.assertEqual(ranking.tournaments, self.ranking.tournaments) self.assertEqual(ranking.ranking, self.ranking.ranking) def test_get_ranking_for_player_id(self): self.assertEqual( self.ranking_entry_1.rank, self.ranking.get_ranking_for_player_id(self.ranking_entry_1.player)) self.assertEqual( self.ranking_entry_2.rank, self.ranking.get_ranking_for_player_id(self.ranking_entry_2.player)) def test_get_ranking_for_player_id_not_found(self): self.assertIsNone(self.ranking.get_ranking_for_player_id(ObjectId()))
class TestRanking(unittest.TestCase): def setUp(self): self.ranking_id = ObjectId() self.region = 'norcal' self.time = datetime.now() self.tournaments = [ObjectId(), ObjectId()] self.ranking_entry_1 = RankingEntry(rank=1, player=ObjectId(), rating=20.5) self.ranking_entry_2 = RankingEntry(rank=2, player=ObjectId(), rating=19.3) self.rankings = [self.ranking_entry_1, self.ranking_entry_2] self.ranking = Ranking(id=self.ranking_id, region=self.region, time=self.time, tournaments=self.tournaments, ranking=self.rankings) self.ranking_json_dict = { '_id': self.ranking_id, 'region': self.region, 'time': self.time, 'tournaments': self.tournaments, 'ranking': [r.dump(context='db') for r in self.rankings] } def test_dump(self): self.assertEqual(self.ranking.dump(context='db'), self.ranking_json_dict) def test_load(self): ranking = Ranking.load(self.ranking_json_dict, context='db') self.assertEqual(ranking.id, self.ranking.id) self.assertEqual(ranking.region, self.ranking.region) self.assertEqual(ranking.time, self.ranking.time) self.assertEqual(ranking.tournaments, self.ranking.tournaments) self.assertEqual(ranking.ranking, self.ranking.ranking) def test_get_ranking_for_player_id(self): self.assertEqual( self.ranking_entry_1.rank, self.ranking.get_ranking_for_player_id( self.ranking_entry_1.player)) self.assertEqual( self.ranking_entry_2.rank, self.ranking.get_ranking_for_player_id( self.ranking_entry_2.player)) def test_get_ranking_for_player_id_not_found(self): self.assertIsNone(self.ranking.get_ranking_for_player_id(ObjectId()))
def get(self, uid): ranking = Ranking.get_or_none(id=uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 if ranking.user.id != current_user.id: return {'message': f'Ranking belongs to another user'}, 409 ranking_json = {'id': ranking.id, 'name': ranking.name, 'datasource': ranking.datasource, 'item_count': len(ranking.items), 'comp_count': len(ranking.comparisons)} if len(ranking.items) != 0: model = ranking.get_pairwise_model() ranking_json['items'] = [] for item in ranking.items: coeff = model.coeff_by_id(str(item.id)) stderr = coeff[1] if isnan(stderr): stderr = 0 idx = model.coefficients.index(coeff) rating = (idx / len(model.coefficients)) * 10 ranking_json['items'].append({ 'id': item.id, 'label': item.label, 'img_url': item.img_url, 'init_rating': item.init_rating, 'curr_rating': round(rating, 2), 'stderr': round(stderr, 2)}) return jsonify(ranking=ranking_json)
def test_load(self): ranking = Ranking.load(self.ranking_json_dict, context='db') self.assertEqual(ranking.id, self.ranking.id) self.assertEqual(ranking.region, self.ranking.region) self.assertEqual(ranking.time, self.ranking.time) self.assertEqual(ranking.tournaments, self.ranking.tournaments) self.assertEqual(ranking.ranking, self.ranking.ranking)
def setUp(self): self.ranking_id = ObjectId() self.region = 'norcal' self.time = datetime.now() self.tournaments = [ObjectId(), ObjectId()] self.ranking_entry_1 = RankingEntry( rank=1, player=ObjectId(), rating=20.5) self.ranking_entry_2 = RankingEntry( rank=2, player=ObjectId(), rating=19.3) self.rankings = [self.ranking_entry_1, self.ranking_entry_2] self.ranking = Ranking( id=self.ranking_id, region=self.region, time=self.time, tournaments=self.tournaments, ranking=self.rankings) self.ranking_json_dict = { '_id': self.ranking_id, 'region': self.region, 'time': self.time, 'tournaments': self.tournaments, 'ranking': [r.dump(context='db') for r in self.rankings] }
def get(self, ranking_uid): ranking = Ranking.get_or_none(id=ranking_uid) if ranking is None: return {'message': f'Ranking `{uid}` not found.'}, 404 if ranking.user.id != current_user.id: return {'message': f'Ranking belongs to another user.'}, 409 model = ranking.get_pairwise_model() comp = model.next_comparison() item1 = Item.get_or_none(id=UUID(comp[0])) if item1 is None: return {'message': f'Item `{comp[0]}` not found'}, 404 item2 = Item.get_or_none(id=UUID(comp[1])) if item2 is None: return {'message': f'Item `{comp[1]}` not found'}, 404 items = [] items.append({ 'id': item1.id, 'label': item1.label, 'img_url': item1.img_url }) items.append({ 'id': item2.id, 'label': item2.label, 'img_url': item2.img_url }) return jsonify(comparison=items)
def get(self): user_rankings = Ranking.select().where(Ranking.user == current_user.id) ranking_dicts = [] for ranking in user_rankings: ranking_dicts.append({'id': ranking.id, 'name': ranking.name, 'datasource': ranking.datasource, 'item_count': len(ranking.items)}) return jsonify(rankings=ranking_dicts)
def put(self, uid): ranking = Ranking.get_or_none(Ranking.id == uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True) parser.add_argument('name') args = parser.parse_args() if 'name' in args: ranking.name = args['name'] ranking.save() return jsonify(message=f'Ranking {ranking.id} was modified')
def post(self, uid): ranking = Ranking.get_or_none(id=uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('anilist_username') parser.add_argument('anilist_statuses', action="append") parser.add_argument('steam_id') data = parser.parse_args() if ranking.datasource == 'anilist': ranking.add_items_from_anilist(data['anilist_username'], data['anilist_statuses']) elif ranking.datasource == 'steam': ranking.add_items_from_steam(data['steam_id']) return jsonify(message=f'Now ranking has {len(ranking.items)} items.')
def post(self, ranking_uid): ranking = Ranking.get_or_none(id=ranking_uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('winitem') parser.add_argument('loseitem') data = parser.parse_args() id1 = UUID(data['winitem']) item1 = Item.get_or_none(id=id1) if item1 is None: return {'message': f'Item `{id1}` not found'}, 404 id2 = UUID(data['loseitem']) item2 = Item.get_or_none(id=id2) if item2 is None: return {'message': f'Item `{id2}` not found'}, 404 comp = Comparison.compare(item1, item2, item1.id) model = ranking.get_pairwise_model() return jsonify(coeff=str(model.coefficients), comparison_count=len(ranking.comparisons), comp=model_to_dict(comp))
def setUp(self): self.mongo_client.drop_database(DATABASE_NAME) self.player_1_id = ObjectId() self.player_2_id = ObjectId() self.player_3_id = ObjectId() self.player_4_id = ObjectId() self.player_5_id = ObjectId() self.player_1 = Player(name='gaR', aliases=['gar', 'garr'], ratings={ 'norcal': Rating(), 'texas': Rating() }, regions=['norcal', 'texas'], id=self.player_1_id) self.player_2 = Player(name='sfat', aliases=['sfat', 'miom | sfat'], ratings={'norcal': Rating()}, regions=['norcal'], id=self.player_2_id) self.player_3 = Player(name='mango', aliases=['mango', 'gar'], ratings={'norcal': Rating(mu=2, sigma=3)}, regions=['socal'], id=self.player_3_id) self.player_4 = Player(name='shroomed', aliases=['shroomed'], ratings={'norcal': Rating()}, regions=['norcal'], id=self.player_4_id) self.player_5 = Player(name='pewpewu', aliases=['pewpewu'], ratings={'norcal': Rating()}, regions=['norcal', 'socal'], id=self.player_5_id) self.merge_player_1 = Player(name='CLGsFaT', aliases=['clg | sfat'], ratings={'norcal': Rating()}, regions=['norcal'], id=ObjectId()) # only includes players 1-3 self.players = [self.player_1, self.player_2, self.player_3] self.tournament_id_1 = ObjectId() self.tournament_type_1 = 'tio' self.tournament_raw_id_1 = ObjectId() self.tournament_date_1 = datetime(2013, 10, 16) self.tournament_name_1 = 'tournament 1' self.tournament_players_1 = [ self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id ] self.tournament_matches_1 = [ Match(winner=self.player_1_id, loser=self.player_2_id), Match(winner=self.player_3_id, loser=self.player_4_id) ] self.tournament_regions_1 = ['norcal'] # tournament 2 is earlier than tournament 1, but inserted after self.tournament_id_2 = ObjectId() self.tournament_type_2 = 'challonge' self.tournament_raw_id_2 = ObjectId() self.tournament_date_2 = datetime(2013, 10, 10) self.tournament_name_2 = 'tournament 2' self.tournament_players_2 = [ self.player_5_id, self.player_2_id, self.player_3_id, self.player_4_id ] self.tournament_matches_2 = [ Match(winner=self.player_5_id, loser=self.player_2_id), Match(winner=self.player_3_id, loser=self.player_4_id) ] self.tournament_regions_2 = ['norcal', 'texas'] self.tournament_1 = Tournament(id=self.tournament_id_1, name=self.tournament_name_1, type=self.tournament_type_1, date=self.tournament_date_1, regions=self.tournament_regions_1, raw_id=self.tournament_raw_id_1, players=self.tournament_players_1, matches=self.tournament_matches_1) self.tournament_2 = Tournament(id=self.tournament_id_2, name=self.tournament_name_2, type=self.tournament_type_2, date=self.tournament_date_2, regions=self.tournament_regions_2, raw_id=self.tournament_raw_id_2, players=self.tournament_players_2, matches=self.tournament_matches_2) self.tournament_ids = [self.tournament_id_1, self.tournament_id_2] self.tournaments = [self.tournament_1, self.tournament_2] self.pending_tournament_id_1 = ObjectId() self.pending_tournament_type_1 = 'tio' self.pending_tournament_raw_id_1 = ObjectId() self.pending_tournament_date_1 = datetime(2013, 10, 11) self.pending_tournament_name_1 = 'pending tournament 1' self.pending_tournament_players_1 = [ self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name ] self.pending_tournament_matches_1 = [ AliasMatch(winner=self.player_1.name, loser=self.player_2.name), AliasMatch(winner=self.player_3.name, loser=self.player_4.name) ] self.pending_tournament_regions_1 = ['norcal'] self.pending_tournament_1 = PendingTournament( id=self.pending_tournament_id_1, name=self.pending_tournament_name_1, type=self.pending_tournament_type_1, date=self.pending_tournament_date_1, regions=self.pending_tournament_regions_1, raw_id=self.pending_tournament_raw_id_1, players=self.pending_tournament_players_1, matches=self.pending_tournament_matches_1) self.pending_tournaments = [self.pending_tournament_1] self.ranking_entry_1 = RankingEntry(rank=1, player=self.player_1_id, rating=20) self.ranking_entry_2 = RankingEntry(rank=2, player=self.player_2_id, rating=19) self.ranking_entry_3 = RankingEntry(rank=3, player=self.player_3_id, rating=17.5) self.ranking_entry_4 = RankingEntry(rank=3, player=self.player_4_id, rating=16.5) self.ranking_time_1 = datetime(2013, 4, 20) self.ranking_time_2 = datetime(2013, 4, 21) self.ranking_1 = Ranking(id=ObjectId(), region='norcal', time=self.ranking_time_1, tournaments=self.tournament_ids, ranking=[ self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_3 ]) self.ranking_2 = Ranking(id=ObjectId(), region='norcal', time=self.ranking_time_2, tournaments=self.tournament_ids, ranking=[ self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_4 ]) self.ranking_3 = Ranking( id=ObjectId(), region='norcal', time=self.ranking_time_2, tournaments=self.tournament_ids, ranking=[self.ranking_entry_1, self.ranking_entry_2]) self.rankings = [self.ranking_1, self.ranking_2, self.ranking_3] self.user_id_1 = 'abc123' self.user_admin_regions_1 = ['norcal', 'texas'] self.user_1 = User(id=self.user_id_1, admin_regions=self.user_admin_regions_1, username='******', salt='nacl', hashed_password='******') self.user_id_2 = 'asdfasdf' self.user_full_name_2 = 'Full Name' self.user_admin_regions_2 = [] self.user_2 = User(id=self.user_id_2, admin_regions=self.user_admin_regions_2, username=self.user_full_name_2, salt='nacl', hashed_password='******') self.users = [self.user_1, self.user_2] self.region_1 = Region(id='norcal', display_name='Norcal') self.region_2 = Region(id='texas', display_name='Texas') self.regions = [self.region_1, self.region_2] for region in self.regions: Dao.insert_region(region, self.mongo_client, database_name=DATABASE_NAME) self.norcal_dao = Dao('norcal', self.mongo_client, database_name=DATABASE_NAME) for player in self.players: self.norcal_dao.insert_player(player) for tournament in self.tournaments: self.norcal_dao.insert_tournament(tournament) for pending_tournament in self.pending_tournaments: self.norcal_dao.insert_pending_tournament(pending_tournament) for ranking in self.rankings: self.norcal_dao.insert_ranking(ranking) for user in self.users: self.norcal_dao.insert_user(user)
def delete(self, uid): deleted_rows = Ranking.get(Ranking.id == uid).delete_instance() return jsonify(message=f'Deleted ({deleted_rows}) rankings.')