@jolibox/implement 1.1.49 → 1.1.51

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -9,3 +9,4 @@ import './ads';
9
9
  import './navigate';
10
10
  import './runtime';
11
11
  import './is-native-support';
12
+ import './call-host-method';
@@ -1,9 +1,9 @@
1
1
  Invoking: npm run clean && npm run build:esm && tsc
2
2
 
3
- > @jolibox/implement@1.1.49 clean
3
+ > @jolibox/implement@1.1.51 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.1.49 build:esm
7
+ > @jolibox/implement@1.1.51 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,15 +1,15 @@
1
1
  {
2
2
  "name": "@jolibox/implement",
3
3
  "description": "This project is Jolibox JS-SDk implement for Native && H5",
4
- "version": "1.1.49",
4
+ "version": "1.1.51",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@jolibox/common": "1.1.49",
10
- "@jolibox/types": "1.1.49",
11
- "@jolibox/native-bridge": "1.1.49",
12
- "@jolibox/ads": "1.1.49",
9
+ "@jolibox/common": "1.1.51",
10
+ "@jolibox/types": "1.1.51",
11
+ "@jolibox/native-bridge": "1.1.51",
12
+ "@jolibox/ads": "1.1.51",
13
13
  "localforage": "1.10.0",
14
14
  "@jolibox/ui": "1.0.0",
15
15
  "web-vitals": "4.2.4"
@@ -100,7 +100,7 @@ export const createAPIFactory = (track: Track) => {
100
100
  };
101
101
  } finally {
102
102
  const duration = Date.now() - startTime;
103
- track('apiInvoked', { ...reportExtra, duration, status });
103
+ // track('apiInvoked', { ...reportExtra, duration, status });
104
104
  }
105
105
  };
106
106
  }
@@ -179,7 +179,7 @@ export const createAPIFactory = (track: Track) => {
179
179
  return res;
180
180
  } finally {
181
181
  const duration = Date.now() - startTime;
182
- track('apiInvoked', { ...reportExtra, duration, status });
182
+ // track('apiInvoked', { ...reportExtra, duration, status });
183
183
  }
184
184
  };
185
185
  }
@@ -9,10 +9,13 @@ type TaskEvent =
9
9
  | 'USE_GAME_ITEM' // TODO: deprecate
10
10
  | 'IN_GAME_PURCHASES' // 游戏内购
11
11
  | 'ADS_UNLOCK_GAME' // 广告解锁
12
- | 'GAME_TASK_EVENT'; // 关卡行为
12
+ | 'GAME_TASK_EVENT' // 关卡行为
13
+ | 'GAME_ENDED' // 游戏结束
14
+ | 'GAME_LEVEL_UP'; // 游戏关卡升级
13
15
  export type TaskPoint = {
14
16
  event: TaskEvent;
15
17
  params?: Record<string, unknown>;
18
+ extraParams?: Record<string, unknown>;
16
19
  };
