@remix-gg/sdk 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,120 +1,67 @@
1
- (() => {
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __moduleCache = /* @__PURE__ */ new WeakMap;
7
- var __toCommonJS = (from) => {
8
- var entry = __moduleCache.get(from), desc;
9
- if (entry)
10
- return entry;
11
- entry = __defProp({}, "__esModule", { value: true });
12
- if (from && typeof from === "object" || typeof from === "function")
13
- __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
14
- get: () => from[key],
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- }));
17
- __moduleCache.set(from, entry);
18
- return entry;
19
- };
20
- var __export = (target, all) => {
21
- for (var name in all)
22
- __defProp(target, name, {
23
- get: all[name],
24
- enumerable: true,
25
- configurable: true,
26
- set: (newValue) => all[name] = () => newValue
27
- });
28
- };
29
-
30
- // src/index.ts
31
- var exports_src = {};
32
- __export(exports_src, {
33
- sdk: () => sdk,
34
- ZERO_SAFE_AREA_INSET: () => ZERO_SAFE_AREA_INSET,
35
- RemixSDK: () => RemixSDK
36
- });
37
- var ZERO_SAFE_AREA_INSET = {
38
- top: 0,
39
- right: 0,
40
- bottom: 0,
41
- left: 0
42
- };
43
-
44
- class RemixSDK {
45
- isClient;
46
- target = null;
47
- eventListeners = new Map;
48
- readyPromiseResolve = null;
49
- purchasePromiseResolve = null;
50
- _gameInfo;
51
- _gameState;
52
- constructor() {
53
- this.isClient = typeof window !== "undefined";
54
- this.target = this.isClient ? window.parent : null;
55
- if (this.isClient) {
56
- window.addEventListener("message", this.handleMessage);
57
- window.addEventListener("error", this.handleGlobalError);
58
- window.addEventListener("unhandledrejection", this.handleUnhandledRejection);
59
- this.sendMessage("ready", undefined);
60
- }
61
- }
62
- on(eventType, callback) {
63
- if (!this.eventListeners.has(eventType)) {
64
- this.eventListeners.set(eventType, new Set);
65
- }
66
- this.eventListeners.get(eventType)?.add(callback);
67
- }
68
- off(eventType, callback) {
69
- this.eventListeners.get(eventType)?.delete(callback);
70
- }
71
- onPlayAgain(callback) {
72
- this.on("play_again", () => callback());
73
- }
74
- onToggleMute(callback) {
75
- this.on("toggle_mute", (data) => callback(data));
76
- }
77
- onGameStateUpdated(callback) {
78
- this.on("game_state_updated", (data) => callback(data));
79
- }
80
- onGameInfo(callback) {
81
- this.on("game_info", (data) => callback(data));
82
- }
83
- onPurchaseComplete(callback) {
84
- this.on("purchase_complete", (data) => callback(data));
85
- }
86
- setTarget(target) {
87
- this.target = target;
88
- }
89
- get purchasedItems() {
90
- return this._gameInfo?.player.purchasedItems || [];
91
- }
92
- get inventory() {
93
- const counts = new Map;
94
- for (const item of this.purchasedItems) {
95
- counts.set(item, (counts.get(item) || 0) + 1);
96
- }
97
- return [...counts.entries()].map(([slug, quantity]) => ({ slug, quantity }));
98
- }
99
- get shopItems() {
100
- return this._gameInfo?.shopItems || [];
101
- }
102
- get gameInfo() {
103
- return this._gameInfo;
104
- }
105
- get gameState() {
106
- return this._gameState;
107
- }
108
- get players() {
109
- return this._gameInfo?.players;
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
110
19
  }
111
- get player() {
112
- return this._gameInfo?.player;
113
- }
114
- get isReady() {
115
- return !!this._gameInfo;
116
- }
117
- ready = () => {
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+
37
+ // src/index.ts
38
+ var src_exports = {};
39
+ __export(src_exports, {
40
+ RemixSDK: () => RemixSDK,
41
+ ZERO_SAFE_AREA_INSET: () => ZERO_SAFE_AREA_INSET,
42
+ sdk: () => sdk
43
+ });
44
+ module.exports = __toCommonJS(src_exports);
45
+ var ZERO_SAFE_AREA_INSET = {
46
+ top: 0,
47
+ right: 0,
48
+ bottom: 0,
49
+ left: 0
50
+ };
51
+ var SDK_VERSION = true ? "0.8.2" : UNKNOWN_SDK_VERSION;
52
+ var RemixSDK = class {
53
+ constructor() {
54
+ /**
55
+ * The published version of this bundle. The host compares it against the
56
+ * version it asked the browser to load, so a cached bundle from a previous
57
+ * release can be detected before any game code runs.
58
+ */
59
+ this.version = SDK_VERSION;
60
+ this.target = null;
61
+ this.eventListeners = /* @__PURE__ */ new Map();
62
+ this.readyPromiseResolve = null;
63
+ this.purchasePromiseResolve = null;
64
+ this.ready = () => {
118
65
  if (this._gameInfo) {
119
66
  return Promise.resolve(this._gameInfo);
120
67
  }
@@ -122,29 +69,29 @@
122
69
  this.readyPromiseResolve = resolve;
123
70
  });
124
71
  };
125
- purchase = (data) => {
72
+ this.purchase = (data) => {
126
73
  this.sendMessage("purchase", data);
127
74
  return new Promise((resolve) => {
128
75
  this.purchasePromiseResolve = resolve;
129
76
  });
130
77
  };
131
- reportError = (data) => {
78
+ this.reportError = (data) => {
132
79
  this.sendMessage("error", data);
133
80
  };
134
- hapticFeedback = (type) => {
135
- this.sendMessage("haptic_feedback", type ? { type } : undefined);
81
+ this.hapticFeedback = (type) => {
82
+ this.sendMessage("haptic_feedback", type ? { type } : void 0);
136
83
  };
137
- hasItem = (item) => {
84
+ this.hasItem = (item) => {
138
85
  return this.getItemPurchaseCount(item) > 0;
139
86
  };
140
- getItemPurchaseCount = (item) => {
87
+ this.getItemPurchaseCount = (item) => {
141
88
  const inventoryItem = this.inventory.find((entry) => entry.slug === item);
142
- return inventoryItem?.quantity || 0;
89
+ return (inventoryItem == null ? void 0 : inventoryItem.quantity) || 0;
143
90
  };
144
- getShopItem = (slug) => {
91
+ this.getShopItem = (slug) => {
145
92
  return this.shopItems.find((item) => item.slug === slug);
146
93
  };
147
- singlePlayer = {
94
+ this.singlePlayer = {
148
95
  actions: {
149
96
  ready: this.ready,
150
97
  hapticFeedback: this.hapticFeedback,
@@ -158,9 +105,8 @@
158
105
  }
159
106
  }
160
107
  };
161
- multiplayer = {
162
- actions: {
163
- ...this.singlePlayer.actions,
108
+ this.multiplayer = {
109
+ actions: __spreadProps(__spreadValues({}, this.singlePlayer.actions), {
164
110
  gameOver: (data) => {
165
111
  this.sendMessage("multiplayer_game_over", data);
166
112
  },
@@ -170,54 +116,21 @@
170
116
  saveGameState: (data) => {
171
117
  this.sendMessage("multiplayer_save_game_state", data);
172
118
  }
173
- }
119
+ })
174
120
  };
175
- emit(eventType, data) {
176
- if (eventType === "game_info") {
177
- const eventData = data;
178
- this._gameInfo = eventData;
179
- if (!this._gameState && eventData.initialGameState) {
180
- this._gameState = eventData.initialGameState.gameState;
181
- }
182
- if (this.readyPromiseResolve) {
183
- this.readyPromiseResolve(this._gameInfo);
184
- this.readyPromiseResolve = null;
185
- }
186
- }
187
- if (eventType === "purchase_complete") {
188
- const eventData = data;
189
- if (eventData.success && eventData.item && this._gameInfo?.player) {
190
- this.applyPurchaseToCurrentPlayer(eventData.item);
191
- }
192
- if (this.purchasePromiseResolve) {
193
- this.purchasePromiseResolve(eventData);
194
- this.purchasePromiseResolve = null;
195
- }
196
- }
197
- if (eventType === "game_state_updated") {
198
- const eventData = data;
199
- if (eventData) {
200
- this._gameState = eventData.gameState;
201
- } else {
202
- this._gameState = null;
203
- }
204
- }
205
- for (const callback of this.eventListeners.get(eventType) || []) {
206
- callback(data);
207
- }
208
- }
209
- handleMessage = (event) => {
210
- if (event.data?.type !== "game_event")
121
+ this.handleMessage = (event) => {
122
+ var _a;
123
+ if (((_a = event.data) == null ? void 0 : _a.type) !== "game_event")
211
124
  return;
212
125
  this.emit(event.data.event.type, event.data.event.data);
213
126
  };
214
- sendMessage = (type, data) => {
127
+ this.sendMessage = (type, data) => {
215
128
  if (!this.isClient || !this.target)
216
129
  return;
217
130
  const gameEvent = { type: "game_event", event: { type, data } };
218
131
  this.target.postMessage(gameEvent, "*");
219
132
  };
220
- handleGlobalError = (event) => {
133
+ this.handleGlobalError = (event) => {
221
134
  this.sendMessage("error", {
222
135
  message: event.message || "Unknown error",
223
136
  source: event.filename,
@@ -226,33 +139,192 @@
226
139
  error: event.error
227
140
  });
228
141
  };
229
- handleUnhandledRejection = (event) => {
142
+ this.handleUnhandledRejection = (event) => {
230
143
  const error = event.reason instanceof Error ? event.reason : new Error(String(event.reason));
231
144
  this.sendMessage("error", {
232
145
  message: error.message,
233
146
  error
234
147
  });
235
148
  };
236
- applyPurchaseToCurrentPlayer(item) {
237
- if (!this._gameInfo)
238
- return;
239
- const currentPlayer = this._gameInfo.player;
240
- currentPlayer.purchasedItems = [...currentPlayer.purchasedItems, item];
241
- this._gameInfo.players = this._gameInfo.players.map((player) => {
242
- if (player.id !== currentPlayer.id)
243
- return player;
244
- return {
245
- ...player,
246
- purchasedItems: currentPlayer.purchasedItems
247
- };
248
- });
149
+ this.isClient = typeof window !== "undefined";
150
+ this.target = this.isClient ? window.parent : null;
151
+ if (this.isClient) {
152
+ window.addEventListener("message", this.handleMessage);
153
+ window.addEventListener("error", this.handleGlobalError);
154
+ window.addEventListener("unhandledrejection", this.handleUnhandledRejection);
155
+ this.sendMessage("ready", void 0);
249
156
  }
250
157
  }
251
- var sdk = new RemixSDK;
252
- if (typeof window !== "undefined") {
253
- window.FarcadeSDK = sdk;
254
- window.RemixSDK = sdk;
158
+ on(eventType, callback) {
159
+ var _a;
160
+ if (!this.eventListeners.has(eventType)) {
161
+ this.eventListeners.set(eventType, /* @__PURE__ */ new Set());
162
+ }
163
+ (_a = this.eventListeners.get(eventType)) == null ? void 0 : _a.add(callback);
255
164
  }
256
- })();
257
-
258
- //# debugId=5DCD609576A0948564756E2164756E21
165
+ off(eventType, callback) {
166
+ var _a;
167
+ (_a = this.eventListeners.get(eventType)) == null ? void 0 : _a.delete(callback);
168
+ }
169
+ // explicitly named event listeners for better understanding and type safety
170
+ /**
171
+ * Listen for the host starting play: a replay after game over, or — for level
172
+ * games — a host-directed level via `data.levelIndex`. `data` may be omitted
173
+ * for a plain restart.
174
+ */
175
+ onPlay(callback) {
176
+ this.on("play_again", (data) => callback(data));
177
+ }
178
+ /** Alias of {@link onPlay}. */
179
+ onPlayAgain(callback) {
180
+ this.onPlay(callback);
181
+ }
182
+ onToggleMute(callback) {
183
+ this.on("toggle_mute", (data) => callback(data));
184
+ }
185
+ onGameStateUpdated(callback) {
186
+ this.on("game_state_updated", (data) => callback(data));
187
+ }
188
+ onGameInfo(callback) {
189
+ this.on("game_info", (data) => callback(data));
190
+ }
191
+ onPurchaseComplete(callback) {
192
+ this.on("purchase_complete", (data) => callback(data));
193
+ }
194
+ // end of explicitly named event listeners
195
+ setTarget(target) {
196
+ this.target = target;
197
+ }
198
+ get purchasedItems() {
199
+ var _a;
200
+ return ((_a = this._gameInfo) == null ? void 0 : _a.player.purchasedItems) || [];
201
+ }
202
+ get inventory() {
203
+ const counts = /* @__PURE__ */ new Map();
204
+ for (const item of this.purchasedItems) {
205
+ counts.set(item, (counts.get(item) || 0) + 1);
206
+ }
207
+ return [...counts.entries()].map(([slug, quantity]) => ({ slug, quantity }));
208
+ }
209
+ get shopItems() {
210
+ var _a;
211
+ return ((_a = this._gameInfo) == null ? void 0 : _a.shopItems) || [];
212
+ }
213
+ get gameInfo() {
214
+ return this._gameInfo;
215
+ }
216
+ get gameState() {
217
+ return this._gameState;
218
+ }
219
+ get players() {
220
+ var _a;
221
+ return (_a = this._gameInfo) == null ? void 0 : _a.players;
222
+ }
223
+ get player() {
224
+ var _a;
225
+ return (_a = this._gameInfo) == null ? void 0 : _a.player;
226
+ }
227
+ get isReady() {
228
+ return !!this._gameInfo;
229
+ }
230
+ /** True when the host configured this game with a fixed level count. */
231
+ get isLevelBased() {
232
+ var _a, _b;
233
+ const levelCount = (_b = (_a = this._gameInfo) == null ? void 0 : _a.levelBased) == null ? void 0 : _b.levelCount;
234
+ return levelCount != null && levelCount > 0;
235
+ }
236
+ /** Total levels for level-based games (from host metadata). */
237
+ get levelCount() {
238
+ var _a, _b;
239
+ const levelCount = (_b = (_a = this._gameInfo) == null ? void 0 : _a.levelBased) == null ? void 0 : _b.levelCount;
240
+ return levelCount != null && levelCount > 0 ? levelCount : void 0;
241
+ }
242
+ /** 1-based level index from score-derived progression, defaulting to 1. */
243
+ get currentLevelIndex() {
244
+ var _a, _b, _c;
245
+ const value = (_c = (_b = (_a = this._gameInfo) == null ? void 0 : _a.levelBased) == null ? void 0 : _b.progress) == null ? void 0 : _c.currentLevelIndex;
246
+ return typeof value === "number" && Number.isFinite(value) && value >= 1 ? value : 1;
247
+ }
248
+ /** Best stars per level from score-derived progression. Keys are 1-based level indices. */
249
+ get levelStars() {
250
+ var _a, _b, _c;
251
+ const raw = (_c = (_b = (_a = this._gameInfo) == null ? void 0 : _a.levelBased) == null ? void 0 : _b.progress) == null ? void 0 : _c.levelStars;
252
+ const levelStars = {};
253
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
254
+ for (const [key, starValue] of Object.entries(raw)) {
255
+ if (starValue === 1 || starValue === 2 || starValue === 3)
256
+ levelStars[key] = starValue;
257
+ }
258
+ }
259
+ return levelStars;
260
+ }
261
+ /** Highest unlocked level from score-derived progression, defaulting to 1. */
262
+ get highestUnlockedLevel() {
263
+ var _a, _b, _c;
264
+ const value = (_c = (_b = (_a = this._gameInfo) == null ? void 0 : _a.levelBased) == null ? void 0 : _b.progress) == null ? void 0 : _c.highestUnlockedLevel;
265
+ return typeof value === "number" && Number.isFinite(value) && value >= 1 ? value : 1;
266
+ }
267
+ /** Sum of best stars across all completed levels. */
268
+ get totalStars() {
269
+ return Object.values(this.levelStars).reduce((sum, stars) => sum + stars, 0);
270
+ }
271
+ emit(eventType, data) {
272
+ var _a;
273
+ if (eventType === "game_info") {
274
+ const eventData = data;
275
+ this._gameInfo = eventData;
276
+ if (!this._gameState && eventData.initialGameState) {
277
+ this._gameState = eventData.initialGameState.gameState;
278
+ }
279
+ if (this.readyPromiseResolve) {
280
+ this.readyPromiseResolve(this._gameInfo);
281
+ this.readyPromiseResolve = null;
282
+ }
283
+ }
284
+ if (eventType === "purchase_complete") {
285
+ const eventData = data;
286
+ if (eventData.success && eventData.item && ((_a = this._gameInfo) == null ? void 0 : _a.player)) {
287
+ this.applyPurchaseToCurrentPlayer(eventData.item);
288
+ }
289
+ if (this.purchasePromiseResolve) {
290
+ this.purchasePromiseResolve(eventData);
291
+ this.purchasePromiseResolve = null;
292
+ }
293
+ }
294
+ if (eventType === "game_state_updated") {
295
+ const eventData = data;
296
+ if (eventData) {
297
+ this._gameState = eventData.gameState;
298
+ } else {
299
+ this._gameState = null;
300
+ }
301
+ }
302
+ for (const callback of this.eventListeners.get(eventType) || []) {
303
+ callback(data);
304
+ }
305
+ }
306
+ applyPurchaseToCurrentPlayer(item) {
307
+ if (!this._gameInfo)
308
+ return;
309
+ const currentPlayer = this._gameInfo.player;
310
+ currentPlayer.purchasedItems = [...currentPlayer.purchasedItems, item];
311
+ this._gameInfo.players = this._gameInfo.players.map((player) => {
312
+ if (player.id !== currentPlayer.id)
313
+ return player;
314
+ return __spreadProps(__spreadValues({}, player), {
315
+ purchasedItems: currentPlayer.purchasedItems
316
+ });
317
+ });
318
+ }
319
+ };
320
+ var sdk = new RemixSDK();
321
+ if (typeof window !== "undefined") {
322
+ window.FarcadeSDK = sdk;
323
+ window.RemixSDK = sdk;
324
+ }
325
+ // Annotate the CommonJS export names for ESM import in node:
326
+ 0 && (module.exports = {
327
+ RemixSDK,
328
+ ZERO_SAFE_AREA_INSET,
329
+ sdk
330
+ });
package/dist/index.min.js CHANGED
@@ -40,8 +40,10 @@
40
40
  bottom: 0,
41
41
  left: 0
42
42
  };
43
+ var SDK_VERSION = "0.8.2";
43
44
 
44
45
  class RemixSDK {
46
+ version = SDK_VERSION;
45
47
  isClient;
46
48
  target = null;
47
49
  eventListeners = new Map;
@@ -68,8 +70,11 @@
68
70
  off(eventType, callback) {
69
71
  this.eventListeners.get(eventType)?.delete(callback);
70
72
  }
73
+ onPlay(callback) {
74
+ this.on("play_again", (data) => callback(data));
75
+ }
71
76
  onPlayAgain(callback) {
72
- this.on("play_again", () => callback());
77
+ this.onPlay(callback);
73
78
  }
74
79
  onToggleMute(callback) {
75
80
  this.on("toggle_mute", (data) => callback(data));
@@ -114,6 +119,36 @@
114
119
  get isReady() {
115
120
  return !!this._gameInfo;
116
121
  }
122
+ get isLevelBased() {
123
+ const levelCount = this._gameInfo?.levelBased?.levelCount;
124
+ return levelCount != null && levelCount > 0;
125
+ }
126
+ get levelCount() {
127
+ const levelCount = this._gameInfo?.levelBased?.levelCount;
128
+ return levelCount != null && levelCount > 0 ? levelCount : undefined;
129
+ }
130
+ get currentLevelIndex() {
131
+ const value = this._gameInfo?.levelBased?.progress?.currentLevelIndex;
132
+ return typeof value === "number" && Number.isFinite(value) && value >= 1 ? value : 1;
133
+ }
134
+ get levelStars() {
135
+ const raw = this._gameInfo?.levelBased?.progress?.levelStars;
136
+ const levelStars = {};
137
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
138
+ for (const [key, starValue] of Object.entries(raw)) {
139
+ if (starValue === 1 || starValue === 2 || starValue === 3)
140
+ levelStars[key] = starValue;
141
+ }
142
+ }
143
+ return levelStars;
144
+ }
145
+ get highestUnlockedLevel() {
146
+ const value = this._gameInfo?.levelBased?.progress?.highestUnlockedLevel;
147
+ return typeof value === "number" && Number.isFinite(value) && value >= 1 ? value : 1;
148
+ }
149
+ get totalStars() {
150
+ return Object.values(this.levelStars).reduce((sum, stars) => sum + stars, 0);
151
+ }
117
152
  ready = () => {
118
153
  if (this._gameInfo) {
119
154
  return Promise.resolve(this._gameInfo);
@@ -255,4 +290,4 @@
255
290
  }
256
291
  })();
257
292
 
258
- //# debugId=5C4052FCC20A9CB164756E2164756E21
293
+ //# debugId=806FC7E16EA1A87164756E2164756E21