示例#1
0
 public void Update(Model.Match M, string current)
 {
     if (M.Guess != current)
     {
         M.Guess = current;
         using (var context = new CardContext())
         {
             context.Entry(M).State = System.Data.Entity.EntityState.Modified;
             context.SaveChanges();
         }
     }
 }
        public void Start(IEnumerable <Competition> competitions)
        {
            if (!_awsHelper.User.IsAdmin() || _timer != null)
            {
                return;
            }

            foreach (var competition in competitions)
            {
                competition.Matches = new AwsGraphQlList <Model.Match>
                {
                    Items = new List <Model.Match>()
                };
            }

            _timer = new Timer(async state =>
            {
                foreach (var competition in competitions)
                {
                    var message     = await _httpClient.GetAsync("https://api.fifa.com/api/v1/live/football/recent/" + competition.Id);
                    var content     = await message.Content.ReadAsStringAsync();
                    var response    = JsonSerializer.Deserialize <FifaResponse <Admin.Model.FIFA.Match> >(content);
                    var fifaMatches = response.Results.Where(m => m.MatchStatus == 3 || m.MatchStatus == 0);
                    var matches     = competition.Matches.Items;
                    foreach (var fifaMatch in fifaMatches)
                    {
                        var match      = matches.FirstOrDefault(m => m.Id == fifaMatch.IdMatch);
                        var isFinished = fifaMatch.MatchStatus == 0;
                        if (match == null)
                        {
                            match = new Model.Match
                            {
                                Id     = fifaMatch.IdMatch,
                                Scores = new List <Score>
                                {
                                    new Score {
                                        IsHome = true, Value = fifaMatch.HomeTeam.Score.Value
                                    },
                                    new Score {
                                        IsHome = false, Value = fifaMatch.AwayTeam.Score.Value
                                    }
                                }
                            };
                            ((List <Model.Match>)matches).Add(match);
                        }
                        else
                        {
                            var homeScore = match.Scores.First(s => s.IsHome);
                            var awayScore = match.Scores.First(s => !s.IsHome);

                            if (homeScore.Value == fifaMatch.HomeTeam.Score &&
                                awayScore.Value == fifaMatch.AwayTeam.Score &&
                                match.IsFinished == isFinished)
                            {
                                continue;
                            }
                            homeScore.Value = fifaMatch.HomeTeam.Score.Value;
                            awayScore.Value = fifaMatch.AwayTeam.Score.Value;
                        }
                        match.IsFinished = isFinished;
                        Console.WriteLine($"Update score {JsonSerializer.Serialize(match)}");
                        await _awsJsInterop.GraphQlAsync <MatchesResponse>(Admin.Model.Mutations.UPDATE_MATCH,
                                                                           new
                        {
                            input = new
                            {
                                id     = match.Id,
                                scores = match.Scores,
                                isFinished
                            }
                        });
                    }
                }
            }, null, 0, 5000);
        }