Example #1
0
        public async Task <int> GetCurrentGameWeekId()
        {
            var response = await _httpClient.GetAsync("bootstrap-static/");

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            //events = gameweeks
            var resultObjects = AllChildren(JObject.Parse(content))
                                .First(c => c.Type == JTokenType.Array && c.Path.Contains("events"))
                                .Children <JObject>();

            List <GameWeek> gws = new List <GameWeek>();

            foreach (JObject result in resultObjects)
            {
                GameWeek gw = result.ToObject <GameWeek>();
                gws.Add(gw);
            }

            GameWeek currentGameweek = gws.FirstOrDefault(a => a.is_current);

            if (currentGameweek == null)
            {
                return(0);
            }

            return(currentGameweek.id);
        }
        public async Task <ActionResult> Index(string id, int?gameweek)
        {
            // If no ID is provided, look at your own profile
            var user = await _userService.GetById(id) ?? await _userService.GetUserByName(User.Identity.Name);

            // Look at the current gameweek unless a specific one is provided
            GameWeek selectedGameweek = null;

            if (gameweek != null && gameweek.Value > 0)
            {
                selectedGameweek = await _gameWeekService.GetGameWeekByInternalId(gameweek.Value);
            }

            // Fall back to the current gameweek if no specific gameweek is provided.
            if (selectedGameweek == null)
            {
                selectedGameweek = await _gameWeekService.GetCurrentGameweek();
            }

            // Any predictions the user made for this gameweek.
            //var predictions = await _predictionService.GetPredictionsForGameWeek(selectedGameweek, user.UserName, _context);

            ViewBag.PredictionResults = await _predictionService.GetPredictionResultsForGameWeek(selectedGameweek, user.UserName);

            var viewModel = new ProfileViewModel
            {
                User     = user,
                GameWeek = selectedGameweek,
                //Predictions = predictions,
                OwnProfile = user.UserName == User.Identity.Name
            };

            return(View(viewModel));
        }
Example #3
0
        //public async Task<List<Game>> GetGwFixtures(int gameweekId)
        //{
        //    var response = await _httpClient.GetAsync("fixtures/?event=" + gameweekId);

        //    response.EnsureSuccessStatusCode();

        //    var content = await response.Content.ReadAsStringAsync();

        //    List<Game> games = JsonConvert.DeserializeObject<List<Game>>(content);

        //    return games;
        //}

        public async Task <List <GameWeek> > GetAllStartedGameWeeks()
        {
            var response = await _httpClient.GetAsync("bootstrap-static/");

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            //events = gameweeks
            var resultObjects = AllChildren(JObject.Parse(content))
                                .First(c => c.Type == JTokenType.Array && c.Path.Contains("events"))
                                .Children <JObject>();

            List <GameWeek> startedGameWeeks = new List <GameWeek>();

            foreach (JObject result in resultObjects)
            {
                GameWeek gw = result.ToObject <GameWeek>();
                if (gw.is_current || gw.finished)
                {
                    startedGameWeeks.Add(gw);
                }
            }

            return(startedGameWeeks);
        }
Example #4
0
        /// <summary>
        /// Returns the predictions the provided user has made for the specified gameweek. For any fixtures where a
        /// prediction has not been made, a plain Prediction object is returned ready to be filled in.
        /// </summary>
        /// <param name="gameWeek"></param>
        /// <param name="userName"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <List <Prediction> > GetPredictionsForGameWeek(GameWeek gameWeek, string userName)
        {
            // Not logged in.
            if (string.IsNullOrEmpty(userName) || gameWeek == null)
            {
                return(new List <Prediction>());
            }

            var maybePredictions = await context.Predictions.Where(p =>
                                                                   p.User != null && p.User.UserName == userName &&
                                                                   p.Fixture != null && p.Fixture.GameWeek.Id == gameWeek.Id
                                                                   ).ToListAsync();

            var fixtures = await gameWeekService.GetFixturesForGameWeek(gameWeek);

            // If the user has not made a prediction for one (or more) of the fixtures, add in a default prediction ready for
            // them to edit.
            foreach (var fixture in fixtures)
            {
                var fixturedPredicted = maybePredictions.FirstOrDefault(p => p.Fixture.Id == fixture.Id);

                if (fixturedPredicted == null)
                {
                    maybePredictions.Add(new Prediction {
                        Fixture   = fixture,
                        User      = await userService.GetUserByName(userName),
                        HomeScore = -1,
                        AwayScore = -1,
                        Id        = Guid.NewGuid()
                    });
                }
            }

            return(maybePredictions.OrderBy(p => p.Fixture.KickoffDate).ToList());
        }
