public void UpdateLeagueTablePositions(LeagueTable leagueTable)
        {
            var teams = leagueTable.LeagueTablePositions.Select(x => x.Team);

             using (var seasonTeamStatsticsRepository = _repositoryFactory.CreateRepository<SeasonTeamStatistics>())
             {
            foreach (var team in teams)
            {
               var teamStatistics = seasonTeamStatsticsRepository.Find(x => x.TeamId == team.Id).Single();
               teamStatistics.LeagueTablePositions += team.CurrentLeaguePosition + ",";
               _repository.RegisterUpdate(teamStatistics);
            }
             }
        }
Example #2
0
        private void CreateLeague(CompetitionSchedule competitionSchedule, Competition league, List<Team> teams, Season season, MatchDateManager matchDateManager)
        {
            // Create a competition for the League and save it to the database.
             var leagueSeasonCompetition = new SeasonCompetition
             {
            Competition = league,
            Season = season
             };

             competitionSchedule.SeasonCompetitions.Add(leagueSeasonCompetition);

             // Add the teams to the league.
             foreach (var team in teams)
             {
            var seasonCompetitionTeam = new SeasonCompetitionTeam
            {
               SeasonCompetition = leagueSeasonCompetition,
               Team = team
            };
            competitionSchedule.SeasonCompetitionTeams.Add(seasonCompetitionTeam);

            // Update current league for the team.
            team.CurrentLeagueCompetition = league;
             }

             // Create a match schedule.
             var roundRobinTournamentManager = new RoundRobinTournamentManager();
             var matchSchedule = roundRobinTournamentManager.GetSchedule(teams);
             foreach (var round in matchSchedule)
             {
            var leagueRound = new Round
            {
               Id = IdGenerator.GetId(),
               Name = $"{league.Name} round {round.Key + 1}",
               SeasonCompetition = leagueSeasonCompetition,
               Order = round.Key,
               CompetitionName = league.Name,
               CompetitionType = CompetitionType.League
            };

            competitionSchedule.Rounds.Add(leagueRound);

            foreach (var match in round.Value)
            {
               match.Season = season;
               match.Round = leagueRound;
               match.Date = matchDateManager.GetNextMatchDate(CompetitionType.League, round.Key);
               competitionSchedule.Matches.Add(match);
            }
             }

             // Create a league table.
             var leagueTable = new LeagueTable
             {
            CompetitionName = league.Name,
            SeasonCompetition = leagueSeasonCompetition,
            Game = season.Game
             };

             leagueTable.LeagueTablePositions = new List<LeagueTablePosition>();
             foreach (var team in teams)
             {
            leagueTable.LeagueTablePositions.Add(new LeagueTablePosition { Team = team, LeagueTable = leagueTable, Position = team.CurrentLeaguePosition });
             }

             competitionSchedule.LeagueTables.Add(leagueTable);
        }
Example #3
0
        public void UpdateLeagueTable(LeagueTable leagueTable, IEnumerable<Match> matches)
        {
            foreach (var match in matches)
             {
            // Determine the home and away team.
            var homeTeam = leagueTable.LeagueTablePositions.First(pos => pos.Team.Equals(match.HomeTeam));
            var awayTeam = leagueTable.LeagueTablePositions.First(pos => pos.Team.Equals(match.AwayTeam));

            // Increment number of played matches.
            homeTeam.Matches++;
            awayTeam.Matches++;

            // Calculate goals scored, conceded and differential for both teams.
            homeTeam.GoalsScored += match.HomeScore;
            homeTeam.GoalsConceded += match.AwayScore;
            homeTeam.GoalDifference = homeTeam.GoalsScored - homeTeam.GoalsConceded;
            awayTeam.GoalsScored += match.AwayScore;
            awayTeam.GoalsConceded += match.HomeScore;
            awayTeam.GoalDifference = awayTeam.GoalsScored - awayTeam.GoalsConceded;

            // Determine the number of points for both teams.
            // Increment the number of wins, losses and/or draws for both teams.
            int homePoints = 1;
            int awayPoints = 1;
            if (match.HomeScore == match.AwayScore)
            {
               homeTeam.Draws++;
               awayTeam.Draws++;
            }
            else
            {
               bool homeTeamWins = match.HomeScore > match.AwayScore;
               bool awayTeamWins = match.AwayScore > match.HomeScore;
               if (homeTeamWins)
               {
                  homePoints = 3;
                  awayPoints = 0;
                  homeTeam.Wins++;
                  awayTeam.Losses++;
               }
               else if (awayTeamWins)
               {
                  homePoints = 0;
                  awayPoints = 3;
                  homeTeam.Losses++;
                  awayTeam.Wins++;
               }
            }

            homeTeam.Points += homePoints;
            awayTeam.Points += awayPoints;
             }

             // Sort the league table.
             leagueTable.LeagueTablePositions.Sort((x, y) =>
             {
            // Points will be sorted ascending, hence Y followed by X.
            int result = y.Points.CompareTo(x.Points);

            // If necessary, matches will be sorted descending, hence X followed by Y.
            if (result == 0)
            {
               result = x.Matches.CompareTo(y.Matches);
            }

            // If necessary, goal difference will be sorted.
            if (result == 0)
            {
               result = y.GoalDifference.CompareTo(x.GoalDifference);
            }

            // If necessary, goals scored will be sorted.
            if (result == 0)
            {
               result = y.GoalsScored.CompareTo(x.GoalsScored);
            }

            //TODO Eigenlijk moet er gekeken worden naar onderling resultaat, maar we doen het eerst maar random...
            if (result == 0)
            {
               var randomizer = new Randomizer();
               bool randomBoolean = randomizer.GetRandomBoolean();
               if (randomBoolean)
               {
                  result = 1;
               }
            }

            return result;
             });

             // Update position numbers, starting with 1.
             int position = 1;
             foreach (var leagueTablePosition in leagueTable.LeagueTablePositions)
             {
            leagueTablePosition.Position = position;
            leagueTablePosition.Team.CurrentLeaguePosition = position;
            position++;
             }
        }