import { createCommands } from '@jolibox/common'; import { createAPI, registerCanIUse, t, canIUseNative } from './base'; import { taskTracker } from '../report'; import { emitNative, invokeNative } from '@jolibox/native-bridge'; import { context } from '@/common/context'; const commands = createCommands(); let lastScoreData: any = null; let scheduled = false; let lastEmitTime = 0; const THROTTLE_INTERVAL = 200; function emitScoreUpdate(data: any) { emitNative('cpUpdateScoreEvent', data, context.webviewId); } function throttledEmitScore(data: any) { lastScoreData = data; if (!scheduled) { scheduled = true; requestAnimationFrame(function frame() { const now = Date.now(); if (now - lastEmitTime >= THROTTLE_INTERVAL) { if (lastScoreData !== null) { emitScoreUpdate(lastScoreData); lastEmitTime = now; lastScoreData = null; } scheduled = false; } else { requestAnimationFrame(frame); } }); } } function reportTaskGameInfo( event: 'COMPLETE_GAME_LEVEL' | 'GAME_ENDED' | 'GAME_LEVEL_UP', inGameInfo: Record ) { return canIUseNative('notifyTaskGameInfoAsync') ? invokeNative('notifyTaskGameInfoAsync', { event, inGameInfo }) : taskTracker.reporter({ event, extraParams: { inGameInfo } }); } const onLevelFinished = createAPI('levelFinished', { paramsSchema: t.tuple( t.object({ levelId: t.or(t.string(), t.number()), duration: t.number().optional(), rating: t.number().optional(), score: t.number().optional(), variation: t.string().optional() }) ), implement: async (parmas) => { const { levelId, duration, rating, score, variation } = parmas; const tasks = []; tasks.push( taskTracker.tracker('LevelFinished', { levelId, duration, rating, score, variation }) ); tasks.push( reportTaskGameInfo('COMPLETE_GAME_LEVEL', { levelId, duration, rating, score }) ); throttledEmitScore({ levelId, duration, rating, score }); await Promise.all(tasks); } }); const onGamePlayEnded = createAPI('gamePlayEnded', { paramsSchema: t.tuple( t.object({ duration: t.number().optional(), rating: t.number().optional(), score: t.number(), variation: t.string().optional() }) ), implement: async (params) => { const { duration, rating, score, variation } = params; const tasks = []; tasks.push( taskTracker.tracker('GamePlayEnded', { duration, rating, score, variation }) ); tasks.push( reportTaskGameInfo('GAME_ENDED', { duration, rating, score }) ); throttledEmitScore({ duration, rating, score }); await Promise.all(tasks); } }); const onLevelUpgrade = createAPI('levelUpgrade', { paramsSchema: t.tuple( t.object({ levelId: t.or(t.string(), t.number()), name: t.string().optional() }) ), implement: async ({ levelId, name }) => { const tasks = []; tasks.push( taskTracker.tracker('LevelUpgrade', { name, levelId }) ); tasks.push( reportTaskGameInfo('GAME_LEVEL_UP', { levelId, levelName: name }) ); emitNative('cpUpdateScoreEvent', { levelId }, context.webviewId); await Promise.all(tasks); } }); const onLevelStart = createAPI('levelStart', { paramsSchema: t.tuple( t.object({ levelId: t.or(t.string(), t.number()) }) ), implement: async (params) => { const { levelId } = params; await taskTracker.tracker('LevelStart', { levelId }); } }); const onLevelFailed = createAPI('levelFailed', { paramsSchema: t.tuple( t.object({ levelId: t.or(t.string(), t.number()), score: t.number().optional(), rating: t.number().optional(), duration: t.number().optional() }) ), implement: async (params) => { const { levelId, score, rating, duration } = params; await taskTracker.tracker('LevelFailed', { levelId, score, rating, duration }); } }); commands.registerCommand('TaskTrackerSDK.levelFinished', onLevelFinished); commands.registerCommand('TaskTrackerSDK.gamePlayEnded', onGamePlayEnded); commands.registerCommand('TaskTrackerSDK.levelUpgrade', onLevelUpgrade); commands.registerCommand('TaskTrackerSDK.levelStart', onLevelStart); commands.registerCommand('TaskTrackerSDK.levelFailed', onLevelFailed); registerCanIUse('TaskTrackerSDK.onLevelFinished', { version: '1.1.25', properties: { params: { levelId: '1.1.25', duration: '1.1.25', rating: '1.1.25', score: '1.1.25', variation: '1.1.50' } } }); registerCanIUse('TaskTrackerSDK.onGamePlayEnded', { version: '1.1.25', properties: { params: { duration: '1.1.25', rating: '1.1.25', score: '1.1.25', variation: '1.1.50' } } }); registerCanIUse('TaskTrackerSDK.onLevelUpgrade', { version: '1.1.25', properties: { params: { levelId: '1.1.25', name: '1.1.25' } } }); registerCanIUse('TaskTrackerSDK.onLevelStart', { version: '1.3.8', properties: { params: { levelId: '1.3.8' } } }); registerCanIUse('TaskTrackerSDK.onLevelFailed', { version: '1.3.8', properties: { params: { levelId: '1.3.8', score: '1.3.8', rating: '1.3.8', duration: '1.3.8' } } });