Exemple #1
0
        public void ProcessMatch(DeserializedModel deserialized, string username)
        {
            using (var ctx = new Context())
            {
                Match currentMatch = ctx.Matches.Include("League").FirstOrDefault(m => m.ID == deserialized.ID);
                try
                {
                    currentMatch.Locked = true;
                    currentMatch.Date   = Convert.ToDateTime(deserialized.Date);
                    ctx.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    Console.WriteLine(ex);
                }

                if (deserialized.Comment != null)
                {
                    try
                    {
                        Match_comment commentDB = new Match_comment();
                        commentDB.Comment = deserialized.Comment;
                        commentDB.Active  = true;
                        commentDB.Date    = DateTime.UtcNow;
                        commentDB.Match   = currentMatch;
                        commentDB.User    = ctx.Users.FirstOrDefault(u => u.Username == username);
                        ctx.Match_comments.Add(commentDB);
                        ctx.SaveChanges();
                    }
                    catch (DbEntityValidationException ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                foreach (var one in deserialized.Events)
                {
                    try
                    {
                        Event matchEvent = new Event();
                        matchEvent.Length    = one.LE;
                        matchEvent.Timestamp = DateTime.UtcNow;
                        matchEvent.Time      = one.DA;
                        matchEvent.Name      = one.Name;
                        if (one.PO1 != "")
                        {
                            matchEvent.Points1 = Convert.ToInt32(one.PO1);
                        }
                        if (one.PO2 != "")
                        {
                            matchEvent.Points2 = Convert.ToInt32(one.PO2);
                        }
                        matchEvent.Match = currentMatch;
                        var dal = new Player_services();
                        if (one.Player1)
                        {
                            matchEvent.Player1 = ctx.Players.FirstOrDefault(m => m.Name == one.P1);
                        }
                        if (one.Player2)
                        {
                            matchEvent.Player2 = ctx.Players.FirstOrDefault(m => m.Name == one.P2);
                        }
                        ctx.Events.Add(matchEvent);
                        ctx.SaveChanges();
                    }
                    catch (DbEntityValidationException ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
                if (!currentMatch.League.Type_of_competition.LeagueCup)
                {
                    CupNextMatch(currentMatch.ID);
                }
            }
        }
Exemple #2
0
        public void New_competition(SingleCompetitionModel model, string username)
        {
            using (var ctx = new Context())
            {
                List <Contestant> contestants = new List <Contestant>();
                foreach (var one in model.Competitors)
                {
                    var  dal  = new Team_services();
                    var  dal2 = new Player_services();
                    Team x    = dal.Check_existing(one);
                    if (x == null)
                    {
                        Player y = dal2.Check_existing(one);
                        contestants.Add(ctx.Contestants.FirstOrDefault(m => m.ID == y.ID));
                    }
                    else
                    {
                        contestants.Add(ctx.Contestants.FirstOrDefault(m => m.ID == x.ID));
                    }
                }

                Competition competition = new Competition();
                competition.Name                  = model.Competition.Name;
                competition.Country               = model.Competition.Country;
                competition.Emblem                = model.Competition.Emblem;
                competition.Sport                 = ctx.Sports.FirstOrDefault(u => u.ID == model.Competition.Sport.ID);
                competition.User                  = ctx.Users.FirstOrDefault(u => u.Username == username);
                competition.Type_of_competition   = ctx.Type_of_competitions.FirstOrDefault(u => u.ID == model.Competition.Type_of_competition.ID);
                competition.Number_of_competitors = model.Competitors.Count;
                competition.Season                = DateTime.Today.Year;
                ctx.Competitions.Add(competition);
                ctx.SaveChanges();

                if (model.Comment != null)
                {
                    Competition_comment commentDB = new Competition_comment();
                    commentDB.Comment     = model.Comment;
                    commentDB.Active      = true;
                    commentDB.Date        = DateTime.UtcNow;
                    commentDB.Competition = competition;
                    commentDB.User        = ctx.Users.FirstOrDefault(u => u.Username == username);
                    ctx.Competition_comments.Add(commentDB);
                    ctx.SaveChanges();
                }

                foreach (Contestant one in contestants)
                {
                    Competition_contestant contestant = new Competition_contestant();
                    contestant.Competition = competition;
                    contestant.Contestant  = one;
                    ctx.Competition_contestants.Add(contestant);
                    ctx.SaveChanges();
                }
                if (competition.Type_of_competition.LeagueCup)
                {
                    GenerateMatchesLeague(competition, contestants);
                }
                else
                {
                    GenerateMatchesCup(competition, contestants);
                }
            }
        }