public AchievementService() { Type managerType = Type.GetType(Settings.Default.AchievementManagerType); _service = (IAchievementManager)Activator.CreateInstance(managerType); _communityService = new SteamCommunityManager(_service); }
/// <summary> /// Publishes the user's achievements. /// </summary> /// <param name="steamUserId">The steam user id.</param> public void PublishUserAchievements(string steamUserId) { User user = _userService.GetUser(steamUserId); if (user == null) { _log.Log(steamUserId + " does not exist"); return; } _log.Log(String.Format("User {0} ({1})", user.SteamUserId, user.FacebookUserId)); if (String.IsNullOrEmpty(user.AccessToken)) { _log.Log("Empty AccessToken"); // if there is no access token, the user hasn't given the app offline_access return; } try { // update the user's achievements int updated = _achievementService.UpdateAchievements(user.SteamUserId); if (updated == 0) { _log.Log("No updated achievements"); return; } } catch (InvalidGamesXmlException exception) { _log.Log("Invalid games URL for {0}. Disabling auto update.", user.SteamUserId); _log.Log(exception); user.AutoUpdate = false; _userService.UpdateUser(user); throw; } catch (XmlException exception) { _log.Log("Invalid xml for {0}.", user.SteamUserId); _log.Log(exception); throw; } // get unpublished achievements earned in the last 24-48 hours to make up for time zone differences // and the time it takes to run the Auto Update process DateTime oldestDate = DateTime.UtcNow.AddHours(-48).Date; IEnumerable <Achievement> achievements = _achievementService.GetUnpublishedAchievements(user.SteamUserId, oldestDate); if (!achievements.Any()) { _log.Log("No unpublished achievements"); return; } // only publish the top 5 achievements = achievements.Take(5); // since the api only supports one attachment, use the first achievement's image // and build the description from all achievements Achievement firstAchievement = achievements.First(); Uri statsUrl = SteamCommunityManager.GetProfileUrl(user.SteamUserId, false); string message = String.Format("{0} earned new achievements", user.SteamUserId); IDictionary <string, object> parameters = new Dictionary <string, object> { { "link", statsUrl.ToString() }, { "message", message }, { "name", firstAchievement.Name }, { "picture", firstAchievement.ImageUrl }, { "description", BuildDescription(achievements, user.PublishDescription) } }; List <int> publishedAchievements = new List <int>(); try { // publish the post _publisher.Publish(user, parameters); publishedAchievements.AddRange(achievements.Select(a => a.Id)); } catch (FacebookOAuthException exception) { // The user's access token is invalid. They may have changed their password performed another action to invalidate it. _log.Log("User {0} has an invalid AccessToken, the value will be removed.", user.SteamUserId); _log.Log(exception); // Reset the user's access token. user.AccessToken = String.Empty; _userService.UpdateUser(user); return; } catch (FacebookApiException exception) { _log.Log(exception); return; } // update the published flag _achievementService.PublishAchievements(user.SteamUserId, publishedAchievements); _log.Log("Published {0} achievements.", publishedAchievements.Count); return; }
public AchievementService() { _achievementManager = new AchievementManager(); _communityService = new SteamCommunityManager(); }
/// <summary> /// Publishes the user's achievements. /// </summary> /// <param name="steamUserId">The steam user id.</param> public void PublishUserAchievements(string steamUserId) { User user = _userService.GetUser(steamUserId); if (user == null) { _log.Log(steamUserId + " does not exist"); return; } _log.Log("User " + user.SteamUserId + " (" + user.FacebookUserId + ")"); if (String.IsNullOrEmpty(user.AccessToken)) { _log.Log("Empty AccessToken"); // if there is no access token, the user hasn't given the app offline_access return; } // update the user's achievements int updated = _achievementService.UpdateAchievements(user.SteamUserId); if (updated == 0) { _log.Log("No updated achievements"); return; } // get unpublished achievements earned in the last 24-48 hours to make up for time zone differences // and the time it takes to run the Auto Update process DateTime oldestDate = DateTime.UtcNow.AddHours(-48).Date; IEnumerable <SimpleAchievement> achievements = _achievementService.GetUnpublishedAchievements(user.SteamUserId, oldestDate); if (!achievements.Any()) { _log.Log("No unpublished achievements"); return; } // only publish the top 5 achievements = achievements.Take(5); // since the api only supports one attachment, use the first achievement's image // and build the description from all achievements SimpleAchievement firstAchievement = achievements.First(); Uri statsUrl = SteamCommunityManager.GetProfileUrl(user.SteamUserId, false); string message = String.Format("{0} earned new achievements", user.SteamUserId); IDictionary <string, object> parameters = new Dictionary <string, object> { { "link", statsUrl.ToString() }, { "message", message }, { "name", firstAchievement.Name }, { "picture", firstAchievement.ImageUrl }, { "description", BuildDescription(achievements, user.PublishDescription) } }; List <int> publishedAchievements = new List <int>(); try { // publish the post _publisher.Publish(user, parameters); publishedAchievements.AddRange(achievements.Select(a => a.Id)); } catch (FacebookApiException ex) { _log.Log(ex); return; } // update the published flag _achievementService.PublishAchievements(user.SteamUserId, publishedAchievements); _log.Log("User achievements published"); return; }