/// <summary> /// When the corresponding button is clicked, set the relationship between the current logged in gamer and the given other gamer. /// </summary> public void Button_SetRelationshipWithGamer() { // Default hardcoded values to use if no InputField elements references are assigned string gamerID = null; string notification = "{\"en\":\"Friend's relationship changed!\"}"; FriendRelationshipStatus relationship = FriendRelationshipStatus.Add; // Check the gamerID value if (setRelationshipWithGamer_GamerID == null) { Debug.LogWarning(string.Format(noReferenceFormat, "Community", "setRelationshipWithGamer_GamerID")); } else if (!string.IsNullOrEmpty(setRelationshipWithGamer_GamerID.text)) { gamerID = setRelationshipWithGamer_GamerID.text; } // Check the notification value if (setRelationshipWithGamer_Notification == null) { Debug.LogWarning(string.Format(noReferenceFormat, "Community", "setRelationshipWithGamer_Notification")); } else { notification = setRelationshipWithGamer_Notification.text; } // Check the relationship value if (setRelationshipWithGamer_Relationship == null) { Debug.LogWarning(string.Format(noReferenceFormat, "Community", "setRelationshipWithGamer_Relationship")); } // This foreach should give only one active toggle else { foreach (Toggle activeToggle in setRelationshipWithGamer_Relationship.ActiveToggles()) { switch (activeToggle.name) { case "Toggle-Friend": relationship = FriendRelationshipStatus.Add; break; case "Toggle-Blacklist": relationship = FriendRelationshipStatus.Blacklist; break; case "Toggle-Forget": relationship = FriendRelationshipStatus.Forget; break; } } } // Call the template method CommunityFeatures.Handling_SetGamerRelationship(gamerID, relationship, notification); }
/// <summary> /// Set the relationship between the current logged in gamer and the given other gamer. /// </summary> /// <param name="gamerID">Identifier of the gamer with who to change the relationship.</param> /// <param name="relationship">Type of relationship to set.</param> /// <param name="notificationJson">Message to send as notification if the target gamer is offline under {"languageCode1":"text1", "languageCode2":"text2", ...} format. (optional)</param> public static void Handling_SetGamerRelationship(string gamerID, FriendRelationshipStatus relationship, string notificationJson = null) { // The gamer ID should not be empty if (string.IsNullOrEmpty(gamerID)) { DebugLogs.LogError("[CotcSdkTemplate:CommunityFeatures] The gamer ID is empty ›› Please enter a valid gamer ID"); } else { PushNotification pushNotification = CotcConverter.GetPushNotificationFromJson(notificationJson); Backend_ChangeRelationshipStatus(gamerID, relationship, SetGamerRelationship_OnSuccess, SetGamerRelationship_OnError, pushNotification); } }
/// <summary> /// When a "friend's relationship changed" event is received, display it on the event handler. /// </summary> /// <param name="eventData">Event details under the expected format {"type":"...","message":"...","friendProfile":{...}}.</param> /// <param name="relationship">Type of relationship which has been set.</param> private static void OnFriendRelationshipChanged(Bundle eventData, FriendRelationshipStatus relationship) { // An EventHandler instance should be attached to an active object of the scene to display the result if (!EventHandler.HasInstance) { DebugLogs.LogError(string.Format(ExceptionTools.noInstanceErrorFormat, "EventFeatures", "EventHandler")); } else { string message = eventData["message"].AsString(); Bundle friendProfile = Bundle.FromJson(eventData["friendProfile"]); EventHandler.Instance.BuildAndAddEventItem_FriendRelationshipChanged(message, friendProfile, relationship); } }
/// <summary> /// Set the relationship between the current logged in gamer and the given other gamer. /// </summary> /// <param name="gamerID">Identifier of the gamer with who to change the relationship.</param> /// <param name="relationship">Type of relationship to set.</param> /// <param name="OnSuccess">The callback in case of request success.</param> /// <param name="OnError">The callback in case of request error.</param> /// <param name="pushNotification">Message to send as notification if the target gamer is offline. (optional)</param> /// <param name="domain">We use the "private" domain by default (each game holds its own data, not shared with the other games). You may configure shared domains on your FrontOffice.</param> public static void Backend_ChangeRelationshipStatus(string gamerID, FriendRelationshipStatus relationship, Action <Done, string, FriendRelationshipStatus> OnSuccess = null, Action <ExceptionError, string, FriendRelationshipStatus> OnError = null, PushNotification pushNotification = null, string domain = "private") { // Need an initialized Cloud and a logged in gamer to proceed if (!LoginFeatures.IsGamerLoggedIn()) { OnError(ExceptionTools.GetExceptionError(new CotcException(ErrorCode.NotLoggedIn), ExceptionTools.notLoggedInErrorType), gamerID, relationship); return; } // Call the API method which returns a Done result LoginFeatures.gamer.Community.Domain(domain).ChangeRelationshipStatus(gamerID, relationship, pushNotification) // Result if everything went well .Done(delegate(Done changeDone) { DebugLogs.LogVerbose(string.Format("[CotcSdkTemplate:CommunityFeatures] ChangeRelationshipStatus success ›› Successful: {0}", changeDone.Successful)); // Call the OnSuccess action if any callback registered to it if (OnSuccess != null) { OnSuccess(changeDone, gamerID, relationship); } }, // Result if an error occured delegate(Exception exception) { // Call the OnError action if any callback registered to it if (OnError != null) { OnError(ExceptionTools.GetExceptionError(exception), gamerID, relationship); } // Else, log the error (expected to be a CotcException) else { ExceptionTools.LogCotcException("CommunityFeatures", "ChangeRelationshipStatus", exception); } }); }
/// <summary> /// Fill the community friend with new data. (relationship changed) /// </summary> /// <param name="friendProfile">Profile of the friend under the Bundle format.</param> /// <param name="relationship">Type of relationship which has been set.</param> public void FillData(Bundle friendProfile, FriendRelationshipStatus relationship) { // Update fields avatarUrlToDownload = friendProfile["avatar"].AsString(); friendNicknameText.text = friendProfile["displayName"].AsString(); switch (relationship) { case FriendRelationshipStatus.Add: friendMessageText.text = friendRelationshipStatusText; communityFriendBackground.color = friendRelationshipBackgroundColor; break; case FriendRelationshipStatus.Blacklist: friendMessageText.text = blacklistRelationshipStatusText; communityFriendBackground.color = blacklistRelationshipBackgroundColor; break; case FriendRelationshipStatus.Forget: friendMessageText.text = forgotRelationshipStatusText; communityFriendBackground.color = forgotRelationshipBackgroundColor; break; } }
/// <summary> /// Build a "friend's relationship changed" event type then add it to the pending list to display. (events are displayed one by one) /// </summary> /// <param name="eventMessage">Message of the event. (describes the relationship change)</param> /// <param name="friendProfile">Profile of the friend whith who the relationship changed under the Bundle format.</param> /// <param name="relationship">Type of relationship which has been set.</param> public void BuildAndAddEventItem_FriendRelationshipChanged(string eventMessage, Bundle friendProfile, FriendRelationshipStatus relationship) { // Create a community friend GameObject GameObject communityFriendPrefabInstance = Instantiate <GameObject>(communityFriendPrefab); // Fill the newly created GameObject with event friend data CommunityFriendHandler communityFriendHandler = communityFriendPrefabInstance.GetComponent <CommunityFriendHandler>(); communityFriendHandler.FillData(friendProfile, relationship); // Add an event item with the newly created GameObject linked to it AddEventItem(eventMessage, communityFriendPrefabInstance); }
/// <summary> /// What to do if any SetGamerRelationship request failed. /// </summary> /// <param name="exceptionError">Request error details under the ExceptionError format.</param> /// <param name="gamerID">Identifier of the gamer with who to change the relationship.</param> /// <param name="relationship">Type of relationship to set.</param> private static void SetGamerRelationship_OnError(ExceptionError exceptionError, string gamerID, FriendRelationshipStatus relationship) { switch (exceptionError.type) { // Error type: not initialized Cloud or no logged in gamer case ExceptionTools.notLoggedInErrorType: // Do whatever... break; // Unhandled error types default: DebugLogs.LogError(string.Format(ExceptionTools.unhandledErrorFormat, "CommunityFeatures", exceptionError)); break; } }
/// <summary> /// What to do if any SetGamerRelationship request succeeded. /// </summary> /// <param name="changeDone">Request result details.</param> /// <param name="gamerID">Identifier of the gamer with who to change the relationship.</param> /// <param name="relationship">Type of relationship to set.</param> private static void SetGamerRelationship_OnSuccess(Done changeDone, string gamerID, FriendRelationshipStatus relationship) { Bundle eventData = Bundle.CreateObject(); string notificationJson = null; // Build a custom event depending on the case switch (relationship) { // Event type: another gamer added the currently logged in one as friend (custom) case FriendRelationshipStatus.Add: eventData["type"] = "friend_add"; eventData["message"] = "Someone added you as friend!"; eventData["friendProfile"] = LoginFeatures.gamer["profile"].ToString(); notificationJson = "{\"en\":\"You have a new friend!\"}"; break; // Event type: another gamer added the currently logged in one as blacklisted (custom) case FriendRelationshipStatus.Blacklist: eventData["type"] = "friend_blacklist"; eventData["message"] = "Someone added you as blacklisted!"; eventData["friendProfile"] = LoginFeatures.gamer["profile"].ToString(); break; // Event type: another gamer removed the currently logged in one from friend/blacklisted (custom) case FriendRelationshipStatus.Forget: eventData["type"] = "friend_forget"; eventData["message"] = "Someone forgot about your existence!"; eventData["friendProfile"] = LoginFeatures.gamer["profile"].ToString(); break; } // Send a custom event to avoid to rely on automatic relationship changes events (may be deprecated soon) Handling_SendEventToGamer(gamerID, eventData.ToString(), notificationJson); }
/// <summary>Allows to change the relation of a friendship inside the application.</summary> /// <returns>Promise resolved when the operation has completed.</returns> /// <param name="gamerId">ID of the gamer to change the relationship (fetched using ListFriends for instance).</param> /// <param name="state">The new state to set.</param> /// <param name="notification">Optional OS notification to be sent to indicate the player that the status has changed.</param> public Promise<Done> ChangeRelationshipStatus(string gamerId, FriendRelationshipStatus state, PushNotification notification = null) { UrlBuilder url = new UrlBuilder("/v2.6/gamer/friends").Path(domain).Path(gamerId).QueryParam("status", state.ToString().ToLower()); HttpRequest req = Gamer.MakeHttpRequest(url); req.BodyJson = Bundle.CreateObject("osn", notification != null ? notification.Data : null); return Common.RunInTask<Done>(req, (response, task) => { task.PostResult(new Done(response.BodyJson)); }); }
internal FriendStatusChangeEvent(string status, Bundle serverData) { FriendId = serverData["event"]["friend"]; NewStatus = Common.ParseEnum<FriendRelationshipStatus>(status); }