Skip to content

Commit 605eec3

Browse files
authored
Merge pull request #151 from EffanByte/discord-mode
Server Reward
2 parents b284c16 + c28b0c8 commit 605eec3

File tree

8 files changed

+317
-10
lines changed

8 files changed

+317
-10
lines changed

Assets/PlayroomKit/modules/Helpers/Helpers.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static string SerializeInitOptions(InitOptions options)
2424
node["roomCode"] = options.roomCode;
2525
node["skipLobby"] = options.skipLobby;
2626
node["reconnectGracePeriod"] = options.reconnectGracePeriod;
27-
node["discord"] = options.discord;
27+
node["discord"] = SerializeDiscord(options.discord);
2828
node["persistentMode"] = options.persistentMode;
2929

3030
if (options.avatars != null)
@@ -98,6 +98,27 @@ public static string SerializeInitOptions(InitOptions options)
9898
return node.ToString();
9999
}
100100

101+
private static JSONNode SerializeDiscord(object discord)
102+
{
103+
if (discord is bool b)
104+
return new JSONBool(b);
105+
106+
if (discord is DiscordOptions opts)
107+
{
108+
var discordNode = new JSONObject();
109+
if (opts.Scope != null)
110+
{
111+
var scopeArray = new JSONArray();
112+
foreach (var s in opts.Scope)
113+
scopeArray.Add(s);
114+
discordNode["scope"] = scopeArray;
115+
}
116+
return discordNode;
117+
}
118+
119+
return JSONNull.CreateOrGet();
120+
}
121+
101122
private static JSONNode ConvertValueToJSON(object value)
102123
{
103124
if (value is string stringValue)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
3+
namespace Playroom
4+
{
5+
public class DiscordOptions
6+
{
7+
public string? Prompt;
8+
public List<string>? Scope;
9+
public string? State;
10+
}
11+
}
12+

Assets/PlayroomKit/modules/Options/DiscordOptions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PlayroomKit/modules/Options/InitOptions.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ public class InitOptions
1515
public bool skipLobby = false;
1616
public int reconnectGracePeriod = 0;
1717
public int? maxPlayersPerRoom;
18-
18+
1919
[CanBeNull]
2020
public string gameId;
21-
public bool discord = false;
2221
public bool persistentMode = false;
2322

2423
public Dictionary<string, object> defaultStates = null;
@@ -27,6 +26,9 @@ public class InitOptions
2726
private object matchmakingField;
2827
private object turnBasedField;
2928

29+
public object discord = false;
30+
31+
3032
public object matchmaking
3133
{
3234
get => matchmakingField;
@@ -59,5 +61,20 @@ public object turnBased
5961
}
6062
}
6163
}
64+
public object discordOptions
65+
{
66+
get => discord;
67+
set
68+
{
69+
if (value is bool || value is DiscordOptions)
70+
{
71+
discord = value;
72+
}
73+
else
74+
{
75+
throw new ArgumentException("discordOptions must be a boolean or a DiscordOptions object.");
76+
}
77+
}
78+
}
6279
}
6380
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using SimpleJSON;
4+
using UnityEngine;
5+
6+
namespace Playroom
7+
{
8+
[Serializable]
9+
public class ServerReward
10+
{
11+
public string serverId;
12+
public string id;
13+
public string title;
14+
public string description;
15+
public string image;
16+
public string message;
17+
public bool active;
18+
public string key;
19+
public string skuId;
20+
public string type;
21+
public bool status;
22+
23+
private static ServerReward FromJSONNode(JSONNode node)
24+
{
25+
ServerReward reward = new ServerReward
26+
{
27+
serverId = node["server_id"]?.Value ?? string.Empty,
28+
id = node["id"]?.Value ?? string.Empty,
29+
title = node["title"]?.Value ?? string.Empty,
30+
description = node["description"]?.Value ?? string.Empty,
31+
image = node["image"]?.Value ?? string.Empty,
32+
message = node["message"]?.Value ?? string.Empty,
33+
active = node["active"] != null && node["active"].AsBool,
34+
key = node["key"]?.Value ?? string.Empty,
35+
skuId = node["sku_id"]?.Value ?? string.Empty,
36+
type = node["type"]?.Value ?? string.Empty,
37+
status = node["status"] != null && node["status"].AsBool
38+
};
39+
40+
return reward;
41+
}
42+
43+
public static List<ServerReward> FromJSON(string jsonString)
44+
{
45+
List<ServerReward> serverRewards = new();
46+
JSONNode root = JSON.Parse(jsonString);
47+
48+
if (!root.IsArray)
49+
Debug.LogWarning("Expected an array");
50+
51+
foreach (JSONNode item in root.AsArray)
52+
{
53+
var data = FromJSONNode(item);
54+
serverRewards.Add(data);
55+
}
56+
57+
return serverRewards;
58+
}
59+
}
60+
}

Assets/PlayroomKit/modules/Store/ServerReward.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scenes/TestScene.unity

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,16 @@ MonoBehaviour:
286286
m_Script: {fileID: 11500000, guid: dc3a2b9cdc24aab40906ce4bcdee9943, type: 3}
287287
m_Name:
288288
m_EditorClassIdentifier:
289+
gameId: FmOBeUfQO2AOLNIrJNSJ
289290
baseUrl: https://ws.joinplayroom.com/api/store
290-
gameApiKey:
291+
gameApiKey: 510a71af-3a69-4f5d-9b9b-296a1871e624
292+
token: eyJhbGciOiJIUzI1NiJ9.eyJkaXNjb3JkSWQiOiI0NzY3MDk1MjQwMTE2MTQyMTkiLCJyb29tSWQiOiJEQ1JEX2ktMTM4OTk1NjgzMTE0NDgzNzE0MC1wYy0xMzcxOTI3NzM0MTY2NDg3MDkwIiwiZ2FtZUlkIjoiRm1PQmVVZlFPMkFPTE5JckpOU0oiLCJndWlsZElkIjpudWxsLCJjaGFubmVsSWQiOiIxMzcxOTI3NzM0MTY2NDg3MDkwIiwiYWNjZXNzVG9rZW4iOiJNVE0zTURReE5qSTRORFk0T0RNeU1qWTRNZy5CNjdvckR6UjV1V0Q4dWE2TlZlUzNtczRpdEM2cGoiLCJhdXRoIjoiZGlzY29yZCIsInQiOjE3NTE0NjE5NzF9.Te4Fgg6KrSs9bqsxKPlyDYZotcumECoGCi_Q5cWCCAM
291293
text: {fileID: 1547749}
292294
skus: []
293295
entitlements: []
296+
discordSkus: []
297+
discordEntitlements: []
298+
serverRewards: []
294299
--- !u!4 &1054460207
295300
Transform:
296301
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)