17
20
  export abstract class TaskTracker {
18
21
  interval: number;
@@ -10,6 +10,7 @@ import {
10
10
  DefaltLoginGuide
11
11
  } from './reward-emitter';
12
12
  import { StandardResponse } from '@jolibox/types';
13
+ import { hostEmitter } from '@jolibox/common';
13
14
 
14
15
  const priority = () => {
15
16
  return (a: RewardType, b: RewardType) => {
@@ -64,6 +65,10 @@ export const createRewardFrequencyConfigFetcher = (rewardsHelper: RewardsHelper)
64
65
  loginGuide: res.data?.loginGuide || DefaltLoginGuide
65
66
  });
66
67
 
68
+ hostEmitter.emit('onGlobalConfigChanged', {
69
+ globalConfig: res.data
70
+ });
71
+
67
72
  return {
68
73
  joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge,
69
74
  loginGuide: res.data?.loginGuide || DefaltLoginGuide
@@ -4,8 +4,12 @@ import { JoliboxAdsForGame } from '@jolibox/ads';
4
4
  export type AdsRewardsHandler = (params: IRewardParams) => Promise<boolean>;
5
5
  export const createAdsRewardHandler = (ads: JoliboxAdsForGame): AdsRewardsHandler => {
6
6
  return async (params: IRewardParams) => {
7
- console.log('trigger @jolibox/ads reward', params);
8
- ads.adBreak(params);
7
+ try {
8
+ console.log('trigger @jolibox/ads reward', params);
9
+ await ads.adBreak(params);
10
+ } catch (e) {
11
+ console.error('-----createAdsRewardHandler error-----', e);
12
+ }
9
13
  return true;
10
14
  };
11
15
  };
@@ -97,15 +97,20 @@ export const createCommonJolicoinRewardHandler = (
97
97
 
98
98
  console.log('-----unlockWithJolicoin request result-----', unlockWithJolicoin);
99
99
  if (unlockWithJolicoin.code == 'SUCCESS') {
100
- params.adBreakDone?.({
101
- breakType: params.type,
102
- breakName: 'name' in params ? params.name ?? '' : '',
103
- breakFormat: 'reward',
104
- breakStatus: 'viewed'
105
- });
106
- if ('adViewed' in params) {
107
- params.adViewed?.();
100
+ try {
101
+ params.adBreakDone?.({
102
+ breakType: params.type,
103
+ breakName: 'name' in params ? params.name ?? '' : '',
104
+ breakFormat: 'reward',
105
+ breakStatus: 'viewed'
106
+ });
107
+ if ('adViewed' in params) {
108
+ params.adViewed?.();
109
+ }
110
+ } catch (e) {
111
+ console.error('-----unlockWithJolicoin adBreakDone error-----', e);
108
112
  }
113
+
109
114
  handleUnlockSuccess?.({
110
115
  quantity: unlockWithJolicoin.data.quantity,
111
116
  balance: unlockWithJolicoin.data.balance
@@ -10,21 +10,31 @@ const onLevelFinished = createAPI('levelFinished', {
10
10
  levelId: t.or(t.string(), t.number()),
11
11
  duration: t.number().optional(),
12
12
  rating: t.number().optional(),
13
- score: t.number().optional()
13
+ score: t.number().optional(),
14
+ variation: t.string().optional()
14
15
  })
15
16
  ),
16
17
  implement: async (parmas) => {
17
- const { levelId, duration, rating, score } = parmas;
18
- logger.info(`onLevelFinished`, duration, rating, score);
18
+ const { levelId, duration, rating, score, variation } = parmas;
19
+ logger.info(`onLevelFinished`, duration, rating, score, variation);
19
20
  await Promise.all([
20
21
  taskTracker.reporter({
21
- event: 'COMPLETE_GAME_LEVEL'
22
+ event: 'COMPLETE_GAME_LEVEL',
23
+ extraParams: {
24
+ inGameInfo: {
25
+ levelId,
26
+ duration,
27
+ rating,
28
+ score
29
+ }
30
+ }
22
31
  }),
23
32
  taskTracker.tracker('LevelFinished', {
24
33
  levelId,
25
34
  duration,
26
35
  rating,
27
- score
36
+ score,
37
+ variation
28
38
  })
29
39
  ]);
30
40
  }
@@ -35,17 +45,31 @@ const onGamePlayEnded = createAPI('gamePlayEnded', {
35
45
  t.object({
36
46
  duration: t.number().optional(),
37
47
  rating: t.number().optional(),
38
- score: t.number()
48
+ score: t.number(),
49
+ variation: t.string().optional()
39
50
  })
40
51
  ),
41
52
  implement: async (params) => {
42
- const { duration, rating, score } = params;
43
- logger.info(`onGamePlayEnded`, duration, rating, score);
44
- return taskTracker.tracker('GamePlayEnded', {
45
- duration,
46
- rating,
47
- score
48
- });
53
+ const { duration, rating, score, variation } = params;
54
+ logger.info(`onGamePlayEnded`, duration, rating, score, variation);
55
+ await Promise.all([
56
+ taskTracker.tracker('GamePlayEnded', {
57
+ duration,
58
+ rating,
59
+ score,
60
+ variation
61
+ }),
62
+ taskTracker.reporter({
63
+ event: 'GAME_ENDED',
64
+ extraParams: {
65
+ inGameInfo: {
66
+ duration,
67
+ rating,
68
+ score
69
+ }
70
+ }
71
+ })
72
+ ]);
49
73
  }
50
74
  });
51
75
 
@@ -57,10 +81,21 @@ const onLevelUpgrade = createAPI('levelUpgrade', {
57
81
  })
58
82
  ),
59
83
  implement: async ({ levelId, name }) => {
60
- return taskTracker.tracker('LevelUpgrade', {
61
- name,
62
- levelId
63
- });
84
+ await Promise.all([
85
+ taskTracker.tracker('LevelUpgrade', {
86
+ name,
87
+ levelId
88
+ }),
89
+ taskTracker.reporter({
90
+ event: 'GAME_LEVEL_UP',
91
+ extraParams: {
92
+ inGameInfo: {
93
+ levelId,
94
+ levelName: name
95
+ }
96
+ }
97
+ })
98
+ ]);
64
99
  }
65
100
  });
66
101
 
@@ -75,7 +110,8 @@ registerCanIUse('TaskTrackerSDK.onLevelFinished', {
75
110
  levelId: '1.1.25',
76
111
  duration: '1.1.25',
77
112
  rating: '1.1.25',
78
- score: '1.1.25'
113
+ score: '1.1.25',
114
+ variation: '1.1.50'
79
115
  }
80
116
  }
81
117
  });
