def test_from_pending_tournament_throws_exception(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] alias_to_id_map = [AliasMapping(player_alias=self.player_1.name, player_id=None)] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, regions=['norcal'], raw_id=self.raw_id, players=player_aliases, matches=matches, alias_to_id_map=alias_to_id_map) with self.assertRaises(Exception) as e: Tournament.from_pending_tournament(pending_tournament) self.assertTrue('Alias gar has no ID in map' in str(e.exception))
def test_from_pending_tournament_throws_exception(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=None) ] pending_tournament = PendingTournament(name=self.name, type=self.type, date=self.date, regions=['norcal'], raw_id=self.raw_id, players=player_aliases, matches=matches, alias_to_id_map=alias_to_id_map) with self.assertRaises(Exception) as e: Tournament.from_pending_tournament(pending_tournament) self.assertTrue('Alias gar has no ID in map' in str(e.exception))
def test_Tournement(self): assert Tournament.Insert(sport_test.randomString(self), "overcooked", "2020-01-0714:30:00") with pytest.raises(turnamentError) as context: result = Tournament.Insert(sport_test.randomString(self), "null", "2020-01-0714:30:00") print(str(context.value))
def _import_files(self): for f in NORCAL_FILES: scraper = TioScraper(f[0], f[1]) self._import_players(scraper, self.norcal_dao) self.norcal_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.norcal_dao)) for f in TEXAS_FILES: scraper = TioScraper(f[0], f[1]) self._import_players(scraper, self.texas_dao) self.texas_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.texas_dao))
def callback(ch, method, properties, body): print(" [x] Received %r" % body) output_json = json.loads(body) # add title print('add title') print(output_json["data"]["title"], output_json["data"]["date_start_text"]) Title.Insert(output_json["data"]["title"], output_json["data"]["state"]) # add tournamte trnmt = "" print('add tournamte', output_json["data"]["title"]) tmp = output_json["data"]["tournament"] if len(tmp) == 2: print(tmp["name"]) Tournament.Insert(tmp["name"], output_json["data"]["title"], output_json["data"]["date_start_text"]) trnmt = tmp["name"] else: print(output_json["data"]["tournament"]) Tournament.Insert(output_json["data"]["tournament"], output_json["data"]["title"], output_json["data"]["date_start_text"]) trnmt = output_json["data"]["tournament"] # add team print('add team') for tmp in output_json["data"]["teams"]: Team.Insert(tmp["id"], tmp["name"]) print(tmp["id"], tmp["name"]) # add Matches print('add matches') print(output_json["data"]["scores"][0]["team"]) print(output_json["data"]["scores"][1]["team"]) Matches.Insert(trnmt, output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"]) # add Score print('add Score') # Insert(_Tournament, _Team,_Score,_winner,_Team1,_Team2) Scores.Insert(trnmt, output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][0]["score"], output_json["data"]["scores"][0]["winner"], output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"]) Scores.Insert(trnmt, output_json["data"]["scores"][1]["team"], output_json["data"]["scores"][1]["score"], output_json["data"]["scores"][1]["winner"], output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"])
def post(self, region, id): dao = Dao(region, mongo_client=mongo_client) pending_tournament = dao.get_pending_tournament_by_id(ObjectId(id)) if not pending_tournament: return 'No pending tournament found with that id.', 400 user = get_user_from_request(request, dao) if not user: return 'Permission denied', 403 if not is_user_admin_for_region(user, pending_tournament.regions): return 'Permission denied', 403 new_player_names = [] for mapping in pending_tournament.alias_to_id_map: if mapping["player_id"] == None: new_player_names.append(mapping["player_alias"]) for player_name in new_player_names: player = Player.create_with_default_values(player_name, region) player_id = dao.insert_player(player) pending_tournament.set_alias_id_mapping(player_name, player_id) dao.update_pending_tournament(pending_tournament) try: tournament = Tournament.from_pending_tournament(pending_tournament) tournament_id = dao.insert_tournament(tournament) dao.delete_pending_tournament(pending_tournament) return {"success": True, "tournament_id": str(tournament_id)} except ValueError: return 'Not all player aliases in this pending tournament have been mapped to player ids.', 400
def test_from_pending_tournament(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, raw_id=self.raw_id, regions=['norcal'], players=player_aliases, matches=matches, alias_to_id_map=self.alias_to_id_map) tournament = Tournament.from_pending_tournament(pending_tournament) self.assertIsNone(tournament.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches[0].winner, self.matches[0].winner) self.assertEqual(tournament.matches[0].loser, self.matches[0].loser) self.assertEqual(tournament.matches[1].winner, self.matches[1].winner) self.assertEqual(tournament.matches[1].loser, self.matches[1].loser) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, ['norcal'])
def post(self, region, id): dao = Dao(region, mongo_client=mongo_client) pending_tournament = dao.get_pending_tournament_by_id(ObjectId(id)) if not pending_tournament: return 'No pending tournament found with that id.', 400 user = get_user_from_request(request, dao) if not user: return 'Permission denied', 403 if not is_user_admin_for_regions(user, pending_tournament.regions): return 'Permission denied', 403 new_player_names = [] for mapping in pending_tournament.alias_to_id_map: if mapping["player_id"] == None: new_player_names.append(mapping["player_alias"]) for player_name in new_player_names: player = Player.create_with_default_values(player_name, region) player_id = dao.insert_player(player) pending_tournament.set_alias_id_mapping(player_name, player_id) dao.update_pending_tournament(pending_tournament) try: tournament = Tournament.from_pending_tournament(pending_tournament) tournament_id = dao.insert_tournament(tournament) dao.delete_pending_tournament(pending_tournament) return {"success": True, "tournament_id": str(tournament_id)} except ValueError: return 'Not all player aliases in this pending tournament have been mapped to player ids.', 400
def test_load(self): tournament = Tournament.load(self.tournament_json_dict, context='db') self.assertEqual(tournament.id, self.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches, self.matches) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, self.regions)
class TestTournament(unittest.TestCase): def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id, match_id=1) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id, match_id=2) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.url = "challonge.com/tournament" self.player_ids = [ self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id ] self.players = [ self.player_1, self.player_2, self.player_3, self.player_4 ] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.excluded = False self.tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'url': self.url, 'players': self.player_ids, 'orig_ids': self.player_ids, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'excluded': self.excluded } self.tournament = Tournament(id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.player_ids, orig_ids=self.player_ids, matches=self.matches, excluded=self.excluded) def test_replace_player(self): self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.tournament.matches[1].contains_player( self.player_3_id)) self.assertFalse(self.player_5_id in self.tournament.players) for match in self.tournament.matches: self.assertFalse(match.contains_player(self.player_5_id)) self.assertEqual(len(self.tournament.players), 4) self.tournament.replace_player(player_to_remove=self.player_3, player_to_add=self.player_5) self.assertFalse(self.player_3_id in self.tournament.players) for match in self.tournament.matches: self.assertFalse(match.contains_player(self.player_3_id)) self.assertTrue(self.player_5_id in self.tournament.players) self.assertTrue(self.tournament.matches[1].contains_player( self.player_5_id)) self.assertEqual(len(self.tournament.players), 4) def test_replace_player_none(self): with self.assertRaises(TypeError): self.tournament.replace_player(player_to_add=self.player_1) with self.assertRaises(TypeError): self.tournament.replace_player(player_to_remove=self.player_1) def test_replace_player_invalid_player_to_remove(self): self.assertTrue(self.player_1_id in self.tournament.players) self.assertTrue(self.player_2_id in self.tournament.players) self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.player_4_id in self.tournament.players) self.assertEqual(len(self.tournament.players), 4) self.tournament.replace_player(player_to_remove=self.player_5, player_to_add=self.player_6) self.assertTrue(self.player_1_id in self.tournament.players) self.assertTrue(self.player_2_id in self.tournament.players) self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.player_4_id in self.tournament.players) self.assertEqual(len(self.tournament.players), 4) def test_dump(self): self.assertEqual(self.tournament.dump(context='db'), self.tournament_json_dict) def test_load(self): tournament = Tournament.load(self.tournament_json_dict, context='db') self.assertEqual(tournament.id, self.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches, self.matches) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, self.regions) def test_from_pending_tournament(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, raw_id=self.raw_id, regions=['norcal'], players=player_aliases, matches=matches, alias_to_id_map=self.alias_to_id_map) tournament = Tournament.from_pending_tournament(pending_tournament) self.assertIsNone(tournament.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches[0].winner, self.matches[0].winner) self.assertEqual(tournament.matches[0].loser, self.matches[0].loser) self.assertEqual(tournament.matches[1].winner, self.matches[1].winner) self.assertEqual(tournament.matches[1].loser, self.matches[1].loser) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, ['norcal']) def test_from_pending_tournament_throws_exception(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=None) ] pending_tournament = PendingTournament(name=self.name, type=self.type, date=self.date, regions=['norcal'], raw_id=self.raw_id, players=player_aliases, matches=matches, alias_to_id_map=alias_to_id_map) with self.assertRaises(Exception) as e: Tournament.from_pending_tournament(pending_tournament) self.assertTrue('Alias gar has no ID in map' in str(e.exception))
def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id, match_id=1) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id, match_id=2) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.url = "challonge.com/tournament" self.player_ids = [self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id] self.players = [self.player_1, self.player_2, self.player_3, self.player_4] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'url': self.url, 'players': self.player_ids, 'orig_ids': self.player_ids, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions } self.tournament = Tournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.player_ids, orig_ids=self.player_ids, matches=self.matches)
class TestTournament(unittest.TestCase): def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id, match_id=1) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id, match_id=2) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.url = "challonge.com/tournament" self.player_ids = [self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id] self.players = [self.player_1, self.player_2, self.player_3, self.player_4] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'url': self.url, 'players': self.player_ids, 'orig_ids': self.player_ids, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions } self.tournament = Tournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.player_ids, orig_ids=self.player_ids, matches=self.matches) def test_replace_player(self): self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.tournament.matches[ 1].contains_player(self.player_3_id)) self.assertFalse(self.player_5_id in self.tournament.players) for match in self.tournament.matches: self.assertFalse(match.contains_player(self.player_5_id)) self.assertEqual(len(self.tournament.players), 4) self.tournament.replace_player( player_to_remove=self.player_3, player_to_add=self.player_5) self.assertFalse(self.player_3_id in self.tournament.players) for match in self.tournament.matches: self.assertFalse(match.contains_player(self.player_3_id)) self.assertTrue(self.player_5_id in self.tournament.players) self.assertTrue(self.tournament.matches[ 1].contains_player(self.player_5_id)) self.assertEqual(len(self.tournament.players), 4) def test_replace_player_none(self): with self.assertRaises(TypeError): self.tournament.replace_player(player_to_add=self.player_1) with self.assertRaises(TypeError): self.tournament.replace_player(player_to_remove=self.player_1) def test_replace_player_invalid_player_to_remove(self): self.assertTrue(self.player_1_id in self.tournament.players) self.assertTrue(self.player_2_id in self.tournament.players) self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.player_4_id in self.tournament.players) self.assertEqual(len(self.tournament.players), 4) self.tournament.replace_player( player_to_remove=self.player_5, player_to_add=self.player_6) self.assertTrue(self.player_1_id in self.tournament.players) self.assertTrue(self.player_2_id in self.tournament.players) self.assertTrue(self.player_3_id in self.tournament.players) self.assertTrue(self.player_4_id in self.tournament.players) self.assertEqual(len(self.tournament.players), 4) def test_contains_player(self): self.assertTrue(self.tournament.contains_player(self.player_1)) self.assertTrue(self.tournament.contains_player(self.player_2)) self.assertTrue(self.tournament.contains_player(self.player_3)) self.assertTrue(self.tournament.contains_player(self.player_4)) def test_does_not_contain_player(self): self.assertFalse(self.tournament.contains_player(self.player_5)) def test_dump(self): self.assertEqual(self.tournament.dump( context='db'), self.tournament_json_dict) def test_load(self): tournament = Tournament.load(self.tournament_json_dict, context='db') self.assertEqual(tournament.id, self.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches, self.matches) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, self.regions) def test_validate_document(self): result, msg = self.tournament.validate_document() self.assertTrue(result) def test_validate_document_duplicate_players(self): self.tournament.players = [ self.player_1_id, self.player_2_id, self.player_3_id, self.player_3_id] result, msg = self.tournament.validate_document() self.assertFalse(result) def test_from_pending_tournament(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, raw_id=self.raw_id, regions=['norcal'], players=player_aliases, matches=matches, alias_to_id_map=self.alias_to_id_map) tournament = Tournament.from_pending_tournament(pending_tournament) self.assertIsNone(tournament.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches[0].winner, self.matches[0].winner) self.assertEqual(tournament.matches[0].loser, self.matches[0].loser) self.assertEqual(tournament.matches[1].winner, self.matches[1].winner) self.assertEqual(tournament.matches[1].loser, self.matches[1].loser) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, ['norcal']) def test_from_pending_tournament_throws_exception(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] alias_to_id_map = [AliasMapping(player_alias=self.player_1.name, player_id=None)] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, regions=['norcal'], raw_id=self.raw_id, players=player_aliases, matches=matches, alias_to_id_map=alias_to_id_map) with self.assertRaises(Exception) as e: Tournament.from_pending_tournament(pending_tournament) self.assertTrue('Alias gar has no ID in map' in str(e.exception))
class Controller: """Represents the controller.""" def __init__(self): """Init class with attributes.""" self.view = MainMenu(observer=self, tournaments=Tournament.list()) self.running = True self.context = {} self.tournament = None def run(self): """Launch the program.""" if len(Player.list()) < 1: Player.create_height_players() while self.running: self.view.display() def execute(self, command): """Execute the given command.""" name = command["name"] if name == "main_page": self.view = MainMenu(observer=self) elif name == "create_tournament": self.view = CreateTournamentView(observer=self) self.view.display() self.tournament = Tournament(**self.context["tournament_data"]) self.view.display_player_menu() elif name == "retrieve_tournament": self.view = RetrieveTournamentView(observer=self) self.view.display(Tournament.list()) self.tournament = Tournament.get(self.context["chosen_tournament"]) if self.tournament.turn_finished(): if self.tournament.is_finished(): self.execute({"name": "end_tournament"}) else: self.execute({"name": "create_matchs"}) else: self.execute({"name": "choose_match"}) elif name == "player_menu": self.view = PlayerMenuView(observer=self, players=self.tournament.players) elif name == "select_player": self.view = SelectPlayerView( observer=self, players=Player.list(), tournament_players=self.tournament.players, ) elif name == "add_player": self.tournament.players.append(Player.get(self.context["chosen_player"])) elif name == "ask_player_creation": self.view = CreatePlayerView(observer=self) self.view.display() player = Player(**self.context["player_to_create"]) player.save() self.tournament.players.append(player) elif name == "create_matchs": self.tournament.create_turn() self.tournament.save() self.view = CreateMatchView(observer=self, matches=self.tournament.matchs) elif name == "choose_match": self.view = ChooseMatchView(observer=self, matches=self.tournament.matchs) elif name == "modify_match": self.view = ModifyMatchView( observer=self, match=self.tournament.get_match(self.context["chosen_match"]), ) self.view.display() self.view.match.finish(self.context["chosen_winner"]) if self.tournament.turn_finished(): if self.tournament.is_finished(): self.execute({"name": "end_tournament"}) else: self.execute({"name": "create_matchs"}) else: self.view.change_match() elif name == "end_tournament": self.tournament.save() self.view = EndTournamentView( observer=self, players=self.tournament.get_players_by_score() ) self.running = False elif name == "wrong_command": self.view.wrong_command()
def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id, match_id=1) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id, match_id=2) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.url = "challonge.com/tournament" self.player_ids = [ self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id ] self.players = [ self.player_1, self.player_2, self.player_3, self.player_4 ] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.excluded = False self.tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'url': self.url, 'players': self.player_ids, 'orig_ids': self.player_ids, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'excluded': self.excluded } self.tournament = Tournament(id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.player_ids, orig_ids=self.player_ids, matches=self.matches, excluded=self.excluded)
def __init__(self): """Init class with attributes.""" self.view = MainMenu(observer=self, tournaments=Tournament.list()) self.running = True self.context = {} self.tournament = None
def execute(self, command): """Execute the given command.""" name = command["name"] if name == "main_page": self.view = MainMenu(observer=self) elif name == "create_tournament": self.view = CreateTournamentView(observer=self) self.view.display() self.tournament = Tournament(**self.context["tournament_data"]) self.view.display_player_menu() elif name == "retrieve_tournament": self.view = RetrieveTournamentView(observer=self) self.view.display(Tournament.list()) self.tournament = Tournament.get(self.context["chosen_tournament"]) if self.tournament.turn_finished(): if self.tournament.is_finished(): self.execute({"name": "end_tournament"}) else: self.execute({"name": "create_matchs"}) else: self.execute({"name": "choose_match"}) elif name == "player_menu": self.view = PlayerMenuView(observer=self, players=self.tournament.players) elif name == "select_player": self.view = SelectPlayerView( observer=self, players=Player.list(), tournament_players=self.tournament.players, ) elif name == "add_player": self.tournament.players.append(Player.get(self.context["chosen_player"])) elif name == "ask_player_creation": self.view = CreatePlayerView(observer=self) self.view.display() player = Player(**self.context["player_to_create"]) player.save() self.tournament.players.append(player) elif name == "create_matchs": self.tournament.create_turn() self.tournament.save() self.view = CreateMatchView(observer=self, matches=self.tournament.matchs) elif name == "choose_match": self.view = ChooseMatchView(observer=self, matches=self.tournament.matchs) elif name == "modify_match": self.view = ModifyMatchView( observer=self, match=self.tournament.get_match(self.context["chosen_match"]), ) self.view.display() self.view.match.finish(self.context["chosen_winner"]) if self.tournament.turn_finished(): if self.tournament.is_finished(): self.execute({"name": "end_tournament"}) else: self.execute({"name": "create_matchs"}) else: self.view.change_match() elif name == "end_tournament": self.tournament.save() self.view = EndTournamentView( observer=self, players=self.tournament.get_players_by_score() ) self.running = False elif name == "wrong_command": self.view.wrong_command()
def show_tournament(): tournament = Tournament.read_tournament() View.show_tournament(tournament)
def choice_menu(choice): if choice == 1: name = input("nom du tournoi: ") localisation = input("lieu: ") date = input("date: ") nbturn = input("nombre de rounds: ") rounds = 0 players = 0 timecontrol = input("bullet, blitz ou coup rapide: ") description = input("description: ") Tournament.create_tournament(name, localisation, date, nbturn, rounds, players, timecontrol, description) elif choice == 2: print("Veuillez rentrer les données du nouveau joueur") name = str(input("Nom: ")) birthday = str(input("Date de naissance: ")) gender = str(input("Genre: ")) rank = str(input('Rang: ')) point = 0 Controller.insert_player(name, birthday, gender, rank, point) print(name, birthday, gender, rank) rep = int(input("1-Oui 2-Non: ")) while rep == 1: print("Veuillez rentrer les données du nouveau joueur") name = str(input("Nom: ")) birthday = str(input("Date de naissance: ")) gender = str(input("Genre: ")) rank = str(input('Rang: ')) point = 0 Controller.insert_player(name, birthday, gender, rank, point) print(name, birthday, gender, rank) rep = int(input("1-Oui 2-Non: ")) print('Ajout de joueur terminé') elif choice == 3: Controller.show_players_alpha() elif choice == 4: Controller.show_players_rank() elif choice == 5: Controller.show_tournament() elif choice == 6: Controller.first_round() elif choice == 7: choice = input("1-Sauvegarder 2-Charger") if choice == 1: pass if choice == 2: pass else: print("commande erreur") pass elif choice == 8: Controller.modify_player() elif choice == 0: quit() elif choice != 0: print("commande inconnus") Controller.start_menu()
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)
from model import Player, Tournament, Round if __name__ == "__main__": player1 = Player("Ezame", "Milad", "20/01/1994", "M") tournament = Tournament("Tournoi2", "Paris", "30/03/2022") round = Round() player1.ranking(5) print(player1.ranking) # tester tous les getters et les setters