コード例 #1
0
        public void ProcessData(GamervineDataContext DataContext, Gamertag Gamertag, XDocument CurrentData)
        {
            var latestData = from xd in DataContext.XboxData
                             where xd.TagId == Gamertag.TagId
                             orderby xd.RetrieveDate descending
                             select xd;

            //If we have a set of most recent data & the most recent data doesn't equal the data just retrieved, store it
            //Or, if the RecentGames element doesn't have any children, their authorization isn't set correctly and move on

            //TODO: Need to flag the account and notify the user that we are unable to retrieve their data
            if ((latestData.Count() != 0 && latestData.First().XmlData.Equals(CurrentData.ToString())) ||
                CurrentData.Descendants("RecentGames").Descendants().Count() == 0)
            {
                return;
            }

            XboxData dcXboxInfo = new XboxData();

            dcXboxInfo.XboxDataId   = Guid.NewGuid().ToString();
            dcXboxInfo.TagId        = Gamertag.TagId;
            dcXboxInfo.XmlData      = CurrentData.ToString();
            dcXboxInfo.RetrieveDate = DateTime.UtcNow;

            var xboxInfos = from xi in CurrentData.Descendants("XboxInfo")
                            select new bcXboxInfo
            {
                AccountStatus      = xi.Element("AccountStatus").Value,
                State              = xi.Element("State").Value,
                Gamertag           = xi.Element("Gamertag").Value,
                ProfileUrl         = new Uri(xi.Element("ProfileUrl").Value),
                TileUrl            = new Uri(xi.Element("TileUrl").Value),
                Country            = xi.Element("Country").Value,
                Reputation         = decimal.Parse(xi.Element("Reputation").Value),
                Bio                = xi.Element("Bio").Value,
                Location           = xi.Element("Location").Value,
                ReputationImageUrl = new Uri(xi.Element("ReputationImageUrl").Value),
                GamerScore         = int.Parse(xi.Element("GamerScore").Value),
                Zone               = xi.Element("Zone").Value,
            };

            var presenceInfos = from p in CurrentData.Descendants("PresenceInfo")
                                select new bcPresenceInfo
            {
                Valid      = bool.Parse(p.Element("Valid").Value),
                Info       = p.Element("Info").Value.Trim(' '),
                Info2      = p.Element("Info2").Value.Trim(' '),
                LastSeen   = p.Element("LastSeen").Value.Trim(' '),
                Online     = bool.Parse(p.Element("Online").Value),
                StatusText = p.Element("StatusText").Value.Trim(' '),
                Title      = p.Element("Title").Value.Trim(' ')
            };

            bcXboxInfo xInfo = xboxInfos.First <bcXboxInfo>();

            xInfo.PresenceInfo = presenceInfos.First <bcPresenceInfo>();

            if (latestData.Count() != 0)
            {
                //If the most recent data was offline and the current data is offline, move on - unless gamerscore changed
                if (latestData.First().Online.HasValue&& latestData.First().Online == 0 && !xInfo.PresenceInfo.Online &&
                    latestData.First().Gamerscore == xInfo.GamerScore)
                {
                    return;
                }

                //If the most recent data was away and the current data is away, move on - unless gamerscore changed
                if (latestData.First().StatusText.ToLower().Equals("away") && !xInfo.PresenceInfo.StatusText.ToLower().Equals("away") &&
                    latestData.First().Gamerscore == xInfo.GamerScore)
                {
                    return;
                }
            }

            dcXboxInfo.AccountStatus = xInfo.AccountStatus;
            dcXboxInfo.Bio           = xInfo.Bio;
            dcXboxInfo.Country       = xInfo.Country;
            dcXboxInfo.Gamerscore    = xInfo.GamerScore;
            dcXboxInfo.Gamertag      = xInfo.Gamertag;
            dcXboxInfo.Info          = xInfo.PresenceInfo.Info;
            dcXboxInfo.Info2         = xInfo.PresenceInfo.Info2;
            if (DateTime.Parse(xInfo.PresenceInfo.LastSeen) != DateTime.MinValue)
            {
                dcXboxInfo.LastSeen = DateTime.Parse(xInfo.PresenceInfo.LastSeen);
            }
            dcXboxInfo.Location           = xInfo.Location;
            dcXboxInfo.Online             = (xInfo.PresenceInfo.Online ? bcPresenceInfo.Status.Online.GetHashCode() : bcPresenceInfo.Status.Offline.GetHashCode());
            dcXboxInfo.ProfileUrl         = xInfo.ProfileUrl.ToString();
            dcXboxInfo.Reputation         = xInfo.Reputation;
            dcXboxInfo.ReputationImageUrl = xInfo.ReputationImageUrl.ToString();
            dcXboxInfo.StatusText         = xInfo.PresenceInfo.StatusText;
            dcXboxInfo.TileUrl            = xInfo.TileUrl.ToString();
            dcXboxInfo.Title = xInfo.PresenceInfo.Title;
            dcXboxInfo.Valid = (xInfo.PresenceInfo.Valid ? 1 : 0);
            dcXboxInfo.Zone  = xInfo.Zone;

            //xboxInfo.First<XboxInfo>().PresenceInfo = presence.First<PresenceInfo>();

            //var recentGames = from xugi in xmlDoc.Descendants("XboxUserGameInfo")
            //                  select new XboxUserGameInfo
            //                  {
            //                      Game = new Game
            //                      {
            //                          Name = xugi.Element("Game").Element("Name").Value,
            //                          TotalAchievements = int.Parse(xugi.Element("Game").Element("TotalAchievements").Value),
            //                          TotalGamerScore = int.Parse(xugi.Element("Game").Element("TotalGamerScore").Value),
            //                          Image32Url = new Uri(xugi.Element("Game").Element("Image32Url").Value),
            //                          Image64Url = new Uri(xugi.Element("Game").Element("Image64Url").Value)
            //                      },
            //                      LastPlayed = xugi.Element("LastPlayed").Value,
            //                      Achievements = int.Parse(xugi.Element("Achievements").Value),
            //                      GamerScore = int.Parse(xugi.Element("GamerScore").Value),
            //                      DetailsUrl = new Uri(xugi.Element("DetailsURL").Value)
            //                  };

            //XboxInfo xInfo = xboxInfo.First<XboxInfo>();
            //PresenceInfo pInfo = presence.First<PresenceInfo>();
            //List<XboxUserGameInfo> rGames = recentGames.ToList<XboxUserGameInfo>();

            //xInfo.PresenceInfo = pInfo;
            //xInfo.RecentGames = rGames;

            DataContext.XboxData.InsertOnSubmit(dcXboxInfo);
        }