Example #5
0
        public CupWeekSummary()
        {
            GameWeek = new GameWeek();

            Scores = new List <CupScore>();

            Groups = new List <Table>();
        }
Example #6
0
 public CupWeekSummary(GameWeek gameWeek, string cup, int cupId, List <CupScore> scores, List <Table> groups = null)
 {
     GameWeek = gameWeek;
     Cup      = cup;
     CupId    = cupId;
     Scores   = scores;
     Groups   = groups;
 }
Example #7
0
        public GameWeekSummary()
        {
            GameWeek = new GameWeek();

            Scores = new List <Score>();

            Table = new Table();
        }
Example #8
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            GameWeek gameWeek = await db.GameWeeks.FindAsync(id);

            db.GameWeeks.Remove(gameWeek);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task Test_Edit_Edits_GameWeek()
        {
            var gameWeek = new GameWeek {
                Number = 1
            };

            await controller.Edit(gameWeek);

            context.MockContext.Verify(x => x.SetModified(It.Is <object>(t => t == gameWeek)));
        }
        public async Task Test_Create_Creates_GameWeek()
        {
            var gameWeek = new GameWeek {
                Number = 3
            };

            await controller.Create(gameWeek);

            context.MockGameWeeks.Verify(x => x.Add(It.Is <GameWeek>(t => t == gameWeek)));
        }
Example #11
0
        public async Task <ActionResult> Edit([Bind(Include = "GameWeekId,Number,Start,Complete")] GameWeek gameWeek)
        {
            if (ModelState.IsValid)
            {
                db.SetModified(gameWeek);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(gameWeek));
        }
Example #12
0
        private static DateTime GetPreviousGameweekCloseDateTime(Competition comp, GameWeek currentGameWeek)
        {
            var previousGameWeek = comp.GameWeeks.FirstOrDefault(gw => gw.Number == currentGameWeek.Number - 1);

            if (previousGameWeek == null)
            {
                return(DateTime.UtcNow);
            }

            return(previousGameWeek.Fixtures.Max(f => f.KickOffDateTime).AddMinutes(15));
        }
Example #13
0
        /// <summary>
        /// Returns whether or not the provided user has made a full set of predictions for a specified gameweek.
        /// </summary>
        /// <param name="gameWeek"></param>
        /// <param name="userName"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <bool> HasSetPredictions(GameWeek gameWeek, string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(false);
            }

            var predictions = await this.GetPredictionsForGameWeek(gameWeek, userName);

            var missingPredictions = predictions.Any(p => p.HomeScore == -1 || p.AwayScore == -1);

            return(!missingPredictions);
        }
Example #14
0
        public async Task <ActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            GameWeek gameWeek = await db.GameWeeks.FindAsync(id);

            if (gameWeek == null)
            {
                return(HttpNotFound());
            }
            return(View(gameWeek));
        }
Example #15
0
        /// <summary>
        /// Returns a list of Prediction Results for a set of gameweek fixtures.
        /// </summary>
        /// <param name="gameWeek"></param>
        /// <param name="userName"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <List <PredictionResult> > GetPredictionResultsForGameWeek(GameWeek gameWeek, string userName)
        {
            var predictionResults = new List <PredictionResult>();

            // No user, no results!
            if (string.IsNullOrEmpty(userName))
            {
                return(predictionResults);
            }

            var predictions = await GetPredictionsForGameWeek(gameWeek, userName);

            predictionResults.AddRange(predictions.Select(p => new PredictionResult {
                Fixture = p.Fixture, Prediction = p
            }));

            return(predictionResults);
        }
