Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System;
using UBB;
using Discord;


namespace Playroom
Expand Down Expand Up @@ -358,6 +359,24 @@ public void StartDiscordPurchase(string skuId, Action<string> responseCallback =
CheckPlayRoomInitialized();
_playroomService.StartDiscordPurchase(skuId, responseCallback);
}

public void GetDiscordSkus(Action<List<DiscordSku>> callback)
{
CheckPlayRoomInitialized();
_playroomService.GetDiscordSkus(callback);
}

public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
{
CheckPlayRoomInitialized();
_playroomService.GetDiscordEntitlements(callback);
}

public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
{
CheckPlayRoomInitialized();
_playroomService.DiscordPriceFormat(price, currency, locale, callback);
}
#endregion
}
}
106 changes: 106 additions & 0 deletions Assets/PlayroomKit/modules/Discord/DiscordEntitlement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using SimpleJSON;

namespace Discord
{
[Serializable]
public class DiscordEntitlement
{
// Required
public string Id;
public string SkuId;
public string ApplicationId;
public string UserId;
public int GiftCodeFlags;
public string Type; // always a string (coerced from number if needed)

// Optionals
public string? GifterUserId;
public List<string>? Branches;
public string? StartsAt; // ISO-8601 as string
public string? EndsAt;
public string? ParentId;
public bool? Consumed;
public bool? Deleted;
public string? GiftCodeBatchId;

private static DiscordEntitlement FromJSONNode(JSONNode node)
{
// Required fields
if (!node.HasKey("type"))
throw new FormatException("Required field 'type' is missing.");

var e = new DiscordEntitlement
{
Id = node["id"].Value,
SkuId = node["sku_id"].Value,
ApplicationId = node["application_id"].Value,
UserId = node["user_id"].Value,
GiftCodeFlags = node["gift_code_flags"].AsInt,
Type = node["type"].IsNumber ? node["type"].AsInt.ToString() : node["type"].Value
};

// Optionals
if (node.HasKey("gifter_user_id"))
e.GifterUserId = node["gifter_user_id"].Value;

if (node.HasKey("branches"))
{
e.Branches = new List<string>();
foreach (var b in node["branches"].AsArray)
e.Branches.Add(b.Value);
}

if (node.HasKey("starts_at"))
e.StartsAt = node["starts_at"].Value;

if (node.HasKey("ends_at"))
e.EndsAt = node["ends_at"].Value;

if (node.HasKey("parent_id"))
e.ParentId = node["parent_id"].Value;

if (node.HasKey("consumed"))
e.Consumed = node["consumed"].AsBool;

if (node.HasKey("deleted"))
e.Deleted = node["deleted"].AsBool;

if (node.HasKey("gift_code_batch_id"))
e.GiftCodeBatchId = node["gift_code_batch_id"].Value;

return e;
}

/// <summary>
/// Parse a JSON string into a list of DiscordEntitlement instances.
/// </summary>
public static List<DiscordEntitlement> FromJSON(string jsonString)
{
var list = new List<DiscordEntitlement>();
var root = JSON.Parse(jsonString);

if (root == null)
throw new FormatException("Invalid JSON: root is null.");

if (root.HasKey("entitlements") && root["entitlements"].IsArray)
{
foreach (var item in root["entitlements"].AsArray)
list.Add(FromJSONNode(item));
}
else if (root.IsArray)
{
foreach (var item in root.AsArray)
list.Add(FromJSONNode(item));
}
else
{
list.Add(FromJSONNode(root));
}

return list;
}

}
}
95 changes: 95 additions & 0 deletions Assets/PlayroomKit/modules/Discord/DiscordSkus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using SimpleJSON;

