feat: update shop
This commit is contained in:
@@ -25,6 +25,8 @@ function InitModule(ctx, logger, nk, initializer) {
|
||||
|
||||
// Store RPCs
|
||||
initializer.registerRpc("purchase_item", rpcPurchaseItem);
|
||||
initializer.registerRpc("get_shop_catalog", rpcGetShopCatalog);
|
||||
initializer.registerRpc("buy_currency", rpcBuyCurrency);
|
||||
|
||||
// Leaderboard RPCs
|
||||
initializer.registerRpc("get_leaderboard_stats", rpcGetLeaderboardStats);
|
||||
@@ -384,6 +386,54 @@ function rpcAdminSetUserRole(ctx, logger, nk, payload) {
|
||||
// Store / Economy RPCs
|
||||
// =============================================================================
|
||||
|
||||
function rpcGetShopCatalog(ctx, logger, nk, payload) {
|
||||
if (!ctx.userId) throw new Error("Not authenticated");
|
||||
|
||||
// We could read this from storage, but for now we hardcode it to keep it simple and authoritative
|
||||
var catalog = {
|
||||
"head": [
|
||||
{"id": "head_hat1", "name": "Cap", "gold": 100, "star": 0},
|
||||
{"id": "head_crown", "name": "Crown", "gold": 0, "star": 50}
|
||||
],
|
||||
"costume": [
|
||||
{"id": "costume_red", "name": "Red Suit", "gold": 200, "star": 0},
|
||||
{"id": "costume_gold", "name": "Gold Suit", "gold": 0, "star": 100}
|
||||
],
|
||||
"glove": [
|
||||
{"id": "glove_leather", "name": "Leather Gloves", "gold": 50, "star": 0}
|
||||
],
|
||||
"accessory": [
|
||||
{"id": "acc_glasses", "name": "Sunglasses", "gold": 80, "star": 0}
|
||||
]
|
||||
};
|
||||
|
||||
return JSON.stringify({ catalog: catalog });
|
||||
}
|
||||
|
||||
function rpcBuyCurrency(ctx, logger, nk, payload) {
|
||||
if (!ctx.userId) throw new Error("Not authenticated");
|
||||
|
||||
var request = JSON.parse(payload);
|
||||
var packageId = request.package_id;
|
||||
|
||||
var changeset = { "gold": 0, "star": 0 };
|
||||
|
||||
if (packageId === "gold_pack_1") changeset["gold"] = 1000;
|
||||
else if (packageId === "gold_pack_2") changeset["gold"] = 5000;
|
||||
else if (packageId === "star_pack_1") changeset["star"] = 100;
|
||||
else if (packageId === "star_pack_2") changeset["star"] = 500;
|
||||
else throw new Error("Invalid package ID");
|
||||
|
||||
try {
|
||||
nk.walletUpdate(ctx.userId, changeset, {}, true);
|
||||
logger.info("User " + ctx.userId + " bought currency package " + packageId);
|
||||
return JSON.stringify({ success: true, package_id: packageId });
|
||||
} catch (e) {
|
||||
logger.error("Currency purchase failed: " + e.message);
|
||||
throw new Error("Currency purchase failed: " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
function rpcPurchaseItem(ctx, logger, nk, payload) {
|
||||
if (!ctx.userId) throw new Error("Not authenticated");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user