/// <summary> /// Declines a friend request. /// </summary> /// <param name="friendID">The friend's username.</param> /// <param name="callback">Callback.</param> public void DeclineFriendRequest(string friendID, Action <bool> callback) { var endpoint = "/users/" + userID + "/friend-requests"; var payload = new Dictionary <string, object>() { { "friend", friendID }, { "decline", true } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.POST, payload, success => { var resp = success as Dictionary <string, object>; if (resp.ContainsKey("friend_requests")) { friendRequests = ParseFriends(resp["friend_requests"] as IList); } if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
static void DoRequest(Uri uri, Action <string> callback) { var request = WebRequest.Create(uri); request.ContentType = "application/json; charset=utf-8"; var authorizationHeader = LumosRequest.GenerateAuthorizationHeader(LumosCredentialsManager.GetCredentials(), null); request.Headers.Add("Authorization", authorizationHeader); var failedSSLCallback = new RemoteCertificateValidationCallback(delegate { return(true); }); ServicePointManager.ServerCertificateValidationCallback += failedSSLCallback; string text; try { var response = (HttpWebResponse)request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); callback(text); } } catch (WebException e) { LumosUnity.Debug.Log("Web exception: " + e.Message); } finally { ServicePointManager.ServerCertificateValidationCallback -= failedSSLCallback; } }
/// <summary> /// Loads the leaderboard descriptions. /// </summary> /// <param name="callback">Callback.</param> public static void LoadLeaderboardDescriptions(Action <bool> callback) { var endpoint = "/leaderboards/info"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as IList; _leaderboards = new Dictionary <string, LumosLeaderboard>(); foreach (Dictionary <string, object> info in resp) { var leaderboard = new LumosLeaderboard(info); _leaderboards[leaderboard.id] = leaderboard; } if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Registers a new user. /// </summary> /// <param name="user">The user object.</param> /// <param name="callback">Callback.</param> public static void RegisterUser(LumosUser user, Action <bool> callback) { var endpoint = "/users/" + user.id; var payload = new Dictionary <string, object>() { { "password", user.password } }; LumosUnity.Util.AddToDictionaryIfNonempty(payload, "email", user.email); LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.PUT, payload, success => { var info = success as Dictionary <string, object>; user.authenticated = true; user.Update(info); _localUser = user; if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Notifies the server that the player is playing. /// </summary> public static Coroutine Ping(Action <bool> callback) { return(LumosRequest.Send(instance, "/ping", LumosRequest.Method.POST, success => { LumosUnity.Debug.Log("Returned Ping request with user " + Lumos.playerID); var resp = success as Dictionary <string, object>; var powerupInfo = resp["powerups"] as IList; LumosPowerups.LoadPowerupInfo(powerupInfo); callback(true); }, error => { var message = "There was a problem establishing communication with Lumos. No information will be recorded for this play session."; if (error != null) { var resp = error as Dictionary <string, object>; if (resp.ContainsKey("message")) { message += " Error: " + resp["message"]; } } LumosUnity.Debug.LogWarning(message, true); callback(false); })); }
/// Reports a new score. public void ReportScore(System.Int64 score, string leaderboardID, Action <bool> callback) { if (localUser == null) { LumosUnity.Debug.LogWarning("The user must be authenticated before recording their score.", true); callback(false); return; } var endpoint = "/users/" + localUser.id + "/scores/" + leaderboardID; var payload = new Dictionary <string, object>() { { "score", (int)score } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.PUT, payload, success => { if (Application.platform == RuntimePlatform.IPhonePlayer && LumosSocial.useGameCenter) { ReportScoreToGameCenter(leaderboardID, score); } if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Fetches the scores surrounding the playing user. /// </summary> /// <param name="limit">Limit.</param> /// <param name="callback">Callback.</param> void FetchUserScores(int limit, Action <Score[]> callback) { var endpoint = "/users/" + Social.localUser.id + "/scores/" + id; loading = true; var payload = new Dictionary <string, object>() { { "limit", limit } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, payload, success => { var resp = success as Dictionary <string, object>; var scoreList = resp["scores"] as IList; var newScores = ParseScores(id, scoreList); IndexScores(newScores); loading = false; if (callback != null) { callback(scores as Score[]); } }, error => { loading = false; if (callback != null) { callback(null); } }); }
/// <summary> /// Record this player's location info. /// </summary> public static void Record() { var prefsKey = "lumospowered_" + Lumos.credentials.gameID + "_" + Lumos.playerID + "_sent_location"; // Only record location information once. if (PlayerPrefs.HasKey(prefsKey)) { return; } var endpoint = "/location/" + Lumos.playerID; var payload = new Dictionary <string, object>() { { "language", Application.systemLanguage.ToString() } }; if (Application.isWebPlayer) { payload["origin"] = Application.absoluteURL; } LumosRequest.Send(LumosAnalytics.instance, endpoint, LumosRequest.Method.PUT, payload, success => { PlayerPrefs.SetString(prefsKey, System.DateTime.Now.ToString()); LumosUnity.Debug.Log("Location information successfully sent."); }, error => { LumosUnity.Debug.LogError("Failed to send Location information."); }); }
/// <summary> /// Loads the description of the leaderboard. /// </summary> /// <param name="callback">Callback.</param> public void LoadDescription(Action <bool> callback) { if (id == null) { LumosUnity.Debug.LogWarning("Leaderboard must have an ID before loading its description."); return; } var endpoint = "/leaderboards/" + id; loading = true; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var info = success as Dictionary <string, object>; title = info["name"] as string; //scores = ParseScores(id, info["scores"] as IList); loading = false; if (callback != null) { callback(true); } }, error => { loading = false; if (callback != null) { callback(false); } }); }
/// Loads the specified users. public void LoadUsers(string[] userIDs, Action <IUserProfile[]> callback) { var endpoint = "/users"; var payload = new Dictionary <string, object>() { { "user_ids", userIDs } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, payload, success => { var resp = success as List <object>; var users = new List <IUserProfile>(resp.Count); foreach (Dictionary <string, object> info in resp) { var user = new LumosUserProfile(info); users.Add(user); } if (callback != null) { callback(users.ToArray()); } }, error => { if (callback != null) { callback(null); } }); }
/// <summary> /// Sends the events. /// </summary> public static void Send() { if (events.Count == 0) { return; } var endpoint = "/events"; var payload = new List <Dictionary <string, object> >(events.Values); LumosRequest.Send(LumosAnalytics.instance, endpoint, LumosRequest.Method.POST, payload, success => { var now = System.DateTime.Now.ToString(); // Save unrepeatable events to player prefs with a timestamp. foreach (var key in unsentUniqueEvents) { var prefsKey = "lumospowered_event_" + key + "_recorded"; PlayerPrefs.SetString(prefsKey, now); } events.Clear(); unsentUniqueEvents.Clear(); }, error => { LumosUnity.Debug.LogWarning("Events not sent. Will try again at next timer interval."); }); }
// Updates the user. public void Update(Dictionary <string, object> info) { if (info.ContainsKey("user_id")) { userID = info["user_id"] as string; } if (info.ContainsKey("name")) { userName = info["name"] as string; } if (info.ContainsKey("underage")) { underage = (bool)info["underage"]; } if (info.ContainsKey("email")) { email = info["email"] as string; } // Load avatar from remote server. if (info.ContainsKey("image")) { var imageLocation = info["image"] as string; LumosRequest.LoadImage(imageLocation, image); } if (info.ContainsKey("other")) { other = LumosUnity.Json.Deserialize(info["other"] as string) as Dictionary <string, object>; } }
/// <summary> /// Asks the server to generate and return a new player ID. /// </summary> public static Coroutine RequestPlayerID() { return(LumosRequest.Send(instance, "/players", LumosRequest.Method.POST, success => { var idPrefsKey = "lumospowered_" + Lumos.credentials.gameID + "_playerid"; var resp = success as Dictionary <string, object>; Lumos.playerID = resp["player_id"].ToString(); PlayerPrefs.SetString(idPrefsKey, Lumos.playerID); LumosUnity.Debug.Log("Using new player " + Lumos.playerID); })); }
/// <summary> /// Sends feedback to the server. /// </summary> public static void Send(Dictionary <string, object> feedback) { var endpoint = "/feedback"; LumosRequest.Send(LumosDiagnostics.instance, endpoint, LumosRequest.Method.POST, feedback, success => { LumosUnity.Debug.Log("Feedback sent."); }, error => { LumosUnity.Debug.LogWarning("Log messages not sent. Will try again at next timer interval."); } ); }
// Creates a new user profile. public LumosUserProfile(Dictionary <string, object> info) { this.userID = info["user_id"] as string; if (info.ContainsKey("name")) { this.userName = info["name"] as string; } // Load avatar from remote server. if (info.ContainsKey("image")) { var imageLocation = info["image"] as string; LumosRequest.LoadImage(imageLocation, image); } }
/// <summary> /// Requests to reset the given user's password. /// </summary> /// <param name="username">Username.</param> /// <param name="callback">Callback.</param> public static void ResetPassword(string username, Action <bool> callback) { var endpoint = "/users/" + username + "/password"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.POST, success => { if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// Loads the player's earned achievements. public void LoadAchievements(Action <IAchievement[]> callback) { if ((achievements == null || achievements.Length == 0) && !loadingAchievements) { // Load the achievements from the server. loadingAchievements = true; var endpoint = "/users/" + localUser.id + "/achievements"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as IList; _achievements = new Dictionary <string, LumosAchievement>(); foreach (Dictionary <string, object> info in resp) { var achievement = new LumosAchievement(info); _achievements[achievement.id] = achievement; } loadingAchievements = false; if (callback != null) { callback(achievements); } }, error => { loadingAchievements = false; if (callback != null) { callback(null); } }); } else { // Use the cached achievements. if (callback != null) { callback(achievements); } } }
/// <summary> /// Fetches the friend scores. /// </summary> void FetchFriendScores(Action <bool> callback) { var endpoint = "/users/" + Social.localUser.id + "/friends/scores/" + id; loading = true; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as Dictionary <string, object>; var scoreList = resp["scores"] as IList; friendScores = ParseScores(id, scoreList); loading = false; callback(true); }, error => { loading = false; callback(false); }); }
/// <summary> /// Sends the queued logs to the server. /// </summary> public static void Send() { if (logs.Count == 0) { return; } var endpoint = "/logs"; var payload = new List <Dictionary <string, object> >(logs.Values); LumosRequest.Send(LumosDiagnostics.instance, endpoint, LumosRequest.Method.POST, payload, success => { logs.Clear(); }, error => { LumosUnity.Debug.LogWarning("Log messages not sent. Will try again at next timer interval."); } ); }
/// <summary> /// Loads a leaderboard with only friend scores. /// </summary> /// <param name="callback">Callback.</param> public void LoadFriendLeaderboardScores(Action <bool> callback) { var endpoint = "/users/" + id + "/friends/scores"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as IList; var leaderboards = new LumosLeaderboard[resp.Count]; for (int i = 0; i < resp.Count; i++) { leaderboards[i] = new LumosLeaderboard(resp[i] as Dictionary <string, object>); } foreach (var leaderboard in leaderboards) { var current = LumosSocial.GetLeaderboard(leaderboard.id); // Leaderboard already exists; update friend scores only. if (current != null) { current.friendScores = leaderboard.friendScores; } // Leaderboard doesn't exist yet; add entire leaderboard. else { LumosSocial.AddLeaderboard(leaderboard); } } if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Updates the user's info. /// </summary> /// <param name="name">The user's name.</param> /// <param name="email">The user's email address.</param> /// <param name="password">Password.</param> /// <param name="other">Additional information.</param> /// <param name="callback">Callback.</param> public void UpdateInfo(string name = null, string email = null, string password = null, string new_password = null, Dictionary <string, object> other = null, Action <bool> callback = null) { // Check if the user is updating their password // If they are, make sure both the current and new password are provided. if ((!string.IsNullOrEmpty(password) && new_password == null) || (password == null && !string.IsNullOrEmpty(new_password))) { LumosUnity.Debug.LogError("If you update a user's password, you must provide both their current and new password.", true); callback(false); return; } var endpoint = "/users/" + userID; var payload = new Dictionary <string, object>(); LumosUnity.Util.AddToDictionaryIfNonempty(payload, "name", name); LumosUnity.Util.AddToDictionaryIfNonempty(payload, "email", email); LumosUnity.Util.AddToDictionaryIfNonempty(payload, "password", password); LumosUnity.Util.AddToDictionaryIfNonempty(payload, "new_password", new_password); if (other != null) { payload["other"] = LumosUnity.Json.Serialize(other); } LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.PUT, payload, success => { var resp = success as Dictionary <string, object>; Update(resp); if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(true); } }); }
// Creates an achievement description object. public LumosAchievementDescription(Dictionary <string, object> info) { this.id = info["achievement_id"] as string; this.title = info["name"] as string; this.achievedDescription = info["achieved_description"] as string; this.unachievedDescription = info["unachieved_description"] as string; this.points = Convert.ToInt32(info["points"] as string); var hiddenInt = Convert.ToInt32(info["hidden"] as string); this.hidden = Convert.ToBoolean(hiddenInt); // Load image from remote server. if (info.ContainsKey("icon")) { var imageLocation = info["icon"] as string; image = new Texture2D(512, 512); LumosRequest.LoadImage(imageLocation, image); } }
/// <summary> /// Removes a friend. /// </summary> /// <param name="friendID">The friend's username.</param> /// <param name="callback">Callback.</param> public void RemoveFriend(string friendID, Action <bool> callback) { var endpoint = "/users/" + userID + "/friends/" + friendID; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.DELETE, success => { RemoveFriendByID(friendID); if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// Reports progress for the achievement. public void ReportProgress(Action <bool> callback) { if (Social.localUser == null) { LumosUnity.Debug.LogWarning("The user must be authenticated before reporting an achievement.", true); callback(false); return; } var endpoint = "/users/" + Social.localUser.id + "/achievements/" + id; var payload = new Dictionary <string, object>() { { "percent_completed", percentCompleted } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.PUT, payload, success => { var info = success as Dictionary <string, object>; // Update timestamp. var timestamp = Convert.ToDouble(info["updated"]); lastReportedDate = LumosUnity.Util.UnixTimestampToDateTime(timestamp); if (Application.platform == RuntimePlatform.IPhonePlayer && LumosSocial.useGameCenter) { ReportProgressToGameCenter(id, percentCompleted); } if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
// Loads the user's friends list. public void LoadFriends(Action <bool> callback) { var endpoint = "/users/" + userID + "/friends"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as IList; friends = ParseFriends(resp); if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Accepts a friend request. /// </summary> /// <param name="friendID">The friend's username.</param> /// <param name="callback">Callback.</param> public void AcceptFriendRequest(string friendID, Action <bool> callback) { var endpoint = "/users/" + userID + "/friends/" + friendID; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.PUT, success => { var newFriend = success as Dictionary <string, object>; AddFriend(newFriend); if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// Fetches the achievement descriptions. public void LoadAchievementDescriptions(Action <IAchievementDescription[]> callback) { if (achievementDescriptions == null && !loadingAchievementDescriptions) { // Load the achievement descriptions from the server. loadingAchievementDescriptions = true; var endpoint = "/achievements"; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, success => { var resp = success as IList; achievementDescriptions = new LumosAchievementDescription[resp.Count]; for (int i = 0; i < resp.Count; i++) { achievementDescriptions[i] = new LumosAchievementDescription(resp[i] as Dictionary <string, object>); } loadingAchievementDescriptions = false; if (callback != null) { callback(achievementDescriptions); } }, error => { loadingAchievementDescriptions = false; if (callback != null) { callback(null); } }); } else { // Use the cached achievement descriptions. callback(achievementDescriptions); } }
// Authenticate the user. public void Authenticate(Action <bool> callback) { // ID should be set prior to this call if login system is intended. if (userID == null) { userID = Lumos.playerID; } var endpoint = "/users/" + userID; if (password == null) { password = "******"; } var payload = new Dictionary <string, object>() { { "password", password } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, payload, success => { var resp = success as Dictionary <string, object>; authenticated = true; (Social.Active as LumosSocial).SetLocalUser(this); Update(resp); if (callback != null) { callback(true); } }, error => { if (callback != null) { callback(false); } }); }
/// <summary> /// Fetches the scores. /// </summary> /// <param name="limit">Limit.</param> /// <param name="offset">Offset.</param> /// <param name="callback">Callback.</param> void FetchScores(int limit, int offset, Action <Score[]> callback) { loading = true; var endpoint = "/leaderboards/" + id + "/scores"; var payload = new Dictionary <string, object>() { { "limit", limit }, { "offset", offset } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.GET, payload, success => { var resp = success as Dictionary <string, object>; var scoreList = resp["scores"] as IList; var newScores = ParseScores(id, scoreList); IndexScores(newScores); loading = false; var tempScores = new List <Score>(); foreach (var score in scores) { tempScores.Add(score as Score); } if (callback != null) { callback(tempScores.ToArray()); } }, error => { loading = false; if (callback != null) { callback(null); } }); }
/// <summary> /// Sends a friend request. /// </summary> /// <param name="friendID">The friend's username.</param> /// <param name="callback">Callback.</param> public void SendFriendRequest(string friendID, Action <bool> callback) { var endpoint = "/users/" + userID + "/friend-requests"; var payload = new Dictionary <string, object>() { { "friend", friendID } }; LumosRequest.Send(LumosSocial.instance, endpoint, LumosRequest.Method.POST, payload, success => { if (callback != null) { callback(false); } }, error => { if (callback != null) { callback(false); } }); }