@inappstory/game-center-api 1.2.1 → 1.2.3

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/README.md CHANGED
@@ -30,16 +30,72 @@ createSdkApi({
30
30
  onBackGesture: () => {
31
31
  /* Call on Android back gesture */
32
32
  },
33
+ gameShouldForeground: () => {
34
+ /* splash animation finished, now we can start bg music and etc */
35
+ },
33
36
  });
34
37
  ```
35
38
 
36
39
  ## Game first render
37
40
 
38
41
  ```tsx
42
+ const gameLoaded = () => {
43
+ GameCenterApi.gameLoadedSdkCallback();
44
+ };
45
+
46
+ const gameStarted = () => {
47
+ setTimeout(() => {
48
+ // Wait for render complete - then remove native loader screen
49
+ GameCenterApi.gameShouldForegroundCallback();
50
+ }, 50);
51
+ };
52
+
53
+ const gameShouldForeground = () => {
54
+ // splash animation finished, now we can start bg music and etc
55
+ gameStarted();
56
+ };
57
+
39
58
  // calling gameLoadedSdkCallback removes the loading screen
40
59
  import { gameLoadedSdkCallback } from "@inappstory/game-center-api";
41
60
 
42
61
  const rootElement = document.getElementById("root");
43
62
  const root = createRoot(rootElement!);
