From 47609a712952c311a8b4bb8fb3e8ee613baf39ee Mon Sep 17 00:00:00 2001 From: Artsiom Trubchyk Date: Wed, 19 Jun 2024 23:24:53 +0300 Subject: [PATCH] Implement `getPriceCurrencyImage` (#24) Closes #22 --- README.md | 2 +- example/ysdkdebug/pg_payments.lua | 6 +++++- yagames/helpers/mock.lua | 6 +++++- yagames/lib/web/lib_yagames.js | 12 +++++++++++- yagames/src/main.cpp | 4 ++-- yagames/yagames.lua | 16 ++++++++++++++-- 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f38d6dc..13fd59a 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ The best way to integrate SDK into your game is to read [the official documentat | `ysdk.getPayments(options)` | `yagames.payments_init(options, callback)` | | `payments.purchase(options)` | `yagames.payments_purchase(options, callback)` | | `payments.getPurchases()` | `yagames.payments_get_purchases(callback)`
The result has the format `{ purchases = { ... }, signature = "..." }` | -| `payments.getCatalog()` | `yagames.payments_get_catalog(callback)` | +| `payments.getCatalog()` | `yagames.payments_get_catalog([options], callback)`
The argument `options` is an optional Lua table `{ getPriceCurrencyImage = "size" }`, where `size` (string) can be `medium`, `small` and `svg`, the currency image url will be injected to the `getPriceCurrencyImage` field of each product. | | `payments.consumePurchase(purchaseToken)` | `yagames.payments_consume_purchase(purchase_token, callback)` | | **Leaderboards** [(docs)](https://yandex.ru/dev/games/doc/en/sdk/sdk-leaderboard) | | | `ysdk.getLeaderboards()` | `yagames.leaderboards_init(callback)` | diff --git a/example/ysdkdebug/pg_payments.lua b/example/ysdkdebug/pg_payments.lua index 1d881c8..642c0ff 100644 --- a/example/ysdkdebug/pg_payments.lua +++ b/example/ysdkdebug/pg_payments.lua @@ -25,7 +25,11 @@ function M.init_handler(self) end function M.get_catalog_handler(self) - yagames.payments_get_catalog(function(self, err, catalog) + -- `options` is optional and can be `nil` or omitted. + local options = { + getPriceCurrencyImage = "medium" + } + yagames.payments_get_catalog(options, function(self, err, catalog) print("yagames.payments_get_catalog:", err or table_util.tostring(catalog)) end) end diff --git a/yagames/helpers/mock.lua b/yagames/helpers/mock.lua index 2c9910b..43fcf56 100755 --- a/yagames/helpers/mock.lua +++ b/yagames/helpers/mock.lua @@ -188,8 +188,12 @@ function M.payments_get_purchases(cb_id) M.send(cb_id, NO_ERR, rxi_json.encode(tmp)) end -function M.payments_get_catalog(cb_id) +function M.payments_get_catalog(cb_id, options) assert(M._payments) + if type(options) == "string" then + options = rxi_json.decode(options) + -- do something... + end M.send(cb_id, NO_ERR, rxi_json.encode(M._payments.catalog)) end diff --git a/yagames/lib/web/lib_yagames.js b/yagames/lib/web/lib_yagames.js index 7f8fa9a..20b4868 100755 --- a/yagames/lib/web/lib_yagames.js +++ b/yagames/lib/web/lib_yagames.js @@ -494,12 +494,22 @@ var LibYaGamesPrivate = { } }, - YaGamesPrivate_Payments_GetCatalog: function (cb_id) { + YaGamesPrivate_Payments_GetCatalog: function (cb_id, coptions) { var self = YaGamesPrivate; try { + var options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions)); self._payments .getCatalog() .then((products) => { + if (typeof options.getPriceCurrencyImage === "string") { + const newResults = []; + for (const product of products) { + const result = JSON.parse(JSON.stringify(product)); + result.getPriceCurrencyImage = product.getPriceCurrencyImage(options.getPriceCurrencyImage); + newResults.push(result); + } + products = newResults; + } self.send(cb_id, null, JSON.stringify(products)); }) .catch((err) => { diff --git a/yagames/src/main.cpp b/yagames/src/main.cpp index db3f02f..e163a6a 100644 --- a/yagames/src/main.cpp +++ b/yagames/src/main.cpp @@ -44,7 +44,7 @@ extern "C" void YaGamesPrivate_GetPayments(const int cb_id, const char* options); void YaGamesPrivate_Payments_Purchase(const int cb_id, const char* options); void YaGamesPrivate_Payments_GetPurchases(const int cb_id); - void YaGamesPrivate_Payments_GetCatalog(const int cb_id); + void YaGamesPrivate_Payments_GetCatalog(const int cb_id, const char* options); void YaGamesPrivate_Payments_ConsumePurchase(const int cb_id, const char* purchase_token); void YaGamesPrivate_GetPlayer(const int cb_id, const char* options); const char* YaGamesPrivate_Player_GetPayingStatus(); @@ -541,7 +541,7 @@ static int Payments_GetPurchases(lua_State* L) static int Payments_GetCatalog(lua_State* L) { - YaGamesPrivate_Payments_GetCatalog(luaL_checkint(L, 1)); + YaGamesPrivate_Payments_GetCatalog(luaL_checkint(L, 1), lua_isstring(L, 2) ? luaL_checkstring(L, 2) : 0); return 0; } diff --git a/yagames/yagames.lua b/yagames/yagames.lua index b62944a..d1fd77c 100755 --- a/yagames/yagames.lua +++ b/yagames/yagames.lua @@ -359,9 +359,16 @@ function M.payments_get_purchases(callback) end --- Get a list of available purchases and their cost. +-- @tparam[opt] {getPriceCurrencyImage=string} options -- @tparam function callback -function M.payments_get_catalog(callback) +function M.payments_get_catalog(options, callback) assert(M.payments_ready, "Payments subsystem is not initialized.") + -- Backward compatibility + if type(options) == "function" and not callback then + callback = options + options = nil + end + assert(type(options) == "table" or type(options) == "nil", "`options` should be a table or nil.") assert(type(callback) == "function") yagames_private.payments_get_catalog(helper.wrap_for_promise(function(self, err, catalog) @@ -369,7 +376,7 @@ function M.payments_get_catalog(callback) catalog = rxi_json.decode(catalog) end callback(self, err, catalog) - end)) + end), options and rxi_json.encode(options) or nil) end --- Consume an in-game purchase. @@ -708,6 +715,7 @@ function M.flags_get(options, callback) end), options and rxi_json.encode(options) or nil) end +--- DEPRECATED -- @tparam function callback function M.banner_init(callback) assert(type(callback) == "function") @@ -721,6 +729,7 @@ function M.banner_init(callback) end)) end +--- DEPRECATED function M.banner_create(rtb_id, options, callback) assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.") assert(type(rtb_id) == "string") @@ -735,6 +744,7 @@ function M.banner_create(rtb_id, options, callback) end) or 0) end +--- DEPRECATED function M.banner_destroy(rtb_id) assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.") assert(type(rtb_id) == "string") @@ -742,6 +752,7 @@ function M.banner_destroy(rtb_id) yagames_private.banner_destroy(rtb_id) end +--- DEPRECATED function M.banner_refresh(rtb_id, callback) assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.") assert(type(rtb_id) == "string") @@ -755,6 +766,7 @@ function M.banner_refresh(rtb_id, callback) end) or 0) end +--- DEPRECATED function M.banner_set(rtb_id, property, value) assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.") assert(type(rtb_id) == "string")