Example #16
0
        private void AddRequiredFixtures(GameWeek gameWeek)
        {
            var fixtures = _matchApi.Get().Where(m => m.Matchday == gameWeek.Number);

            foreach (var fix in fixtures)
            {
                if (gameWeek.Fixtures == null ||
                    !gameWeek.Fixtures.Any(gw => gw?.HomeTeam?.Name == fix.HomeTeam.Name && gw?.AwayTeam?.Name == fix.AwayTeam.Name))
                {
                    gameWeek.Fixtures.Add(new Fixture
                    {
                        HomeTeam        = gameWeek.Competition.Teams.FirstOrDefault(t => t.Name == fix.HomeTeam.Name),
                        AwayTeam        = gameWeek.Competition.Teams.FirstOrDefault(t => t.Name == fix.AwayTeam.Name),
                        KickOffDateTime = fix.UtcDate
                    });
                }

                var gameweekFixture = gameWeek.Fixtures.Single(gw => gw.HomeTeam.Name == fix.HomeTeam.Name && gw.AwayTeam.Name == fix.AwayTeam.Name);

                if (fix.IsFixtureInFinished)
                {
                    if (!gameweekFixture.Results.Any())
                    {
                        if (fix.Score?.FullTime != null && fix.Score?.FullTime?.AwayTeam != null && fix.Score?.FullTime?.HomeTeam != null)
                        {
                            var result = new Result
                            {
                                HomeScore = (int)(fix.Score.FullTime.HomeTeam),
                                AwayScore = (int)(fix.Score.FullTime.AwayTeam)
                            };

                            gameweekFixture.Results.Add(result);
                        }
                        else
                        {
                            //TODO: completed fixture but result or scores null!?
                        }

                        //_db.SaveChanges();
                    }
                }
            }
        }
Example #17
0
        public Ladder(List <BsonDocument> GameWeeksDoc)
        {
            NumRowsDivFive = 0;
            var lGameWeeks = new List <GameWeek>();

            foreach (var document in GameWeeksDoc.OrderByDescending(x => x))
            {
                GameWeek gameweek = BsonSerializer.Deserialize <GameWeek>(document);
                foreach (LadderPlayer lp in gameweek.Ladder)
                {
                    Game PlayerGame = gameweek.Games.Where(x => x.GamePlayers.Exists(y => y.playername == lp.PlayerName)).FirstOrDefault();
                    lp.GameNumber = PlayerGame == null ? (int?)null : PlayerGame.GameNumber;
                }
                lGameWeeks.Add(gameweek);
                NumRowsDivFive = Math.Max(NumRowsDivFive, gameweek.Ladder.Where(x => x.Playing).Count() / 5 + 1);
                NumRowsDivFive = Math.Max(NumRowsDivFive, gameweek.Games.Count());
            }

            GameWeeks = lGameWeeks.OrderByDescending(x => x.ProcessingOrder).ToList();
        }
Example #18
0
        public async Task <IActionResult> Index()
        {
            var viewModel = new GameWeekViewModel();

            var client = new FPLHttpClient();

            var response = await client.GetAsync("bootstrap-static/");

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            //events = gameweeks
            var resultObjects = AllChildren(JObject.Parse(content))
                                .First(c => c.Type == JTokenType.Array && c.Path.Contains("events"))
                                .Children <JObject>();

            List <GameWeek> gws = new List <GameWeek>();

            foreach (JObject result in resultObjects)
            {
                GameWeek gw = result.ToObject <GameWeek>();

                gws.Add(gw);

                //foreach (JProperty property in result.Properties())
                //{

                //}
            }

            GameWeek currentGameweek = gws.FirstOrDefault(a => a.is_current);

            viewModel.Gameweeks       = gws;
            viewModel.CurrentGameweek = currentGameweek;

            return(View(viewModel));
        }
