Skip to content

Commit 22710e8

Browse files
committed
fix: simplified formatPrice
1 parent 22b76af commit 22710e8

File tree

8 files changed

+113
-45
lines changed

8 files changed

+113
-45
lines changed

Assets/PlayroomKit/PlayroomKit.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using UBB;
55
using Discord;
6+
using System.Runtime.InteropServices;
67

78

89
namespace Playroom
@@ -372,11 +373,19 @@ public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
372373
_playroomService.GetDiscordEntitlements(callback);
373374
}
374375

375-
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
376+
// public void DiscordPriceFormat(DiscordSkuPrice skuPrice, string locale, Action<string> callback)
377+
// {
378+
// CheckPlayRoomInitialized();
379+
// _playroomService.DiscordPriceFormat(skuPrice.Amount, skuPrice.Currency, locale, callback);
380+
// }
381+
382+
public string DiscordFormatPrice(DiscordSkuPrice skuPrice, string locale = "en-US")
376383
{
377384
CheckPlayRoomInitialized();
378-
_playroomService.DiscordPriceFormat(price, currency, locale, callback);
385+
return DiscordPriceFormatInternal(skuPrice.Amount, skuPrice.Currency, locale);
379386
}
387+
388+
380389
#endregion
381390
}
382391
}

Assets/PlayroomKit/modules/Headers.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ private static extern string GetPersistentDataInternal(string key,
179179
[DllImport("__Internal")]
180180
private static extern void GetDiscordEntitlementsInternal(Action<string> callback);
181181

182-
[DllImport("__Internal")]
183-
private static extern string DiscordPriceFormatInternal(float price, string currency, string locale, Action<string> callback);
182+
// [DllImport("__Internal")]
183+
// private static extern void DiscordPriceFormatInternal(float price, string currency, string locale, Action<string> callback);
184184

185+
[DllImport("__Internal")]
186+
private static extern string DiscordPriceFormatInternal(int price, string currency, string locale);
185187
#endregion
186188
}
187189
}

Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
7676
public void StartDiscordPurchase(string skuId, Action<string> callback = null);
7777
public void GetDiscordSkus(Action<List<DiscordSku>> callback);
7878
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);
79-
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback);
79+
public string DiscordFormatPrice(int price, string currency, string locale);
8080
#endregion
8181

8282

Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
338338
callback?.Invoke(new List<DiscordEntitlement>());
339339
}
340340

341-
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
341+
public string DiscordFormatPrice(int price, string currency, string locale)
342342
{
343343
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in browser mock mode!");
344-
callback?.Invoke("");
344+
return $"${price}";
345345
}
346346
#endregion
347347
}

Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
238238
callback?.Invoke(new List<DiscordEntitlement>());
239239
}
240240

241-
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
241+
public string DiscordFormatPrice(int price, string currency, string locale)
242242
{
243243
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in local mode!");
244-
callback?.Invoke($"${price}");
244+
return $"${price}";
245245
}
246246
#endregion
247247
}

Assets/PlayroomKit/modules/PlayroomBuildService.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -496,23 +496,11 @@ private static void DiscordEntitlementsCallback(string data)
496496
throw;
497497
}
498498
}
499-
500-
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
499+
500+
public string DiscordFormatPrice(int price, string currency, string locale)
501501
{
502502
CheckPlayRoomInitialized();
503-
504-
Debug.Log($"[Unity]: price {price}, currency {currency}, locale {locale}");
505-
506-
CallbackManager.RegisterCallback(callback, "formattedPrice");
507-
DiscordPriceFormatInternal(price, currency, locale, DiscordPriceFormatCallbackInvoker);
508-
}
509-
510-
[MonoPInvokeCallback(typeof(Action<string>))]
511-
private static void DiscordPriceFormatCallbackInvoker(string formattedPrice)
512-
{
513-
Debug.LogWarning($"UNITY: {formattedPrice}");
514-
515-
CallbackManager.InvokeCallback("formattedPrice", formattedPrice);
503+
return DiscordPriceFormatInternal(price, currency, locale);
516504
}
517505
#endregion
518506

Assets/PlayroomKit/src/index.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,28 +1172,54 @@ mergeInto(LibraryManager.library, {
11721172
});
11731173

