public static GetUser GetUser(string user) { // Check if there's a cache entry UserCache cached = null; try { cached = userCache.Single(cache => cache.username == user); } catch { } if (cached != null) { TimeSpan cacheAge = DateTime.Now - cached.date; if (cacheAge.TotalMinutes <= 5) { Log.Write(LogLevels.INFO, "osu!stats", "Getting " + user + " from cache."); return(cached.data); } else { userCache.Remove(cached); } } string json = ApiRequest("get_user", new Dictionary <string, string>() { { "u", user } }); GetUser get; // Check the length of the return to see if the user doesn't exist if (json.Length < 3) { get = new GetUser(); } else { get = JsonConvert.DeserializeObject <List <GetUser> >(json)[0]; } // Add to cache Log.Write(LogLevels.INFO, "osu!stats", "Added " + user + " to cache."); userCache.Add(new UserCache() { date = DateTime.Now, username = user, data = get }); return(get); }
public void Handle(string[] data) { if (data[0] == "2") { // Clean message string message = Regex.Replace(data[3], @"\[[^]]+\]", ""); if (message.StartsWith("!osu:")) { string[] cArgs = message.Substring(5).Split(' '); switch (cArgs[0]) { case "stats": // Create a WebClient object WebClient webClient = new WebClient(); // Set headers webClient.Headers["User-Agent"] = "Railgun osu!stats Extension"; string[] usernames = { null, null }; string username = ""; try { username = cArgs[1]; } catch { username = data[2]; } // Try to get the osu username try { usernames = webClient.DownloadString("http://flashii.net/web/osu.php?u=" + username + "&z=meow").Split('|'); } catch { Log.Write(LogLevels.ERROR, "osu!stats", "Failed to connect to Flashii!"); return; } // Check if usernames isn't empty if (usernames.Length < 2) { Chat.SendMessage("[i][b]" + username + "[/b] is not a member here![/i]"); return; } GetUser osu = new GetUser(); // Get osu! account details try { osu = osuStats.GetUser(usernames[1]); } catch { } // Check if the osu username is anything if (osu.user_id == null) { Chat.SendMessage("[i][b]" + usernames[0] + "[/b] does not have an (active) osu! account connected to their account![/i]"); return; } CultureInfo cultureInfo = new CultureInfo("en-GB"); Chat.SendMessage( "[i][b]osu! statistics for [url=https://osu.ppy.sh/u/" + (osu.user_id ?? "-1") + "]" + (osu.username ?? "[deleted user]") + "[/url]" + (usernames[0] != osu.username ? " (" + usernames[0] + ")" : "") + "[/b][/i]\r\n" + "Performance: " + Math.Round(float.Parse((osu.pp_raw ?? "-1"), CultureInfo.InvariantCulture), MidpointRounding.AwayFromZero).ToString("N0", cultureInfo) + "pp :: Rank #" + long.Parse((osu.pp_rank ?? "-1")).ToString("N0", cultureInfo) + " (" + osu.country + " #" + long.Parse((osu.pp_country_rank ?? "-1")).ToString("N0", cultureInfo) + ") :: Ranked Score: " + long.Parse((osu.ranked_score ?? "-1")).ToString("N0", cultureInfo) + " :: Accuracy: " + Math.Round(float.Parse((osu.accuracy ?? "-1"), CultureInfo.InvariantCulture), 2, MidpointRounding.AwayFromZero).ToString("#.##", cultureInfo) + "%\r\n" + "Total Score: " + long.Parse((osu.total_score ?? "-1")).ToString("N0", cultureInfo) + " :: SS: " + int.Parse((osu.count_rank_ss ?? "-1")).ToString("N0", cultureInfo) + " :: S: " + int.Parse((osu.count_rank_s ?? "-1")).ToString("N0", cultureInfo) + " :: A: " + int.Parse((osu.count_rank_a ?? "-1")).ToString("N0", cultureInfo) + "\r\n" + "Level: " + Math.Floor(float.Parse((osu.level ?? "-1"), CultureInfo.InvariantCulture)) + " :: Plays: " + int.Parse((osu.playcount ?? "-1")).ToString("N0", cultureInfo) + " :: 300s: " + int.Parse((osu.count300 ?? "-1")).ToString("N0", cultureInfo) + " :: 100s: " + int.Parse((osu.count100 ?? "-1")).ToString("N0", cultureInfo) + " :: 50s: " + int.Parse((osu.count50 ?? "-1")).ToString("N0", cultureInfo) ); break; } } } }