Пример #1
0
 protected ServiceBase(Game game)
 {
     Game = game;
      NumberRandomizer = new NumberRandomizer();
      ListRandomizer = new ListRandomizer();
      ServiceFactory = new ServiceFactory();
 }
Пример #2
0
 public Team GetMyTeam(Game game)
 {
     using (var teamRepository = new RepositoryFactory().CreateTeamRepository())
      {
     return teamRepository.GetTeam(game.CurrentTeamId);
      }
 }
Пример #3
0
        public IEnumerable<Team> Create(Game game, int howMany)
        {
            var teams = new List<Team>();

             using (var formationRepository = new RepositoryFactory().CreateFormationRepository())
             {
            var formations = formationRepository.GetAll();
            bool teamGenerationReady = false;
            while (!teamGenerationReady)
            {
               var team = _teamGenerator.Generate();

               // Team names must be unique.
               bool teamExists = teams.Any(t => t.Name == team.Name);
               if (!teamExists)
               {
                  team.Game = game;
                  team.Formation = _listRandomizer.GetItem(formations);
                  teams.Add(team);
               }

               teamGenerationReady = (teams.Count == howMany);
            }
             }

             return teams;
        }
Пример #4
0
        public Season CreateFirstSeason(Game game, List<Team> teams, IUnitOfWorkRepository repository)
        {
            // First check the number of teams can be evenly divided into the number of leagues.
             bool teamsOk = teams.Count % Constants.HowManyLeagues == 0;
             if (!teamsOk)
             {
            throw new Exception($"The number of teams must be divided by {Constants.HowManyLeagues}");
             }

             var newSeasonInfo = new NewSeasonInfo { Game = game, SeasonNumber = 0 };

             // Divide all teams between the four leagues based on the team rating.
             teams.Sort((team1, team2) => team2.Rating.CompareTo(team1.Rating));
             int countTeamsPerLeague = teams.Count / Constants.HowManyLeagues;
             newSeasonInfo.TeamsLeague1 = teams.Take(countTeamsPerLeague).ToList();
             newSeasonInfo.TeamsLeague2 = teams.Skip(countTeamsPerLeague).Take(countTeamsPerLeague).ToList();
             newSeasonInfo.TeamsLeague3 = teams.Skip(countTeamsPerLeague * 2).Take(countTeamsPerLeague).ToList();
             newSeasonInfo.TeamsLeague4 = teams.Skip(countTeamsPerLeague * 3).ToList();

             // The teams have been sorted on rating, so given them an initial league table position.
             AssignInitialLeagueTablePosition(teams);

             // In the first season there are no champion and cup winner yet, so pick the two best teams.
             newSeasonInfo.NationalChampion = teams[0];
             newSeasonInfo.NationalCupWinner = teams[1];

             // Now all teams have been placed in the right leagues, so create match schedules for all competitions.
             var seasonAndCompetitionSchedules = CreateSeasonAndCompetitionSchedules(newSeasonInfo);

             // Insert the season and all competition schedules.
             var season = InsertSeasonAndCompetitionSchedule(repository, seasonAndCompetitionSchedules);

             return season;
        }
Пример #5
0
 /// <summary>
 /// Gets all teams of the game.
 /// </summary>
 public IEnumerable<Team> GetAll(Game game)
 {
     using (var teamRepository = new RepositoryFactory().CreateTeamRepository())
      {
     var teams = teamRepository.GetTeamsByGame(game.Id);
     return teams;
      }
 }
Пример #6
0
 public IEnumerable<Player> GetAll(Game game)
 {
     using (var playerRepository = new RepositoryFactory().CreatePlayerRepository())
      {
     var players = playerRepository.GetPlayersByGame(game);
     return players;
      }
 }
Пример #7
0
        internal bool TryFindGameForCurrentUser(string gameId, out Game game)
        {
            var gameService = ServiceFactory.CreateGameService();

             //TODO Deze methode wordt voor elke request aangeroepen en de uitkomst is altijd hetzelfde,
             // =======> dus Games/users cachen, dus Dictionary<string, Game> maken
             var userGames = gameService.GetGames(User.Identity.Id);
             game = userGames.SingleOrDefault(g => g.Id.Equals(gameId));
             bool gameFound = game != null;

             return gameFound;
        }