11741174
},
1175-
1176-
DiscordPriceFormatInternal: function (amount, currency, locale) {
1177-
if (!window.Playroom) {
1178-
console.error(
1179-
"Playroom library is not loaded. Please make sure to call InsertCoin first."
1175+
1176+
// callback variant
1177+
/* DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr, callbackPtr) {
1178+
var currency = (typeof currencyOrPtr === 'string')
1179+
? currencyOrPtr
1180+
: UTF8ToString(currencyOrPtr);
1181+
var locale = (typeof localeOrPtr === 'string')
1182+
? localeOrPtr
1183+
: UTF8ToString(localeOrPtr);
1184+
console.warn("[jslib] args received:", amount, currency, locale);
1185+
Playroom.getDiscordSDK().then(discordSDK => {
1186+
var formatted = discordSDK.PriceUtils.formatPrice(
1187+
{ amount: amount, currency: currency },
1188+
locale
11801189
);
1181-
return 0;
1182-
}
1190+
console.warn("[jslib] formatted:", formatted);
1191+
{{{ makeDynCall("vi", "callbackPtr") }}}(stringToNewUTF8(formatted));
1192+
}).catch(err => {
1193+
console.error("Discord SDK load failed:", err);
1194+
{{{ makeDynCall("vi", "callbackPtr") }}}(0);
1195+
});
1196+
},
1197+
*/
11831198

1184-
Playroom.getDiscordSDK().then(discordSDK => {
1185-
var a = UTF8ToString(amount);
1186-
var c = UTF8ToString(currency);
1187-
var l = UTF8ToString(locale);
1188-
var formatted = discordSDK.PriceUtils.formatPrice({price: a, currency: c}, l);
1189-
console.log(formatted);
1190-
var bufferSize = lengthBytesUTF8(str) + 1;
1199+
DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr) {
1200+
var currency = (typeof currencyOrPtr === 'string')
1201+
? currencyOrPtr
1202+
: UTF8ToString(currencyOrPtr);
1203+
var locale = (typeof localeOrPtr === 'string')
1204+
? localeOrPtr
1205+
: UTF8ToString(localeOrPtr);
1206+
1207+
console.warn("[jslib] args received:", amount, currency, locale);
1208+
1209+
try {
1210+
var formatted = Playroom.getDiscordSDK().PriceUtils.formatPrice(
1211+
{ amount: amount, currency: currency },
1212+
locale
1213+
);
1214+
1215+
var bufferSize = lengthBytesUTF8(formatted) + 1;
11911216
var buffer = _malloc(bufferSize);
1192-
stringToUTF8(str, buffer, bufferSize);
1217+
stringToUTF8(formatted, buffer, bufferSize);
11931218
return buffer;
1194-
}).catch(err => {
1195-
console.error("Failed to load Discord SDK:", err);
1196-
});
1219+
} catch (err) {
1220+
console.error("[JSLIB] error while formatting prices:", err);
1221+
return 0;
1222+
}
11971223
},
11981224
//#endregion
11991225

Assets/Scripts/GameManager.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ void Awake()
8383
baseUrl = "https://ws.joinplayroom.com/api/store";
8484
}
8585

86+
// Initialize fake Discord SKUs
87+
discordSkus = new List<DiscordSku>
88+
{
89+
new DiscordSku
90+
{
91+
Id = "premium_pack_1",
92+
Name = "Premium Pack",
93+
Type = DiscordSkuType.APPLICATION,
94+
ApplicationId = "123456789",
95+
Price = new DiscordSkuPrice { Amount = 999, Currency = "USD" }
96+
},
97+
new DiscordSku
98+
{
99+
Id = "starter_pack",
100+
Name = "Starter Pack",
101+
Type = DiscordSkuType.APPLICATION,
102+
ApplicationId = "123456789",
103+
Price = new DiscordSkuPrice { Amount = 499, Currency = "USD" }
104+
},
105+
new DiscordSku
106+
{
107+
Id = "deluxe_edition",
108+
Name = "Deluxe Edition",
109+
Type = DiscordSkuType.APPLICATION,
110+
ApplicationId = "123456789",
111+
Price = new DiscordSkuPrice { Amount = 1999, Currency = "USD" }
112+
}
113+
};
114+
86115
playroomKit = new PlayroomKit();
87116
}
88117

@@ -159,12 +188,19 @@ private void Update()
159188
});
160189
}
161190

191+
if (Input.GetKeyDown(KeyCode.R))
192+
{
193+
194+
}
195+
162196
if (Input.GetKeyDown(KeyCode.F))
163197
{
198+
text.text = "";
199+
164200
discordSkus.ForEach((sku) =>
165201
{
166-
Debug.LogWarning($"{sku.Name}: {playroomKit.DiscordPriceFormat(sku.Price.Amount, sku.Price.Currency)}");
167-
text.text = playroomKit.DiscordPriceFormat(sku.Price.Amount, sku.Price.Currency);
202+
Debug.LogWarning($"{sku.Name}: {playroomKit.DiscordFormatPrice(sku.Price)}");
203+
text.text += playroomKit.DiscordFormatPrice(sku.Price);
168204
});
169205
}
170206

@@ -323,7 +359,14 @@ void DrawDiscordDebugWindow(int windowID)
323359
GUILayout.BeginVertical(GUI.skin.box);
324360
DrawInspectorField("Id", sku.Id);
325361
DrawInspectorField("Name", sku.Name);
326-
DrawInspectorField("Price", sku.Price.ToString());
362+
if (sku.Price != null)
363+
{
364+
GUILayout.Label("Price", EditorLabelBold());
365+
GUILayout.BeginVertical(GUI.skin.box);
366+
DrawInspectorField("Amount", sku.Price.Amount.ToString());
367+
DrawInspectorField("Currency", sku.Price.Currency);
368+
GUILayout.EndVertical();
369+
}
327370
DrawInspectorField("Type", sku.Type.ToString());
328371
DrawInspectorField("Application Id", sku.ApplicationId);
329372
GUILayout.EndVertical();

0 commit comments

Comments
 (0)