@jolibox/implement 1.4.1 → 1.4.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/esbuild.config.js CHANGED
@@ -65,7 +65,7 @@ function build(format) {
65
65
  target: ['es2015', 'safari14'],
66
66
  format: format,
67
67
  minify: true,
68
- // external: ['@jolibox/native-bridge'], // 排除 native-bridge,H5 不需要
68
+ // external: ['@jolibox/native-bridge'],
69
69
  loader: {
70
70
  '.ts': 'ts',
71
71
  '.tsx': 'tsx',
@@ -1,9 +1,9 @@
1
1
  Invoking: npm run clean && npm run build:esm && tsc
2
2
 
3
- > @jolibox/implement@1.4.1 clean
3
+ > @jolibox/implement@1.4.3 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.4.1 build:esm
7
+ > @jolibox/implement@1.4.3 build:esm
8
8
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=esm
9
9
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@jolibox/implement",
3
3
  "description": "This project is Jolibox JS-SDk implement for Native && H5",
4
- "version": "1.4.1",
4
+ "version": "1.4.3",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@jolibox/common": "1.4.1",
10
- "@jolibox/types": "1.4.1",
11
- "@jolibox/native-bridge": "1.4.1",
12
- "@jolibox/ads": "1.4.1",
9
+ "@jolibox/common": "1.4.3",
10
+ "@jolibox/types": "1.4.3",
11
+ "@jolibox/native-bridge": "1.4.3",
12
+ "@jolibox/ads": "1.4.3",
13
13
  "localforage": "1.10.0",
14
- "@jolibox/ui": "1.4.1",
14
+ "@jolibox/ui": "1.4.3",
15
15
  "web-vitals": "4.2.4"
16
16
  },
17
17
  "devDependencies": {
@@ -1,6 +1,6 @@
1
1
  import { BaseError, wrapUserFunction, createCommands } from '@jolibox/common';
2
2
  import { canIUseNative, createAPI, createSyncAPI, registerCanIUse, t } from './base';
3
- import { StandardResponse, ICoinDetailsData } from '@jolibox/types';
3
+ import { StandardResponse, ICoinDetailsData, IGameLeaderboardData } from '@jolibox/types';
4
4
  import { context } from '@/common/context';
5
5
  import { createAPIError } from '@/common/report/errors';
6
6
  import { invokeNative } from '@jolibox/native-bridge';
@@ -233,3 +233,76 @@ commands.registerCommand('Rewards.createRewardsAdsManager', createRewardsAdsMana
233
233
  registerCanIUse('rewards.createRewardsAdsManager', {
234
234
  version: '1.3.4'
235
235
  });
236
+
237
+ const getLeaderBoardData = createAPI('getLeaderBoardData', {
238
+ paramsSchema: t.tuple(
239
+ t.object({
240
+ gameId: t.string(),
241
+ onUpdate: t.function().optional()
242
+ })
243
+ ),
244
+ implement: async (params: {
245
+ gameId: string;
246
+ onUpdate?: (data: IGameLeaderboardData) => void | Promise<void>;
247
+ }): Promise<StandardResponse<IGameLeaderboardData>> => {
248
+ try {
249
+ if (!canIUseNative('requestGameLeaderboardSync')) {
250
+ return {
251
+ code: 'FAILURE',
252
+ message: '[JoliboxSDK]: getLeaderBoardData not supported in this platform.'
253
+ };
254
+ }
255
+
256
+ const customOnUpdate = params.onUpdate
257
+ ? safeCallbackWrapper(params.onUpdate)
258
+ : (() => {
259
+ console.log('default update function called');
260
+ }).bind(this);
261
+
262
+ const { errNo, errMsg, data } = invokeNative('requestGameLeaderboardSync', {
263
+ gameId: params.gameId
264
+ });
265
+
266
+ if (errNo !== 0) {
267
+ throw createAPIError({
268
+ code: errNo ?? -1,
269
+ msg: errMsg
270
+ });
271
+ }
272
+
273
+ invokeNative('requestGameLeaderboardAsync', {
274
+ gameId: params.gameId
275
+ })
276
+ .then((res) => {
277
+ if (res.errNo === 0 && res.data) {
278
+ customOnUpdate(res.data);
279
+ } else {
280
+ throw createAPIError({
281
+ code: res.errNo ?? -1,
282
+ msg: res.errMsg
283
+ });
284
+ }
285
+ })
286
+ .catch((error) => {
287
+ console.error('Failed to fetch leaderboard data asynchronously: ', error);
288
+ });
289
+
290
+ return {
291
+ code: 'SUCCESS',
292
+ message: 'Successfully retrieved leaderboard data',
293
+ data
294
+ };
295
+ } catch (error) {
296
+ return {
297
+ code: 'FAILURE',
298
+ message: '[JoliboxSDK]: Failed to get leaderboard data: ' + (error as Error).message
299
+ };
300
+ }
301
+ }
302
+ });
303
+
304
+ commands.registerCommand('RewardsSDK.getLeaderBoardData', getLeaderBoardData);
305
+
306
+ registerCanIUse('rewards.getLeaderBoardData', {
307
+ version: '1.3.9'
308
+ });
@@ -1,9 +1,49 @@
1
1
  import { createCommands } from '@jolibox/common';
2
- import { createAPI, registerCanIUse, t } from './base';
2
+ import { createAPI, registerCanIUse, t, canIUseNative } from './base';
3
3
  import { taskTracker } from '../report';
4
+ import { emitNative, invokeNative } from '@jolibox/native-bridge';
5
+ import { context } from '@/common/context';
4
6
 
5
7
  const commands = createCommands();
6
8
 
9
+ let lastScoreData: any = null;
10
+ let scheduled = false;
11
+ let lastEmitTime = 0;
12
+ const THROTTLE_INTERVAL = 200;
13
+
14
+ function emitScoreUpdate(data: any) {
15
+ emitNative('cpUpdateScoreEvent', data, context.webviewId);
16
+ }
17
+
18
+ function throttledEmitScore(data: any) {
19
+ lastScoreData = data;
20
+ if (!scheduled) {
21
+ scheduled = true;
22
+ requestAnimationFrame(function frame() {
23
+ const now = Date.now();
24
+ if (now - lastEmitTime >= THROTTLE_INTERVAL) {
25
+ if (lastScoreData !== null) {
26
+ emitScoreUpdate(lastScoreData);
27
+ lastEmitTime = now;
28
+ lastScoreData = null;
29
+ }
30
+ scheduled = false;
31
+ } else {
32
+ requestAnimationFrame(frame);
33
+ }
34
+ });
35
+ }
36
+ }
37
+
38
+ function reportTaskGameInfo(
39
+ event: 'COMPLETE_GAME_LEVEL' | 'GAME_ENDED' | 'GAME_LEVEL_UP',
40
+ inGameInfo: Record<string, any>
41
+ ) {
42
+ return canIUseNative('notifyTaskGameInfoAsync')
43
+ ? invokeNative('notifyTaskGameInfoAsync', { event, inGameInfo })
44
+ : taskTracker.reporter({ event, extraParams: { inGameInfo } });
45
+ }
46
+
7
47
  const onLevelFinished = createAPI('levelFinished', {
8
48
  paramsSchema: t.tuple(
9
49
  t.object({
@@ -28,18 +68,14 @@ const onLevelFinished = createAPI('levelFinished', {
28
68
  );
29
69
 
30
70
  tasks.push(
31
- taskTracker.reporter({
32
- event: 'COMPLETE_GAME_LEVEL',
33
- extraParams: {
34
- inGameInfo: {
35
- levelId,
36
- duration,
37
- rating,
38
- score
39
- }
40
- }
71
+ reportTaskGameInfo('COMPLETE_GAME_LEVEL', {
72
+ levelId,
73
+ duration,
74
+ rating,
75
+ score
41
76
  })
42
77
  );
78
+ throttledEmitScore({ levelId, duration, rating, score });
43
79
  await Promise.all(tasks);
44
80
  }
45
81
  });
@@ -66,17 +102,13 @@ const onGamePlayEnded = createAPI('gamePlayEnded', {
66
102
  );
67
103
 
68
104
  tasks.push(
69
- taskTracker.reporter({
70
- event: 'GAME_ENDED',
71
- extraParams: {
72
- inGameInfo: {
73
- duration,
74
- rating,
75
- score
76
- }
77
- }
105
+ reportTaskGameInfo('GAME_ENDED', {
106
+ duration,
107
+ rating,
108
+ score
78
109
  })
79
110
  );
111
+ throttledEmitScore({ duration, rating, score });
80
112
  await Promise.all(tasks);
81
113
  }
82
114
  });
@@ -97,16 +129,12 @@ const onLevelUpgrade = createAPI('levelUpgrade', {
97
129
  })
98
130
  );
99
131
  tasks.push(
100
- taskTracker.reporter({
101
- event: 'GAME_LEVEL_UP',
102
- extraParams: {
103
- inGameInfo: {
104
- levelId,
105
- levelName: name
106
- }
107
- }
132
+ reportTaskGameInfo('GAME_LEVEL_UP', {
133
+ levelId,
134
+ levelName: name
108
135
  })
109
136
  );
137
+ emitNative('cpUpdateScoreEvent', { levelId }, context.webviewId);
110
138
  await Promise.all(tasks);
111
139
  }
112
140
  });