using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace ConqueringOneself { public class Score { public int uid = -2; public string name = "ERROR"; public int level = 0; public Score() { } public Score(int uid, string name, int level) { this.uid = uid; this.name = name; this.level = level; } } public static class HighScores { public static string SERVER_URL = "http://vdzserver.org/php/co/"; public static List scores = new List(); public static void UpdateScores() { try { string[] scoresd = FetchHTTP(SERVER_URL + "listscores.php").Split(new string[] { "
" }, StringSplitOptions.RemoveEmptyEntries); if (scoresd.Length > 0) { scores.Clear(); for (int i = 0; i < scoresd.Length; i++) { string scorestring = scoresd[i]; Score toAdd = new Score(); try { string[] scoreParts = scorestring.Split(new string[] { "|" }, StringSplitOptions.None); if (scoreParts.Length == 3) { toAdd = new Score(Convert.ToInt32(scoreParts[0]), scoreParts[1].Replace("_", " "), Convert.ToInt32(scoreParts[2])); } } catch { } scores.Add(toAdd); } } } catch { scores.Clear(); scores.Add(new Score()); } } public static int GetID() { try { int toReturn = -1; string[] result = FetchHTTP(SERVER_URL + "getid.php").Split(new string[] { "=" }, StringSplitOptions.None); if (result.Length == 2) toReturn = Convert.ToInt32(result[1]); return toReturn; } catch { return -1; } } public static void SubmitScore(int id, string name, int level) { try { FetchHTTP(SERVER_URL + "submitscore.php?uid=" + id + "&name=" + name.Replace(" ", "_") + "&level=" + level); } catch { } } public static string FetchHTTP(string url) { try { string toReturn = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); System.IO.Stream fetchText = resp.GetResponseStream(); byte[] buffer = new byte[8192]; int bytesRead = -1; while (bytesRead != 0) { bytesRead = fetchText.Read(buffer, 0, buffer.Length); if (bytesRead > 0) toReturn += Encoding.ASCII.GetString(buffer, 0, bytesRead); } return toReturn; } catch { return "ERROR"; } } } }