Пример #8
0
        public Game CreateGame()
        {
            using (var log = Logger.GetLogger(this))
             {
            using (var repository = new RepositoryFactory().CreateUnitOfWorkRepository())
            {
               // Insert the game into the database.
               string gameName = DateTime.Now.ToString("yyyyMMddHHmmss");
               var game = new Game { Name = gameName };

               // Create teams and players.
               var teamsAndPlayers = CreateTeamsAndPlayers(game);

               // Insert teams.
               repository.RegisterInsert(teamsAndPlayers.Teams);

               // Insert players and skill scores.
               repository.RegisterInsert(teamsAndPlayers.Players);
               foreach (var player in teamsAndPlayers.Players)
               {
                  repository.RegisterInsert(player.SkillScores);
               }

               var seasonManager = new SeasonManager(game);

               // Create a season with match schedules.
               var season = seasonManager.CreateFirstSeason(game, teamsAndPlayers.Teams, repository);

               // Insert the season statistics and season/team statistics.
               var seasonStatistics = new SeasonStatistics(season);
               repository.RegisterInsert(seasonStatistics);
               foreach (var team in teamsAndPlayers.Teams)
               {
                  var seasonTeamStatistics = new SeasonTeamStatistics(season, team);
                  repository.RegisterInsert(seasonTeamStatistics);
               }

               // Assign the user a random team.
               //TODO let the user choose a team via the GUI...
               //var teamToManage = _listRandomizer.GetItem(teamsAndPlayers.Teams);
               //game.CurrentTeam = teamToManage;

               repository.RegisterInsert(game);

               repository.Save();

               return game;
            }
             }
        }
Пример #9
0
 public SeasonManager(Game game)
 {
     _game = game;
      _leagueManager = new LeagueManager();
      _repositoryFactory = new RepositoryFactory();
 }
Пример #10
0
 internal SeasonService(Game game)
     : base(game)
 {
 }
Пример #11
0
 internal TeamService(Game game)
     : base(game)
 {
     _teamGenerator = new TeamGenerator();
      _listRandomizer = new ListRandomizer();
 }
Пример #12
0
 internal LeagueTableService(Game game)
     : base(game)
 {
 }
Пример #13
0
 public ILeagueTableService CreateLeagueTableService(Game game)
 {
     Assert(game);
      return new LeagueTableService(game);
 }
Пример #14
0
        private TeamsAndPlayers CreateTeamsAndPlayers(Game game)
        {
            var teamService = ServiceFactory.CreateTeamService(game);
             var playerService = ServiceFactory.CreatePlayerService(game);

             var teamsAndPlayers = new TeamsAndPlayers();

             // Generate all teams for this game.
             const int howManyTeams = Constants.HowManyTeamsPerLeague * Constants.HowManyLeagues;
             var teams = teamService.Create(game, howManyTeams).ToList();

             teamsAndPlayers.Teams = teams;

             var averageRatingsPerLeague = new[] { 5, 9, 13, 17 };
             int averageRatingIndex = 0;
             foreach (var team in teams)
             {
            int teamIndex = teams.IndexOf(team);
            if (teamIndex > 0 && teamIndex % Constants.HowManyLeagues == 0)
            {
               averageRatingIndex++;
            }

            int currentAverageRating = averageRatingsPerLeague[averageRatingIndex];
            var players = playerService.GenerateSquad(team, currentAverageRating);
            teamsAndPlayers.Players.AddRange(players);
             }

             return teamsAndPlayers;
        }
Пример #15
0
        public void DeleteGame(Game game)
        {
            using (Logger.GetLogger(this, game))
             {
            var playerService = ServiceFactory.CreatePlayerService(game);
            var teamService = ServiceFactory.CreateTeamService(game);
            var seasonManager = new SeasonManager(game);

            using (var repository = new RepositoryFactory().CreateUnitOfWorkRepository())
            {
               // Delete all players.
               var players = playerService.GetAll(game);
               repository.RegisterDelete(players);

               // Delete all teams.
               var teams = teamService.GetAll(game);
               repository.RegisterDelete(teams);

               // Delete the seasons, matches etc.
               seasonManager.DeleteSeasons(repository);

               // Delete the game itself.
               repository.RegisterDelete(game);

               repository.Save();
            }
             }
        }
Пример #16
0
 public IMatchService CreateMatchService(Game game)
 {
     Assert(game);
      return new MatchService(game);
 }
Пример #17
0
 private void Assert(Game game)
 {
     if (game == null) throw new ArgumentException("game");
 }
Пример #18
0
 public ITeamService CreateTeamService(Game game)
 {
     Assert(game);
      return new TeamService(game);
 }
Пример #19
0
 public ISeasonService CreateSeasonService(Game game)
 {
     Assert(game);
      return new SeasonService(game);
 }
Пример #20
0
 public IPlayerService CreatePlayerService(Game game)
 {
     Assert(game);
      return new PlayerService(game);
 }
Пример #21
0
 internal MatchService(Game game)
     : base(game)
 {
 }
Пример #22
0
 internal PlayerService(Game game)
     : base(game)
 {
     _playerGenerator = new PlayerGenerator();
 }