// rewards-emitter-wrapper.ts import { EventEmitter } from '@jolibox/common'; import { IUnlockOption, IJoliCoin, IGem } from './type'; export interface IUnlockOptionsEvent { options: IUnlockOption[]; userJoliCoin: IJoliCoin; userGem?: IGem; } export const UnlockOptionsEventName = 'UNLOCK_OPTIONS_CHANGED' as const; export const InvokePaymentEventName = 'INVOKE_PAYMENT' as const; export const PaymentResultEventName = 'ON_PAYMENT_RESULT' as const; export const UseModalEventName = 'ON_USE_MODAL_EVENT' as const; export const UseModalResultEventName = 'ON_USE_MODAL_RESULT' as const; export const UseModalFrequencyEventName = 'ON_USE_MODAL_FREQUENCY' as const; export const UseUnloginModalResultEventName = 'ON_USE_UNLOGIN_MODAL_EVENT' as const; export const InvokeUnloginModalEventName = 'INVOKE_UNLOGIN_MODAL_EVENT' as const; export const InvokeSubscriptionEventName = 'INVOKE_SUBSCRIPTION_EVENT' as const; export const UseSubscriptionResultEventName = 'ON_USE_SUBSCRIPTION_RESULT' as const; type IPaymentResult = 'SUCCESS' | 'FAILED' | 'CANCEL'; export interface IPaymentEvent { userJoliCoin?: IJoliCoin; userGem?: IGem; currency: 'JOLI_COIN' | 'JOLI_GEM'; paymentResult: IPaymentResult; } export interface IPaymentChoice { productId: string; totalAmountStr?: string; quantity: number; appStoreProductId?: string; } export interface IInvokePaymentEvent { userCurrency: IJoliCoin | IGem; quantity: number; currency: 'JOLI_COIN' | 'JOLI_GEM'; enableAutoDeduct: boolean; confirmButtonText: string; cancelButtonText: string; sequenceId?: string; } export interface IUseModalEvent { enableAutoDeduct: boolean; userCurrency: IJoliCoin | IGem; quantity: number; currency: 'JOLI_COIN' | 'JOLI_GEM'; confirmButtonText: string; cancelButtonText: string; sequenceId?: string; } export interface IUseUnloginModalEvent { confirmButtonText: string; cancelButtonText: string; currency: 'JOLI_COIN' | 'JOLI_GEM'; } export interface IUseModalResultEvent { useModalResult: 'CONFIRM' | 'CANCEL' | 'FAILED'; } export interface IUseUnloginModalResultEvent { result: 'SUCCESS' | 'CANCEL' | 'ADS' | 'FAILED' | 'NOT_SUPPORT'; rewards?: { claimedRewardValue: number; isFirstLogin: boolean; currency?: 'JOLI_COIN' | 'JOLI_GEM'; }; } export interface IUseModalFrequencyConfig { joliCoinUseAndCharge: { useJolicoin: { dailyMaxPopUps?: number; minInterval?: number; }; charge: { dailyMaxPopUps?: number; minInterval?: number; }; joinMember: { dailyMaxPopUps?: number; minInterval?: number; }; }; loginGuide: { show: boolean; dailyMaxPopUps?: number; minInterval?: number; }; } export interface IUseSubscriptionResultEvent { result: 'SUCCESS' | 'CANCEL' | 'FAILED'; } export interface RewardsEventMap extends Record { [UnlockOptionsEventName]: [IUnlockOptionsEvent]; [InvokePaymentEventName]: [ 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', IInvokePaymentEvent ]; [PaymentResultEventName]: [IPaymentEvent]; [UseModalResultEventName]: [IUseModalResultEvent]; [UseModalEventName]: ['JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', IUseModalEvent]; [UseModalFrequencyEventName]: [IUseModalFrequencyConfig]; [InvokeUnloginModalEventName]: [ 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', IUseUnloginModalEvent ]; [UseUnloginModalResultEventName]: [IUseUnloginModalResultEvent]; [InvokeSubscriptionEventName]: ['SUBSCRIPTION']; [UseSubscriptionResultEventName]: [IUseSubscriptionResultEvent]; } export const originalRewardsEmitter = new EventEmitter(); export type RewardsEventType = keyof RewardsEventMap; export interface RewardsEmitter { on(event: T, callback: (...args: RewardsEventMap[T]) => void): void; off(event: T, callback: (...args: RewardsEventMap[T]) => void): void; emit(event: T, ...args: RewardsEventMap[T]): void; } // 创建包装器 export const createRewardsEmitterWrapper = (): RewardsEmitter => { return { on: originalRewardsEmitter.on.bind(originalRewardsEmitter), off: originalRewardsEmitter.off.bind(originalRewardsEmitter), emit: originalRewardsEmitter.emit.bind(originalRewardsEmitter) }; }; export const rewardsEmitter = createRewardsEmitterWrapper(); export const DefaltJoliCoinUseAndCharge = { // 充值弹窗: charge: { dailyMaxPopUps: 2, // 一天内最多2次弹窗 minInterval: 8 * 60 * 60 * 1000 // 同一用户若在8小时内刚点过弹窗,则不再弹出 }, // 使用弹窗: useJolicoin: { dailyMaxPopUps: 2, minInterval: 8 * 60 * 60 * 1000 }, joinMember: { dailyMaxPopUps: 2, minInterval: 8 * 60 * 60 * 1000 } }; export const DefaltLoginGuide = { show: true, minInterval: 8 * 60 * 60 * 1000 };