@jolibox/implement 1.1.52-beta.1 → 1.1.53-beta.0
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 +33 -26
- package/dist/common/context/index.d.ts +1 -0
- package/dist/common/rewards/index.d.ts +2 -0
- package/dist/common/rewards/registers/use-gem-only.d.ts +10 -0
- package/dist/common/rewards/registers/use-gem.d.ts +10 -0
- package/dist/common/rewards/registers/utils/coins/commands/currency-handlers.d.ts +15 -0
- package/dist/common/rewards/registers/utils/coins/commands/index.d.ts +1 -1
- package/dist/common/rewards/registers/utils/coins/commands/use-jolicoin.d.ts +10 -8
- package/dist/common/rewards/registers/utils/coins/commands/use-payment.d.ts +10 -8
- package/dist/common/rewards/registers/utils/coins/commands/use-unlogin.d.ts +6 -8
- package/dist/common/rewards/registers/utils/coins/currency-config.d.ts +8 -0
- package/dist/common/rewards/registers/utils/coins/index.d.ts +2 -18
- package/dist/common/rewards/registers/utils/coins/jolicoin/jolicoin-handler.d.ts +18 -0
- package/dist/common/rewards/registers/utils/coins/joligem/fetch-gem-balance.d.ts +4 -0
- package/dist/common/rewards/registers/utils/coins/joligem/gem-handler.d.ts +19 -0
- package/dist/common/rewards/registers/utils/index.d.ts +3 -1
- package/dist/common/rewards/reward-emitter.d.ts +23 -13
- package/dist/common/rewards/reward-helper.d.ts +3 -1
- package/dist/common/rewards/type.d.ts +10 -1
- package/dist/index.js +25 -25
- package/dist/index.native.js +67 -67
- package/dist/native/payment/payment-helper.d.ts +5 -1
- package/dist/native/rewards/check-frequency.d.ts +24 -0
- package/implement.build.log +2 -2
- package/package.json +6 -6
- package/src/common/context/index.ts +3 -0
- package/src/common/rewards/__tests__/can-use-jolicoin.test.ts +265 -9
- package/src/common/rewards/fetch-reward.ts +18 -3
- package/src/common/rewards/index.ts +2 -0
- package/src/common/rewards/registers/use-gem-only.ts +58 -0
- package/src/common/rewards/registers/use-gem.ts +53 -0
- package/src/common/rewards/registers/use-jolicoin-only.ts +4 -4
- package/src/common/rewards/registers/use-jolicoin.ts +4 -4
- package/src/common/rewards/registers/utils/coins/commands/currency-handlers.ts +48 -0
- package/src/common/rewards/registers/utils/coins/commands/index.ts +1 -1
- package/src/common/rewards/registers/utils/coins/commands/use-jolicoin.ts +31 -19
- package/src/common/rewards/registers/utils/coins/commands/use-payment.ts +32 -25
- package/src/common/rewards/registers/utils/coins/commands/use-unlogin.ts +22 -24
- package/src/common/rewards/registers/utils/coins/currency-config.ts +23 -0
- package/src/common/rewards/registers/utils/coins/index.ts +2 -134
- package/src/common/rewards/registers/utils/coins/jolicoin/jolicoin-handler.ts +149 -0
- package/src/common/rewards/registers/utils/coins/joligem/fetch-gem-balance.ts +15 -0
- package/src/common/rewards/registers/utils/coins/joligem/gem-handler.ts +131 -0
- package/src/common/rewards/registers/utils/index.ts +20 -1
- package/src/common/rewards/reward-emitter.ts +23 -13
- package/src/common/rewards/reward-helper.ts +3 -1
- package/src/common/rewards/type.ts +13 -1
- package/src/h5/rewards/index.ts +33 -20
- package/src/native/api/ads.ts +22 -1
- package/src/native/api/call-host-method.ts +36 -1
- package/src/native/payment/index.ts +2 -0
- package/src/native/payment/payment-helper.ts +2 -1
- package/src/native/rewards/check-frequency.ts +10 -0
- package/src/native/rewards/ui/payment-modal.ts +110 -33
- package/src/native/rewards/ui/unlogin-modal.ts +182 -87
- package/src/native/rewards/ui/use-modal.ts +126 -68
- package/implementation.build.log +0 -9
- /package/dist/common/rewards/registers/utils/coins/{fetch-balance.d.ts → jolicoin/fetch-balance.d.ts} +0 -0
- /package/src/common/rewards/registers/utils/coins/{fetch-balance.ts → jolicoin/fetch-balance.ts} +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { IHttpClient } from '@/common/http';
|
|
2
|
+
import { IJoliCoin, IGem } from '@/common/rewards/type';
|
|
3
|
+
import { IUnlockOptionsEvent } from '@/common/rewards/reward-emitter';
|
|
4
|
+
import { fetchBalance as fetchJoliCoinBalance } from '../jolicoin/fetch-balance';
|
|
5
|
+
import { fetchGemBalance } from '../joligem/fetch-gem-balance';
|
|
6
|
+
|
|
7
|
+
export type CurrencyType = 'JOLI_COIN' | 'JOLI_GEM';
|
|
8
|
+
|
|
9
|
+
export interface CurrencyHandler {
|
|
10
|
+
fetchBalance: (httpClient: IHttpClient) => Promise<{ balance: number } | undefined>;
|
|
11
|
+
getQuantity: (unlockOptions: IUnlockOptionsEvent | null) => number;
|
|
12
|
+
getCurrentUser: (unlockOptions: IUnlockOptionsEvent | null) => IJoliCoin | IGem;
|
|
13
|
+
createUpdatedUser: (balance: number, enableAutoDeduct: boolean) => IJoliCoin | IGem;
|
|
14
|
+
getUpdateKey: () => keyof Pick<IUnlockOptionsEvent, 'userJoliCoin' | 'userGem'>;
|
|
15
|
+
isEnough: (unlockOptions: IUnlockOptionsEvent | null, user: IJoliCoin | IGem) => boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const CURRENCY_HANDLERS: Record<CurrencyType, CurrencyHandler> = {
|
|
19
|
+
JOLI_COIN: {
|
|
20
|
+
fetchBalance: fetchJoliCoinBalance,
|
|
21
|
+
getQuantity: (unlockOptions) =>
|
|
22
|
+
unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0]
|
|
23
|
+
.joliCoinQuantity ?? 0,
|
|
24
|
+
getCurrentUser: (unlockOptions) => unlockOptions?.userJoliCoin ?? { balance: 0, enableAutoDeduct: false },
|
|
25
|
+
createUpdatedUser: (balance, enableAutoDeduct) => ({ balance, enableAutoDeduct }),
|
|
26
|
+
getUpdateKey: () => 'userJoliCoin',
|
|
27
|
+
isEnough: (unlockOptions, user) =>
|
|
28
|
+
unlockOptions?.options?.some(
|
|
29
|
+
(option) =>
|
|
30
|
+
option.type === 'JOLI_COIN' &&
|
|
31
|
+
option.joliCoinChoices.some((choice) => choice.joliCoinQuantity <= (user?.balance ?? 0))
|
|
32
|
+
) ?? false
|
|
33
|
+
},
|
|
34
|
+
JOLI_GEM: {
|
|
35
|
+
fetchBalance: fetchGemBalance,
|
|
36
|
+
getQuantity: (unlockOptions) =>
|
|
37
|
+
unlockOptions?.options?.find((option) => option.type === 'JOLI_GEM')?.joliGemChoices?.joliGemQuantity ??
|
|
38
|
+
0,
|
|
39
|
+
getCurrentUser: (unlockOptions) => unlockOptions?.userGem ?? { balance: 0, enableAutoDeduct: false },
|
|
40
|
+
createUpdatedUser: (balance, enableAutoDeduct) => ({ balance, enableAutoDeduct }),
|
|
41
|
+
getUpdateKey: () => 'userGem',
|
|
42
|
+
isEnough: (unlockOptions, user) =>
|
|
43
|
+
unlockOptions?.options?.some(
|
|
44
|
+
(option) =>
|
|
45
|
+
option.type === 'JOLI_GEM' && (option.joliGemChoices?.joliGemQuantity ?? 0) <= (user?.balance ?? 0)
|
|
46
|
+
) ?? false
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { registerUseJolicoinCommand,
|
|
1
|
+
export { registerUseJolicoinCommand, createShowUnlockWithCurrencyModal } from './use-jolicoin';
|
|
2
2
|
export { registerUsePaymentCommand, createInitiateAndAwaitPayment } from './use-payment';
|
|
3
3
|
export { registerUseUnloginCommand, createShowUnloginModal } from './use-unlogin';
|
|
@@ -1,33 +1,39 @@
|
|
|
1
|
-
import { IJoliCoin } from '@/common/rewards/type';
|
|
1
|
+
import { IGem, IJoliCoin } from '@/common/rewards/type';
|
|
2
2
|
import { rewardsCommands } from '../rewards-command';
|
|
3
3
|
import { unlockOptionsHandler } from '@/common/rewards/registers/utils/common';
|
|
4
4
|
import { createEventPromiseHandler } from '../../event-listener';
|
|
5
5
|
import { IUseModalResultEvent, UseModalResultEventName } from '@/common/rewards/reward-emitter';
|
|
6
6
|
import { rewardsEmitter, UseModalEventName } from '@/common/rewards/reward-emitter';
|
|
7
|
+
import { CURRENCY_HANDLERS } from './currency-handlers';
|
|
7
8
|
|
|
8
|
-
export const registerUseJolicoinCommand = (
|
|
9
|
-
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
9
|
+
export const registerUseJolicoinCommand = <T extends IJoliCoin | IGem>(
|
|
10
|
+
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
11
|
+
currency: 'JOLI_COIN' | 'JOLI_GEM',
|
|
10
12
|
params: {
|
|
11
|
-
|
|
13
|
+
showUnlockWithCurrencyModal: (params: {
|
|
12
14
|
enableAutoDeduct: boolean;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
userCurrency: T;
|
|
16
|
+
quantity: number;
|
|
17
|
+
currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM';
|
|
15
18
|
}) => Promise<'CONFIRM' | 'CANCEL' | 'FAILED'>;
|
|
16
19
|
}
|
|
17
20
|
) => {
|
|
18
|
-
const {
|
|
21
|
+
const { showUnlockWithCurrencyModal } = params;
|
|
19
22
|
rewardsCommands.registerCommand(`Rewards.${prefix}.useJolicoin`, async () => {
|
|
20
23
|
const unlockOptions = await unlockOptionsHandler.getData();
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
// Use currency handler for unified logic
|
|
26
|
+
const handler = CURRENCY_HANDLERS['JOLI_COIN'];
|
|
27
|
+
const currentUser = handler.getCurrentUser(unlockOptions) as IJoliCoin;
|
|
28
|
+
const joliCoinQuantity = handler.getQuantity(unlockOptions);
|
|
29
|
+
const enableAutoDeduct = !!currentUser?.enableAutoDeduct;
|
|
25
30
|
|
|
26
31
|
console.log('-----unlockOptions usemodal-----', unlockOptions, enableAutoDeduct, joliCoinQuantity);
|
|
27
|
-
const shouldUnlock = await
|
|
32
|
+
const shouldUnlock = await showUnlockWithCurrencyModal({
|
|
28
33
|
enableAutoDeduct,
|
|
29
|
-
|
|
30
|
-
joliCoinQuantity
|
|
34
|
+
userCurrency: currentUser as T,
|
|
35
|
+
quantity: joliCoinQuantity,
|
|
36
|
+
currency: currency as T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM'
|
|
31
37
|
});
|
|
32
38
|
|
|
33
39
|
if (shouldUnlock !== 'CONFIRM') {
|
|
@@ -42,22 +48,28 @@ export const registerUseJolicoinCommand = (
|
|
|
42
48
|
};
|
|
43
49
|
|
|
44
50
|
// EVENTS
|
|
45
|
-
export const
|
|
46
|
-
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
51
|
+
export const createShowUnlockWithCurrencyModal = <T extends IJoliCoin | IGem>(
|
|
52
|
+
type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
47
53
|
buttons: {
|
|
48
54
|
confirmButtonText: string;
|
|
49
55
|
cancelButtonText: string;
|
|
50
56
|
}
|
|
51
57
|
) => {
|
|
52
|
-
return async (params: {
|
|
58
|
+
return async (params: {
|
|
59
|
+
enableAutoDeduct: boolean;
|
|
60
|
+
userCurrency: T;
|
|
61
|
+
quantity: number;
|
|
62
|
+
currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM';
|
|
63
|
+
}) => {
|
|
53
64
|
const modalHandler = createEventPromiseHandler<IUseModalResultEvent, typeof UseModalResultEventName>(
|
|
54
65
|
rewardsEmitter,
|
|
55
66
|
UseModalResultEventName
|
|
56
67
|
);
|
|
57
68
|
rewardsEmitter.emit(UseModalEventName, type, {
|
|
58
69
|
enableAutoDeduct: params.enableAutoDeduct,
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
userCurrency: params.userCurrency as T,
|
|
71
|
+
quantity: params.quantity,
|
|
72
|
+
currency: params.currency,
|
|
61
73
|
confirmButtonText: buttons.confirmButtonText,
|
|
62
74
|
cancelButtonText: buttons.cancelButtonText
|
|
63
75
|
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { IJoliCoin } from '@/common/rewards/type';
|
|
2
|
-
import { fetchBalance } from '../fetch-balance';
|
|
1
|
+
import { IJoliCoin, IGem } from '@/common/rewards/type';
|
|
3
2
|
import { rewardsCommands } from '../rewards-command';
|
|
4
3
|
import { IHttpClient } from '@/common/http';
|
|
5
4
|
import { unlockOptionsHandler } from '../../common';
|
|
6
|
-
import { joliCoinIsEnough } from '../../index';
|
|
7
5
|
import { rewardsEmitter } from '@/common/rewards/reward-emitter';
|
|
8
6
|
import { createEventPromiseHandler } from '../../event-listener';
|
|
9
7
|
import {
|
|
@@ -11,39 +9,43 @@ import {
|
|
|
11
9
|
PaymentResultEventName,
|
|
12
10
|
InvokePaymentEventName
|
|
13
11
|
} from '@/common/rewards/reward-emitter';
|
|
12
|
+
import { CURRENCY_HANDLERS, CurrencyType } from './currency-handlers';
|
|
14
13
|
|
|
15
|
-
export const registerUsePaymentCommand = (
|
|
16
|
-
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
14
|
+
export const registerUsePaymentCommand = <T extends IJoliCoin | IGem>(
|
|
15
|
+
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
16
|
+
currency: 'JOLI_COIN' | 'JOLI_GEM',
|
|
17
17
|
params: {
|
|
18
18
|
httpClient: IHttpClient;
|
|
19
19
|
initiateAndAwaitPayment: (params: {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
userCurrency: T;
|
|
21
|
+
currencyQuantity: number;
|
|
22
|
+
currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM';
|
|
22
23
|
}) => Promise<'SUCCESS' | 'FAILED' | 'CANCEL'>;
|
|
23
24
|
}
|
|
24
25
|
) => {
|
|
25
26
|
const { httpClient, initiateAndAwaitPayment } = params;
|
|
26
27
|
rewardsCommands.registerCommand(`Rewards.${prefix}.usePayment`, async () => {
|
|
27
28
|
const unlockOptions = await unlockOptionsHandler.getData();
|
|
28
|
-
const newBalance = await fetchBalance(httpClient);
|
|
29
|
-
const joliCoinQuantity =
|
|
30
|
-
unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0]
|
|
31
|
-
.joliCoinQuantity ?? 0;
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
// Use currency handler for balance and quantity calculation
|
|
31
|
+
const handler = CURRENCY_HANDLERS[currency as CurrencyType];
|
|
32
|
+
const newBalance = await handler.fetchBalance(httpClient);
|
|
33
|
+
const currencyQuantity = handler.getQuantity(unlockOptions);
|
|
34
|
+
const currentUser = handler.getCurrentUser(unlockOptions);
|
|
35
|
+
|
|
36
|
+
const newUser = handler.createUpdatedUser(newBalance?.balance ?? 0, currentUser.enableAutoDeduct);
|
|
37
37
|
|
|
38
|
+
const updateKey = handler.getUpdateKey();
|
|
38
39
|
unlockOptionsHandler.updateData({
|
|
39
|
-
|
|
40
|
+
[updateKey]: newUser
|
|
40
41
|
});
|
|
41
42
|
|
|
42
|
-
console.log('-----unlockOptions payment-----', unlockOptions,
|
|
43
|
-
if (!
|
|
43
|
+
console.log('-----unlockOptions payment-----', unlockOptions, newUser, currencyQuantity);
|
|
44
|
+
if (!handler.isEnough(unlockOptions, newUser)) {
|
|
44
45
|
const paymentResult = await initiateAndAwaitPayment({
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
userCurrency: newUser as T,
|
|
47
|
+
currencyQuantity,
|
|
48
|
+
currency: currency as T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM'
|
|
47
49
|
});
|
|
48
50
|
|
|
49
51
|
return {
|
|
@@ -58,22 +60,27 @@ export const registerUsePaymentCommand = (
|
|
|
58
60
|
|
|
59
61
|
// events
|
|
60
62
|
export const createInitiateAndAwaitPayment = (
|
|
61
|
-
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
63
|
+
type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
62
64
|
buttons: {
|
|
63
65
|
confirmButtonText: string;
|
|
64
66
|
cancelButtonText: string;
|
|
65
67
|
}
|
|
66
68
|
) => {
|
|
67
|
-
return async
|
|
69
|
+
return async <T extends IJoliCoin | IGem>(params: {
|
|
70
|
+
userCurrency: T;
|
|
71
|
+
currencyQuantity: number;
|
|
72
|
+
currency: T extends IJoliCoin ? 'JOLI_COIN' : 'JOLI_GEM';
|
|
73
|
+
}) => {
|
|
68
74
|
const paymentHandler = createEventPromiseHandler<IPaymentEvent, typeof PaymentResultEventName>(
|
|
69
75
|
rewardsEmitter,
|
|
70
76
|
PaymentResultEventName
|
|
71
77
|
);
|
|
72
78
|
|
|
73
79
|
rewardsEmitter.emit(InvokePaymentEventName, type, {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
userCurrency: params.userCurrency as T,
|
|
81
|
+
quantity: params.currencyQuantity,
|
|
82
|
+
currency: params.currency,
|
|
83
|
+
enableAutoDeduct: params.userCurrency.enableAutoDeduct,
|
|
77
84
|
confirmButtonText: buttons.confirmButtonText,
|
|
78
85
|
cancelButtonText: buttons.cancelButtonText
|
|
79
86
|
});
|
|
@@ -3,23 +3,21 @@ import { rewardsCommands } from '../rewards-command';
|
|
|
3
3
|
import { createEventPromiseHandler } from '../../event-listener';
|
|
4
4
|
import { IUseUnloginModalResultEvent, UseUnloginModalResultEventName } from '@/common/rewards/reward-emitter';
|
|
5
5
|
import { rewardsEmitter, InvokeUnloginModalEventName } from '@/common/rewards/reward-emitter';
|
|
6
|
-
import { createToast } from '@jolibox/ui';
|
|
7
6
|
import { sleep } from '@jolibox/common';
|
|
8
|
-
import { fetchBalance } from '../fetch-balance';
|
|
9
7
|
import { IHttpClient } from '@/common/http';
|
|
10
8
|
import { unlockOptionsHandler } from '../../common';
|
|
9
|
+
import { CURRENCY_HANDLERS, CurrencyType } from './currency-handlers';
|
|
11
10
|
|
|
12
11
|
export const registerUseUnloginCommand = (
|
|
13
|
-
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
12
|
+
prefix: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
13
|
+
currency: 'JOLI_COIN' | 'JOLI_GEM',
|
|
14
14
|
params: {
|
|
15
15
|
httpClient: IHttpClient;
|
|
16
|
-
showUnloginModal: () => Promise<{
|
|
16
|
+
showUnloginModal: (currency: 'JOLI_COIN' | 'JOLI_GEM') => Promise<{
|
|
17
17
|
result: 'CANCEL' | 'FAILED' | 'SUCCESS' | 'ADS' | 'NOT_SUPPORT';
|
|
18
18
|
rewards?: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
claimedRewardValue: number;
|
|
22
|
-
};
|
|
19
|
+
isFirstLogin: boolean;
|
|
20
|
+
claimedRewardValue: number;
|
|
23
21
|
};
|
|
24
22
|
}>;
|
|
25
23
|
}
|
|
@@ -30,7 +28,7 @@ export const registerUseUnloginCommand = (
|
|
|
30
28
|
const unlockOptions = await unlockOptionsHandler.getData();
|
|
31
29
|
console.log('-----isLogin-----', !!isLogin);
|
|
32
30
|
if (!isLogin) {
|
|
33
|
-
const unloginModalResult = await showUnloginModal();
|
|
31
|
+
const unloginModalResult = await showUnloginModal(currency);
|
|
34
32
|
|
|
35
33
|
console.log('-----unloginModalResult-----', unloginModalResult);
|
|
36
34
|
if (unloginModalResult.result == 'FAILED' || unloginModalResult.result == 'CANCEL') {
|
|
@@ -39,27 +37,26 @@ export const registerUseUnloginCommand = (
|
|
|
39
37
|
};
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
const { isFirstLogin, claimedRewardValue } = unloginModalResult.rewards
|
|
40
|
+
const { isFirstLogin, claimedRewardValue } = unloginModalResult.rewards ?? {};
|
|
43
41
|
if (unloginModalResult.result == 'SUCCESS') {
|
|
44
42
|
if (isFirstLogin && claimedRewardValue) {
|
|
45
43
|
await sleep(3000); // wait for center toast to show
|
|
46
44
|
}
|
|
47
45
|
|
|
48
|
-
//check balance
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
//check balance using currency handler
|
|
47
|
+
const handler = CURRENCY_HANDLERS[currency as CurrencyType];
|
|
48
|
+
const newBalance = await handler.fetchBalance(httpClient);
|
|
49
|
+
const currencyQuantity = handler.getQuantity(unlockOptions);
|
|
50
|
+
const currentUser = handler.getCurrentUser(unlockOptions);
|
|
53
51
|
|
|
54
|
-
const
|
|
55
|
-
balance: newBalance?.balance ?? 0,
|
|
56
|
-
enableAutoDeduct: unlockOptions?.userJoliCoin?.enableAutoDeduct ?? false
|
|
57
|
-
};
|
|
52
|
+
const newUser = handler.createUpdatedUser(newBalance?.balance ?? 0, currentUser.enableAutoDeduct);
|
|
58
53
|
|
|
54
|
+
const updateKey = handler.getUpdateKey();
|
|
59
55
|
unlockOptionsHandler.updateData({
|
|
60
|
-
|
|
56
|
+
[updateKey]: newUser
|
|
61
57
|
});
|
|
62
|
-
|
|
58
|
+
|
|
59
|
+
if (newUser.balance >= currencyQuantity) {
|
|
63
60
|
return {
|
|
64
61
|
result: 'SUCCESS'
|
|
65
62
|
};
|
|
@@ -75,20 +72,21 @@ export const registerUseUnloginCommand = (
|
|
|
75
72
|
//EVENTS
|
|
76
73
|
|
|
77
74
|
export const createShowUnloginModal = (
|
|
78
|
-
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
75
|
+
type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM',
|
|
79
76
|
buttons: {
|
|
80
77
|
confirmButtonText: string;
|
|
81
78
|
cancelButtonText: string;
|
|
82
79
|
}
|
|
83
80
|
) => {
|
|
84
|
-
return async () => {
|
|
81
|
+
return async (currency: 'JOLI_COIN' | 'JOLI_GEM') => {
|
|
85
82
|
const modalHandler = createEventPromiseHandler<
|
|
86
83
|
IUseUnloginModalResultEvent,
|
|
87
84
|
typeof UseUnloginModalResultEventName
|
|
88
85
|
>(rewardsEmitter, UseUnloginModalResultEventName);
|
|
89
86
|
rewardsEmitter.emit(InvokeUnloginModalEventName, type, {
|
|
90
87
|
confirmButtonText: buttons.confirmButtonText,
|
|
91
|
-
cancelButtonText: buttons.cancelButtonText
|
|
88
|
+
cancelButtonText: buttons.cancelButtonText,
|
|
89
|
+
currency
|
|
92
90
|
});
|
|
93
91
|
const modalResult = await modalHandler.getFreshData();
|
|
94
92
|
return modalResult;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type CurrencyType = 'JOLI_COIN' | 'GEM';
|
|
2
|
+
|
|
3
|
+
export interface CurrencyConfig {
|
|
4
|
+
balanceEndpoint: string;
|
|
5
|
+
unlockEndpoint: string;
|
|
6
|
+
balanceDetailEndpoint: string;
|
|
7
|
+
unlockType: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const CURRENCY_CONFIG: Record<CurrencyType, CurrencyConfig> = {
|
|
11
|
+
JOLI_COIN: {
|
|
12
|
+
balanceEndpoint: '/api/joli-coin/balance',
|
|
13
|
+
balanceDetailEndpoint: '/api/joli-coin/balance-detail',
|
|
14
|
+
unlockEndpoint: '/api/joli-coin/unlock',
|
|
15
|
+
unlockType: 'JOLI_COIN'
|
|
16
|
+
},
|
|
17
|
+
GEM: {
|
|
18
|
+
balanceEndpoint: '/api/joli-gem/balance',
|
|
19
|
+
balanceDetailEndpoint: '/api/joli-gem/balance-detail',
|
|
20
|
+
unlockEndpoint: '/api/joli-gem/unlock',
|
|
21
|
+
unlockType: 'GEM'
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -1,134 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { uuidv4 } from '@jolibox/common';
|
|
4
|
-
import { UnlockOptionsEventName, IUnlockOptionsEvent } from '@/common/rewards/reward-emitter';
|
|
5
|
-
import { IAdBreakParams } from '@/common/ads';
|
|
6
|
-
import { EventPromiseHandler } from '../event-listener';
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
createInitiateAndAwaitPayment,
|
|
10
|
-
registerUseJolicoinCommand,
|
|
11
|
-
registerUsePaymentCommand,
|
|
12
|
-
registerUseUnloginCommand,
|
|
13
|
-
createShowUnlockWithJolicoinModal,
|
|
14
|
-
createShowUnloginModal
|
|
15
|
-
} from './commands';
|
|
16
|
-
import { rewardsCommands } from './rewards-command';
|
|
17
|
-
import { RewardsCommandType } from '@jolibox/types';
|
|
18
|
-
|
|
19
|
-
interface IJolicoinUnlockRes {
|
|
20
|
-
code: 'SUCCESS' | 'BALANCE_NOT_ENOUGH' | 'EPISODE_LOCK_JUMP' | 'EPISODE_UNLOCK_ALREADY';
|
|
21
|
-
message: string;
|
|
22
|
-
data: {
|
|
23
|
-
transactionId: string;
|
|
24
|
-
quantity: number;
|
|
25
|
-
balance: number;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const createCommonJolicoinRewardHandler = (
|
|
30
|
-
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
31
|
-
httpClient: IHttpClient,
|
|
32
|
-
{
|
|
33
|
-
handlers: {
|
|
34
|
-
handleUnlockSuccess,
|
|
35
|
-
handleUnlockFailed,
|
|
36
|
-
unlockOptionsHandler,
|
|
37
|
-
initiateAndAwaitPayment,
|
|
38
|
-
showUnlockWithJolicoinModal,
|
|
39
|
-
showUnloginModal
|
|
40
|
-
}
|
|
41
|
-
}: {
|
|
42
|
-
handlers: {
|
|
43
|
-
handleUnlockSuccess?: (data: { quantity: number; balance: number }) => void;
|
|
44
|
-
handleUnlockFailed?: (params: IAdBreakParams) => void;
|
|
45
|
-
unlockOptionsHandler: EventPromiseHandler<IUnlockOptionsEvent, typeof UnlockOptionsEventName>;
|
|
46
|
-
initiateAndAwaitPayment: ReturnType<typeof createInitiateAndAwaitPayment>;
|
|
47
|
-
showUnlockWithJolicoinModal: ReturnType<typeof createShowUnlockWithJolicoinModal>;
|
|
48
|
-
showUnloginModal: ReturnType<typeof createShowUnloginModal>;
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
) => {
|
|
52
|
-
// register commands
|
|
53
|
-
registerUseUnloginCommand(type, {
|
|
54
|
-
httpClient: httpClient,
|
|
55
|
-
showUnloginModal: showUnloginModal
|
|
56
|
-
});
|
|
57
|
-
registerUsePaymentCommand(type, {
|
|
58
|
-
httpClient: httpClient,
|
|
59
|
-
initiateAndAwaitPayment: initiateAndAwaitPayment
|
|
60
|
-
});
|
|
61
|
-
registerUseJolicoinCommand(type, {
|
|
62
|
-
showUnlockWithJolicoinModal: showUnlockWithJolicoinModal
|
|
63
|
-
});
|
|
64
|
-
return async (params: IAdBreakParams) => {
|
|
65
|
-
try {
|
|
66
|
-
let result = true;
|
|
67
|
-
|
|
68
|
-
const commands = [
|
|
69
|
-
`Rewards.${type}.useUnloginModal`,
|
|
70
|
-
`Rewards.${type}.usePayment`,
|
|
71
|
-
`Rewards.${type}.useJolicoin`
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
for (const command of commands) {
|
|
75
|
-
const commandResult = await rewardsCommands.executeCommand(command as RewardsCommandType);
|
|
76
|
-
result = result && commandResult.result !== 'FAILED';
|
|
77
|
-
if (commandResult.result !== 'CONTINUE') {
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!result) {
|
|
83
|
-
handleUnlockFailed?.(params);
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const unlockWithJolicoin = await httpClient.post<IJolicoinUnlockRes>('/api/joli-coin/unlock', {
|
|
88
|
-
data: {
|
|
89
|
-
// TODO: support drama
|
|
90
|
-
type: 'GAME_REWARD',
|
|
91
|
-
reqId: `${uuidv4()}-${context.mpType}-${Date.now()}`,
|
|
92
|
-
gameInfo: {
|
|
93
|
-
gameId: context.mpId
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
console.log('-----unlockWithJolicoin request result-----', unlockWithJolicoin);
|
|
99
|
-
if (unlockWithJolicoin.code == 'SUCCESS') {
|
|
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);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
handleUnlockSuccess?.({
|
|
115
|
-
quantity: unlockWithJolicoin.data.quantity,
|
|
116
|
-
balance: unlockWithJolicoin.data.balance
|
|
117
|
-
});
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
handleUnlockFailed?.(params);
|
|
121
|
-
return false;
|
|
122
|
-
} catch (e) {
|
|
123
|
-
console.info(`JolicoinRewardHandler error:`, e);
|
|
124
|
-
if (e instanceof Error && e.message == 'CANCEL') {
|
|
125
|
-
// Cancel should terminate the reward handler, invoke unlock failed
|
|
126
|
-
throw e;
|
|
127
|
-
}
|
|
128
|
-
handleUnlockFailed?.(params);
|
|
129
|
-
return false;
|
|
130
|
-
} finally {
|
|
131
|
-
unlockOptionsHandler.clearCache();
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
};
|
|
1
|
+
export { createCommonJolicoinRewardHandler } from './jolicoin/jolicoin-handler';
|
|
2
|
+
export { createCommonGemRewardHandler } from './joligem/gem-handler';
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { IHttpClient } from '@/common/http';
|
|
2
|
+
import { context } from '@/common/context';
|
|
3
|
+
import { uuidv4 } from '@jolibox/common';
|
|
4
|
+
import { UnlockOptionsEventName, IUnlockOptionsEvent } from '@/common/rewards/reward-emitter';
|
|
5
|
+
import { IAdBreakParams } from '@/common/ads';
|
|
6
|
+
import { EventPromiseHandler } from '../../event-listener';
|
|
7
|
+
import { CURRENCY_CONFIG, CurrencyType } from '../currency-config';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
createInitiateAndAwaitPayment,
|
|
11
|
+
registerUseJolicoinCommand,
|
|
12
|
+
registerUsePaymentCommand,
|
|
13
|
+
registerUseUnloginCommand,
|
|
14
|
+
createShowUnlockWithCurrencyModal,
|
|
15
|
+
createShowUnloginModal
|
|
16
|
+
} from '../commands';
|
|
17
|
+
import { rewardsCommands } from '../rewards-command';
|
|
18
|
+
import { RewardsCommandType } from '@jolibox/types';
|
|
19
|
+
|
|
20
|
+
interface ICurrencyUnlockRes {
|
|
21
|
+
code: 'SUCCESS' | 'BALANCE_NOT_ENOUGH' | 'EPISODE_LOCK_JUMP' | 'EPISODE_UNLOCK_ALREADY';
|
|
22
|
+
message: string;
|
|
23
|
+
data: {
|
|
24
|
+
transactionId: string;
|
|
25
|
+
quantity: number;
|
|
26
|
+
balance: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const createCommonCurrencyRewardHandler = (
|
|
31
|
+
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
32
|
+
httpClient: IHttpClient,
|
|
33
|
+
currencyType: CurrencyType,
|
|
34
|
+
{
|
|
35
|
+
handlers: {
|
|
36
|
+
handleUnlockSuccess,
|
|
37
|
+
handleUnlockFailed,
|
|
38
|
+
unlockOptionsHandler,
|
|
39
|
+
initiateAndAwaitPayment,
|
|
40
|
+
showUnlockWithCurrencyModal,
|
|
41
|
+
showUnloginModal
|
|
42
|
+
}
|
|
43
|
+
}: {
|
|
44
|
+
handlers: {
|
|
45
|
+
handleUnlockSuccess?: (data: { quantity: number; balance: number }) => void;
|
|
46
|
+
handleUnlockFailed?: (params: IAdBreakParams) => void;
|
|
47
|
+
unlockOptionsHandler: EventPromiseHandler<IUnlockOptionsEvent, typeof UnlockOptionsEventName>;
|
|
48
|
+
initiateAndAwaitPayment: ReturnType<typeof createInitiateAndAwaitPayment>;
|
|
49
|
+
showUnlockWithCurrencyModal: ReturnType<typeof createShowUnlockWithCurrencyModal>;
|
|
50
|
+
showUnloginModal: ReturnType<typeof createShowUnloginModal>;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
) => {
|
|
54
|
+
registerUseUnloginCommand(type, 'JOLI_COIN', {
|
|
55
|
+
httpClient: httpClient,
|
|
56
|
+
showUnloginModal: showUnloginModal
|
|
57
|
+
});
|
|
58
|
+
registerUsePaymentCommand(type, 'JOLI_COIN', {
|
|
59
|
+
httpClient: httpClient,
|
|
60
|
+
initiateAndAwaitPayment: initiateAndAwaitPayment
|
|
61
|
+
});
|
|
62
|
+
registerUseJolicoinCommand(type, 'JOLI_COIN', {
|
|
63
|
+
showUnlockWithCurrencyModal
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const commands = [`Rewards.${type}.usePayment`, `Rewards.${type}.useJolicoin`];
|
|
67
|
+
return async (params: IAdBreakParams) => {
|
|
68
|
+
try {
|
|
69
|
+
let result = true;
|
|
70
|
+
for (const command of commands) {
|
|
71
|
+
const commandResult = await rewardsCommands.executeCommand(command as RewardsCommandType);
|
|
72
|
+
result = result && commandResult.result !== 'FAILED';
|
|
73
|
+
if (commandResult.result !== 'CONTINUE') {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!result) {
|
|
79
|
+
handleUnlockFailed?.(params);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const config = CURRENCY_CONFIG[currencyType];
|
|
84
|
+
const unlockWithCurrency = await httpClient.post<ICurrencyUnlockRes>(config.unlockEndpoint, {
|
|
85
|
+
data: {
|
|
86
|
+
// TODO: support drama
|
|
87
|
+
type: 'GAME_REWARD',
|
|
88
|
+
reqId: `${uuidv4()}-${context.mpType}-${Date.now()}`,
|
|
89
|
+
gameInfo: {
|
|
90
|
+
gameId: context.mpId
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
console.log('-----unlockWithCurrency request result-----', unlockWithCurrency);
|
|
96
|
+
if (unlockWithCurrency.code == 'SUCCESS') {
|
|
97
|
+
try {
|
|
98
|
+
params.adBreakDone?.({
|
|
99
|
+
breakType: params.type,
|
|
100
|
+
breakName: 'name' in params ? params.name ?? '' : '',
|
|
101
|
+
breakFormat: 'reward',
|
|
102
|
+
breakStatus: 'viewed'
|
|
103
|
+
});
|
|
104
|
+
if ('adViewed' in params) {
|
|
105
|
+
params.adViewed?.();
|
|
106
|
+
}
|
|
107
|
+
} catch (e) {
|
|
108
|
+
console.error('-----unlockWithJolicoin adBreakDone error-----', e);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
handleUnlockSuccess?.({
|
|
112
|
+
quantity: unlockWithCurrency.data.quantity,
|
|
113
|
+
balance: unlockWithCurrency.data.balance
|
|
114
|
+
});
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
handleUnlockFailed?.(params);
|
|
118
|
+
return false;
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.info(`JolicoinRewardHandler error:`, e);
|
|
121
|
+
if (e instanceof Error && e.message == 'CANCEL') {
|
|
122
|
+
// Cancel should terminate the reward handler, invoke unlock failed
|
|
123
|
+
throw e;
|
|
124
|
+
}
|
|
125
|
+
handleUnlockFailed?.(params);
|
|
126
|
+
return false;
|
|
127
|
+
} finally {
|
|
128
|
+
unlockOptionsHandler.clearCache();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 向后兼容的 jolicoin 处理器
|
|
134
|
+
export const createCommonJolicoinRewardHandler = (
|
|
135
|
+
type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
|
|
136
|
+
httpClient: IHttpClient,
|
|
137
|
+
config: {
|
|
138
|
+
handlers: {
|
|
139
|
+
handleUnlockSuccess?: (data: { quantity: number; balance: number }) => void;
|
|
140
|
+
handleUnlockFailed?: (params: IAdBreakParams) => void;
|
|
141
|
+
unlockOptionsHandler: EventPromiseHandler<IUnlockOptionsEvent, typeof UnlockOptionsEventName>;
|
|
142
|
+
initiateAndAwaitPayment: ReturnType<typeof createInitiateAndAwaitPayment>;
|
|
143
|
+
showUnlockWithCurrencyModal: ReturnType<typeof createShowUnlockWithCurrencyModal>;
|
|
144
|
+
showUnloginModal: ReturnType<typeof createShowUnloginModal>;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
) => {
|
|
148
|
+
return createCommonCurrencyRewardHandler(type, httpClient, 'JOLI_COIN', config);
|
|
149
|
+
};
|