namespace Discord
{
public enum DiscordSkuType
{
UNHANDLED = -1,
APPLICATION = 1,
DLC = 2,
CONSUMABLE = 3,
BUNDLE = 4,
SUBSCRIPTION = 5
}

[Serializable]
public class DiscordSkuPrice
{
public int Amount;
public string Currency;

internal static DiscordSkuPrice FromJSONNode(JSONNode n)
{
return new DiscordSkuPrice
{
Amount = n["amount"].AsInt,
Currency = n["currency"].Value
};
}
}

[Serializable]
public class DiscordSku
{
// Required
public string Id;
public string Name;
public DiscordSkuType Type;
public DiscordSkuPrice Price;
public string ApplicationId;
public int Flags;

// Optional
public string? ReleaseDate;

internal static DiscordSku FromJSONNode(JSONNode n)
{
var rawType = n["type"].AsInt;
if (!Enum.IsDefined(typeof(DiscordSkuType), rawType))
throw new FormatException($"Unknown SkuType code: {rawType}");

var sku = new DiscordSku
{
Id = n["id"].Value,
Name = n["name"].Value,
Type = (DiscordSkuType)rawType,
Price = DiscordSkuPrice.FromJSONNode(n["price"]),
ApplicationId = n["application_id"].Value,
Flags = n["flags"].AsInt
};

if (n.HasKey("release_date"))
{
var rd = n["release_date"].Value;
sku.ReleaseDate = string.IsNullOrEmpty(rd) ? null : rd;
}

return sku;
}

public static List<DiscordSku> FromJSON(string jsonString)
{
var list = new List<DiscordSku>();
var root = JSON.Parse(jsonString);

if (root.HasKey("skus") && root["skus"].IsArray)
{
foreach (var item in root["skus"].AsArray)
list.Add(FromJSONNode(item));
}
else if (root.IsArray)
{
foreach (var item in root.AsArray)
list.Add(FromJSONNode(item));
}
else
{
list.Add(FromJSONNode(root));
}

return list;
}
}
}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/modules/Discord/DiscordSkus.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 0 additions & 74 deletions Assets/PlayroomKit/modules/Discord/Entitlement.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Assets/PlayroomKit/modules/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ private static extern string GetPersistentDataInternal(string key,

[DllImport("__Internal")]
private static extern void StartDiscordPurchaseInternal(string skuId, Action<string, string> callback);

[DllImport("__Internal")]
private static extern void GetDiscordSkusInternal(Action<string> callback);

[DllImport("__Internal")]
private static extern void GetDiscordEntitlementsInternal(Action<string> callback);

[DllImport("__Internal")]
private static extern string DiscordPriceFormatInternal(float price, string currency, string locale, Action<string> callback);

#endregion
}
}
18 changes: 15 additions & 3 deletions Assets/PlayroomKit/modules/Helpers/CallbackManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Discord;
using UnityEngine;

namespace Playroom
Expand Down Expand Up @@ -114,14 +115,25 @@ public static void InvokeCallback(string key, List<TurnData> turnData)
}
}

public static void InvokeCallback(string key, List<Entitlement> Entitlement)
public static void InvokeCallback(string key, List<DiscordEntitlement> entitlements)
{
if (callbacks.TryGetValue(key, out Delegate callback))
{
if (callback is Action<List<Entitlement>> action) action?.Invoke(Entitlement);
if (callback is Action<List<DiscordEntitlement>> action) action?.Invoke(entitlements);
else
Debug.LogError(
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {Entitlement}!");
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {entitlements}!");
}
}

public static void InvokeCallback(string key, List<DiscordSku> skus)
{
if (callbacks.TryGetValue(key, out Delegate callback))
{
if (callback is Action<List<DiscordSku>> action) action?.Invoke(skus);
else
Debug.LogError(
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {skus}!");
}
}

Expand Down
4 changes: 4 additions & 0 deletions Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using AOT;
using Discord;
using UnityEngine;

namespace Playroom
Expand Down Expand Up @@ -73,6 +74,9 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
#region Discord
public void OpenDiscordInviteDialog(Action callback = null);
public void StartDiscordPurchase(string skuId, Action<string> callback = null);
public void GetDiscordSkus(Action<List<DiscordSku>> callback);
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback);
#endregion


Expand Down
Loading