Exemple #1
0
        public ActionResult _CupResults(int gameWeekId)
        {
            List <CupWeekSummary> cupWeekSummaries = new List <CupWeekSummary>();

            var cups = db.Cups.AsNoTracking();

            foreach (var cup in cups)
            {
                var fixtureGameWeekId = db.Fixtures.AsNoTracking().Where(x => x.GameWeekId == gameWeekId && x.CupId == cup.CupId).Select(x => x.GameWeekId).FirstOrDefault();

                if (fixtureGameWeekId != 0)
                {
                    if (db.GameWeeks.Find(fixtureGameWeekId).Complete)
                    {
                        CupWeekSummary cupWeekSummary = cupWeekSerializer.DeSerialize(gameWeekId, string.Format("CupWeek_{0}", cup.CupId));

                        if (cupWeekSummary != null)
                        {
                            cupWeekSummaries.Add(cupWeekSummary);
                        }
                    }
                }
            }

            return(PartialView(cupWeekSummaries));
        }
        public void Create(int gameWeekId)
        {
            var gameWeek = db.GameWeeks.Find(gameWeekId);
            var fixtures = db.Fixtures.AsNoTracking().Include(x => x.Cup).Where(x => x.GameWeekId == gameWeekId);

            if (fixtures.Count() == 0)
            {
                return;
            }

            var cups = fixtures.Select(x => x.Cup).Distinct();

            var scores = GetScores(gameWeekId);

            List <CupScore> cupScores = new List <CupScore>();

            foreach (var fixture in fixtures)
            {
                var homeScore = scores.Where(x => x.ManagerId == fixture.HomeManagerId).FirstOrDefault();
                var awayScore = scores.Where(x => x.ManagerId == fixture.AwayManagerId).FirstOrDefault();

                cupScores.Add(new CupScore(fixture.CupId, fixture.FixtureId, fixture.Round, homeScore, awayScore));
            }

            foreach (var cup in cups)
            {
                List <Table> groups = new List <Table>();

                if (cup.HasGroupStage && db.Fixtures.AsNoTracking().Where(x => x.Round == 1 && x.CupId == cup.CupId && x.GameWeek.Number <= gameWeek.Number).Count() > 0)
                {
                    foreach (var group in cup.Groups)
                    {
                        groups.Add(GetTable(gameWeekId, group.GroupId));
                    }
                }

                CupWeekSummary cupWeekSummary = new CupWeekSummary(gameWeek, cup.Name, cup.CupId, cupScores.Where(x => x.CupId == cup.CupId).ToList(), groups);

                gameWeekSerializer.Serialize(cupWeekSummary, gameWeekId, string.Format("CupWeek_{0}", cup.CupId));
            }
        }
        public CupViewModel GetData(int cupId)
        {
            Cup cup = db.Cups.AsNoTracking().Include(x => x.Groups.Select(g => g.Managers)).Include(x => x.Fixtures).Where(x => x.CupId == cupId).FirstOrDefault();

            var gameWeekId = cup.Fixtures.Where(x => x.GameWeek.Complete).OrderByDescending(x => x.GameWeek.Number).Select(x => x.GameWeekId).FirstOrDefault();

            CupWeekSummary cupWeekSummary = new CupWeekSummary();

            if (gameWeekId != 0)
            {
                cupWeekSummary = cupWeekSerializer.DeSerialize(gameWeekId, string.Format("CupWeek_{0}", cup.CupId));
            }

            List <GroupTable>    groupTables = new List <GroupTable>();
            List <FixtureResult> fixtures    = new List <FixtureResult>();

            foreach (var group in cup.Groups.OrderBy(x => x.Name))
            {
                Table table = cupWeekSummary.Groups.Where(x => x.GroupId == group.GroupId).FirstOrDefault();

                if (table == null)
                {
                    table = new Table(group.Name, group.GroupId, group.Managers);
                }

                groupTables.Add(new GroupTable(group, table));
            }

            var completedGameWeeks = db.GameWeeks.AsNoTracking().Where(x => x.Complete);
            var fixtureGameWeeks   = db.Fixtures.AsNoTracking().Where(x => x.CupId == cup.CupId && completedGameWeeks.Any(p => p.GameWeekId == x.GameWeekId)).Select(x => x.GameWeekId).Distinct();

            List <CupScore> cupScores = new List <CupScore>();

            foreach (var fixtureGameWeek in fixtureGameWeeks)
            {
                cupWeekSummary = cupWeekSerializer.DeSerialize(fixtureGameWeek, string.Format("CupWeek_{0}", cup.CupId));

                if (cupWeekSummary != null)
                {
                    foreach (var cupScore in cupWeekSummary.Scores)
                    {
                        cupScores.Add(cupScore);
                    }
                }
            }

            foreach (var fixture in cup.Fixtures.OrderByDescending(x => x.GameWeek.Number))
            {
                CupScore cupScore = cupScores.Where(x => x.FixtureId == fixture.FixtureId).FirstOrDefault();

                if (cupScore != null)
                {
                    fixtures.Add(new FixtureResult(fixture, cupScore.HomeScore.Margin, cupScore.AwayScore.Margin));
                }
                else
                {
                    fixtures.Add(new FixtureResult(fixture));
                }
            }

            return(new CupViewModel(cup, groupTables, fixtures));
        }