@jolibox/implement 1.1.56 → 1.2.1

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.
Files changed (61) hide show
  1. package/.rush/temp/package-deps_build.json +35 -28
  2. package/dist/common/rewards/index.d.ts +2 -0
  3. package/dist/common/rewards/registers/use-gem-only.d.ts +10 -0
  4. package/dist/common/rewards/registers/use-gem.d.ts +10 -0
  5. package/dist/common/rewards/registers/utils/coins/commands/currency-handlers.d.ts +15 -0
  6. package/dist/common/rewards/registers/utils/coins/commands/index.d.ts +1 -1
  7. package/dist/common/rewards/registers/utils/coins/commands/use-jolicoin.d.ts +10 -8
  8. package/dist/common/rewards/registers/utils/coins/commands/use-payment.d.ts +10 -8
  9. package/dist/common/rewards/registers/utils/coins/commands/use-unlogin.d.ts +6 -8
  10. package/dist/common/rewards/registers/utils/coins/currency-config.d.ts +8 -0
  11. package/dist/common/rewards/registers/utils/coins/index.d.ts +2 -18
  12. package/dist/common/rewards/registers/utils/coins/jolicoin/jolicoin-handler.d.ts +18 -0
  13. package/dist/common/rewards/registers/utils/coins/joligem/fetch-gem-balance.d.ts +4 -0
  14. package/dist/common/rewards/registers/utils/coins/joligem/gem-handler.d.ts +19 -0
  15. package/dist/common/rewards/registers/utils/index.d.ts +3 -1
  16. package/dist/common/rewards/reward-emitter.d.ts +23 -13
  17. package/dist/common/rewards/reward-helper.d.ts +3 -1
  18. package/dist/common/rewards/type.d.ts +10 -1
  19. package/dist/index.js +9 -9
  20. package/dist/index.native.js +68 -53
  21. package/dist/native/api/ads.d.ts +4 -0
  22. package/dist/native/payment/payment-helper.d.ts +5 -1
  23. package/dist/native/rewards/check-frequency.d.ts +24 -0
  24. package/dist/native/rewards/ui/utils.d.ts +1 -1
  25. package/implement.build.log +2 -2
  26. package/package.json +5 -5
  27. package/src/common/rewards/__tests__/can-use-jolicoin.test.ts +265 -9
  28. package/src/common/rewards/fetch-reward.ts +18 -3
  29. package/src/common/rewards/index.ts +2 -0
  30. package/src/common/rewards/registers/use-gem-only.ts +58 -0
  31. package/src/common/rewards/registers/use-gem.ts +53 -0
  32. package/src/common/rewards/registers/use-jolicoin-only.ts +4 -4
  33. package/src/common/rewards/registers/use-jolicoin.ts +4 -4
  34. package/src/common/rewards/registers/utils/coins/commands/currency-handlers.ts +50 -0
  35. package/src/common/rewards/registers/utils/coins/commands/index.ts +1 -1
  36. package/src/common/rewards/registers/utils/coins/commands/use-jolicoin.ts +31 -19
  37. package/src/common/rewards/registers/utils/coins/commands/use-payment.ts +32 -25
  38. package/src/common/rewards/registers/utils/coins/commands/use-unlogin.ts +22 -24
  39. package/src/common/rewards/registers/utils/coins/currency-config.ts +23 -0
  40. package/src/common/rewards/registers/utils/coins/index.ts +2 -139
  41. package/src/common/rewards/registers/utils/coins/jolicoin/jolicoin-handler.ts +148 -0
  42. package/src/common/rewards/registers/utils/coins/joligem/fetch-gem-balance.ts +15 -0
  43. package/src/common/rewards/registers/utils/coins/joligem/gem-handler.ts +130 -0
  44. package/src/common/rewards/registers/utils/index.ts +20 -1
  45. package/src/common/rewards/reward-emitter.ts +23 -13
  46. package/src/common/rewards/reward-helper.ts +3 -1
  47. package/src/common/rewards/type.ts +13 -1
  48. package/src/h5/api/ads.ts +3 -0
  49. package/src/h5/rewards/index.ts +33 -20
  50. package/src/native/api/ads.ts +69 -1
  51. package/src/native/api/call-host-method.ts +36 -1
  52. package/src/native/bootstrap/index.ts +11 -7
  53. package/src/native/payment/index.ts +2 -0
  54. package/src/native/payment/payment-helper.ts +2 -1
  55. package/src/native/rewards/check-frequency.ts +10 -0
  56. package/src/native/rewards/ui/payment-modal.ts +115 -34
  57. package/src/native/rewards/ui/unlogin-modal.ts +182 -87
  58. package/src/native/rewards/ui/use-modal.ts +129 -68
  59. package/src/native/rewards/ui/utils.ts +2 -2
  60. /package/dist/common/rewards/registers/utils/coins/{fetch-balance.d.ts → jolicoin/fetch-balance.d.ts} +0 -0
  61. /package/src/common/rewards/registers/utils/coins/{fetch-balance.ts → jolicoin/fetch-balance.ts} +0 -0