44
- root.render(<AppWithCallbackAfterRender cb={() => gameLoadedSdkCallback()} />);
63
+ root.render(<AppWithCallbackAfterRender cb={gameLoaded} />);
64
+ ```
65
+
66
+ ## Get user ID
67
+
68
+ ```ts
69
+ import GameCenterApi from "@inappstory/game-center-api";
70
+ // call only after mounted event (from GameCenterApi.createSdkApi
71
+ const getUserId = () => GameCenterApi.gameLaunchConfig.clientConfig.userId;
72
+ ```
73
+
74
+ ## Get placeholder
75
+
76
+ ```ts
77
+ import GameCenterApi, { type PlaceholderType, Placeholder } from "@inappstory/game-center-api";
78
+ // call only after mounted event (from GameCenterApi.createSdkApi
79
+ const getPlaceholder = (name: string, type: PlaceholderType): Placeholder | undefined => {
80
+ return GameCenterApi.gameLaunchConfig.clientConfig.placeholders.find(placeholder => placeholder.name === name && placeholder.type === type);
81
+ };
82
+
83
+ const avatarUrl = getPlaceholder("avatar", PlaceholderType.IMAGE);
84
+ ```
85
+
86
+ ## Open url
87
+
88
+ ```ts
89
+ import GameCenterApi from "@inappstory/game-center-api";
90
+
91
+ // closeGameReader - close or not GameReader
92
+ const openUrl = (url: string) => GameCenterApi.openUrl({ url, closeGameReader: true });
93
+ ```
94
+
95
+ ## Close game
96
+
97
+ ```ts
98
+ import GameCenterApi from "@inappstory/game-center-api";
99
+
100
+ const closeGame = () => GameCenterApi.closeGameReader();
45
101
  ```
package/lib/env.d.ts CHANGED
@@ -14,4 +14,12 @@ declare let iosMh: Record<string, any>;
14
14
  declare const isDev: boolean;
15
15
  declare const getSdkVersion: () => string | false | null;
16
16
  declare const getSemverSdkVersion: () => false | SemVer | null;
17
- export { isIos, isWeb, isAndroid, iosMh, isDev, getSdkVersion, getSemverSdkVersion };
17
+ /**
18
+ * Get host application build version
19
+ */
20
+ declare const getApplicationBuildVersion: () => number | null;
21
+ /**
22
+ * Get host application sem version
23
+ */
24
+ declare const getApplicationVersion: () => string | null;
25
+ export { isIos, isWeb, isAndroid, iosMh, isDev, getSdkVersion, getSemverSdkVersion, getApplicationBuildVersion, getApplicationVersion };
package/lib/env.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.iosMh = exports.isAndroid = exports.isWeb = exports.isIos = void 0;
3
+ exports.getApplicationVersion = exports.getApplicationBuildVersion = exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.iosMh = exports.isAndroid = exports.isWeb = exports.isIos = void 0;
4
4
  const gameLaunchConfig_1 = require("./gameLaunchConfig");
5
5
  const semver = require("semver");
6
6
  const isAndroid = Boolean(window.Android && window.Android.gameLoaded);
@@ -45,3 +45,36 @@ const getSemverSdkVersion = () => {
45
45
  return semverSdkVersion;
46
46
  };
47
47
  exports.getSemverSdkVersion = getSemverSdkVersion;
48
+ /**
49
+ * For match app build version and app sem version from user agent
50
+ * InAppStorySDK/750 Dalvik/2.1.0 (Linux; U; Android 11; XQ-AT51 Build/58.1.A.5.530) Application/258 (com.inappstory.android 3.1.0)
51
+ */
52
+ const appVersionFromUserAgentRegexp = /.*Application\/(\d+)\s\([a-zA-Z.]+\s([0-9.]+)\)/;
53
+ /**
54
+ * Get host application build version
55
+ */
56
+ const getApplicationBuildVersion = () => {
57
+ const userAgent = gameLaunchConfig_1.gameLaunchConfig.clientConfig.userAgent;
58
+ if (userAgent) {
59
+ const match = userAgent.match(appVersionFromUserAgentRegexp);
60
+ if (match && match[1]) {
61
+ return parseInt(match[1]);
62
+ }
63
+ }
64
+ return null;
65
+ };
66
+ exports.getApplicationBuildVersion = getApplicationBuildVersion;
67
+ /**
68
+ * Get host application sem version
69
+ */
70
+ const getApplicationVersion = () => {
71
+ const userAgent = gameLaunchConfig_1.gameLaunchConfig.clientConfig.userAgent;
72
+ if (userAgent) {
73
+ const match = userAgent.match(appVersionFromUserAgentRegexp);
74
+ if (match && match[2]) {
75
+ return match[2];
76
+ }
77
+ }
78
+ return null;
79
+ };
80
+ exports.getApplicationVersion = getApplicationVersion;
package/lib/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { getSdkVersion, getSemverSdkVersion, isAndroid, isDev, isIos, isWeb } from "./env";
1
+ import { getApplicationBuildVersion, getApplicationVersion, getSdkVersion, getSemverSdkVersion, isAndroid, isDev, isIos, isWeb } from "./env";
2
2
  import { gameLaunchConfig, getIsDemoMode, getSessionId, getApiBaseUrl } from "./gameLaunchConfig";
3
3
  import { getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilyStylesheet, dynamicResourceAssets, dynamicResourceFonts, staticResourcesImagePlaceholders, StaticResourceList } from "./gameResources";
4
4
  import { sendIasApiRequest } from "./iasApi";
5
5
  import { gameLocalData } from "./localData";
6
6
  import { closeGameReader, createSdkApi, reloadGameReader } from "./sdkApi";
7
- import { gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameShouldForegroundCallback, isSdkSupportGameShouldForegroundCallback, gameOnForeground } from "./sdkApi/initGame";
7
+ import { gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameShouldForegroundCallback, gameOnForeground } from "./sdkApi/initGame";
8
8
  import { openUrl } from "./sdkApi/openUrl";
9
9
  import { share } from "./sdkApi/share";
10
10
  import { vibrate } from "./sdkApi/vibrate";
@@ -23,7 +23,7 @@ export type { RequestInfo as APIRequestInfo, Response as APIResponse } from "./i
23
23
  export type { Placeholder, GameLaunchConfig };
24
24
  export type { ProjectFontFamily } from "./gameResources";
25
25
  export type { ResourceInterface } from "./ResourceManager";
26
- export { createSdkApi, closeGameReader, gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameLaunchConfig, isIos, isWeb, isAndroid, isDev, getSdkVersion, getSemverSdkVersion, gameLocalData, sendIasApiRequest, openUrl, share, vibrate, getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilyStylesheet, getIsDemoMode, getSessionId, getApiBaseUrl, ScreenOrientation, PlaceholderType, fetchLocalFile, openStory, ResourceManager, dynamicResourceAssets, dynamicResourceFonts, staticResourcesImagePlaceholders, StaticResourceList, eventGame, reloadGameReader, openFilePicker, FilePickerResultType, isFilePickerResultFileList, isFilePickerResultLocalFileList, isLocalFile, hasFilePickerApi, gameShouldForegroundCallback, isSdkSupportGameShouldForegroundCallback, gameOnForeground, };
26
+ export { createSdkApi, closeGameReader, gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameLaunchConfig, isIos, isWeb, isAndroid, isDev, getSdkVersion, getSemverSdkVersion, gameLocalData, sendIasApiRequest, openUrl, share, vibrate, getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilyStylesheet, getIsDemoMode, getSessionId, getApiBaseUrl, ScreenOrientation, PlaceholderType, fetchLocalFile, openStory, ResourceManager, dynamicResourceAssets, dynamicResourceFonts, staticResourcesImagePlaceholders, StaticResourceList, eventGame, reloadGameReader, openFilePicker, FilePickerResultType, isFilePickerResultFileList, isFilePickerResultLocalFileList, isLocalFile, hasFilePickerApi, gameShouldForegroundCallback, gameOnForeground, getApplicationVersion, getApplicationBuildVersion, };
27
27
  declare const GameCenterApi: {
28
28
  createSdkApi: ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, gameShouldForeground, }: Partial<{
29
29
  mounted: () => void;
@@ -37,10 +37,7 @@ declare const GameCenterApi: {
37
37
  gameShouldForeground: () => void;
38
38
  }>) => void;
39
39
  closeGameReader: (data?: import("./sdkApi").CloseGameReaderOptions | undefined) => void;
40
- gameLoadedSdkCallback: (config?: Partial<Partial<{
41
- showClose: boolean;
42
- backGesture: boolean;
43
- }>> | undefined) => void;
40
+ gameLoadedSdkCallback: () => void;
44
41
  gameLoadFailedSdkCallback: (reason: string, canTryReload: boolean) => void;
45
42
  gameLaunchConfig: GameLaunchConfig;
46
43
  isIos: any;
@@ -90,7 +87,8 @@ declare const GameCenterApi: {
90
87
  showClose: boolean;
91
88
  backGesture: boolean;
92
89
  }>> | undefined) => void;
93
- isSdkSupportGameShouldForegroundCallback: () => boolean | undefined;
94
90
  gameOnForeground: Promise<void>;
91
+ getApplicationVersion: () => string | null;
92
+ getApplicationBuildVersion: () => number | null;
95
93
  };
96
94
  export { GameCenterApi as default };
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = exports.gameOnForeground = exports.isSdkSupportGameShouldForegroundCallback = exports.gameShouldForegroundCallback = exports.hasFilePickerApi = exports.isLocalFile = exports.isFilePickerResultLocalFileList = exports.isFilePickerResultFileList = exports.FilePickerResultType = exports.openFilePicker = exports.reloadGameReader = exports.eventGame = exports.StaticResourceList = exports.staticResourcesImagePlaceholders = exports.dynamicResourceFonts = exports.dynamicResourceAssets = exports.ResourceManager = exports.openStory = exports.fetchLocalFile = exports.PlaceholderType = exports.ScreenOrientation = exports.getApiBaseUrl = exports.getSessionId = exports.getIsDemoMode = exports.getProjectFontFamilyStylesheet = exports.getDynamicResourceFont = exports.getDynamicResourceAsset = exports.vibrate = exports.share = exports.openUrl = exports.sendIasApiRequest = exports.gameLocalData = exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.isAndroid = exports.isWeb = exports.isIos = exports.gameLaunchConfig = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.closeGameReader = exports.createSdkApi = void 0;
3
+ exports.default = exports.getApplicationBuildVersion = exports.getApplicationVersion = exports.gameOnForeground = exports.gameShouldForegroundCallback = exports.hasFilePickerApi = exports.isLocalFile = exports.isFilePickerResultLocalFileList = exports.isFilePickerResultFileList = exports.FilePickerResultType = exports.openFilePicker = exports.reloadGameReader = exports.eventGame = exports.StaticResourceList = exports.staticResourcesImagePlaceholders = exports.dynamicResourceFonts = exports.dynamicResourceAssets = exports.ResourceManager = exports.openStory = exports.fetchLocalFile = exports.PlaceholderType = exports.ScreenOrientation = exports.getApiBaseUrl = exports.getSessionId = exports.getIsDemoMode = exports.getProjectFontFamilyStylesheet = exports.getDynamicResourceFont = exports.getDynamicResourceAsset = exports.vibrate = exports.share = exports.openUrl = exports.sendIasApiRequest = exports.gameLocalData = exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.isAndroid = exports.isWeb = exports.isIos = exports.gameLaunchConfig = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.closeGameReader = exports.createSdkApi = void 0;
4
4
  const env_1 = require("./env");
5
+ Object.defineProperty(exports, "getApplicationBuildVersion", { enumerable: true, get: function () { return env_1.getApplicationBuildVersion; } });
6
+ Object.defineProperty(exports, "getApplicationVersion", { enumerable: true, get: function () { return env_1.getApplicationVersion; } });
5
7
  Object.defineProperty(exports, "getSdkVersion", { enumerable: true, get: function () { return env_1.getSdkVersion; } });
6
8
  Object.defineProperty(exports, "getSemverSdkVersion", { enumerable: true, get: function () { return env_1.getSemverSdkVersion; } });
7
9
  Object.defineProperty(exports, "isAndroid", { enumerable: true, get: function () { return env_1.isAndroid; } });
@@ -33,7 +35,6 @@ const initGame_1 = require("./sdkApi/initGame");
33
35
  Object.defineProperty(exports, "gameLoadedSdkCallback", { enumerable: true, get: function () { return initGame_1.gameLoadedSdkCallback; } });
34
36
  Object.defineProperty(exports, "gameLoadFailedSdkCallback", { enumerable: true, get: function () { return initGame_1.gameLoadFailedSdkCallback; } });
35
37
  Object.defineProperty(exports, "gameShouldForegroundCallback", { enumerable: true, get: function () { return initGame_1.gameShouldForegroundCallback; } });
36
- Object.defineProperty(exports, "isSdkSupportGameShouldForegroundCallback", { enumerable: true, get: function () { return initGame_1.isSdkSupportGameShouldForegroundCallback; } });
37
38
  Object.defineProperty(exports, "gameOnForeground", { enumerable: true, get: function () { return initGame_1.gameOnForeground; } });
38
39
  const openUrl_1 = require("./sdkApi/openUrl");
39
40
  Object.defineProperty(exports, "openUrl", { enumerable: true, get: function () { return openUrl_1.openUrl; } });
@@ -101,8 +102,9 @@ const GameCenterApi = {
101
102
  isLocalFile: filePicker_h_1.isLocalFile,
102
103
  hasFilePickerApi: filePicker_1.hasFilePickerApi,
103
104
  gameShouldForegroundCallback: initGame_1.gameShouldForegroundCallback,
104
- isSdkSupportGameShouldForegroundCallback: initGame_1.isSdkSupportGameShouldForegroundCallback,
105
105
  gameOnForeground: initGame_1.gameOnForeground,
106
+ getApplicationVersion: env_1.getApplicationVersion,
107
+ getApplicationBuildVersion: env_1.getApplicationBuildVersion,
106
108
  };
107
109
  exports.default = GameCenterApi;
108
110
  // export { Placeholder as default};
@@ -27,14 +27,10 @@ type GameReaderInit = {
27
27
  ready: (cb: () => void) => void;
28
28
  };
29
29
  export declare const createInitGame: (initLocalData: () => Promise<void>, mounted?: () => void) => void;
30
- type GameLoadedSdkConfig = Partial<{
31
- showClose: boolean;
32
- backGesture: boolean;
33
- }>;
34
30
  /**
35
31
  * API method for remove loader screen from Reader
36
32
  */
37
- export declare const gameLoadedSdkCallback: (config?: Partial<GameLoadedSdkConfig>) => void;
33
+ export declare const gameLoadedSdkCallback: () => void;
38
34
  export declare const gameLoadFailedSdkCallback: (reason: string, canTryReload: boolean) => void;
39
35
  export declare const createGameShouldForeground: (gameShouldForeground: () => void) => void;
40
36
  type GameShouldForegroundConfig = Partial<{
@@ -45,6 +41,5 @@ type GameShouldForegroundConfig = Partial<{
45
41
  * API method for remove loader screen from Reader
46
42
  */
47
43
  export declare const gameShouldForegroundCallback: (config?: Partial<GameShouldForegroundConfig>) => void;
48
- export declare const isSdkSupportGameShouldForegroundCallback: () => boolean | undefined;
49
44
  export declare const gameOnForeground: Promise<void>;
50
45
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.gameOnForeground = exports.isSdkSupportGameShouldForegroundCallback = exports.gameShouldForegroundCallback = exports.createGameShouldForeground = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.createInitGame = void 0;
3
+ exports.gameOnForeground = exports.gameShouldForegroundCallback = exports.createGameShouldForeground = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.createInitGame = void 0;
4
4
  const isObject_1 = require("../helpers/isObject");
5
5
  const gameLaunchConfig_1 = require("../gameLaunchConfig");
6
6
  const env_1 = require("../env");
@@ -138,7 +138,17 @@ exports.createInitGame = createInitGame;
138
138
  /**
139
139
  * API method for remove loader screen from Reader
140
140
  */
141
- const gameLoadedSdkCallback = (config) => {
141
+ const gameLoadedSdkCallback = () => {
142
+ if (isSdkSupportGameShouldForegroundCallback()) {
143
+ gameLoadedSdkCallbackInternal();
144
+ }
145
+ else {
146
+ /** Old sdk - call shouldForeground, emulate new sdk */
147
+ window.gameShouldForeground();
148
+ }
149
+ };
150
+ exports.gameLoadedSdkCallback = gameLoadedSdkCallback;
151
+ const gameLoadedSdkCallbackInternal = (config) => {
142
152
  window.gameLoadingInfo.state = "before call gameLoadedSdkCallback";
143
153
  window.gameLoadingInfo.description = "";
144
154
  try {
@@ -152,7 +162,7 @@ const gameLoadedSdkCallback = (config) => {
152
162
  }
153
163
  if (env_1.isAndroid) {
154
164
  if (window.Android.gameLoaded !== undefined) {
155
- if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
165
+ if (isSdkSupportGameShouldForegroundCallback()) {
156
166
  window.Android.gameLoaded();
157
167
  }
158
168
  else {
@@ -163,7 +173,7 @@ const gameLoadedSdkCallback = (config) => {
163
173
  }
164
174
  else if (env_1.isIos) {
165
175
  if (env_1.iosMh.gameLoaded !== undefined) {
166
- if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
176
+ if (isSdkSupportGameShouldForegroundCallback()) {
167
177
  env_1.iosMh.gameLoaded.postMessage("");
168
178
  }
169
179
  else {
@@ -174,7 +184,7 @@ const gameLoadedSdkCallback = (config) => {
174
184
  }
175
185
  else if (env_1.isWeb) {
176
186
  if (Source_1.webSource.sourceWindow && Source_1.webSource.sourceWindowOrigin) {
177
- if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
187
+ if (isSdkSupportGameShouldForegroundCallback()) {
178
188
  Source_1.webSource.sourceWindow.postMessage(["gameLoaded"], Source_1.webSource.sourceWindowOrigin);
179
189
  }
180
190
  else {
@@ -192,7 +202,7 @@ const gameLoadedSdkCallback = (config) => {
192
202
  window.gameLoadingInfo.state = "after call gameLoadedSdkCallback";
193
203
  window.gameLoadingInfo.description = "";
194
204
  window.gameLoadingInfo.loaded = true;
195
- if (!(0, exports.isSdkSupportGameShouldForegroundCallback)()) {
205
+ if (!isSdkSupportGameShouldForegroundCallback()) {
196
206
  gameOnForegroundResolve();
197
207
  }
198
208
  }
@@ -206,7 +216,6 @@ const gameLoadedSdkCallback = (config) => {
206
216
  console.error(e);
207
217
  }
208
218
  };
209
- exports.gameLoadedSdkCallback = gameLoadedSdkCallback;
210
219
  window.gameLoadingInfo = {
211
220
  loaded: false,
212
221
  state: "gameReader API created",
@@ -245,6 +254,16 @@ exports.createGameShouldForeground = createGameShouldForeground;
245
254
  * API method for remove loader screen from Reader
246
255
  */
247
256
  const gameShouldForegroundCallback = (config) => {
257
+ if (isSdkSupportGameShouldForegroundCallback()) {
258
+ gameShouldForegroundCallbackInternal(config);
259
+ }
260
+ else {
261
+ /** For old sdk - use gameLoadedSdkCallbackInternal with config (for remove splash) */
262
+ gameLoadedSdkCallbackInternal(config);
263
+ }
264
+ };
265
+ exports.gameShouldForegroundCallback = gameShouldForegroundCallback;
266
+ const gameShouldForegroundCallbackInternal = (config) => {
248
267
  let showClose = config?.showClose;
249
268
  if (showClose == null) {
250
269
  showClose = false;
@@ -276,7 +295,6 @@ const gameShouldForegroundCallback = (config) => {
276
295
  }
277
296
  gameOnForegroundResolve();
278
297
  };
279
- exports.gameShouldForegroundCallback = gameShouldForegroundCallback;
280
298
  const isSdkSupportGameShouldForegroundCallback = () => {
281
299
  if (env_1.isAndroid) {
282
300
  return "gameShouldForegroundCallback" in window.Android;
@@ -296,7 +314,6 @@ const isSdkSupportGameShouldForegroundCallback = () => {
296
314
  return support;
297
315
  }
298
316
  };
299
- exports.isSdkSupportGameShouldForegroundCallback = isSdkSupportGameShouldForegroundCallback;
300
317
  let gameOnForegroundResolve = () => { };
301
318
  let gameOnForegroundReject = () => { };
302
319
  exports.gameOnForeground = new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inappstory/game-center-api",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "",
5
5
  "dependencies": {
6
6
  "semver": "^7.3.8",