コード例 #2
0
        private void retrieveGamerData()
        {
            GamervineDataContext dataContext = new GamervineDataContext();

            try
            {
                var tags = from t in dataContext.Gamertags
                           where t.State == 1
                           select t;

                foreach (Gamertag gt in tags)
                {
                    WebRequest  wRequest  = HttpWebRequest.Create("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=" + gt.Tag);
                    WebResponse wResponse = wRequest.GetResponse();

                    StreamReader sr     = new StreamReader(wResponse.GetResponseStream());
                    string       result = sr.ReadToEnd();

                    XDocument xmlDoc = XDocument.Parse(result);

                    XboxData dcXboxInfo = new XboxData();
                    dcXboxInfo.XboxDataId   = _nextXboxDataID++;
                    dcXboxInfo.TagId        = gt.TagId;
                    dcXboxInfo.XmlData      = xmlDoc.ToString();
                    dcXboxInfo.RetrieveDate = DateTime.UtcNow;

                    var xboxInfos = from xi in xmlDoc.Descendants("XboxInfo")
                                    select new bcXboxInfo
                    {
                        AccountStatus      = xi.Element("AccountStatus").Value,
                        State              = xi.Element("State").Value,
                        Gamertag           = xi.Element("Gamertag").Value,
                        ProfileUrl         = new Uri(xi.Element("ProfileUrl").Value),
                        TileUrl            = new Uri(xi.Element("TileUrl").Value),
                        Country            = xi.Element("Country").Value,
                        Reputation         = decimal.Parse(xi.Element("Reputation").Value),
                        Bio                = xi.Element("Bio").Value,
                        Location           = xi.Element("Location").Value,
                        ReputationImageUrl = new Uri(xi.Element("ReputationImageUrl").Value),
                        GamerScore         = int.Parse(xi.Element("GamerScore").Value),
                        Zone               = xi.Element("Zone").Value,
                    };

                    var presenceInfos = from p in xmlDoc.Descendants("PresenceInfo")
                                        select new bcPresenceInfo
                    {
                        Valid      = bool.Parse(p.Element("Valid").Value),
                        Info       = p.Element("Info").Value,
                        Info2      = p.Element("Info2").Value,
                        LastSeen   = p.Element("LastSeen").Value,
                        Online     = bool.Parse(p.Element("Online").Value),
                        StatusText = p.Element("StatusText").Value,
                        Title      = p.Element("Title").Value
                    };

                    bcXboxInfo xInfo = xboxInfos.First <bcXboxInfo>();
                    xInfo.PresenceInfo = presenceInfos.First <bcPresenceInfo>();

                    dcXboxInfo.AccountStatus      = xInfo.AccountStatus;
                    dcXboxInfo.Bio                = xInfo.Bio;
                    dcXboxInfo.Country            = xInfo.Country;
                    dcXboxInfo.Gamerscore         = xInfo.GamerScore;
                    dcXboxInfo.Gamertag           = xInfo.Gamertag;
                    dcXboxInfo.Info               = xInfo.PresenceInfo.Info;
                    dcXboxInfo.Info2              = xInfo.PresenceInfo.Info2;
                    dcXboxInfo.LastSeen           = DateTime.Parse(xInfo.PresenceInfo.LastSeen);
                    dcXboxInfo.Location           = xInfo.Location;
                    dcXboxInfo.Online             = (xInfo.PresenceInfo.Online ? 1 : 0);
                    dcXboxInfo.ProfileUrl         = xInfo.ProfileUrl.ToString();
                    dcXboxInfo.Reputation         = xInfo.Reputation;
                    dcXboxInfo.ReputationImageUrl = xInfo.ReputationImageUrl.ToString();
                    dcXboxInfo.StatusText         = xInfo.PresenceInfo.StatusText;
                    dcXboxInfo.TileUrl            = xInfo.TileUrl.ToString();
                    dcXboxInfo.Title              = xInfo.PresenceInfo.Title;
                    dcXboxInfo.Valid              = (xInfo.PresenceInfo.Valid ? 1 : 0);
                    dcXboxInfo.Zone               = xInfo.Zone;

                    dataContext.XboxData.InsertOnSubmit(dcXboxInfo);

                    //xboxInfo.First<XboxInfo>().PresenceInfo = presence.First<PresenceInfo>();

                    //var recentGames = from xugi in xmlDoc.Descendants("XboxUserGameInfo")
                    //                  select new XboxUserGameInfo
                    //                  {
                    //                      Game = new Game
                    //                      {
                    //                          Name = xugi.Element("Game").Element("Name").Value,
                    //                          TotalAchievements = int.Parse(xugi.Element("Game").Element("TotalAchievements").Value),
                    //                          TotalGamerScore = int.Parse(xugi.Element("Game").Element("TotalGamerScore").Value),
                    //                          Image32Url = new Uri(xugi.Element("Game").Element("Image32Url").Value),
                    //                          Image64Url = new Uri(xugi.Element("Game").Element("Image64Url").Value)
                    //                      },
                    //                      LastPlayed = xugi.Element("LastPlayed").Value,
                    //                      Achievements = int.Parse(xugi.Element("Achievements").Value),
                    //                      GamerScore = int.Parse(xugi.Element("GamerScore").Value),
                    //                      DetailsUrl = new Uri(xugi.Element("DetailsURL").Value)
                    //                  };

                    //XboxInfo xInfo = xboxInfo.First<XboxInfo>();
                    //PresenceInfo pInfo = presence.First<PresenceInfo>();
                    //List<XboxUserGameInfo> rGames = recentGames.ToList<XboxUserGameInfo>();

                    //xInfo.PresenceInfo = pInfo;
                    //xInfo.RecentGames = rGames;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                dataContext.SubmitChanges();
            }
        }