Exemplo n.º 1
0
        public SteamUserDetails GetByUserID(long id, bool reloadWishlistPrices = false)
        {
            DataCacheRepository repo  = _uow.GetRepo <DataCacheRepository>();
            DataCache           cache = repo.Get(id, CacheType.SteamUserDetails);

            if (cache != null && cache.StoredOn > DateTime.Now.AddHours(-1))
            {
                SteamUserDetails user = cache.Deserialize <SteamUserDetails>();
                if (reloadWishlistPrices)
                {
                    LoadWishlistPrices(user);
                    user.Wishlist = user.Wishlist.OrderByDescending(x => x.Pricing.IsOnSale).ToList();
                }
                return(user);
            }
            SteamUserDetails userDetails = new SteamUserDetails {
                User      = _steamApi.GetPlayerSummary(id),
                OwnedApps = _steamApi.GetOwnedApps(id)
            };

            SteamScraper scraper      = new SteamScraper();
            long?        lastPlayedID = scraper.GetLastPlayedGame(userDetails.User.ProfileUrl);

            if (lastPlayedID.HasValue)
            {
                userDetails.LastPlayedGame = _steamApi.GetAppStoreDetails(lastPlayedID.Value);
            }

            List <long> wishlistIDs = scraper.GetWishlist(userDetails.User.ProfileUrl);

            foreach (long wishlistID in wishlistIDs)
            {
                userDetails.Wishlist.Add(_steamApi.GetAppStoreDetails(wishlistID));
            }
            userDetails.Wishlist = userDetails.Wishlist.OrderByDescending(x => x.Pricing != null && x.Pricing.IsOnSale).ToList();

            userDetails.SteamLevel = _steamApi.GetSteamLevel(userDetails.User.ID);
            userDetails.Friends    = _steamApi.GetFriendList(userDetails.User.ID);

            bool isNew = cache == null;

            cache = new DataCache {
                EntityID    = userDetails.User.ID.ToString(),
                CacheTypeID = CacheType.SteamUserDetails,
                StoredOn    = DateTime.Now,
                JsonData    = JsonConvert.SerializeObject(userDetails)
            };

            if (isNew)
            {
                repo.Add(cache);
            }
            else
            {
                repo.Update(cache);
            }

            return(userDetails);
        }