@@ -86,7 +122,8 @@ registerCanIUse('TaskTrackerSDK.onGamePlayEnded', {
86
122
  params: {
87
123
  duration: '1.1.25',
88
124
  rating: '1.1.25',
89
- score: '1.1.25'
125
+ score: '1.1.25',
126
+ variation: '1.1.50'
90
127
  }
91
128
  }
92
129
  });
@@ -23,16 +23,20 @@ export class H5TaskTracker extends TaskTracker {
23
23
  }
24
24
 
25
25
  async reporter(point: TaskPoint): Promise<void> {
26
- const { event, params } = point;
27
- await this.httpClient.post(`/api/base/app-event`, {
28
- data: {
29
- eventType: event,
30
- gameInfo: {
31
- gameId: this.gameId,
32
- sessionId: this.sessionId,
33
- ...params
34
- }
26
+ const { event, params, extraParams } = point;
27
+ const reportBody = {
28
+ eventType: event,
29
+ gameInfo: {
30
+ gameId: this.gameId,
31
+ sessionId: this.sessionId,
32
+ ...params
35
33
  }
34
+ };
35
+ if (extraParams) {
36
+ Object.assign(reportBody, extraParams);
37
+ }
38
+ await this.httpClient.post(`/api/base/app-event`, {
39
+ data: reportBody
36
40
  });
37
41
  }
38
42
  tracker(event: TrackEvent, info: Record<string, unknown> | null = null) {
@@ -0,0 +1,64 @@
1
+ import { createAPI, registerCanIUse, t } from './base';
2
+ import { applyNative, invokeNative } from '@jolibox/native-bridge';
3
+ import { hostEmitter, UserCustomError } from '@jolibox/common';
4
+ import { createCommands } from '@jolibox/common';
5
+
6
+ const commands = createCommands();
7
+ let supportedMethods: string[] = [];
8
+ let isInitialized = false;
9
+
10
+ hostEmitter.on('onGlobalConfigChanged', (params) => {
11
+ supportedMethods = params.globalConfig.supportedHostMethods ?? [];
12
+ isInitialized = true;
13
+ });
14
+
15
+ const getSupportedMethods = (): Promise<string[]> => {
16
+ return new Promise((resolve) => {
17
+ if (isInitialized) {
18
+ resolve(supportedMethods);
19
+ } else {
20
+ const handler = (params: any) => {
21
+ supportedMethods = params.globalConfig.supportedHostMethods ?? [];
22
+ isInitialized = true;
23
+ hostEmitter.off('onGlobalConfigChanged', handler);
24
+ resolve(supportedMethods);
25
+ };
26
+ hostEmitter.on('onGlobalConfigChanged', handler);
27
+ }
28
+ });
29
+ };
30
+
31
+ const callHostMethodAsync = createAPI('callHostMethod', {
32
+ paramsSchema: t.tuple(
33
+ t.object({
34
+ method: t.string(),
35
+ params: t.object().optional().default({})
36
+ })
37
+ ),
38
+ implement: async ({ method, params }) => {
39
+ const supportedMethods = await getSupportedMethods();
40
+ if (!supportedMethods.includes(method)) {
41
+ throw new UserCustomError('Method not supported', 10001);
42
+ }
43
+ const { errNo, errMsg, data } = await invokeNative('callHostMethodAsync', {
44
+ method,
45
+ params: {
46
+ ...params
47
+ }
48
+ });
49
+ if (errNo !== 0) {
50
+ throw new UserCustomError(errMsg, errNo);
51
+ }
52
+
53
+ const { callHostMethodId, ...rest } = data;
54
+ return {
55
+ ...rest
56
+ };
57
+ }
58
+ });
59
+
60
+ commands.registerCommand('API.callHostMethod', callHostMethodAsync);
61
+
62
+ registerCanIUse('callHostMethod', {
63
+ version: '1.1.49'
64
+ });
@@ -9,3 +9,4 @@ import './ads';
9
9
  import './navigate';
10
10
  import './runtime';
11
11
  import './is-native-support';
12
+ import './call-host-method';
@@ -60,15 +60,12 @@ const navigateToNativePage = createSyncAPI('navigateToNativePage', {
60
60
  t.object()
61
61
  ),
62
62
  implement: (path, params) => {
63
- const { errNo, errMsg } = invokeNative('callHostMethodAsync', {
63
+ invokeNative('callHostMethodAsync', {
64
64
  method: path,
65
65
  params: {
66
66
  ...params
67
67
  }
68
68
  });
69
- if (errNo !== 0) {
70
- throw new UserCustomError(errMsg, errNo);
71
- }
72
69
  }
73
70
  });
74
71
 
@@ -10,24 +10,34 @@ const onLevelFinished = createAPI('levelFinished', {
10
10
  levelId: t.or(t.string(), t.number()),
11
11
  duration: t.number().optional(),
12
12
  rating: t.number().optional(),
13
- score: t.number().optional()
13
+ score: t.number().optional(),
14
+ variation: t.string().optional()
14
15
  })
15
16
  ),
16
17
  implement: async (parmas) => {
17
- const { levelId, duration, rating, score } = parmas;
18
+ const { levelId, duration, rating, score, variation } = parmas;
18
19
  const tasks = [];
19
20
  tasks.push(
20
21
  taskTracker.tracker('LevelFinished', {
21
22
  levelId,
22
23
  duration,
23
24
  rating,
24
- score
25
+ score,
26
+ variation
25
27
  })
26
28
  );
27
29
 
28
30
  tasks.push(
29
31
  taskTracker.reporter({
30
- event: 'COMPLETE_GAME_LEVEL'
32
+ event: 'COMPLETE_GAME_LEVEL',
33
+ extraParams: {
34
+ inGameInfo: {
35
+ levelId,
36
+ duration,
37
+ rating,
38
+ score
39
+ }
40
+ }
31
41
  })
32
42
  );
33
43
  await Promise.all(tasks);
@@ -39,16 +49,35 @@ const onGamePlayEnded = createAPI('gamePlayEnded', {
39
49
  t.object({
40
50
  duration: t.number().optional(),
41
51
  rating: t.number().optional(),
42
- score: t.number()
52
+ score: t.number(),
53
+ variation: t.string().optional()
43
54
  })
44
55
  ),
45
56
  implement: async (params) => {
46
- const { duration, rating, score } = params;
47
- return taskTracker.tracker('GamePlayEnded', {
48
- duration,
49
- rating,
50
- score
51
- });
57
+ const { duration, rating, score, variation } = params;
58
+ const tasks = [];
59
+ tasks.push(
60
+ taskTracker.tracker('GamePlayEnded', {
61
+ duration,
62
+ rating,
63
+ score,
64
+ variation
65
+ })
66
+ );
67
+
68
+ tasks.push(
69
+ taskTracker.reporter({
70
+ event: 'GAME_ENDED',
71
+ extraParams: {
72
+ inGameInfo: {
73
+ duration,
74
+ rating,
75
+ score
76
+ }
77
+ }
78
+ })
79
+ );
80
+ await Promise.all(tasks);
52
81
  }
53
82
  });
54
83
 
@@ -67,6 +96,17 @@ const onLevelUpgrade = createAPI('levelUpgrade', {
67
96
  levelId
68
97
  })
69
98
  );
99
+ tasks.push(
100
+ taskTracker.reporter({
101
+ event: 'GAME_LEVEL_UP',
102
+ extraParams: {
103
+ inGameInfo: {
104
+ levelId,
105
+ levelName: name
106
+ }
107
+ }
108
+ })
109
+ );
70
110
  await Promise.all(tasks);
71
111
  }
72
112
  });
