Beispiel #1
0
        public async Task BuildUser(AppUser user, string apiKey)
        {
            UnitOfWork     tempUOW = new UnitOfWork(AppIdentityDbContext.Create());
            AppUserManager tempUM  = new AppUserManager(new UserStore <AppUser>(tempUOW.DbContext));

            user = await tempUM.FindByIdAsyncWithStoreFilters(user.Id);

            if (user.UserSteamID == 0)
            {
                // possibly add default data here
                return;
            }

            string playerURL = String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}&format=json", apiKey, user.UserSteamID);

            string result = new System.Net.WebClient().DownloadString(playerURL);

            JObject playerData = JObject.Parse(result);

            if (playerData["response"] != null && playerData["response"]["players"] != null)
            {
                user.LargeAvatar  = (string)playerData["response"]["players"][0]["avatarfull"] ?? "";
                user.MediumAvatar = (string)playerData["response"]["players"][0]["avatarmedium"] ?? "";
                user.SmallAvatar  = (string)playerData["response"]["players"][0]["avatar"] ?? "";
            }

            string gamesURL = String.Format("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={0}&steamid={1}&format=json", apiKey, user.UserSteamID);

            result = new System.Net.WebClient().DownloadString(gamesURL);

            JObject gameData = JObject.Parse(result);

            if (gameData["response"] != null && gameData["response"]["games"] != null)
            {
                JArray jGames = (JArray)gameData["response"]["games"];
                for (int i = 0; i < jGames.Count; i++)
                {
                    int appId = (int)jGames[i]["appid"];
                    if (user.OwnedGames.Any(o => o.AppID == appId) == false)
                    {
                        user.AddOwnedGame(new OwnedGame((int)jGames[i]["appid"], (int)jGames[i]["playtime_forever"]));
                    }
                }
            }

            tempUM.Update(user);
            tempUOW.Save();
            tempUOW.Dispose();
        }
        public static void UpdateUserOwnedGames(string apiKey)
        {
            AppIdentityDbContext context;

            int day = (int)DateTime.Now.DayOfWeek;

            using (context = AppIdentityDbContext.Create())
            {
                IUnitOfWork     unitOfWork     = new UnitOfWork(context);
                IUserRepository userRepository = new UserRepository(unitOfWork);
                AppUserManager  UserManager    = new AppUserManager(new UserStore <AppUser>(unitOfWork.DbContext));

                int usersPerDay = (int)Math.Ceiling((double)UserManager.Users.Count() / 7);

                IList <AppUser> users = UserManager.Users.OrderBy(u => u.Id).Skip(day * usersPerDay).Take(usersPerDay).ToList();

                foreach (AppUser userHolder in users)
                {
                    if (UserManager.IsInRole(userHolder.Id, "Member") == false && UserManager.IsInRole(userHolder.Id, "Admin") == false)
                    {
                        continue;
                    }

                    // we've already updated this user today, the job was suspended when the site was unloaded and is rerunning to completion, thus we ignore users already updated
                    if (userHolder.LastUpdated.DayOfYear == DateTime.Now.DayOfYear)
                    {
                        continue;
                    }

                    AppUser user = UserManager.Users.Include(u => u.OwnedGames).SingleOrDefault(x => userHolder.Id == x.Id);

                    string playerURL = String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}&format=json", apiKey, user.UserSteamID);

                    string result = new System.Net.WebClient().DownloadString(playerURL);

                    JObject playerData = JObject.Parse(result);

                    if (playerData["response"] != null && playerData["response"]["players"] != null)
                    {
                        user.LargeAvatar  = (string)playerData["response"]["players"][0]["avatarfull"] ?? "";
                        user.MediumAvatar = (string)playerData["response"]["players"][0]["avatarmedium"] ?? "";
                        user.SmallAvatar  = (string)playerData["response"]["players"][0]["avatar"] ?? "";
                    }

                    string gamesURL = String.Format("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={0}&steamid={1}&format=json", apiKey, user.UserSteamID);

                    result = new System.Net.WebClient().DownloadString(gamesURL);

                    JObject gameData = JObject.Parse(result);

                    if (gameData["response"] != null && gameData["response"]["games"] != null)
                    {
                        JArray jGames = (JArray)gameData["response"]["games"];
                        for (int i = 0; i < jGames.Count; i++)
                        {
                            int appId = (int)jGames[i]["appid"];
                            if (user.OwnedGames.Any(o => o.AppID == appId) == false)
                            {
                                user.AddOwnedGame(new OwnedGame((int)jGames[i]["appid"], (int)jGames[i]["playtime_forever"]));
                            }
                        }
                    }

                    user.LastUpdated = DateTime.Now;
                    UserManager.Update(user);
                    unitOfWork.Save();
                }
            }
        }