@@ -0,0 +1,50 @@
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')?.joliGemChoice?.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
+ const joliGemConfig = unlockOptions?.options.find((option) => option.type === 'JOLI_GEM');
44
+ if (joliGemConfig) {
45
+ return (joliGemConfig.joliGemChoice?.joliGemQuantity ?? 0) <= (user?.balance ?? 0);
46
+ }
47
+ return false;
48
+ }
49
+ }
50
+ };
@@ -1,3 +1,3 @@
1
- export { registerUseJolicoinCommand, createShowUnlockWithJolicoinModal } from './use-jolicoin';
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
- showUnlockWithJolicoinModal: (params: {
13
+ showUnlockWithCurrencyModal: (params: {
12
14
  enableAutoDeduct: boolean;
13
- userJoliCoin: IJoliCoin;
14
- joliCoinQuantity: number;
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 { showUnlockWithJolicoinModal } = params;
21
+ const { showUnlockWithCurrencyModal } = params;
19
22
  rewardsCommands.registerCommand(`Rewards.${prefix}.useJolicoin`, async () => {
20
23
  const unlockOptions = await unlockOptionsHandler.getData();
21
- const enableAutoDeduct = !!unlockOptions?.userJoliCoin?.enableAutoDeduct;
22
- const joliCoinQuantity =
23
- unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0]
24
- .joliCoinQuantity ?? 0;
24
+
25
+ // Use currency handler for unified logic
26
+ const handler = CURRENCY_HANDLERS[currency];
27
+ const currentUser = handler.getCurrentUser(unlockOptions);
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 showUnlockWithJolicoinModal({
32
+ const shouldUnlock = await showUnlockWithCurrencyModal({
28
33
  enableAutoDeduct,
29
- userJoliCoin: unlockOptions?.userJoliCoin,
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 createShowUnlockWithJolicoinModal = (
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: { enableAutoDeduct: boolean; userJoliCoin: IJoliCoin; joliCoinQuantity: number }) => {
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
- userJoliCoin: params.userJoliCoin,
60
- joliCoinQuantity: params.joliCoinQuantity,
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
- userJoliCoin: IJoliCoin;
21
- joliCoinQuantity: number;
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
- const newJoliCoin = {
34
- balance: newBalance?.balance ?? 0,
35
- enableAutoDeduct: unlockOptions?.userJoliCoin?.enableAutoDeduct ?? false
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
- userJoliCoin: newJoliCoin
40
+ [updateKey]: newUser
40
41
  });
41
42
 
42
- console.log('-----unlockOptions payment-----', unlockOptions, newJoliCoin, joliCoinQuantity);
43
- if (!joliCoinIsEnough(unlockOptions?.options || [], newJoliCoin)) {
43
+ console.log('-----unlockOptions payment-----', unlockOptions, newUser, currencyQuantity, currency);
44
+ if (!handler.isEnough(unlockOptions, newUser)) {
44
45
  const paymentResult = await initiateAndAwaitPayment({
45
- userJoliCoin: newJoliCoin,
46
- joliCoinQuantity
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 (params: { userJoliCoin: IJoliCoin; joliCoinQuantity: number }) => {
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
- userJoliCoin: params.userJoliCoin,
75
- joliCoinQuantity: params.joliCoinQuantity,
76
- enableAutoDeduct: params.userJoliCoin.enableAutoDeduct,
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
- jolicoin?: {
20
- isFirstLogin: boolean;
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?.jolicoin ?? {};
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 newBalance = await fetchBalance(httpClient);
50
- const joliCoinQuantity =
51
- unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0]
52
- .joliCoinQuantity ?? 0;
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 newJoliCoin = {
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
- userJoliCoin: newJoliCoin
56
+ [updateKey]: newUser
61
57
  });
62
- if (newJoliCoin.balance >= joliCoinQuantity) {
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,139 +1,2 @@
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
-
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
-
65
- const targetABTest = context.abTests.find((abTest) => abTest.startsWith('coinOpt_v1'));
66
- let commands = [];
67
- if (targetABTest) {
68
- commands = [`Rewards.${type}.usePayment`, `Rewards.${type}.useJolicoin`];
69
- } else {
70
- commands = [
71
- `Rewards.${type}.useUnloginModal`,
72
- `Rewards.${type}.usePayment`,
73
- `Rewards.${type}.useJolicoin`
74
- ];
75
- }
76
- return async (params: IAdBreakParams) => {
77
- try {
78
- let result = true;
79
- for (const command of commands) {
80
- const commandResult = await rewardsCommands.executeCommand(command as RewardsCommandType);
81
- result = result && commandResult.result !== 'FAILED';
82
- if (commandResult.result !== 'CONTINUE') {
83
- break;
84
- }
85
- }
86
-
87
- if (!result) {
88
- handleUnlockFailed?.(params);
89
- return false;
90
- }
91
-
92
- const unlockWithJolicoin = await httpClient.post<IJolicoinUnlockRes>('/api/joli-coin/unlock', {
93
- data: {
94
- // TODO: support drama
95
- type: 'GAME_REWARD',
96
- reqId: `${uuidv4()}-${context.mpType}-${Date.now()}`,
97
- gameInfo: {
98
- gameId: context.mpId
99
- }
100
- }
101
- });
102
-
103
- console.log('-----unlockWithJolicoin request result-----', unlockWithJolicoin);
104
- if (unlockWithJolicoin.code == 'SUCCESS') {
105
- try {
106
- params.adBreakDone?.({
107
- breakType: params.type,
108
- breakName: 'name' in params ? params.name ?? '' : '',
109
- breakFormat: 'reward',
110
- breakStatus: 'viewed'
111
- });
112
- if ('adViewed' in params) {
113
- params.adViewed?.();
114
- }
115
- } catch (e) {
116
- console.error('-----unlockWithJolicoin adBreakDone error-----', e);
117
- }
118
-
119
- handleUnlockSuccess?.({
120
- quantity: unlockWithJolicoin.data.quantity,
121
- balance: unlockWithJolicoin.data.balance
122
- });
123
- return true;
124
- }
125
- handleUnlockFailed?.(params);
126
- return false;
127
- } catch (e) {
128
- console.info(`JolicoinRewardHandler error:`, e);
129
- if (e instanceof Error && e.message == 'CANCEL') {
130
- // Cancel should terminate the reward handler, invoke unlock failed
131
- throw e;
132
- }
133
- handleUnlockFailed?.(params);
134
- return false;
135
- } finally {
136
- unlockOptionsHandler.clearCache();
137
- }
138
- };
139
- };
1
+ export { createCommonJolicoinRewardHandler } from './jolicoin/jolicoin-handler';
2
+ export { createCommonGemRewardHandler } from './joligem/gem-handler';
@@ -0,0 +1,148 @@
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
+ type: 'GAME_REWARD',
87
+ reqId: `${uuidv4()}-${context.mpType}-${Date.now()}`,
88
+ gameInfo: {
89
+ gameId: context.mpId
90
+ }
91
+ }
92
+ });
93
+
94
+ console.log('-----unlockWithCurrency request result-----', unlockWithCurrency);
95
+ if (unlockWithCurrency.code == 'SUCCESS') {
96
+ try {
97
+ params.adBreakDone?.({
98
+ breakType: params.type,
99
+ breakName: 'name' in params ? params.name ?? '' : '',
100
+ breakFormat: 'reward',
101
+ breakStatus: 'viewed'
102
+ });
103
+ if ('adViewed' in params) {
104
+ params.adViewed?.();
105
+ }
106
+ } catch (e) {
107
+ console.error('-----unlockWithJolicoin adBreakDone error-----', e);
108
+ }
109
+
110
+ handleUnlockSuccess?.({
111
+ quantity: unlockWithCurrency.data.quantity,
112
+ balance: unlockWithCurrency.data.balance
113
+ });
114
+ return true;
115
+ }
116
+ handleUnlockFailed?.(params);
117
+ return false;
118
+ } catch (e) {
119
+ console.info(`JolicoinRewardHandler error:`, e);
120
+ if (e instanceof Error && e.message == 'CANCEL') {
121
+ // Cancel should terminate the reward handler, invoke unlock failed
122
+ throw e;
123
+ }
124
+ handleUnlockFailed?.(params);
125
+ return false;
126
+ } finally {
127
+ unlockOptionsHandler.clearCache();
128
+ }
129
+ };
130
+ };
131
+
132
+ // 向后兼容的 jolicoin 处理器
133
+ export const createCommonJolicoinRewardHandler = (
134
+ type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
135
+ httpClient: IHttpClient,
136
+ config: {
137
+ handlers: {
138
+ handleUnlockSuccess?: (data: { quantity: number; balance: number }) => void;
139
+ handleUnlockFailed?: (params: IAdBreakParams) => void;
140
+ unlockOptionsHandler: EventPromiseHandler<IUnlockOptionsEvent, typeof UnlockOptionsEventName>;
141
+ initiateAndAwaitPayment: ReturnType<typeof createInitiateAndAwaitPayment>;
142
+ showUnlockWithCurrencyModal: ReturnType<typeof createShowUnlockWithCurrencyModal>;
143
+ showUnloginModal: ReturnType<typeof createShowUnloginModal>;
144
+ };
145
+ }
146
+ ) => {
147
+ return createCommonCurrencyRewardHandler(type, httpClient, 'JOLI_COIN', config);
148
+ };