@@ -82,7 +122,8 @@ registerCanIUse('TaskTrackerSDK.onLevelFinished', {
82
122
  levelId: '1.1.25',
83
123
  duration: '1.1.25',
84
124
  rating: '1.1.25',
85
- score: '1.1.25'
125
+ score: '1.1.25',
126
+ variation: '1.1.50'
86
127
  }
87
128
  }
88
129
  });
@@ -93,7 +134,8 @@ registerCanIUse('TaskTrackerSDK.onGamePlayEnded', {
93
134
  params: {
94
135
  duration: '1.1.25',
95
136
  rating: '1.1.25',
96
- score: '1.1.25'
137
+ score: '1.1.25',
138
+ variation: '1.1.50'
97
139
  }
98
140
  }
99
141
  });
@@ -41,7 +41,7 @@ export class NativeTaskTracker extends TaskTracker {
41
41
  : 'https://api.jolibox.com/api/base/app-event';
42
42
  }
43
43
  async reporter(point: TaskPoint): Promise<void> {
44
- const { event, params } = point;
44
+ const { event, params, extraParams } = point;
45
45
 
46
46
  const reportBody = {
47
47
  eventType: event,
@@ -51,6 +51,9 @@ export class NativeTaskTracker extends TaskTracker {
51
51
  ...params
52
52
  }
53
53
  };
54
+ if (extraParams) {
55
+ Object.assign(reportBody, extraParams);
56
+ }
54
57
  const reportTask =
55
58
  context.platform === 'android'
56
59
  ? fetch(`/api/base/app-event`, {
@@ -48,7 +48,13 @@ rewardsEmitter.on(InvokeUnloginModalEventName, async (type: 'JOLI_COIN' | 'ADS-J
48
48
  return;
49
49
  }
50
50
 
51
- track('LoginGuideModal', {}, EventType.Expose);
51
+ track(
52
+ 'LoginGuideModal',
53
+ {
54
+ eventType: EventType.Expose
55
+ },
56
+ EventType.Expose
57
+ );
52
58
  const modal = createUnloginJolicoinModal({
53
59
  data: {
54
60
  rewardType: type,
@@ -58,14 +64,18 @@ rewardsEmitter.on(InvokeUnloginModalEventName, async (type: 'JOLI_COIN' | 'ADS-J
58
64
  confirm: {
59
65
  text: params.confirmButtonText,
60
66
  onPress: async () => {
61
- track('LoginGuideModal', { type: 'click_confirm' }, EventType.Click);
67
+ track('LoginGuideModal', { type: 'click_confirm', eventType: EventType.Click }, EventType.Click);
62
68
  const { isLogin, extra, isFirstLogin } = await loginImplement({}, true);
63
69
  if (!isLogin) {
64
70
  console.log('login failed');
65
71
  return;
66
72
  }
67
73
 
68
- track('LoginGuideModal', { type: 'click_confirm_login', isFirstLogin }, EventType.Click);
74
+ track(
75
+ 'LoginGuideModal',
76
+ { type: 'click_confirm_login', isFirstLogin, eventType: EventType.Click },
77
+ EventType.Click
78
+ );
69
79
  if (isFirstLogin && extra?.firstLogin?.reward) {
70
80
  const { rewardValue } = extra.firstLogin.reward;
71
81
  createToast(`{slot-i18ntran-loginGuide.rewardTips}`, {
@@ -94,10 +104,10 @@ rewardsEmitter.on(InvokeUnloginModalEventName, async (type: 'JOLI_COIN' | 'ADS-J
94
104
  cancel: {
95
105
  text: params.cancelButtonText,
96
106
  onPress: ({ type }) => {
97
- track('LoginGuideModal', {}, EventType.Hide);
107
+ track('LoginGuideModal', { eventType: EventType.Hide }, EventType.Hide);
98
108
 
99
109
  const trackType = type === 'CANCEL' ? 'click_close' : 'click_watch_ads';
100
- track('LoginGuideModal', { type: trackType }, EventType.Click);
110
+ track('LoginGuideModal', { type: trackType, eventType: EventType.Click }, EventType.Click);
101
111
  rewardsEmitter.emit(UseUnloginModalResultEventName, { result: 'FAILED' });
102
112
  modal.destroy();
103
113
  }