コード例 #1
0
        public frmGameDetails(Game gGame, String sGamerTag)
        {
            InitializeComponent();

            this.gGame = gGame;
            this.sGamerTag = sGamerTag;

            this.Shown += frmGameDetails_Shown;
        }
コード例 #2
0
        /// <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;
        }