Example #19
0
        public ActionResult GameWeeks(DateTime startDate, int total)
        {
            var gameWeeks = db.GameWeeks;

            foreach (var gameWeek in gameWeeks)
            {
                db.GameWeeks.Remove(gameWeek);
            }
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT('DreamLeague.GameWeeks', RESEED, 0)");

            DateTime currentStart = startDate;

            for (int i = 0; i < total; i++)
            {
                GameWeek gameWeek = new GameWeek(i + 1, currentStart);
                db.GameWeeks.Add(gameWeek);

                currentStart = currentStart.AddDays(7);
            }

            db.SaveChanges();

            return(Content("Ok"));
        }
Example #20
0
 public GameWeekSummary(GameWeek gameWeek, List <Score> scores, Table table)
 {
     GameWeek = gameWeek;
     Scores   = scores;
     Table    = table;
 }
Example #21
0
        public async Task <IActionResult> Index()
        {
            var viewModel = new FixturesViewModel();

            var response = await _httpClient.GetAsync("fixtures/");

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            List <Game> games = JsonConvert.DeserializeObject <List <Game> >(content);

            List <GameWeek> gameWeeks = await GetAllGameWeeks();

            GameWeek currentGameWeek = gameWeeks.FirstOrDefault(a => a.is_current);

            if (currentGameWeek.finished)
            {
                currentGameWeek = gameWeeks.FirstOrDefault(a => a.is_next);
            }

            List <Game> currentGameWeekGames = new List <Game>();
            List <Game> liveGames            = new List <Game>();

            foreach (Game g in games)
            {
                if (g.Event == currentGameWeek.id)
                {
                    currentGameWeekGames.Add(g);
                }

                if (g.started ?? false)
                {
                    if (!g.finished || !g.finished_provisional)
                    {
                        if (!g.finished_provisional)
                        {
                            liveGames.Add(g);
                        }
                    }
                }

                if (g.finished || g.finished_provisional)
                {
                    if (g.team_h_score > g.team_a_score)
                    {
                        g.did_team_h_win = true;
                    }
                    else if (g.team_a_score > g.team_h_score)
                    {
                        g.did_team_a_win = true;
                    }
                }
            }

            List <GWPlayer> gwPlayerStatsLive    = new List <GWPlayer>();
            List <GWPlayer> gwPlayerStatsCurrent = new List <GWPlayer>();

            (gwPlayerStatsLive, liveGames) = await PopulateFixtureListByGameWeekId(liveGames, currentGameWeek.id);

            (gwPlayerStatsCurrent, currentGameWeekGames) = await PopulateFixtureListByGameWeekId(currentGameWeekGames, currentGameWeek.id);

            viewModel.LiveGames         = liveGames;
            viewModel.CurrentGameweek   = currentGameWeek;
            viewModel.Fixtures          = currentGameWeekGames;
            viewModel.CurrentGameweekId = currentGameWeek.id;

            return(View(viewModel));
        }
Example #22
0
        public AllPlayers(BsonDocument GameWeekDoc)
        {
            GameWeek gameweek = BsonSerializer.Deserialize <GameWeek>(GameWeekDoc);

            LadderPlayers = gameweek.Ladder;
        }
 /// <summary>
 /// Gets all the fixtures for a provided gameweek
 /// </summary>
 /// <param name="gameWeek"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public async Task <List <Fixture> > GetFixturesForGameWeek(GameWeek gameWeek)
 {
     return(await context.Fixtures.Where(f => f.GameWeek.Id == gameWeek.Id).OrderBy(f => f.KickoffDate).ThenBy(f => f.HomeTeam.Name).ToListAsync());
 }
Example #24
0
 private static DateTime GetCurrentGameweekCloseDateTime(GameWeek currentGameWeek)
 {
     return(currentGameWeek.Fixtures.Min(f => f.KickOffDateTime).AddMinutes(-15));
 }
Example #25
0
 private static bool IsGameweekOpen(GameWeek gameWeek)
 {
     return(DateTime.UtcNow > gameWeek.PickOpenDateTime && DateTime.UtcNow < gameWeek.PickCloseDateTime);
 }