From b0a0e95612c67e0bf38ead4790094313b6cac02a Mon Sep 17 00:00:00 2001 From: EffanByte <94035974+EffanByte@users.noreply.github.com> Date: Mon, 30 Jun 2025 12:53:25 +0500 Subject: [PATCH 1/9] Feat: Created Discord Options Class - Made the class containing the scope, prompt and state variables. - Added the discordOptions object to InitOptions --- .../modules/Options/DiscordOptions.cs | 10 ++++++++++ .../PlayroomKit/modules/Options/InitOptions.cs | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Assets/PlayroomKit/modules/Options/DiscordOptions.cs diff --git a/Assets/PlayroomKit/modules/Options/DiscordOptions.cs b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs new file mode 100644 index 00000000..29ccd8fb --- /dev/null +++ b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs @@ -0,0 +1,10 @@ +namespace Playroom +{ + public class DiscordOptions + { + public string? Prompt; + public List? Scope; + public string? State; + } +} + \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/Options/InitOptions.cs b/Assets/PlayroomKit/modules/Options/InitOptions.cs index 4cde1a3e..0c7e597b 100644 --- a/Assets/PlayroomKit/modules/Options/InitOptions.cs +++ b/Assets/PlayroomKit/modules/Options/InitOptions.cs @@ -15,7 +15,7 @@ public class InitOptions public bool skipLobby = false; public int reconnectGracePeriod = 0; public int? maxPlayersPerRoom; - + [CanBeNull] public string gameId; public bool discord = false; @@ -59,5 +59,20 @@ public object turnBased } } } + public object discordOptions + { + get => discord; + set + { + if (value is bool || value is DiscordOptions) + { + discord = (bool)value; + } + else + { + throw new ArgumentException("discordOptions must be a boolean or a DiscordOptions object."); + } + } + } } } \ No newline at end of file From 3f588b81e995ed7ab23a403476bff4c69486b7c1 Mon Sep 17 00:00:00 2001 From: EffanByte <94035974+EffanByte@users.noreply.github.com> Date: Mon, 30 Jun 2025 13:03:08 +0500 Subject: [PATCH 2/9] Feat: Added a Helper Function - Added a discordHelper that jsonifies the discordOptions object. - Replaced options.discord in Helpers.cs with the DiscordHelper class values. --- Assets/PlayroomKit/modules/Helpers/Helpers.cs | 4 +- .../modules/Helpers/discordHelper.cs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 Assets/PlayroomKit/modules/Helpers/discordHelper.cs diff --git a/Assets/PlayroomKit/modules/Helpers/Helpers.cs b/Assets/PlayroomKit/modules/Helpers/Helpers.cs index 9c8bd9ef..068d5a5d 100644 --- a/Assets/PlayroomKit/modules/Helpers/Helpers.cs +++ b/Assets/PlayroomKit/modules/Helpers/Helpers.cs @@ -11,7 +11,7 @@ namespace Playroom public static class Helpers { static List allTurns = new List(); - + public static string SerializeInitOptions(InitOptions options) { if (options == null) return null; @@ -24,7 +24,7 @@ public static string SerializeInitOptions(InitOptions options) node["roomCode"] = options.roomCode; node["skipLobby"] = options.skipLobby; node["reconnectGracePeriod"] = options.reconnectGracePeriod; - node["discord"] = options.discord; + node["discord"] = DiscordHelper.SerializeDiscordOptions(options.discord); node["persistentMode"] = options.persistentMode; if (options.avatars != null) diff --git a/Assets/PlayroomKit/modules/Helpers/discordHelper.cs b/Assets/PlayroomKit/modules/Helpers/discordHelper.cs new file mode 100644 index 00000000..733a23f4 --- /dev/null +++ b/Assets/PlayroomKit/modules/Helpers/discordHelper.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using SimpleJSON; + +namespace Playroom +{ + public static class DiscordHelper + { + public static JSONNode SerializeDiscordOptions(object discord) + { + if (discord is bool booleanValue) + { + return booleanValue; + } + else if (discord is DiscordOptions discordOptions) + { + var node = new JSONObject(); + + if (!string.IsNullOrEmpty(discordOptions.Prompt)) + node["prompt"] = discordOptions.Prompt; + + if (discordOptions.Scope != null) + { + var scopeArray = new JSONArray(); + foreach (var s in discordOptions.Scope) + { + scopeArray.Add(s); + } + node["scope"] = scopeArray; + } + + if (!string.IsNullOrEmpty(discordOptions.State)) + node["state"] = discordOptions.State; + + return node; + } + else + { + // Not a supported type + return JSON.Parse("{}"); + } + } + } +} From 30cc6ede5e0f1679758555c3032e516cd96b37d8 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Mon, 30 Jun 2025 14:55:36 +0500 Subject: [PATCH 3/9] feat: tested and fixed discord scope --- Assets/PlayroomKit/modules/Helpers/Helpers.cs | 25 ++++++++++- .../modules/Helpers/discordHelper.cs | 43 ------------------- .../modules/Options/DiscordOptions.cs | 2 + .../modules/Options/DiscordOptions.cs.meta | 11 +++++ .../modules/Options/InitOptions.cs | 6 ++- Assets/Scripts/GameManager.cs | 5 ++- 6 files changed, 44 insertions(+), 48 deletions(-) delete mode 100644 Assets/PlayroomKit/modules/Helpers/discordHelper.cs create mode 100644 Assets/PlayroomKit/modules/Options/DiscordOptions.cs.meta diff --git a/Assets/PlayroomKit/modules/Helpers/Helpers.cs b/Assets/PlayroomKit/modules/Helpers/Helpers.cs index 068d5a5d..fede4d94 100644 --- a/Assets/PlayroomKit/modules/Helpers/Helpers.cs +++ b/Assets/PlayroomKit/modules/Helpers/Helpers.cs @@ -11,7 +11,7 @@ namespace Playroom public static class Helpers { static List allTurns = new List(); - + public static string SerializeInitOptions(InitOptions options) { if (options == null) return null; @@ -24,7 +24,7 @@ public static string SerializeInitOptions(InitOptions options) node["roomCode"] = options.roomCode; node["skipLobby"] = options.skipLobby; node["reconnectGracePeriod"] = options.reconnectGracePeriod; - node["discord"] = DiscordHelper.SerializeDiscordOptions(options.discord); + node["discord"] = SerializeDiscord(options.discord); node["persistentMode"] = options.persistentMode; if (options.avatars != null) @@ -98,6 +98,27 @@ public static string SerializeInitOptions(InitOptions options) return node.ToString(); } + private static JSONNode SerializeDiscord(object discord) + { + if (discord is bool b) + return new JSONBool(b); + + if (discord is DiscordOptions opts) + { + var discordNode = new JSONObject(); + if (opts.Scope != null) + { + var scopeArray = new JSONArray(); + foreach (var s in opts.Scope) + scopeArray.Add(s); + discordNode["scope"] = scopeArray; + } + return discordNode; + } + + return JSONNull.CreateOrGet(); + } + private static JSONNode ConvertValueToJSON(object value) { if (value is string stringValue) diff --git a/Assets/PlayroomKit/modules/Helpers/discordHelper.cs b/Assets/PlayroomKit/modules/Helpers/discordHelper.cs deleted file mode 100644 index 733a23f4..00000000 --- a/Assets/PlayroomKit/modules/Helpers/discordHelper.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections.Generic; -using SimpleJSON; - -namespace Playroom -{ - public static class DiscordHelper - { - public static JSONNode SerializeDiscordOptions(object discord) - { - if (discord is bool booleanValue) - { - return booleanValue; - } - else if (discord is DiscordOptions discordOptions) - { - var node = new JSONObject(); - - if (!string.IsNullOrEmpty(discordOptions.Prompt)) - node["prompt"] = discordOptions.Prompt; - - if (discordOptions.Scope != null) - { - var scopeArray = new JSONArray(); - foreach (var s in discordOptions.Scope) - { - scopeArray.Add(s); - } - node["scope"] = scopeArray; - } - - if (!string.IsNullOrEmpty(discordOptions.State)) - node["state"] = discordOptions.State; - - return node; - } - else - { - // Not a supported type - return JSON.Parse("{}"); - } - } - } -} diff --git a/Assets/PlayroomKit/modules/Options/DiscordOptions.cs b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs index 29ccd8fb..8143fb3e 100644 --- a/Assets/PlayroomKit/modules/Options/DiscordOptions.cs +++ b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace Playroom { public class DiscordOptions diff --git a/Assets/PlayroomKit/modules/Options/DiscordOptions.cs.meta b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs.meta new file mode 100644 index 00000000..dc6101bc --- /dev/null +++ b/Assets/PlayroomKit/modules/Options/DiscordOptions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a4d9114789376d04bbfbc54b1f87456c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayroomKit/modules/Options/InitOptions.cs b/Assets/PlayroomKit/modules/Options/InitOptions.cs index 0c7e597b..c2a74c9d 100644 --- a/Assets/PlayroomKit/modules/Options/InitOptions.cs +++ b/Assets/PlayroomKit/modules/Options/InitOptions.cs @@ -18,7 +18,6 @@ public class InitOptions [CanBeNull] public string gameId; - public bool discord = false; public bool persistentMode = false; public Dictionary defaultStates = null; @@ -27,6 +26,9 @@ public class InitOptions private object matchmakingField; private object turnBasedField; + public object discord = false; + + public object matchmaking { get => matchmakingField; @@ -66,7 +68,7 @@ public object discordOptions { if (value is bool || value is DiscordOptions) { - discord = (bool)value; + discord = value; } else { diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 293f4aec..1c80a8db 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -121,7 +121,10 @@ private void Start() { gameId = "FmOBeUfQO2AOLNIrJNSJ", maxPlayersPerRoom = 2, - discord = true, + discord = new DiscordOptions() + { + Scope = new() { "applications.commands", "guilds" } + }, }, OnLaunchCallBack); } From 75fdedad5e0843ec1223bce04aec4952e4aafc10 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Mon, 30 Jun 2025 17:02:48 +0500 Subject: [PATCH 4/9] chore: testing api --- Assets/Scripts/GameManager.cs | 46 ++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 1c80a8db..9d006887 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -11,7 +11,8 @@ public class GameManager : MonoBehaviour { #region Fields - [SerializeField] private string baseUrl = "https://ws.joinplayroom.com/api/store"; + [SerializeField] private string gameId = "FmOBeUfQO2AOLNIrJNSJ"; + [SerializeField] private string baseUrl = "https://ws.joinplayroom.com/api/"; [SerializeField] private string gameApiKey; [SerializeField] @@ -76,7 +77,7 @@ void Awake() { if (Application.absoluteURL.Contains("discord")) { - baseUrl = ".proxy/_ws/api/store"; + baseUrl = ".proxy/_ws/api"; } else { @@ -125,8 +126,40 @@ private void Start() { Scope = new() { "applications.commands", "guilds" } }, + + // discord = true }, OnLaunchCallBack); } + public IEnumerator GetActiveServerRewards( + string gameId, + string jwtToken, + string gameApiKey, + Action onSuccess, + Action onError + ) + { + // Build full URL with query parameter + string url = $"{baseUrl}/discord/server-rewards?gameId={UnityWebRequest.EscapeURL(gameId)}"; + + using (UnityWebRequest request = UnityWebRequest.Get(url)) + { + request.SetRequestHeader("Authorization", $"Bearer {jwtToken}"); + request.SetRequestHeader("x-game-api", gameApiKey); + + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.ConnectionError || + request.result == UnityWebRequest.Result.ProtocolError) + { + onError?.Invoke($"Error fetching server rewards: {request.error}"); + } + else + { + onSuccess?.Invoke(request.downloadHandler.text); + } + } + } + private void Update() { @@ -224,6 +257,11 @@ private void Update() }); } + if (Input.GetKeyDown(KeyCode.M)) + { + StartCoroutine(GetActiveServerRewards(gameId, "eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4NDA2NjA4NTY0MDQ3MDY0MS1nYy0xMjczNjA3Njg2ODE4MTA3NDAyLTEyNzQ5OTUxNDcyNjc4OTk0NTYiLCJnYW1lSWQiOiJGbU9CZVVmUU8yQU9MTklySk5TSiIsImd1aWxkSWQiOiIxMjczNjA3Njg2ODE4MTA3NDAyIiwiY2hhbm5lbElkIjoiMTI3NDk5NTE0NzI2Nzg5OTQ1NiIsImFjY2Vzc1Rva2VuIjoiTVRNM01EUXhOakk0TkRZNE9ETXlNalk0TWcuajAxREp0Wk9UY2xER0RweG45TlBmczVBODB0VThHIiwiYXV0aCI6ImRpc2NvcmQiLCJ0IjoxNzUwMDU3NDk1fQ.uE8NwXB2XCpnjD8TwYdSdEN11bHJ8DE3p1jNG-YPakQ", "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => text.text = result, (error) => text.text = error)); + } + } #endregion @@ -235,7 +273,7 @@ public void FetchSKUS(Action onRequestComplete = null) private IEnumerator GetSKUS(Action onRequestComplete = null) { - var url = $"{baseUrl}/sku?gameId={UnityWebRequest.EscapeURL("FmOBeUfQO2AOLNIrJNSJ")}&platform={UnityWebRequest.EscapeURL("discord")}"; + var url = $"{baseUrl}/store/sku?gameId={UnityWebRequest.EscapeURL("FmOBeUfQO2AOLNIrJNSJ")}&platform={UnityWebRequest.EscapeURL("discord")}"; using (var req = UnityWebRequest.Get(url)) { @@ -260,7 +298,7 @@ public void FetchEntitlements(Action onRequestComplete) private IEnumerator GetEntitlements(Action onRequestComplete) { - var url = $"{baseUrl}/entitlement?gameId={UnityWebRequest.EscapeURL("FmOBeUfQO2AOLNIrJNSJ")}"; + var url = $"{baseUrl}/store/entitlement?gameId={UnityWebRequest.EscapeURL("FmOBeUfQO2AOLNIrJNSJ")}"; using (var req = UnityWebRequest.Get(url)) { From d05bfd54f14009c7b0f4b1781bb5dbdcdaa8e389 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Tue, 1 Jul 2025 14:23:46 +0500 Subject: [PATCH 5/9] feat: server reward class --- .../PlayroomKit/modules/Store/ServerReward.cs | 19 +++++++++++++++++++ .../modules/Store/ServerReward.cs.meta | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 Assets/PlayroomKit/modules/Store/ServerReward.cs create mode 100644 Assets/PlayroomKit/modules/Store/ServerReward.cs.meta diff --git a/Assets/PlayroomKit/modules/Store/ServerReward.cs b/Assets/PlayroomKit/modules/Store/ServerReward.cs new file mode 100644 index 00000000..6cd3b9ec --- /dev/null +++ b/Assets/PlayroomKit/modules/Store/ServerReward.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +namespace Playroom +{ + public class ServerReward + { + public string serverId; + public string id; + public string title; + public string description; + public string image; + public string message; + public bool active; + public string key; + public string skuId; + public string type; + public bool status; + } +} \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/Store/ServerReward.cs.meta b/Assets/PlayroomKit/modules/Store/ServerReward.cs.meta new file mode 100644 index 00000000..8af0862c --- /dev/null +++ b/Assets/PlayroomKit/modules/Store/ServerReward.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: be07406aab4f4f04383c5e2b836c0607 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 83b810b9c454012520c998b01a968842737d3e9d Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Tue, 1 Jul 2025 15:08:23 +0500 Subject: [PATCH 6/9] feat: parse to json --- .../PlayroomKit/modules/Store/ServerReward.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Assets/PlayroomKit/modules/Store/ServerReward.cs b/Assets/PlayroomKit/modules/Store/ServerReward.cs index 6cd3b9ec..573c118e 100644 --- a/Assets/PlayroomKit/modules/Store/ServerReward.cs +++ b/Assets/PlayroomKit/modules/Store/ServerReward.cs @@ -1,7 +1,11 @@ +using System; +using System.Collections.Generic; +using SimpleJSON; using UnityEngine; namespace Playroom { + [Serializable] public class ServerReward { public string serverId; @@ -15,5 +19,42 @@ public class ServerReward public string skuId; public string type; public bool status; + + private static ServerReward FromJSONNode(JSONNode node) + { + ServerReward reward = new ServerReward + { + serverId = node["server_id"]?.Value ?? string.Empty, + id = node["id"]?.Value ?? string.Empty, + title = node["title"]?.Value ?? string.Empty, + description = node["description"]?.Value ?? string.Empty, + image = node["image"]?.Value ?? string.Empty, + message = node["message"]?.Value ?? string.Empty, + active = node["active"] != null && node["active"].AsBool, + key = node["key"]?.Value ?? string.Empty, + skuId = node["sku_id"]?.Value ?? string.Empty, + type = node["type"]?.Value ?? string.Empty, + status = node["status"] != null && node["status"].AsBool + }; + + return reward; + } + + public static List FromJSON(string jsonString) + { + List serverRewards = new(); + JSONNode root = JSON.Parse(jsonString); + + if (!root.IsArray) + Debug.LogWarning("Expected an array"); + + foreach (JSONNode item in root.AsArray) + { + var data = FromJSONNode(item); + serverRewards.Add(data); + } + + return serverRewards; + } } } \ No newline at end of file From 91d460a89b14c64344d72339ea16a1748d2c4b0b Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Tue, 1 Jul 2025 15:08:31 +0500 Subject: [PATCH 7/9] chore: test --- Assets/Scripts/GameManager.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 9d006887..40ea20f8 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -45,6 +45,9 @@ public class GameManager : MonoBehaviour [SerializeField] private List discordEntitlements = new List(); + [SerializeField] + private List serverRewards = new(); + #endregion @@ -81,7 +84,7 @@ void Awake() } else { - baseUrl = "https://ws.joinplayroom.com/api/store"; + baseUrl = "https://ws.joinplayroom.com/api"; } // Initialize fake Discord SKUs @@ -259,7 +262,11 @@ private void Update() if (Input.GetKeyDown(KeyCode.M)) { - StartCoroutine(GetActiveServerRewards(gameId, "eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4NDA2NjA4NTY0MDQ3MDY0MS1nYy0xMjczNjA3Njg2ODE4MTA3NDAyLTEyNzQ5OTUxNDcyNjc4OTk0NTYiLCJnYW1lSWQiOiJGbU9CZVVmUU8yQU9MTklySk5TSiIsImd1aWxkSWQiOiIxMjczNjA3Njg2ODE4MTA3NDAyIiwiY2hhbm5lbElkIjoiMTI3NDk5NTE0NzI2Nzg5OTQ1NiIsImFjY2Vzc1Rva2VuIjoiTVRNM01EUXhOakk0TkRZNE9ETXlNalk0TWcuajAxREp0Wk9UY2xER0RweG45TlBmczVBODB0VThHIiwiYXV0aCI6ImRpc2NvcmQiLCJ0IjoxNzUwMDU3NDk1fQ.uE8NwXB2XCpnjD8TwYdSdEN11bHJ8DE3p1jNG-YPakQ", "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => text.text = result, (error) => text.text = error)); + StartCoroutine(GetActiveServerRewards(gameId, "eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4OTUzMzUzNjg5NzIwNDMxNC1wYy0xMzcxOTI3NzM0MTY2NDg3MDkwIiwiZ2FtZUlkIjoiRm1PQmVVZlFPMkFPTE5JckpOU0oiLCJndWlsZElkIjpudWxsLCJjaGFubmVsSWQiOiIxMzcxOTI3NzM0MTY2NDg3MDkwIiwiYWNjZXNzVG9rZW4iOiJNVE0zTURReE5qSTRORFk0T0RNeU1qWTRNZy5XUVFnOFp5bjNHNnNuYVJTR21qQmZDM1A4bm5tcm4iLCJhdXRoIjoiZGlzY29yZCIsInQiOjE3NTEzNjEwNDJ9.wXL2lVdyCyXNbiNjQGrFV4bqoESt3eLyflAMt50evY8", "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => + { + text.text = result; + serverRewards = ServerReward.FromJSON(result); + }, (error) => text.text = error)); } } From 51bd3284eeec4d5b14ffcdf933ca5c1952421807 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 3 Jul 2025 12:03:40 +0500 Subject: [PATCH 8/9] chore: grant reward test --- Assets/Scripts/GameManager.cs | 97 ++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 40ea20f8..82058014 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Text; using Discord; using Playroom; using SimpleJSON; @@ -163,6 +164,75 @@ Action onError } } + public IEnumerator GrantServerReward( + string gameId, + string jwtToken, + string gameApiKey, + string rewardId, + Action onSuccess, + Action onError +) + { + string url = $"{baseUrl}/discord/server-rewards?gameId={UnityWebRequest.EscapeURL(gameId)}"; + + var bodyJson = $"{{\"rewardId\":\"{rewardId}\"}}"; + + Debug.LogWarning($"body: {bodyJson}"); + + + byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJson); + using (var request = new UnityWebRequest(url, "POST")) + { + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + request.downloadHandler = new DownloadHandlerBuffer(); + + request.SetRequestHeader("Content-Type", "application/json"); + request.SetRequestHeader("Authorization", $"Bearer {jwtToken}"); + request.SetRequestHeader("x-game-api", gameApiKey); + + yield return request.SendWebRequest(); + + // Network or protocol error? + if (request.result == UnityWebRequest.Result.ConnectionError || + request.result == UnityWebRequest.Result.DataProcessingError) + { + onError?.Invoke($"Error granting reward: {request.error}", request.downloadHandler.text); + yield break; + } + + // Handle HTTP response codes + long code = request.responseCode; + string text = request.downloadHandler.text; + + switch (code) + { + case 200: + // { "message": "..." } + onSuccess?.Invoke(text); + break; + + case 409: + onError?.Invoke("Reward already granted.", request.downloadHandler.text); + break; + + case 404: + onError?.Invoke("Server not yet joined. Please direct the player to join the Discord server first.", request.downloadHandler.text); + break; + + case 400: + onError?.Invoke("Invalid request. Check that gameId and rewardId are correct.", request.downloadHandler.text); + break; + + case 500: + onError?.Invoke("Server-side error. Please retry or contact support: " + request.error, request.downloadHandler.text); + break; + + default: + onError?.Invoke($"Unexpected response ({code}): {text}", request.downloadHandler.text); + break; + } + } + } private void Update() { @@ -262,13 +332,36 @@ private void Update() if (Input.GetKeyDown(KeyCode.M)) { - StartCoroutine(GetActiveServerRewards(gameId, "eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4OTUzMzUzNjg5NzIwNDMxNC1wYy0xMzcxOTI3NzM0MTY2NDg3MDkwIiwiZ2FtZUlkIjoiRm1PQmVVZlFPMkFPTE5JckpOU0oiLCJndWlsZElkIjpudWxsLCJjaGFubmVsSWQiOiIxMzcxOTI3NzM0MTY2NDg3MDkwIiwiYWNjZXNzVG9rZW4iOiJNVE0zTURReE5qSTRORFk0T0RNeU1qWTRNZy5XUVFnOFp5bjNHNnNuYVJTR21qQmZDM1A4bm5tcm4iLCJhdXRoIjoiZGlzY29yZCIsInQiOjE3NTEzNjEwNDJ9.wXL2lVdyCyXNbiNjQGrFV4bqoESt3eLyflAMt50evY8", "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => + StartCoroutine(GetActiveServerRewards(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => { - text.text = result; serverRewards = ServerReward.FromJSON(result); + text.text = "id :" +serverRewards[0].id + " - server id: " + serverRewards[0].serverId; }, (error) => text.text = error)); } + if (Input.GetKeyDown(KeyCode.N)) + { + StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", serverRewards[0].serverId, (result) => + { + text.text = result; + }, (error, response) => + { + text.text = response; + Debug.LogError(error); + })); + } + + if (Input.GetKeyDown(KeyCode.L)) + { + StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", serverRewards[0].id, (result) => + { + text.text = result; + }, (error, response) => + { + text.text = response; + Debug.LogError(error); + })); + } } #endregion From c28b0c86f8ae690a6161bd107bc2be5e2d935390 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 3 Jul 2025 17:43:41 +0500 Subject: [PATCH 9/9] chore: test cleanup --- Assets/Scenes/TestScene.unity | 7 +++++- Assets/Scripts/GameManager.cs | 45 ++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index 32c6ca94..bed4987e 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -286,11 +286,16 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: dc3a2b9cdc24aab40906ce4bcdee9943, type: 3} m_Name: m_EditorClassIdentifier: + gameId: FmOBeUfQO2AOLNIrJNSJ baseUrl: https://ws.joinplayroom.com/api/store - gameApiKey: + gameApiKey: 510a71af-3a69-4f5d-9b9b-296a1871e624 + token: eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4OTk1NjgzMTE0NDgzNzE0MC1wYy0xMzcxOTI3NzM0MTY2NDg3MDkwIiwiZ2FtZUlkIjoiRm1PQmVVZlFPMkFPTE5JckpOU0oiLCJndWlsZElkIjpudWxsLCJjaGFubmVsSWQiOiIxMzcxOTI3NzM0MTY2NDg3MDkwIiwiYWNjZXNzVG9rZW4iOiJNVE0zTURReE5qSTRORFk0T0RNeU1qWTRNZy5CNjdvckR6UjV1V0Q4dWE2TlZlUzNtczRpdEM2cGoiLCJhdXRoIjoiZGlzY29yZCIsInQiOjE3NTE0NjE5NzF9.Te4Fgg6KrSs9bqsxKPlyDYZotcumECoGCi_Q5cWCCAM text: {fileID: 1547749} skus: [] entitlements: [] + discordSkus: [] + discordEntitlements: [] + serverRewards: [] --- !u!4 &1054460207 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 82058014..fa3fc150 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -179,7 +179,6 @@ Action onError Debug.LogWarning($"body: {bodyJson}"); - byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJson); using (var request = new UnityWebRequest(url, "POST")) { @@ -192,7 +191,6 @@ Action onError yield return request.SendWebRequest(); - // Network or protocol error? if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError) { @@ -200,14 +198,12 @@ Action onError yield break; } - // Handle HTTP response codes long code = request.responseCode; string text = request.downloadHandler.text; switch (code) { case 200: - // { "message": "..." } onSuccess?.Invoke(text); break; @@ -332,16 +328,28 @@ private void Update() if (Input.GetKeyDown(KeyCode.M)) { - StartCoroutine(GetActiveServerRewards(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", (result) => +#if !UNITY_EDITOR && UNITY_WEBGL + StartCoroutine(GetActiveServerRewards(gameId, playroomKit.GetPlayroomToken(), gameApiKey, (result) => + { + serverRewards = ServerReward.FromJSON(result); + text.text = "id :" + serverRewards[0].id + " - server id: " + serverRewards[0].serverId; + }, (error) => text.text = error)); +#elif UNITY_EDITOR + StartCoroutine(GetActiveServerRewards(gameId, token, gameApiKey, (result) => { serverRewards = ServerReward.FromJSON(result); - text.text = "id :" +serverRewards[0].id + " - server id: " + serverRewards[0].serverId; + text.text = "id :" + serverRewards[0].id + " - server id: " + serverRewards[0].serverId; }, (error) => text.text = error)); +#endif } + + if (Input.GetKeyDown(KeyCode.N)) { - StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", serverRewards[0].serverId, (result) => + +#if !UNITY_EDITOR && UNITY_WEBGL + StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), gameApiKey, serverRewards[0].serverId, (result) => { text.text = result; }, (error, response) => @@ -349,11 +357,31 @@ private void Update() text.text = response; Debug.LogError(error); })); +#else + StartCoroutine(GrantServerReward(gameId, token, gameApiKey, serverRewards[0].serverId, (result) => + { + text.text = result; + }, (error, response) => + { + text.text = response; + Debug.LogError(error); + })); +#endif } if (Input.GetKeyDown(KeyCode.L)) { - StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), "510a71af-3a69-4f5d-9b9b-296a1871e624", serverRewards[0].id, (result) => +#if !UNITY_EDITOR && UNITY_WEBGL + StartCoroutine(GrantServerReward(gameId, playroomKit.GetPlayroomToken(), gameApiKey, serverRewards[0].id, (result) => + { + text.text = result; + }, (error, response) => + { + text.text = response; + Debug.LogError(error); + })); +#elif UNITY_EDITOR + StartCoroutine(GrantServerReward(gameId, token, gameApiKey, serverRewards[0].id, (result) => { text.text = result; }, (error, response) => @@ -361,6 +389,7 @@ private void Update() text.text = response; Debug.LogError(error); })); +#endif } } #endregion