def add_new_teams(): app_service = AppService() team_service = TeamService() game_data = app_service.get_current_data() teams_to_add = get_next_teams(str(game_data.current_year)) for t in teams_to_add: team_service.create(t, 0, True)
def get_team_list(): team_service = TeamService() teams = team_service.get_all() result = [] for t in teams: result.append(t.dictionary()) return jsonify(result)
def get_team_view(team_id): standings_service = StandingsService() standings_view = standings_service.get_standings_team_history_view(team_id) team_service = TeamService() team = team_service.get_by_id(team_id) team_view = TeamPageViewModel(team, standings_view, []) return render_template('teams/pages/team_edit.html', view=team_view)
def get_standings_team_history_view(team_id): record_service = RecordService() app_service = AppService() team_service = TeamService() team = team_service.get_by_id(team_id) teams = team_service.get_all() current_data = app_service.get_current_data() records = record_service.get_by_team(team_id) record_service.sort_by_year(records) return StandingsTeamHistoryViewModel(current_data, team.name, records, teams)
def button_update_team_clicked(): team_id = request.form["id"] team_name = request.form["name"] team_active = RequestUtilities.get_boolean_from_form_input( request.form, "active") team_skill = request.form["skill"] team_service = TeamService() team_service.update(team_id, team_name, team_skill, team_active) return get_team_view(team_id)
def button_update_team_list_clicked(): action_string = request.form["update"] action_string_parsed = action_string.split("_") action = action_string_parsed[0] team_service = TeamService() if action == "All": for key in request.form: if key.startswith("id_"): oid = key.split("_")[1] team_id = request.form["id_" + oid] team_name = request.form["name_" + oid] team_active = RequestUtilities.get_boolean_from_form_input( request.form, "active_" + oid) team_skill = int(request.form["skill_" + oid]) team_service.update(team_id, team_name, team_skill, team_active) pass else: oid = action_string_parsed[1] team_id = request.form["id_" + oid] team_name = request.form["name_" + oid] team_active = RequestUtilities.get_boolean_from_form_input( request.form, "active_" + oid) team_skill = request.form["skill_" + oid] if action == "Delete": team_service.delete_team(team_id) elif action == "Update": team_service.update(team_id, team_name, team_skill, team_active) return get_team_edit_view()
def button_create_team_clicked(): team_name = request.form["name"] team_active = RequestUtilities.get_boolean_from_form_input( request.form, "active") team_skill = request.form["skill"] if team_name is None or team_name == "": pass else: team_service = TeamService() team_service.create(team_name, team_skill, team_active) return get_team_edit_view()
def test_create_and_update(self): BaseTestService.setup_test() service = TeamService() service.create("New Team", 55, True) view = service.get_team_by_name("New Team") service.update(view.oid, "Updated Name", 33, False) view2 = service.get_by_id(view.oid) self.assertEqual(view.oid, view2.oid) self.assertEqual("Updated Name", view2.name) self.assertEqual(33, view2.skill) self.assertFalse(view2.active)
def test_get_all(self): BaseTestService.setup_test() service = TeamService() current = len(service.get_all()) for k in range(10): service.create("Team Get All " + str(k), k, True) all_data = service.get_all() self.assertEqual(10 + current, len(all_data))
def test_get_by_year(self): self.setup_test() team_service = TeamService() service = RecordService() for i in range(10): team_service.create("Team " + str(i), 5, True) teams = team_service.get_all() [service.add(team_service.get_all(), 25)] [service.add(team_service.get_all(), 35)] [service.add(team_service.get_all(), 2)] self.assertEqual(10, len(service.get_by_year(2)))
def test_create_and_get_by_name_and_get_by_oid(self): BaseTestService.setup_test() service = TeamService() service.create("Team 1 create", 12, True) team_view = service.get_team_by_name("Team 1 create") self.assertEqual("Team 1 create", team_view.name) self.assertEqual(12, team_view.skill) self.assertIsNotNone(team_view.oid) team_view2 = service.get_by_id(team_view.oid) self.assertEqual(team_view2.oid, team_view.oid) self.assertEqual(team_view2.name, team_view.name) self.assertEqual(team_view2.skill, team_view.skill)
def test_add_get_by_year_get_by_team(self): self.setup_test() team_service = TeamService() service = RecordService() for i in range(10): team_service.create("Team " + str(i), 5, True) teams = team_service.get_all() [service.add(team_service.get_all(), 25)] [service.add(team_service.get_all(), 35)] [service.add(team_service.get_all(), 2)] result = service.get_by_year(35) [self.assertEqual(35, r.year) for r in result] result = service.get_by_team_and_year(teams[6].oid, 35) self.assertEqual(teams[6].oid, result.team_id, "by year and team") self.assertEqual(35, result.year, "by year and team")
def test_should_get_standings_history_view(self): self.setup_test() team_service = TeamService() record_service = RecordService() app_service = AppService() app_service.setup_data(35, 1, True, True) standings_service = StandingsService() for i in range(10): team_service.create("Team " + str(i), 5, True) record_service.add(team_service.get_all(), 25) record_service.add(team_service.get_all(), 35) record_service.add(team_service.get_all(), 2) result = standings_service.get_standings_history_view(2) self.assertEqual(10, len(result.records))
class GameService(BaseService): team_service = TeamService() record_service = RecordService() repo = GameRepository() team_repo = TeamRepository() record_repo = RecordRepository() game_rules_repo = GameRulesRepository() def create_game_from_schedule_game(self, schedule_game, session=None): commit = False if session is None: session = self.repo.get_session() commit = True team_a = self.team_repo.get_by_oid(schedule_game.home_team, session) team_b = self.team_repo.get_by_oid(schedule_game.away_team, session) rules = self.game_rules_repo.get_by_oid(schedule_game.rules, session) if team_a is None: raise AttributeError("Team A cannot be none.") if team_b is None: raise AttributeError("Team B cannot be none.") game = Game(schedule_game.year, schedule_game.day, team_a, team_b, 0, 0, False, False, rules, self.get_new_id()) if commit: session.Commit() return game def create_games(self, team_list, year, start_day, rules, rounds, home_and_away, session=None): if session is None: session = self.repo.get_session() scheduler = Scheduler() team_ids = [i.oid for i in team_list] schedule_games = [] # schedule_games = scheduler.schedule_games(team_ids, rules.oid, year, start_day, home_and_away) for i in range(rounds): schedule_games.extend( scheduler.schedule_games(team_ids, rules.oid, year, start_day, home_and_away)) start_day = max([sg.day for sg in schedule_games]) start_day += 1 new_games = [ self.create_game_from_schedule_game(sg, session) for sg in schedule_games ] [self.repo.add(g, session) for g in new_games] session.commit() @staticmethod def games_to_game_day_view(games): game_vm = [GameService.game_to_vm(g) for g in games] return GameDayViewModel(None, game_vm[0].day, game_vm[0].year, game_vm) @staticmethod def game_to_vm(g): return GameViewModel(g.oid, g.year, g.day, g.home_team.name, g.home_team.oid, g.away_team.name, g.away_team.oid, g.home_score, g.away_score, g.complete) def get_all_games(self): session = self.get_session() return [self.game_to_vm(g) for g in self.repo.get_all(session)] def play_game(self, game_list, random, session=None): commit = session is None session = self.get_session(session) for g in game_list: game = self.repo.get_by_oid(g.oid, session) game.play() self.commit(session, commit) def get_games_for_days(self, year, first_day, last_day, session=None): commit = session is None session = self.get_session() result = self.repo.get_games_by_day(year, first_day, last_day, session) self.commit(session, commit) return [self.game_to_vm(g) for g in result] def play_games_for_days(self, year, first_day, last_day, random, session=None): commit = session is None session = self.get_session(session) games = self.repo.get_games_by_day(year, first_day, last_day, session) self.play_game(games, random, session) self.commit(session, commit) return [ self.game_to_vm(g) for g in self.repo.get_games_by_day( year, first_day, last_day, session) ] def get_incomplete_games_for_days(self, year, first_day, last_day, session=None): session = self.get_session(session) return [ self.game_to_vm(g) for g in self.repo.get_incomplete_or_unprocessed_games_by_day( year, first_day, last_day, session) ] def get_complete_and_unprocessed_games_for_days(self, year, first_day, last_day, session=None): session = self.get_session(session) return [ self.game_to_vm(g) for g in self.repo.get_by_unprocessed_and_complete( year, first_day, last_day, session) ] # processing games needs to go to a higher level so we can know how to process a given game def process_games_for_days(self, year, first_day, last_day, session=None): commit = session is None session = self.get_session(session) games_to_process = self.repo.get_by_unprocessed_and_complete( year, first_day, last_day, session) for game in games_to_process: home_record = self.record_repo.get_by_team_and_year( game.home_team.oid, year, session) away_record = self.record_repo.get_by_team_and_year( game.away_team.oid, year, session) home_record.process_game(game.home_score, game.away_score) away_record.process_game(game.away_score, game.home_score) game.processed = True self.commit(session, commit) def process_games_before(self, year, before_this_day, session=None): raise NotImplementedError def get_incomplete_games_by_year_count(self, year, session=None): session = self.get_session(session) repo = GameRepository() return repo.get_incomplete_or_unprocessed_games_by_year_count( year, session)
def get_team_edit_view(): team_service = TeamService() teams = team_service.get_all() view = TeamEditListViewModel(teams) return render_template('teams/pages/multi_team_edit.html', view=view)
add_teams = True rounds = 1 do_home_and_away = False rules_name = "Season" # other is Playoff Database.init_db(db_connection_string) if setup: Database.clean_up_database(Database.get_session()) data_setup.setup() if add_teams: data_setup.add_new_teams() team_service = TeamService() for t in team_service.get_all(): print(t.name + " " + str(t.skill)) game_service = GameService() record_service = RecordService() game_rules_service = GameRulesService() app_service = AppService() rules = game_rules_service.get_by_name(rules_name) game_data = app_service.get_current_data() if game_data.is_year_finished: app_service.go_to_next_year() app_service.setup_year(rules, rounds, do_home_and_away)
from teams.data.database import Database from teams.domain.competition_configuration import CompetitionConfiguration, TableSubCompetitionConfiguration, \ RankingGroupConfiguration, CompetitionTeamConfiguration from teams.services.app_service import AppService from teams.services.configuration_service import ConfigurationService from teams.services.game_service import GameRulesService from teams.services.team_service import TeamService setup = True Database.init_db(db_connection_string) # services app_service = AppService() game_rules_service = GameRulesService() team_service = TeamService() config_service = ConfigurationService() def setup(): game_rules_service.create("Season", True) game_rules_service.create("Playoff", False) app_service.setup_data(0, 0, True, True) setup_teams() all_teams = ["Toronto", "Montreal", "Ottawa", "Quebec City", "Vancouver", "Calgary", "Edmonton", "Winnipeg"] western_teams = ["Vancouver", "Calgary", "Edmonton", "Winnipeg"] eastern_teams = ["Toronto", "Montreal", "Ottawa", "Quebec City"]