@jolibox/implement 1.1.13 → 1.1.14-beta.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/.rush/temp/package-deps_build.json +16 -8
- package/dist/common/rewards/__tests__/can-use-jolicoin.test.d.ts +1 -0
- package/dist/common/rewards/fetch-reward.d.ts +2 -0
- package/dist/common/rewards/index.d.ts +18 -0
- package/dist/common/rewards/registers/use-ads.d.ts +3 -0
- package/dist/common/rewards/registers/use-jolicoin.d.ts +10 -0
- package/dist/common/rewards/registers/utils/index.d.ts +2 -0
- package/dist/common/rewards/reward-helper.d.ts +14 -0
- package/dist/common/rewards/type.d.ts +23 -0
- package/dist/common/utils/index.d.ts +7 -0
- package/dist/index.js +3 -3
- package/dist/index.native.js +73 -6
- package/esbuild.config.js +28 -8
- package/implement.build.log +2 -2
- package/package.json +4 -4
- package/src/common/rewards/__tests__/can-use-jolicoin.test.ts +94 -0
- package/src/common/rewards/fetch-reward.ts +39 -0
- package/src/common/rewards/index.ts +20 -0
- package/src/common/rewards/registers/use-ads.ts +9 -0
- package/src/common/rewards/registers/use-jolicoin.ts +99 -0
- package/src/common/rewards/registers/utils/index.ts +11 -0
- package/src/common/rewards/reward-helper.ts +56 -0
- package/src/common/rewards/type.ts +25 -0
- package/src/common/utils/index.ts +7 -0
- package/src/h5/api/ads.ts +20 -1
- package/src/native/api/ads.ts +29 -1
- package/src/native/api/navigate.ts +15 -0
- package/src/native/network/create-fetch.ts +5 -1
- package/src/native/ui/retention.ts +3 -2
package/esbuild.config.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const esbuild = require('esbuild');
|
|
2
2
|
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
3
4
|
|
|
4
5
|
function versionPlugin(version) {
|
|
5
6
|
return {
|
|
@@ -21,14 +22,14 @@ function parseArgs() {
|
|
|
21
22
|
const options = {
|
|
22
23
|
format: 'esm', // 默认值
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
+
|
|
25
26
|
for (let i = 0; i < args.length; i++) {
|
|
26
27
|
const arg = args[i];
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (['esm', 'cjs', 'iife'].includes(
|
|
31
|
-
options.format =
|
|
29
|
+
const [format, value] = arg.split("=");
|
|
30
|
+
if (format === '--format' || format === '-f') {
|
|
31
|
+
if (['esm', 'cjs', 'iife'].includes(value)) {
|
|
32
|
+
options.format = value;
|
|
32
33
|
}
|
|
33
34
|
i++;
|
|
34
35
|
}
|
|
@@ -40,8 +41,25 @@ function parseArgs() {
|
|
|
40
41
|
const options = parseArgs();
|
|
41
42
|
|
|
42
43
|
function build(format) {
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
// native场景支持代码分割
|
|
45
|
+
const nativeBuild = esbuild.build({
|
|
46
|
+
entryPoints: ['src/index.native.ts'],
|
|
47
|
+
bundle: true,
|
|
48
|
+
outdir: 'dist',
|
|
49
|
+
target: ['es2015', 'safari14'],
|
|
50
|
+
format: format,
|
|
51
|
+
minify: true,
|
|
52
|
+
loader: {
|
|
53
|
+
'.ts': 'ts',
|
|
54
|
+
'.tsx': 'tsx',
|
|
55
|
+
'.js': 'jsx'
|
|
56
|
+
},
|
|
57
|
+
plugins: [versionPlugin(process.env.BUILD_VERSION || '1.0.0')],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// h5 完整bundle
|
|
61
|
+
const webBuild = esbuild.build({
|
|
62
|
+
entryPoints: ['src/index.ts'],
|
|
45
63
|
bundle: true,
|
|
46
64
|
outdir: 'dist',
|
|
47
65
|
target: ['es2015', 'safari14'],
|
|
@@ -53,7 +71,9 @@ function build(format) {
|
|
|
53
71
|
'.js': 'jsx'
|
|
54
72
|
},
|
|
55
73
|
plugins: [versionPlugin(process.env.BUILD_VERSION || '1.0.0')],
|
|
56
|
-
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return Promise.all([nativeBuild, webBuild]);
|
|
57
77
|
}
|
|
58
78
|
|
|
59
79
|
(async ()=>{
|
package/implement.build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Invoking: npm run clean && npm run build:esm && tsc
|
|
2
2
|
|
|
3
|
-
> @jolibox/implement@1.1.
|
|
3
|
+
> @jolibox/implement@1.1.14-beta.2 clean
|
|
4
4
|
> rimraf ./dist
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
> @jolibox/implement@1.1.
|
|
7
|
+
> @jolibox/implement@1.1.14-beta.2 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,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jolibox/implement",
|
|
3
3
|
"description": "This project is Jolibox JS-SDk implement for Native && H5",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.14-beta.2",
|
|
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.
|
|
10
|
-
"@jolibox/types": "1.1.
|
|
11
|
-
"@jolibox/native-bridge": "1.1.
|
|
9
|
+
"@jolibox/common": "1.1.14-beta.2",
|
|
10
|
+
"@jolibox/types": "1.1.14-beta.2",
|
|
11
|
+
"@jolibox/native-bridge": "1.1.14-beta.2",
|
|
12
12
|
"localforage": "1.10.0",
|
|
13
13
|
"@jolibox/ui": "1.0.0",
|
|
14
14
|
"web-vitals": "4.2.4"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { IUnlockOptionType } from '../type';
|
|
2
|
+
import { canUseJolicoin } from '../registers/utils';
|
|
3
|
+
|
|
4
|
+
describe('canUseJolicoin', () => {
|
|
5
|
+
it('should return true when joliCoin balance is sufficient and autoDeduct is enabled', () => {
|
|
6
|
+
const unlockOptions = [
|
|
7
|
+
{
|
|
8
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
9
|
+
joliCoinChoices: [{ joliCoinQuantity: 100 }]
|
|
10
|
+
}
|
|
11
|
+
];
|
|
12
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
13
|
+
|
|
14
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should return true when joliCoin balance is more than required', () => {
|
|
18
|
+
const unlockOptions = [
|
|
19
|
+
{
|
|
20
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
21
|
+
joliCoinChoices: [{ joliCoinQuantity: 50 }]
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
25
|
+
|
|
26
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return false when joliCoin balance is insufficient', () => {
|
|
30
|
+
const unlockOptions = [
|
|
31
|
+
{
|
|
32
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
33
|
+
joliCoinChoices: [{ joliCoinQuantity: 150 }]
|
|
34
|
+
}
|
|
35
|
+
];
|
|
36
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
37
|
+
|
|
38
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should return false when autoDeduct is disabled', () => {
|
|
42
|
+
const unlockOptions = [
|
|
43
|
+
{
|
|
44
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
45
|
+
joliCoinChoices: [{ joliCoinQuantity: 50 }]
|
|
46
|
+
}
|
|
47
|
+
];
|
|
48
|
+
const joliCoin = { balance: 100, enableAutoDeduct: false };
|
|
49
|
+
|
|
50
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should return false when there is no JOLI_COIN option', () => {
|
|
54
|
+
const unlockOptions = [
|
|
55
|
+
{
|
|
56
|
+
type: 'ADS' as IUnlockOptionType,
|
|
57
|
+
joliCoinChoices: []
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
61
|
+
|
|
62
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should return false with empty unlockOptions', () => {
|
|
66
|
+
const unlockOptions: any[] = [];
|
|
67
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
68
|
+
|
|
69
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should return false when joliCoin is undefined', () => {
|
|
73
|
+
const unlockOptions = [
|
|
74
|
+
{
|
|
75
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
76
|
+
joliCoinChoices: [{ joliCoinQuantity: 50 }]
|
|
77
|
+
}
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
expect(canUseJolicoin(unlockOptions, undefined)).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should return true when at least one choice is valid', () => {
|
|
84
|
+
const unlockOptions = [
|
|
85
|
+
{
|
|
86
|
+
type: 'JOLI_COIN' as IUnlockOptionType,
|
|
87
|
+
joliCoinChoices: [{ joliCoinQuantity: 200 }, { joliCoinQuantity: 50 }]
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
const joliCoin = { balance: 100, enableAutoDeduct: true };
|
|
91
|
+
|
|
92
|
+
expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { UnlockOptionsEventName, unlockOptionsEmitter } from '.';
|
|
2
|
+
import { IHttpClient } from '../http';
|
|
3
|
+
import { RewardsHelper, RewardType } from './reward-helper';
|
|
4
|
+
import { IJolicoinRewardOption } from './type';
|
|
5
|
+
|
|
6
|
+
const priority = () => {
|
|
7
|
+
return (a: RewardType, b: RewardType) => {
|
|
8
|
+
if (a === 'JOLI_COIN' && b === 'ADS') return -1;
|
|
9
|
+
if (a === 'ADS' && b === 'JOLI_COIN') return 1;
|
|
10
|
+
return 0;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const createRewardFetcher = (rewardsHelper: RewardsHelper) => {
|
|
15
|
+
rewardsHelper.registerRewardsFetcher(async (httpClient: IHttpClient) => {
|
|
16
|
+
const defaultRewards: RewardType[] = ['ADS'];
|
|
17
|
+
try {
|
|
18
|
+
const res = await httpClient.post<IJolicoinRewardOption>('/api/games/unlock-options', {});
|
|
19
|
+
if (res.code !== 'SUCCESS') {
|
|
20
|
+
return defaultRewards;
|
|
21
|
+
}
|
|
22
|
+
unlockOptionsEmitter.emit(UnlockOptionsEventName, {
|
|
23
|
+
options: res.data?.unlockOptions || [],
|
|
24
|
+
userJoliCoin: res.extra?.joliCoin || {
|
|
25
|
+
balance: 0,
|
|
26
|
+
enableAutoDeduct: false
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const rewardsTypes =
|
|
31
|
+
res.data?.unlockOptions?.map((option) => option.type) || Array.from(defaultRewards);
|
|
32
|
+
// Sort reward types with JOLI_COIN having higher priority than ADS
|
|
33
|
+
return rewardsTypes.sort(priority());
|
|
34
|
+
} catch (e) {
|
|
35
|
+
console.error('getRewardOptions error:', e);
|
|
36
|
+
return defaultRewards;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createRewardsHelper } from './reward-helper';
|
|
2
|
+
import { createRewardFetcher } from './fetch-reward';
|
|
3
|
+
import { EventEmitter } from '@jolibox/common';
|
|
4
|
+
import { IUnlockOption, IJoliCoin } from './type';
|
|
5
|
+
|
|
6
|
+
export const rewardsHelper = createRewardsHelper();
|
|
7
|
+
createRewardFetcher(rewardsHelper);
|
|
8
|
+
|
|
9
|
+
export * from './registers/use-ads';
|
|
10
|
+
export * from './registers/use-jolicoin';
|
|
11
|
+
|
|
12
|
+
export interface IUnlockOptionsEvent {
|
|
13
|
+
options: IUnlockOption[];
|
|
14
|
+
userJoliCoin: IJoliCoin;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const UnlockOptionsEventName = 'UNLOCK_OPTIONS_CHANGED' as const;
|
|
18
|
+
export const unlockOptionsEmitter = new EventEmitter<{
|
|
19
|
+
[UnlockOptionsEventName]: [IUnlockOptionsEvent];
|
|
20
|
+
}>();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JoliboxAdsImpl, IRewardParams } from '../../ads';
|
|
2
|
+
|
|
3
|
+
export type AdsRewardsHandler = (params: IRewardParams) => Promise<boolean>;
|
|
4
|
+
export const createAdsRewardHandler = (ads: JoliboxAdsImpl): AdsRewardsHandler => {
|
|
5
|
+
return async (params: IRewardParams) => {
|
|
6
|
+
ads.adBreak(params);
|
|
7
|
+
return true;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { IHttpClient } from '@/common/http';
|
|
2
|
+
import { context } from '@/common/context';
|
|
3
|
+
import { uuidv4 } from '@jolibox/common';
|
|
4
|
+
import { canUseJolicoin } from './utils';
|
|
5
|
+
import { unlockOptionsEmitter, UnlockOptionsEventName, IUnlockOptionsEvent } from '..';
|
|
6
|
+
import { IAdBreakParams } from '@/common/ads';
|
|
7
|
+
|
|
8
|
+
interface IJolicoinUnlockRes {
|
|
9
|
+
code: 'SUCCESS' | 'BALANCE_NOT_ENOUGH' | 'EPISODE_LOCK_JUMP' | 'EPISODE_UNLOCK_ALREADY';
|
|
10
|
+
message: string;
|
|
11
|
+
data: {
|
|
12
|
+
transactionId: string;
|
|
13
|
+
quantity: number;
|
|
14
|
+
balance: number;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type JolicoinRewardsHandler = (params: IAdBreakParams) => Promise<boolean>;
|
|
19
|
+
export const createJolicoinRewardHandler = (
|
|
20
|
+
httpClient: IHttpClient,
|
|
21
|
+
{
|
|
22
|
+
onUnlockSuccess,
|
|
23
|
+
onUnlockFailed
|
|
24
|
+
}: {
|
|
25
|
+
onUnlockSuccess?: (data: { quantity: number; balance: number }) => void;
|
|
26
|
+
onUnlockFailed?: () => void;
|
|
27
|
+
}
|
|
28
|
+
): JolicoinRewardsHandler => {
|
|
29
|
+
let unlockOptionsPromise: Promise<IUnlockOptionsEvent> | null = null;
|
|
30
|
+
let resolveUnlockOptions: ((value: IUnlockOptionsEvent) => void) | null = null;
|
|
31
|
+
let cachedUnlockOptions: IUnlockOptionsEvent | null = null;
|
|
32
|
+
|
|
33
|
+
const createUnlockOptionsPromise = () => {
|
|
34
|
+
if (cachedUnlockOptions) {
|
|
35
|
+
unlockOptionsPromise = Promise.resolve(cachedUnlockOptions);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
unlockOptionsPromise = new Promise<IUnlockOptionsEvent>((resolve) => {
|
|
40
|
+
resolveUnlockOptions = resolve;
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
createUnlockOptionsPromise();
|
|
45
|
+
|
|
46
|
+
unlockOptionsEmitter.on(UnlockOptionsEventName, (options) => {
|
|
47
|
+
cachedUnlockOptions = options;
|
|
48
|
+
if (resolveUnlockOptions) {
|
|
49
|
+
resolveUnlockOptions(options);
|
|
50
|
+
resolveUnlockOptions = null;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return async (params: IAdBreakParams) => {
|
|
55
|
+
try {
|
|
56
|
+
if (!unlockOptionsPromise) {
|
|
57
|
+
createUnlockOptionsPromise();
|
|
58
|
+
}
|
|
59
|
+
const unlockOptions = await unlockOptionsPromise!;
|
|
60
|
+
if (!canUseJolicoin(unlockOptions?.options || [], unlockOptions?.userJoliCoin)) {
|
|
61
|
+
onUnlockFailed?.();
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const unlockWithJolicoin = await httpClient.post<IJolicoinUnlockRes>('/api/joli-coin/unlock', {
|
|
66
|
+
data: {
|
|
67
|
+
// TODO: support drama
|
|
68
|
+
type: 'GAME_REWARD',
|
|
69
|
+
reqId: `${uuidv4()}-${context.mpType}-${Date.now()}`,
|
|
70
|
+
gameInfo: {
|
|
71
|
+
gameId: context.mpId
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if (unlockWithJolicoin.code == 'SUCCESS') {
|
|
76
|
+
params.adBreakDone?.({
|
|
77
|
+
breakType: params.type,
|
|
78
|
+
breakName: 'name' in params ? params.name ?? '' : '',
|
|
79
|
+
breakFormat: 'reward',
|
|
80
|
+
breakStatus: 'viewed'
|
|
81
|
+
});
|
|
82
|
+
if ('adViewed' in params) {
|
|
83
|
+
params.adViewed?.();
|
|
84
|
+
}
|
|
85
|
+
onUnlockSuccess?.({
|
|
86
|
+
quantity: unlockWithJolicoin.data.quantity,
|
|
87
|
+
balance: unlockWithJolicoin.data.balance
|
|
88
|
+
});
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
onUnlockFailed?.();
|
|
92
|
+
return false;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error(`JolicoinRewardHandler error:`, e);
|
|
95
|
+
onUnlockFailed?.();
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IJoliCoin, IUnlockOption } from '@/common/rewards/type';
|
|
2
|
+
|
|
3
|
+
export const canUseJolicoin = (unlockOptions: IUnlockOption[], joliCoin?: IJoliCoin) => {
|
|
4
|
+
return unlockOptions.some(
|
|
5
|
+
(option) =>
|
|
6
|
+
option.type === 'JOLI_COIN' &&
|
|
7
|
+
option.joliCoinChoices.some(
|
|
8
|
+
(choice) => choice.joliCoinQuantity <= (joliCoin?.balance ?? 0) && !!joliCoin?.enableAutoDeduct
|
|
9
|
+
)
|
|
10
|
+
);
|
|
11
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export type RewardType = 'ADS' | 'JOLI_COIN';
|
|
2
|
+
|
|
3
|
+
import { context } from '../context';
|
|
4
|
+
import type { AdsRewardsHandler } from './registers/use-ads';
|
|
5
|
+
|
|
6
|
+
export interface RewardHandlerMap {
|
|
7
|
+
ADS: AdsRewardsHandler;
|
|
8
|
+
JOLI_COIN: (params?: unknown) => Promise<boolean>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type RewardHandler<T extends RewardType> = RewardHandlerMap[T];
|
|
12
|
+
|
|
13
|
+
const isTestMode = context.testMode;
|
|
14
|
+
export function createRewardsHelper() {
|
|
15
|
+
const rewardsHandlers = new Map<RewardType, RewardHandler<any>>();
|
|
16
|
+
let rewardFetcher: ((...args: any[]) => Promise<RewardType[]>) | undefined;
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
registerRewardHandler<T extends RewardType>(type: T, handler: RewardHandler<T>) {
|
|
20
|
+
rewardsHandlers.set(type, handler);
|
|
21
|
+
},
|
|
22
|
+
async handleReward<T extends RewardType>(rewardsTypes: T[], ...args: Parameters<RewardHandler<T>>) {
|
|
23
|
+
const result = await rewardsTypes.reduce(async (prevPromise, type) => {
|
|
24
|
+
const prevResult = await prevPromise;
|
|
25
|
+
if (prevResult === true) return true;
|
|
26
|
+
|
|
27
|
+
isTestMode && console.log(`handleReward ${type}`);
|
|
28
|
+
const handler = rewardsHandlers.get(type);
|
|
29
|
+
const nextResult = handler ? await handler(...args) : prevResult;
|
|
30
|
+
isTestMode && console.log(`handleReward ${type} ${nextResult}`);
|
|
31
|
+
return nextResult;
|
|
32
|
+
}, Promise.resolve(false));
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
},
|
|
36
|
+
async registerRewardsFetcher<T extends RewardType>(fetcher: (...args: any[]) => Promise<T[]>) {
|
|
37
|
+
rewardFetcher = async (...args: unknown[]) => {
|
|
38
|
+
try {
|
|
39
|
+
const rewardsTypes = await fetcher(...args);
|
|
40
|
+
return rewardsTypes;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.error(`getRewardOptions error:`, e);
|
|
43
|
+
return ['ADS'];
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
async getRewardsTypes(...args: unknown[]): Promise<RewardType[]> {
|
|
48
|
+
if (!rewardFetcher) {
|
|
49
|
+
return ['ADS'];
|
|
50
|
+
}
|
|
51
|
+
return await rewardFetcher(...args);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type RewardsHelper = ReturnType<typeof createRewardsHelper>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface IJoliCoin {
|
|
2
|
+
balance: number;
|
|
3
|
+
enableAutoDeduct: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type IUnlockOptionType = 'JOLI_COIN' | 'ADS';
|
|
7
|
+
|
|
8
|
+
interface IJoliCoinChoice {
|
|
9
|
+
joliCoinQuantity: number;
|
|
10
|
+
}
|
|
11
|
+
export interface IUnlockOption {
|
|
12
|
+
type: IUnlockOptionType;
|
|
13
|
+
joliCoinChoices: IJoliCoinChoice[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IJolicoinRewardOption {
|
|
17
|
+
code: 'SUCCESS' | 'ERROR' | 'PARAMETER_ERROR' | 'EPISODE_LOCK_JUMP';
|
|
18
|
+
message: string;
|
|
19
|
+
data: {
|
|
20
|
+
unlockOptions?: IUnlockOption[];
|
|
21
|
+
};
|
|
22
|
+
extra: {
|
|
23
|
+
joliCoin: IJoliCoin;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
const JOLIBOX_CUSTOM_ADS_EVENT_TYPE = 'JOLIBOX_ADS_EVENT';
|
|
2
|
+
const JOLIBOX_CUSTOM_REWARDS_EVENT_TYPE = 'JOLIBOX_CUSTOM_REWARDS_EVENT';
|
|
2
3
|
|
|
3
4
|
interface JoliboxCustomEvent {
|
|
4
5
|
[JOLIBOX_CUSTOM_ADS_EVENT_TYPE]: {
|
|
5
6
|
isAdShowing: boolean;
|
|
6
7
|
};
|
|
8
|
+
[JOLIBOX_CUSTOM_REWARDS_EVENT_TYPE]: {
|
|
9
|
+
['JOLI_COIN']?: {
|
|
10
|
+
quantity: number;
|
|
11
|
+
balance: number;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
7
14
|
}
|
|
8
15
|
|
|
9
16
|
export const notifyCustomEvent = <T extends keyof JoliboxCustomEvent>(
|
package/src/h5/api/ads.ts
CHANGED
|
@@ -3,10 +3,23 @@ import { track } from '../report';
|
|
|
3
3
|
import { createCommands } from '@jolibox/common';
|
|
4
4
|
import { httpClientManager } from '../http';
|
|
5
5
|
import { notifyCustomEvent } from '@/common/utils';
|
|
6
|
+
import { createAdsRewardHandler, rewardsHelper, createJolicoinRewardHandler } from '@/common/rewards';
|
|
7
|
+
|
|
6
8
|
const commands = createCommands();
|
|
7
9
|
|
|
8
10
|
const httpClient = httpClientManager.create();
|
|
9
11
|
const ads = new JoliboxAdsImpl(track, httpClient, () => httpClientManager.getNetworkStatus());
|
|
12
|
+
rewardsHelper.registerRewardHandler('ADS', createAdsRewardHandler(ads));
|
|
13
|
+
rewardsHelper.registerRewardHandler(
|
|
14
|
+
'JOLI_COIN',
|
|
15
|
+
createJolicoinRewardHandler(ads.httpClient, {
|
|
16
|
+
onUnlockSuccess: (params: { quantity: number; balance: number }) => {
|
|
17
|
+
notifyCustomEvent('JOLIBOX_CUSTOM_REWARDS_EVENT', {
|
|
18
|
+
JOLI_COIN: params
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}) as unknown as (params?: unknown) => Promise<boolean>
|
|
22
|
+
);
|
|
10
23
|
|
|
11
24
|
adEventEmitter.on('isAdShowing', (isAdShowing) => {
|
|
12
25
|
notifyCustomEvent('JOLIBOX_ADS_EVENT', { isAdShowing });
|
|
@@ -21,7 +34,13 @@ commands.registerCommand('AdsSDK.adConfig', (params) => {
|
|
|
21
34
|
});
|
|
22
35
|
|
|
23
36
|
commands.registerCommand('AdsSDK.adBreak', (params) => {
|
|
24
|
-
|
|
37
|
+
if (params.type === 'reward') {
|
|
38
|
+
rewardsHelper.getRewardsTypes(ads.httpClient).then((rewardsTypes) => {
|
|
39
|
+
rewardsHelper.handleReward(rewardsTypes, params);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
ads.adBreak(params);
|
|
43
|
+
}
|
|
25
44
|
});
|
|
26
45
|
|
|
27
46
|
commands.registerCommand('AdsSDK.adUnit', (params) => {
|
package/src/native/api/ads.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createCommands } from '@jolibox/common';
|
|
2
2
|
import { createSyncAPI, registerCanIUse } from './base';
|
|
3
|
+
import { createToast } from '@jolibox/ui';
|
|
3
4
|
|
|
4
5
|
const commands = createCommands();
|
|
5
6
|
|
|
@@ -8,6 +9,7 @@ import { track } from '../report';
|
|
|
8
9
|
|
|
9
10
|
import { innerFetch as fetch } from '../network';
|
|
10
11
|
import { invokeNative } from '@jolibox/native-bridge';
|
|
12
|
+
import { rewardsHelper, createAdsRewardHandler, createJolicoinRewardHandler } from '@/common/rewards';
|
|
11
13
|
|
|
12
14
|
const checkNetworkStatus = () => {
|
|
13
15
|
const { data } = invokeNative('getNetworkStatusSync');
|
|
@@ -34,6 +36,26 @@ const ads = new JoliboxAdsImpl(
|
|
|
34
36
|
checkNetworkStatus
|
|
35
37
|
);
|
|
36
38
|
|
|
39
|
+
rewardsHelper.registerRewardHandler('ADS', createAdsRewardHandler(ads));
|
|
40
|
+
rewardsHelper.registerRewardHandler(
|
|
41
|
+
'JOLI_COIN',
|
|
42
|
+
createJolicoinRewardHandler(ads.httpClient, {
|
|
43
|
+
onUnlockSuccess: (params) => {
|
|
44
|
+
const { quantity, balance } = params;
|
|
45
|
+
const balanceStr = balance >= 1000 ? '999+' : balance;
|
|
46
|
+
createToast(`{slot-checkmark} −${quantity} {slot-coin} | Balance: ${balanceStr} {slot-coin}`, {
|
|
47
|
+
customStyle: {
|
|
48
|
+
mark: {
|
|
49
|
+
marginRight: '8px'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
onUnlockFailed: () => {
|
|
55
|
+
console.log('onUnlockFailed');
|
|
56
|
+
}
|
|
57
|
+
}) as unknown as (params?: unknown) => Promise<boolean>
|
|
58
|
+
);
|
|
37
59
|
const adInit = createSyncAPI('adInit', {
|
|
38
60
|
implement: (config?: IAdsInitParams) => {
|
|
39
61
|
ads.init(config);
|
|
@@ -48,7 +70,13 @@ const adConfig = createSyncAPI('adConfig', {
|
|
|
48
70
|
|
|
49
71
|
const adBreak = createSyncAPI('adBreak', {
|
|
50
72
|
implement: (params: IAdBreakParams) => {
|
|
51
|
-
|
|
73
|
+
if (params.type === 'reward') {
|
|
74
|
+
rewardsHelper.getRewardsTypes(ads.httpClient).then((rewardsTypes) => {
|
|
75
|
+
rewardsHelper.handleReward(rewardsTypes, params);
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
ads.adBreak(params);
|
|
79
|
+
}
|
|
52
80
|
}
|
|
53
81
|
});
|
|
54
82
|
|
|
@@ -44,9 +44,20 @@ const closePageSync = createSyncAPI('closePageSync', {
|
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
+
const openSystemBrowserSync = createSyncAPI('openSystemBrowserSync', {
|
|
48
|
+
paramsSchema: t.tuple(t.string()),
|
|
49
|
+
implement: (url) => {
|
|
50
|
+
const { errNo, errMsg } = invokeNative('openSystemBrowserSync', { url });
|
|
51
|
+
if (errNo !== 0) {
|
|
52
|
+
throw new UserCustomError(errMsg, errNo);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
47
57
|
commands.registerCommand('RouterSDK.openSchema', openSchemaSync);
|
|
48
58
|
commands.registerCommand('RouterSDK.openPage', openPageSync);
|
|
49
59
|
commands.registerCommand('RouterSDK.closePage', closePageSync);
|
|
60
|
+
commands.registerCommand('RouterSDK.openSystemBrowser', openSystemBrowserSync);
|
|
50
61
|
|
|
51
62
|
registerCanIUse('router.openSchema', {
|
|
52
63
|
version: '1.1.10'
|
|
@@ -59,3 +70,7 @@ registerCanIUse('router.openPage', {
|
|
|
59
70
|
registerCanIUse('router.closePage', {
|
|
60
71
|
version: '1.1.10'
|
|
61
72
|
});
|
|
73
|
+
|
|
74
|
+
registerCanIUse('router.openSystemBrowser', {
|
|
75
|
+
version: '1.1.13'
|
|
76
|
+
});
|
|
@@ -82,7 +82,11 @@ export function createFetch(
|
|
|
82
82
|
|
|
83
83
|
if (res.state === 'success') {
|
|
84
84
|
const { requestTaskId, state, ...rest } = res;
|
|
85
|
-
|
|
85
|
+
if (res.statusCode === 200) {
|
|
86
|
+
task.resolve(rest);
|
|
87
|
+
} else {
|
|
88
|
+
task.reject(rest);
|
|
89
|
+
}
|
|
86
90
|
promiseMap.delete(requestTaskId);
|
|
87
91
|
} else if (res.state === 'fail') {
|
|
88
92
|
const { requestTaskId, state, ...rest } = res;
|
|
@@ -50,10 +50,11 @@ const openGameSchema = (game: IGame) => {
|
|
|
50
50
|
// Parse the original URL to preserve its structure
|
|
51
51
|
|
|
52
52
|
const url = new URL(data.schema);
|
|
53
|
+
const originalPath = url.pathname;
|
|
53
54
|
const originalSearch = new URLSearchParams(url.search);
|
|
54
55
|
|
|
55
56
|
// Set or replace gameId and joliSource parameters
|
|
56
|
-
originalSearch.set('gameId',
|
|
57
|
+
originalSearch.set('gameId', context.mpId);
|
|
57
58
|
originalSearch.set(
|
|
58
59
|
'joliSource',
|
|
59
60
|
context.encodeJoliSourceQuery({
|
|
@@ -64,7 +65,7 @@ const openGameSchema = (game: IGame) => {
|
|
|
64
65
|
|
|
65
66
|
const host = `https://${game.gameId}.content.jolibox.com/`;
|
|
66
67
|
// Construct the final schema URL
|
|
67
|
-
const schema = `${host}
|
|
68
|
+
const schema = `${host}${originalPath}?${originalSearch.toString()}`;
|
|
68
69
|
|
|
69
70
|
invokeNative('openSchemaSync', {
|
|
70
71
|
schema
|