public async Task <FetchResult <RaceResults> > GetDayStat(int userId, string modeId, DateTime date) { FetchResult <RaceResults> result; string datestr = date.Date.ToString("yyyy'-'MM'-'dd"); var address2 = $"http://klavogonki.ru/api/profile/get-stats-details-data?userId={userId}&gametype={modeId}&fromDate={datestr}&toDate={datestr}&grouping=none"; try { string json = await NetworkClient.DownloadstringAsync(address2); if (json == "{\"err\":\"invalid user id\"}") { result = new FetchResult <RaceResults>(userExists: false); } else if (json == "{\"err\":\"permission blocked\"}") { // we don't know exactly if he has premium result = new FetchResult <RaceResults>(isOpen: false); } else if (json == "{\"err\":\"not pro\"}") { result = new FetchResult <RaceResults>(isOpen: true); } else { DayRacesResponse pageDay = JsonHelper.Deserialize <DayRacesResponse>(json); var list = new RaceResults() { HasPremium = true }; list.AddRange(pageDay.List); result = new FetchResult <RaceResults>(list); } } catch { result = new FetchResult <RaceResults>(isSuccessfulDownload: false); } return(result); }
public async Task <FetchResult <OpenStat> > GetOpenStat(int userId) { FetchResult <OpenStat> result; string address = $"http://klavogonki.ru/api/profile/get-stats-overview?userId={userId}"; try { string json = await NetworkClient.DownloadstringAsync(address); if (json == "{\"err\":\"permission blocked\"}") { result = new FetchResult <OpenStat>(isOpen: false); } else if (json == "{\"err\":\"invalid user id\"}") { result = new FetchResult <OpenStat>(userExists: false); } else { json = json.Replace("\"\":", "\"unknown_mode\":"); var os = JsonHelper.Deserialize <OpenStat>(json); ////62156 260725 //http://klavogonki.ru/api/profile/get-stats-overview?userId=62156 //http://klavogonki.ru/api/profile/get-stats-overview?userId=215941 игрок с null в avg_speed, best_speed, avg_error result = new FetchResult <OpenStat>(os); } } catch (Exception) { result = new FetchResult <OpenStat>(isSuccessfulDownload: false); } return(result); }
public async Task <FetchResult <QuickStat> > GetQuickStat( int id, string modeId = "normal", bool needAwards = true) { FetchResult <QuickStat> result; try { QuickStat qs = new QuickStat(); qs.Id = id; modeId = new Mode(modeId).ModeId; string html = await NetworkClient.DownloadstringAsync($"http://klavogonki.ru/ajax/profile-popup?user_id={id}&gametype={modeId}"); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); string sLevel = doc.DocumentNode.SelectSingleNode(".//div[@class='level_icon']").InnerText; qs.Level = int.Parse(sLevel); var mRank = doc.DocumentNode.SelectSingleNode(".//table/tr/td/div"); qs.Rank = Rank.GetByIndex(int.Parse(mRank.Attributes["class"].Value.Replace("rang", ""))); qs.Nick = doc.DocumentNode.SelectSingleNode(".//div[@class='name']").InnerText; string sOverall = doc.DocumentNode.SelectSingleNode(".//table[2]/tr/td").InnerText; qs.TotalMileage = int.Parse(Regex.Match(sOverall, "\\d+").Value); string _modename = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[3]").InnerText; string modeName = Regex.Match(_modename, "«(.+)»").Groups[1].Value; qs.Mode = new Mode(modeId, modeName); string posTxts = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[4]/td").InnerText.Trim(); string[] _poss = posTxts.Split('|'); int temp; if (_poss.Length == 2) { qs.DayTop = int.TryParse(_poss[0], out temp) ? temp : (int?)null; //топ дня qs.WeekTop = int.TryParse(_poss[1], out temp) ? temp : (int?)null; //топ недели } else if (_poss.Length == 1) { qs.BookTop = int.TryParse(_poss[0], out temp) ? temp : (int?)null; //топ книги } string sRecord = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[5]/td").InnerText; qs.Record = int.Parse(Regex.Match(sRecord, "\\d+").Value); string sAverage = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[6]/td").InnerText; qs.AvgSpeed = int.Parse(Regex.Match(sAverage, "\\d+").Value); string sErrors = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[7]/td").InnerText; qs.AvgErRate = double.Parse(sErrors.Replace("%", ""), new NumberFormatInfo() { NumberDecimalSeparator = "," }) / 100; string sMileage = doc.DocumentNode.SelectSingleNode(".//table[2]/tr[8]/td").InnerText; MatchCollection matches = Regex.Matches(sMileage, "\\d+"); qs.Mileage = int.Parse(matches[0].Value); qs.Time = new TimeSpan(); if (matches.Count == 3) { qs.Time = qs.Time.Add(TimeSpan.FromHours(int.Parse(matches[1].Value))); qs.Time = qs.Time.Add(TimeSpan.FromMinutes(int.Parse(matches[2].Value))); } else if (matches.Count == 2) { qs.Time = qs.Time.Add(TimeSpan.FromMinutes(int.Parse(matches[1].Value))); } if (needAwards) { var nodes = doc.DocumentNode.SelectNodes(".//table[2]/tr/td/a"); if (nodes != null && nodes.Count > 0) { for (int i = 0; i < nodes.Count; i++) { string sModeId = nodes[i].Attributes["href"].Value; var aModeId = Regex.Match(sModeId, "(?<=gametype=).+$").Value; string style = nodes[i].GetAttributeValue("style", ""); var match = Regex.Match(style, @"(-\d+)px 0;$"); int margin = 0; if (match.Success) { margin = int.Parse(match.Groups[1].Value); } AwardType type = GetAwardTypeByMargin(margin); Award award = new Award() { ModeId = aModeId, Type = type }; qs.Awards.Add(award); } qs.BooksGold = qs.Awards.Count(x => x.Type == AwardType.GoldenBook); qs.BooksSilver = qs.Awards.Count(x => x.Type == AwardType.SilverBook); qs.BooksBronze = qs.Awards.Count(x => x.Type == AwardType.BronzeBook); } } result = new FetchResult <QuickStat>(qs); } catch (System.Net.Http.HttpRequestException ex) { result = new FetchResult <QuickStat>(isSuccessfulDownload: false); } return(result); }
public async Task <FetchResult <UserSummary> > GetUserSummary(int id) { FetchResult <UserSummary> result; try { string url = $"http://klavogonki.ru/api/profile/get-summary?id={id}"; string json = await NetworkClient.DownloadstringAsync(url); if (json == "{\"err\":\"permission blocked\"}") { result = new FetchResult <UserSummary>(isOpen: false); } else if (json == "{\"err\":\"invalid user id\"}") { result = new FetchResult <UserSummary>(userExists: false); } else { var us = JsonHelper.Deserialize <UserSummary>(json); if (us.Car.Car == 31) { us.Car.Car = 1005; //замена костылей Arch'а } else if (us.Car.Car == 32) { us.Car.Car = 1007; } else if (us.Car.Car == 1014) { us.Car.Car = 1013; } us.Car.Name = CarsConstants.Cars[us.Car.Car]; Regex regex = new Regex("tuning\":(.*)},\"level"); string tunstr = regex.Match(json).Groups[1].Value; //создание правильного us.car._tuning us.Car.Tuning = new int[10]; if (tunstr[0] == '[') { if (tunstr.Length == 3) { us.Car.Tuning[0] = int.Parse(tunstr.Substring(1, 1)); } else if (tunstr.Length > 3) { string[] temparr = tunstr.Substring(1, tunstr.Length - 2).Split(','); int[] tempint = Array.ConvertAll <string, int>(temparr, int.Parse); Array.Resize(ref tempint, 4); us.Car.Tuning = tempint; } } else { //Dictionary<string, int> dic = (Dictionary<string, int>)jss.Deserialize(tunstr, typeof(Dictionary<string, int>)); //foreach (KeyValuePair<string, int> entry in dic) //{ // us.Car.Tuning[int.Parse(entry.Key)] = entry.Value; //} } //создание url хранения аэрографии если есть, если нет - то "" us.Car.AeroUrl = Regex.Match(us.Car.Color, "'(.*)'").Groups[1].ToString(); result = new FetchResult <UserSummary>(us); } } catch (Exception) { result = new FetchResult <UserSummary>(isSuccessfulDownload: false); } return(result); }