Exemple #1
0
        private void AddPlayNextMatchDayForm(GameInfo gameInfo, Resource halDocument, DateTime matchDate)
        {
            var seasonService = ServiceFactory.CreateSeasonService(gameInfo);
            var matchService  = ServiceFactory.CreateMatchService(gameInfo);

            var currentSeason = seasonService.GetCurrentSeason();
            var nextMatchDate = matchService.GetNextMatchDate(currentSeason.Id);

            if (nextMatchDate.HasValue && matchDate == nextMatchDate)
            {
                // Add a form to play the match day.
                var matchDayResourceFactory = new MatchDayResourceFactory(UriHelper, gameInfo.Id, nextMatchDate.Value);
                var form = matchDayResourceFactory.GetForm();
                halDocument.AddForm(form);

                // Also add a link to the match day.
                var link = matchDayResourceFactory.GetLink();
                halDocument.AddLink("rel:next-match-day", link);
            }
        }
Exemple #2
0
        public Response GetItem(string gameId)
        {
            RequestHelper.ValidateId(gameId);

            var gameInfo = GetGameInfo(gameId);

            if (gameInfo.CurrentTeam == null)
            {
                throw ResponseHelper.Get501NotImplemented("You must pick a team now, but this is not implemented yet...");
            }

            var halDocument = CreateHalDocument(UriHelper.GetGameUri(gameId), gameInfo);

            halDocument.AddLink("game-links", new Link(UriHelper.GetGameLinksUri(gameId)));

            // Add GameDateTime navigation.
            var gameDateTimeService = ServiceFactory.CreateGameDateTimeService(gameInfo);
            var now = gameDateTimeService.GetNow();
            var currentGameDateTimeResource = new GameDateTimeMapper(UriHelper).Map(now);

            halDocument.AddResource("rel:game-datetime-navigation", currentGameDateTimeResource);

            // Add my team.
            var teamResource = new TeamMapper(UriHelper).Map(gameInfo.CurrentTeam, TeamMapper.TeamName, TeamMapper.Rating, TeamMapper.RatingGoalkeeper, TeamMapper.RatingDefence, TeamMapper.RatingMidfield, TeamMapper.RatingAttack, TeamMapper.RatingPercentage);

            halDocument.AddResource("rel:my-team", teamResource);

            // Add season resource.
            var seasonService  = ServiceFactory.CreateSeasonService(gameInfo);
            var currentSeason  = seasonService.GetCurrentSeason();
            var seasonResource = new SeasonMapper(UriHelper).Map(currentSeason, SeasonMapper.SeasonShortName, SeasonMapper.SeasonLongName);

            bool endOfSeason  = seasonService.DetermineSeasonEnded(currentSeason.Id);
            bool endSeasonNow = currentSeason.EndDateTime == now.DateTime;

            if (endOfSeason && endSeasonNow)
            {
                var form = new Form("end-season")
                {
                    Action = UriHelper.GetSeasonUri(gameId, currentSeason.Id),
                    Method = "post",
                    Title  = "END SEASON!"
                };
                seasonResource.AddForm(form);
            }

            halDocument.AddResource("rel:current-season", seasonResource);

            // Add season team statistics.
            var statisticsService            = ServiceFactory.CreateStatisticsService(gameInfo);
            var seasonTeamStatistics         = statisticsService.GetSeasonTeamStatistics(currentSeason.Id, gameInfo.CurrentTeamId);
            var seasonTeamStatisticsResource = new SeasonTeamStatisticsMapper(UriHelper).Map(seasonTeamStatistics);

            halDocument.AddResource("rel:season-team-statistics", seasonTeamStatisticsResource);

            // Add next match day.
            var matchService  = ServiceFactory.CreateMatchService(gameInfo);
            var nextMatchDate = matchService.GetNextMatchDate(currentSeason.Id);

            if (nextMatchDate.HasValue)
            {
                var matchDayResourceFactory = new MatchDayResourceFactory(UriHelper, gameId, nextMatchDate.Value);

                var matchDayResource = matchDayResourceFactory.Create();

                // Add a resource for the match of the current team.
                var matchForCurrentTeam = matchService.GetByMatchDayAndTeam(nextMatchDate.Value, gameInfo.CurrentTeamId);
                if (matchForCurrentTeam != null)
                {
                    var matchResource = new MatchMapper(UriHelper).Map(
                        matchForCurrentTeam,
                        MatchMapper.CompetitionName,
                        MatchMapper.CompetitionType,
                        MatchMapper.Date,
                        MatchMapper.Round);

                    var teamMapper = new TeamMapper(UriHelper);

                    var homeTeamResource = teamMapper.Map(matchForCurrentTeam.HomeTeam, TeamMapper.TeamName, TeamMapper.LeagueName, TeamMapper.CurrentLeaguePosition);
                    matchResource.AddResource("home-team", homeTeamResource);

                    var awayTeamResource = teamMapper.Map(matchForCurrentTeam.AwayTeam, TeamMapper.TeamName, TeamMapper.LeagueName, TeamMapper.CurrentLeaguePosition);
                    matchResource.AddResource("away-team", awayTeamResource);

                    matchResource.AddResource("your-opponent", gameInfo.CurrentTeam.Equals(matchForCurrentTeam.HomeTeam) ? awayTeamResource : homeTeamResource);

                    matchDayResource.AddResource("next-match", matchResource);
                }

                // Only add the play next match day form when the matches are right now.
                if (nextMatchDate.Value == now.DateTime)
                {
                    var playNextMatchDayForm = matchDayResourceFactory.GetForm();
                    matchDayResource.AddForm(playNextMatchDayForm);
                }

                halDocument.AddResource("rel:next-match-day", matchDayResource);
            }

            // Add league table.
            var leagueTableService  = ServiceFactory.CreateLeagueTableService(gameInfo);
            var leagueTable         = leagueTableService.GetBySeasonAndCompetition(currentSeason.Id, gameInfo.CurrentTeam.CurrentLeagueCompetitionId);
            var leagueTableResource = new LeagueTableMapper(UriHelper).Map(leagueTable);

            leagueTableResource.AddLink("leaguetables", new Link(UriHelper.GetSeasonLeagueTablesUri(gameId, currentSeason.Id))
            {
                Name = "all", Title = "All league tables"
            });

            halDocument.AddResource("rel:leaguetable", leagueTableResource);

            var response = GetResponse(halDocument);

            return(response);
        }