import { IHttpClient } from '../http'; import { RewardsHelper, RewardType } from './reward-helper'; import { CachedRewardService } from './cached-reward-service'; import { IUnlockOption } from './type'; import { UnlockOptionsEventName, UseModalFrequencyEventName, rewardsEmitter, DefaltJoliCoinUseAndCharge, DefaltLoginGuide } from './reward-emitter'; import { hostEmitter } from '@jolibox/common'; import { StandardResponse, GlobalConfig } from '@jolibox/types'; const priority = () => { return (a: RewardType, b: RewardType) => { // Priority order: GEM > JOLI_COIN > ADS const priorityMap: Record = { SUBSCRIPTION: 4, JOLI_GEM: 3, JOLI_GEM_ONLY: 3, JOLI_COIN: 2, JOLI_COIN_ONLY: 2, ADS: 1 }; return priorityMap[b] - priorityMap[a]; }; }; const DefaultSupportedHostMethods = [ 'openJolicoinGuide', { method: 'getABTest', platforms: ['android'], nativeVersionCode: 1050100 }, 'userTrackAsync', { method: 'openMiniapp', nativeVersionCode: 1050100 } ]; const sortRewards = (rewardsTypes: RewardType[]): RewardType[] => { if (!rewardsTypes.length) return ['ADS']; // Handle JOLI_GEM cases if (rewardsTypes.includes('JOLI_GEM')) { if (rewardsTypes.length === 1) { return ['JOLI_GEM_ONLY']; } else if (rewardsTypes.includes('SUBSCRIPTION') && rewardsTypes.length === 2) { return ['SUBSCRIPTION', 'JOLI_GEM_ONLY']; } } // Handle JOLI_COIN cases if (rewardsTypes.includes('JOLI_COIN')) { if (rewardsTypes.length === 1) { return ['JOLI_COIN_ONLY']; } else if (rewardsTypes.includes('SUBSCRIPTION') && rewardsTypes.length === 2) { return ['SUBSCRIPTION', 'JOLI_COIN_ONLY']; } } return rewardsTypes.sort(priority()); }; // 创建缓存奖励服务的单例 let cachedRewardServiceInstance: CachedRewardService | null = null; function getCachedRewardService(httpClient: IHttpClient): CachedRewardService { if (!cachedRewardServiceInstance) { cachedRewardServiceInstance = new CachedRewardService(httpClient); } return cachedRewardServiceInstance; } /** * 使用缓存的奖励获取器 * 优化版本:unlockOptions 使用缓存,余额信息实时获取 */ export const createCachedRewardFetcher = (rewardsHelper: RewardsHelper) => { rewardsHelper.registerRewardsFetcher(async (httpClient: IHttpClient) => { const defaultRewards: RewardType[] = ['ADS']; const cachedRewardService = getCachedRewardService(httpClient); try { const res = await cachedRewardService.getUnlockOptions({}); if (res.code !== 'SUCCESS') { return defaultRewards; } // 发送事件通知 rewardsEmitter.emit(UnlockOptionsEventName, { options: res.data?.unlockOptions || [], userJoliCoin: res.extra?.joliCoin || { balance: 0, enableAutoDeduct: false }, userGem: res.extra?.joliGem || { balance: 0, enableAutoDeduct: false } }); const rewardsTypes = res.data?.unlockOptions?.map((option: IUnlockOption) => option.type) || Array.from(defaultRewards); return sortRewards(rewardsTypes); } catch (e) { console.error('getRewardOptions error (cached):', e); return defaultRewards; } }); }; /** * 使用缓存的频率配置获取器 * 优化版本:全局配置使用纯缓存 */ export const createCachedRewardFrequencyConfigFetcher = (rewardsHelper: RewardsHelper) => { rewardsHelper.registerRewardFrequencyConfigFetcher(async (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); try { const res = await cachedRewardService.getGlobalConfig(); // 发送事件通知 rewardsEmitter.emit(UseModalFrequencyEventName, { joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge, loginGuide: res.data?.loginGuide || DefaltLoginGuide }); res.data && hostEmitter.emit('onGlobalConfigChanged', { globalConfig: res.data }); return { joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge, loginGuide: res.data?.loginGuide || DefaltLoginGuide }; } catch (e) { console.error('getGlobalConfig error (cached):', e); // 发送兜底事件通知 rewardsEmitter.emit(UseModalFrequencyEventName, { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge, loginGuide: DefaltLoginGuide }); hostEmitter.emit('onGlobalConfigChanged', { globalConfig: { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge, loginGuide: DefaltLoginGuide, supportedHostMethods: DefaultSupportedHostMethods } }); return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge, loginGuide: DefaltLoginGuide }; } }); }; /** * 获取缓存服务实例(用于手动管理缓存) */ export const getRewardCacheService = (httpClient: IHttpClient): CachedRewardService => { return getCachedRewardService(httpClient); }; /** * 强制刷新奖励数据缓存 */ export const refreshRewardCache = async (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); await cachedRewardService.refreshUnlockOptions({}); console.log('Reward cache refreshed'); }; /** * 强制刷新全局配置缓存 */ export const refreshGlobalConfigCache = async (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); await cachedRewardService.refreshGlobalConfig(); console.log('Global config cache refreshed'); }; /** * 预热所有缓存 */ export const warmupRewardCache = async (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); await cachedRewardService.warmupCache(); console.log('Reward cache warmed up'); }; /** * 清除所有奖励相关缓存 */ export const clearRewardCache = (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); cachedRewardService.clearAllCache(); console.log('All reward cache cleared'); }; /** * 获取缓存统计信息 */ export const getRewardCacheStats = (httpClient: IHttpClient) => { const cachedRewardService = getCachedRewardService(httpClient); return cachedRewardService.getCacheStats(); }; // 保持原有的创建器以便向后兼容 export const createRewardFetcher = (rewardsHelper: RewardsHelper) => { rewardsHelper.registerRewardsFetcher(async (httpClient: IHttpClient) => { const defaultRewards: RewardType[] = ['ADS']; try { const res = await httpClient.post('/api/games/unlock-options', {}); if (res.code !== 'SUCCESS') { return defaultRewards; } console.log('-----res fetch reward-----', res); rewardsEmitter.emit(UnlockOptionsEventName, { options: res.data?.unlockOptions || [], userJoliCoin: res.extra?.joliCoin || { balance: 0, enableAutoDeduct: false }, userGem: res.extra?.joliGem || { balance: 0, enableAutoDeduct: false } }); const rewardsTypes = res.data?.unlockOptions?.map((option: IUnlockOption) => option.type) || Array.from(defaultRewards); return sortRewards(rewardsTypes); } catch (e) { console.error('getRewardOptions error:', e); return defaultRewards; } }); }; export const createRewardFrequencyConfigFetcher = (rewardsHelper: RewardsHelper) => { rewardsHelper.registerRewardFrequencyConfigFetcher(async (httpClient: IHttpClient) => { const res: StandardResponse = await httpClient.get( '/api/fe-configs/web-common/global-config', {} ); rewardsEmitter.emit(UseModalFrequencyEventName, { joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge, loginGuide: res.data?.loginGuide || DefaltLoginGuide }); res.data && hostEmitter.emit('onGlobalConfigChanged', { globalConfig: res.data }); return { joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge, loginGuide: res.data?.loginGuide || DefaltLoginGuide }; }); };