protected ServiceBase(Game game) { Game = game; NumberRandomizer = new NumberRandomizer(); ListRandomizer = new ListRandomizer(); ServiceFactory = new ServiceFactory(); }
public Team GetMyTeam(Game game) { using (var teamRepository = new RepositoryFactory().CreateTeamRepository()) { return teamRepository.GetTeam(game.CurrentTeamId); } }
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; }
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; }
/// <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; } }
public IEnumerable<Player> GetAll(Game game) { using (var playerRepository = new RepositoryFactory().CreatePlayerRepository()) { var players = playerRepository.GetPlayersByGame(game); return players; } }
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; }
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; } } }
public SeasonManager(Game game) { _game = game; _leagueManager = new LeagueManager(); _repositoryFactory = new RepositoryFactory(); }
internal SeasonService(Game game) : base(game) { }
internal TeamService(Game game) : base(game) { _teamGenerator = new TeamGenerator(); _listRandomizer = new ListRandomizer(); }
internal LeagueTableService(Game game) : base(game) { }
public ILeagueTableService CreateLeagueTableService(Game game) { Assert(game); return new LeagueTableService(game); }
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; }
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(); } } }
public IMatchService CreateMatchService(Game game) { Assert(game); return new MatchService(game); }
private void Assert(Game game) { if (game == null) throw new ArgumentException("game"); }
public ITeamService CreateTeamService(Game game) { Assert(game); return new TeamService(game); }
public ISeasonService CreateSeasonService(Game game) { Assert(game); return new SeasonService(game); }
public IPlayerService CreatePlayerService(Game game) { Assert(game); return new PlayerService(game); }
internal MatchService(Game game) : base(game) { }
internal PlayerService(Game game) : base(game) { _playerGenerator = new PlayerGenerator(); }