private async Task <CalcData> GetCalcDataForSegmentAsync(string platform, string username, int season, Playlist playlist, TRNMMRObject obj) { CalcData retVal = new CalcData { Playlist = playlist, Season = season }; DateTime cutOff = DateTime.Now; DateTime seasonStartDate = new DateTime(2020, 12, 09); switch (season) { case 16: { cutOff = cutOffDates[(int)Seasons.S2]; break; } case 17: { cutOff = cutOffDates[(int)Seasons.S3]; seasonStartDate = cutOffDates[(int)Seasons.S2].AddDays(1); break; } case 18: { cutOff = cutOffDates[(int)Seasons.S4]; seasonStartDate = cutOffDates[(int)Seasons.S3].AddDays(1); break; } } try { List <Datum> Datam = new List <Datum>(); var segment = await GetSeasonSegment(season, platform, username); if (segment == null) { retVal.GamesPlayed = 0; } else { Datam.AddRange(segment.data); Datam.RemoveAll(x => _acceptablePlaylists.Contains(x.attributes.playlistId) == false); retVal.GamesPlayed = Datam.Count > 0 ? Datam.Sum(x => x.stats.matchesPlayed.value) : 0; } if (playlist == Playlist.ONES) { if (obj.data.Duel != null) { List <Duel> data = new List <Duel>(obj.data.Duel); data = data.Where(x => x.collectDate <cutOff& x.collectDate> seasonStartDate).ToList(); retVal.Ratings = data.Select(x => x.rating).ToList(); HandleOnesRatings(ref retVal); } } else if (playlist == Playlist.TWOS) { if (obj.data.Duos != null) { List <Duo> data = new List <Duo>(obj.data.Duos); data = data.Where(x => x.collectDate <cutOff && x.collectDate> seasonStartDate).ToList(); retVal.Ratings = data.Select(x => x.rating).ToList(); } } else if (playlist == Playlist.THREES) { if (obj.data.Standard != null) { List <Standard> data = new List <Standard>(obj.data.Standard); data = data.Where(x => x.collectDate <cutOff && x.collectDate> seasonStartDate).ToList(); retVal.Ratings = data.Select(x => x.rating).ToList(); } } if (Datam is null || Datam.Count == 0) { retVal.GamesPlayed = 0; return(retVal); } switch (playlist) { case Playlist.ONES: retVal.GamesPlayed = 0; break; case Playlist.TWOS: retVal.GamesPlayed = Datam[0].stats.matchesPlayed.value; break; case Playlist.THREES: retVal.GamesPlayed = Datam[1].stats.matchesPlayed.value; break; } } catch (Exception ex) { _log.Error(ex); } return(retVal); }
public async Task <List <CalcData> > TRNRequest(string platform, string username) { platform = ConvertPlatform(platform); if (platform == "steam") { username = username.Substring(username.LastIndexOf('/') + 1); if (username.EndsWith("/")) { username.Remove(username.Length - 1); } } using (HttpClient client = new HttpClient()) { string apistring = string.Format(Constants.TRNAPI, platform, username); HttpResponseMessage response = await client.GetAsync(apistring).ConfigureAwait(false); string content = await response.Content.ReadAsStringAsync(); if (string.IsNullOrEmpty(content)) { return(null); } if (content.ToLower().Contains("we could not find the player")) { return(null); } TRNObject obj = null; try { obj = JsonConvert.DeserializeObject <TRNObject>(content); } catch (Exception ex) { _log.Error(ex); return(null); } int playerId = obj.data.metadata.playerId; apistring = string.Format(Constants.TRNMMRAPI, playerId); response = await client.GetAsync(apistring); content = await response.Content.ReadAsStringAsync(); content = MakeJSONFriendly(content); TRNMMRObject mmrObj = JsonConvert.DeserializeObject <TRNMMRObject>(content); List <CalcData> Data = new List <CalcData> { await GetCalcDataForSegmentAsync(platform, username, 16, Playlist.ONES, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 16, Playlist.TWOS, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 16, Playlist.THREES, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 17, Playlist.ONES, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 17, Playlist.TWOS, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 17, Playlist.THREES, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 18, Playlist.ONES, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 18, Playlist.TWOS, mmrObj), await GetCalcDataForSegmentAsync(platform, username, 18, Playlist.THREES, mmrObj) }; return(Data); } }