コード例 #1
0
ファイル: test_game.py プロジェクト: jodyunter/teampython
    def test_stats(self):
        t1_skill = 10
        t2_skill = 0
        team1 = Team("T1", t1_skill, True, "1")
        team2 = Team("T2", t2_skill, True, "2")

        record1 = Record(1, team1, 1, 0, 0, 0, 0, 0, 0, "3")
        record2 = Record(1, team2, 1, 0, 0, 0, 0, 0, 0, "3")

        rules = GameRules("Rules", True, "4")

        for i in range(1000):
            game = Game(1, 1, team1, team2, 0, 0, False, False, rules, str(i))
            game.play()
            record1.process_game(game.home_score, game.away_score)
            record2.process_game(game.away_score, game.home_score)

        print()
        print("Results")
        print(RecordView.get_table_header())
        print(
            RecordView.get_table_row(
                RecordService.get_view_from_model(record1)))
        print(
            RecordView.get_table_row(
                RecordService.get_view_from_model(record2)))
コード例 #2
0
    def get_current_standings_view():
        record_service = RecordService()
        app_service = AppService()
        current_data = app_service.get_current_data()
        records = record_service.get_by_year(current_data.current_year)
        record_service.sort_default(records)

        return StandingsCurrentViewModel(current_data, records)
コード例 #3
0
    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)
コード例 #4
0
    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)))
コード例 #5
0
def print_group(group_name, table_to_print, description):
    group = competition.get_group_by_name(group_name)

    recs = TableSubCompetition.get_records_by_group(group,
                                                    table_to_print.records)
    recs.sort(key=lambda rec: rec.rank)

    print(description)
    print(RecordView.get_table_header())
    for r in recs:
        print(RecordView.get_table_row(RecordService.get_view_from_model(r)))
コード例 #6
0
    def get_standings_history_view(year):
        record_service = RecordService()
        app_service = AppService()
        current_data = app_service.get_current_data()
        records = record_service.get_by_year(year)
        record_service.sort_default(records)
        seasons = record_service.get_all_seasons_for_dropdown()
        seasons.sort(reverse=True)

        return StandingsHistoryViewModel(current_data, year, records, seasons)
コード例 #7
0
    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))
コード例 #8
0
    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")
コード例 #9
0
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)

while not app_service.is_year_complete():
    game_data = app_service.get_current_data()
    # print("Playing games on day " + str(game_data.current_day))
    r = random
    app_service.play_and_process_games_for_current_day(r)
    os.system('cls')
コード例 #10
0
ファイル: game_service.py プロジェクト: jodyunter/teampython
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)