public async Task UpdateMatch() { using (var wc = new WebClient()) { string matchJsonString = await wc.DownloadStringTaskAsync($"https://api.overwatchleague.com/matches/{Id}"); dynamic matchJson = JsonConvert.DeserializeObject(matchJsonString); if (HomeTeam == null && matchJson.competitors[0] != null) { int teamId = matchJson.competitors[0].id; HomeTeam = (from c in OverwatchLeague.Database.Teams where c.Id == teamId select c).First(); } if (AwayTeam == null && matchJson.competitors[1] != null) { int teamId = matchJson.competitors[1].id; AwayTeam = (from c in OverwatchLeague.Database.Teams where c.Id == teamId select c).First(); } if (matchJson["scores"] != null) { HomeScore = matchJson.scores[0].value; AwayScore = matchJson.scores[1].value; } status = matchJson.status; StartTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)matchJson.startDate); EndTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)matchJson.endDate); if (matchJson["actualStartTime"] != null) { ActualStartTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)matchJson.actualStartTime); } if (matchJson["actualEndTime"] != null) { ActualEndTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)matchJson.actualEndTime); } games.Clear(); foreach (var game in matchJson.games) { int gameId = game.id; MatchGame g = games.Find(x => x.Id == gameId); if (g == null) { int gameNumber = game.number; int gameHomePoints = 0; int gameAwayPoints = 0; if (game["points"] != null) { gameHomePoints = game.points[0]; gameAwayPoints = game.points[1]; } ulong mapGuid = 0; if (game["attributes"]["mapGuid"] != null) { mapGuid = Convert.ToUInt64(game.attributes.mapGuid.Value, 16); } string gameStatus = game.status; Map map = (from c in OverwatchLeague.Database.Maps where c.Guid == mapGuid select c).First(); g = new MatchGame(gameId, gameNumber, gameHomePoints, gameAwayPoints, this, map, gameStatus); if (map != null) { map.AddGame(g); } AddGame(g); } else { if (game["points"] != null) { int gameHomePoints = game.points[0]; int gameAwayPoints = game.points[1]; g.SetHomeScore(gameHomePoints); g.SetAwayScore(gameAwayPoints); } string gameStatus = game.status; g.SetStatus(gameStatus); if (g.Map == null) { ulong mapGuid = 0; if (game["attributes"]["mapGuid"] != null) { mapGuid = Convert.ToUInt64(game.attributes.mapGuid.Value, 16); } Map map = (from c in OverwatchLeague.Database.Maps where c.Guid == mapGuid select c).First(); g.SetMap(map); if (map != null) { map.AddGame(g); } } } } } if (Status == MatchStatus.InProgress) { updateTimer.Interval = 1000 * 15; updateTimer.Start(); } }
public void AddGame(MatchGame game) { games.Add(game); }
private async Task LoadMatches() { //TODO: This may be irrelevent if the new endpoint delivers all the appropriate information. botMethods.Log(this, new LogEventArgs { Type = LogType.Log, Message = $"OverwatchLeague is requesting matches..." }); using var wc = new WebClient(); string matchesJsonString = await wc.DownloadStringTaskAsync("https://api.overwatchleague.com/matches"); dynamic matchesJson = JsonConvert.DeserializeObject(matchesJsonString); foreach (var match in matchesJson.content) { int id = match.id; Team homeTeam = teams.Find(t => t.Id == match.competitors[0].id.Value); Team awayTeam = teams.Find(t => t.Id == match.competitors[1].id.Value); int homeScore = match.scores[0].value; int awayScore = match.scores[1].value; string status = match.status; DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)match.startDate); DateTime endTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)match.endDate); DateTime?actualStartTime = null; if (match["actualStartTime"] != null) { actualStartTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)match.actualStartTime); } DateTime?actualEndTime = null; if (match["actualEndTime"] != null) { actualEndTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds((double)match.actualEndTime); } Match m = new Match(botMethods, id, homeTeam, awayTeam, homeScore, awayScore, status, startTime, endTime, actualStartTime, actualEndTime); matches.Add(m); if (homeTeam != null) { homeTeam.AddMatch(m); } if (awayTeam != null) { awayTeam.AddMatch(m); } // Now the individual games of the matches. foreach (var game in match.games) { int gameId = game.id; int gameNumber = game.number; int gameHomePoints = 0; int gameAwayPoints = 0; if (game["points"] != null) { gameHomePoints = game.points[0]; gameAwayPoints = game.points[1]; } ulong mapGuid = 0; if (game["attributes"]["mapGuid"] != null) { mapGuid = Convert.ToUInt64(game.attributes.mapGuid.Value, 16); } Map map = maps.Find(x => x.Guid == mapGuid); string gameStatus = game.status; MatchGame g = new MatchGame(gameId, gameNumber, gameHomePoints, gameAwayPoints, m, map, gameStatus); m.AddGame(g); if (map != null) { map.AddGame(g); } } } }