private void cmdGetData_Click(object sender, EventArgs e) { ListViewItem lsvItem; pProfile = XboxLeadersAPI.DownloadProfileData(txtGamerTag.Text); gGames = XboxLeadersAPI.DownloadGameData(txtGamerTag.Text); fFriends = XboxLeadersAPI.DownloadFriendList(txtGamerTag.Text); pbAvatar.Image = pProfile.Avatar; lblGamerTag.Text = "GamerTag: " + pProfile.GamerTag; lblScore.Text = "Gamer Score: " + pProfile.GamerScore.ToString(); lblRep.Text = "Reputation: " + pProfile.Reputation.ToString(); lblPresence.Text = "Presence: " + pProfile.Presence; lblOnline.Text = "Online: " + pProfile.Online.ToString(); lblLocation.Text = "Location: " + pProfile.Location; lblBiography.Text = "Biography: " + pProfile.Biography; foreach (Activity aActivity in pProfile.RecentActivity) { lsvItem = new ListViewItem(aActivity.Title); lsvItem.SubItems.Add(aActivity.LastPlayed.ToLocalTime().ToString()); lsvActivity.Items.Add(lsvItem); } lblAchievements.Text = String.Format("Achievements: {0}/{1}", gGames.Achievements.Current, gGames.Achievements.Total); lblTotalGames.Text = "Games Played: " + gGames.TotalGames.ToString(); lblProgress.Text = String.Format("Progress: {0}%", gGames.Progress); foreach (Game gGame in gGames.PlayedGames) { lsvItem = new ListViewItem(gGame.Title); lsvItem.SubItems.Add(String.Format("{0}/{1}", gGame.GamerScore.Current, gGame.GamerScore.Total)); lsvItem.SubItems.Add(String.Format("{0}/{1}", gGame.Achievements.Current, gGame.Achievements.Total)); lsvItem.SubItems.Add(String.Format("{0}%", gGame.Progress)); lsvItem.SubItems.Add(gGame.LastPlayed.ToLocalTime().ToString()); lsvGames.Items.Add(lsvItem); } foreach (Friend fFriend in fFriends.GamerFriends) { lsvItem = new ListViewItem(fFriend.GamerTag); if (fFriend.Online) { lsvItem.Group = lsvFriends.Groups[0]; } else { lsvItem.Group = lsvFriends.Groups[1]; } lsvItem.SubItems.Add(fFriend.Status); lsvItem.SubItems.Add(fFriend.LastSeen.ToLocalTime().ToString()); lsvFriends.Items.Add(lsvItem); } }
/// <summary> /// Downloads the game data for a the specified gamer tag. /// </summary> /// <param name="sGamerTag">The gamer tag to download game data for.</param> /// <returns>Returns a <seealso cref="XboxLeadersNETWrapper.Games">Games</seealso> object.</returns> /// <example>XboxLeadersNETWrapper.Games gGames = XboxLeadersNETWrapper.DownloadGameData("Major Nelson");</example> public static Games DownloadGameData(String sGamerTag) { WebClient wcDownloader = new WebClient(); Games gDownloadedGames = new Games(); XmlDocument xGames = new XmlDocument(); XmlReader xReader; XElement xJSONRoot; Byte[] bJSONData; bJSONData = System.Text.Encoding.ASCII.GetBytes(wcDownloader.DownloadString("https://www.xboxleaders.com/api/games.json?gamertag=" + sGamerTag)); xReader = JsonReaderWriterFactory.CreateJsonReader(bJSONData, new XmlDictionaryReaderQuotas()); if (xReader == null) { throw new Exception("Unable to convert data."); } xJSONRoot = XElement.Load(xReader); xGames.LoadXml(xJSONRoot.ToString()); if (xGames.SelectSingleNode("root/status").InnerText.ToLower() != "success") { //TODO: Catch errors in JSON feed (Problem: If JSON returns a 501 the WebClient throws an exception.) throw new Exception(""); } gDownloadedGames.GamerTag = xGames.SelectSingleNode("root/data/gamertag").InnerText; gDownloadedGames.GamerScore = new GamerScore(Int32.Parse(xGames.SelectSingleNode("root/data/gamerscore/current").InnerText), Int32.Parse(xGames.SelectSingleNode("root/data/gamerscore/total").InnerText)); gDownloadedGames.Achievements = new Achievements(Int32.Parse(xGames.SelectSingleNode("root/data/achievements/current").InnerText), Int32.Parse(xGames.SelectSingleNode("root/data/achievements/total").InnerText)); gDownloadedGames.TotalGames = Int32.Parse(xGames.SelectSingleNode("root/data/totalgames").InnerText); gDownloadedGames.Progress = Int32.Parse(xGames.SelectSingleNode("root/data/progress").InnerText); gDownloadedGames.PlayedGames = new List<Game>(); foreach (XmlNode xNode in xGames.SelectNodes("root/data/games/item")) { Game gGame = new Game(); gGame.ID = Int32.Parse(xNode.SelectSingleNode("id").InnerText); gGame.HID = xNode.SelectSingleNode("hid").InnerText; gGame.IsApp = Boolean.Parse(xNode.SelectSingleNode("isapp").InnerText); gGame.Title = HttpUtility.HtmlDecode(xNode.SelectSingleNode("title").InnerText); gGame.Artwork = DownloadImage(xNode.SelectSingleNode("artwork").SelectSingleNode("large").InnerText); gGame.GamerScore = new GamerScore(Int32.Parse(xNode.SelectSingleNode("gamerscore/current").InnerText), Int32.Parse(xNode.SelectSingleNode("gamerscore/total").InnerText)); gGame.Achievements = new Achievements(Int32.Parse(xNode.SelectSingleNode("achievements/current").InnerText), Int32.Parse(xNode.SelectSingleNode("achievements/total").InnerText)); gGame.Progress = Double.Parse(xNode.SelectSingleNode("progress").InnerText); gGame.LastPlayed = DateTimeFromEpoc(Int64.Parse(xNode.SelectSingleNode("lastplayed").InnerText)); gDownloadedGames.PlayedGames.Add(gGame); } return gDownloadedGames; }