91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
/**
|
|
* Game Iframe SDK - React Hook
|
|
* Custom hook để sử dụng SDK trong React components
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* import { useGameIframeSDK } from 'game-iframe-sdk/react';
|
|
*
|
|
* function GamePlayer() {
|
|
* const iframeRef = useRef<HTMLIFrameElement>(null);
|
|
*
|
|
* const {
|
|
* isReady,
|
|
* sendGameData,
|
|
* sendLeaderboard
|
|
* } = useGameIframeSDK({
|
|
* iframeRef,
|
|
* iframeOrigin: 'http://senaai.vn:1357',
|
|
* onGameReady: () => console.log('Game ready!'),
|
|
* onAnswerReport: (data) => submitToServer(data),
|
|
* onFinalResult: (data) => showResults(data),
|
|
* });
|
|
*
|
|
* return <iframe ref={iframeRef} src={gameUrl} />;
|
|
* }
|
|
* ```
|
|
*/
|
|
import { GameIframeSDK } from './GameIframeSDK';
|
|
import { GameIframeSDKConfig, PushDataPayload, LeaderboardData, AnswerReportData, FinalResultData } from './types';
|
|
export interface UseGameIframeSDKOptions extends Omit<GameIframeSDKConfig, 'iframeOrigin'> {
|
|
/**
|
|
* Ref to iframe element
|
|
*/
|
|
iframeRef: React.RefObject<HTMLIFrameElement>;
|
|
/**
|
|
* Origin of iframe (required)
|
|
*/
|
|
iframeOrigin: string;
|
|
/**
|
|
* Callback when game is ready
|
|
*/
|
|
onGameReady?: () => void;
|
|
/**
|
|
* Callback when user answers a question
|
|
*/
|
|
onAnswerReport?: (data: AnswerReportData) => void;
|
|
/**
|
|
* Callback when game ends
|
|
*/
|
|
onFinalResult?: (data: FinalResultData) => void;
|
|
/**
|
|
* Callback when game requests leaderboard
|
|
*/
|
|
onLeaderboardRequest?: (top: number) => void;
|
|
/**
|
|
* Callback for errors
|
|
*/
|
|
onError?: (error: {
|
|
message: string;
|
|
error?: any;
|
|
}) => void;
|
|
}
|
|
export interface UseGameIframeSDKReturn {
|
|
/**
|
|
* SDK instance
|
|
*/
|
|
sdk: GameIframeSDK | null;
|
|
/**
|
|
* Whether game is ready
|
|
*/
|
|
isReady: boolean;
|
|
/**
|
|
* Send game data to iframe
|
|
*/
|
|
sendGameData: (data: PushDataPayload) => boolean;
|
|
/**
|
|
* Send leaderboard data
|
|
*/
|
|
sendLeaderboard: (data: LeaderboardData) => boolean;
|
|
/**
|
|
* Queue data to send when ready
|
|
*/
|
|
queueGameData: (data: PushDataPayload) => void;
|
|
/**
|
|
* Force reload iframe
|
|
*/
|
|
reloadIframe: () => boolean;
|
|
}
|
|
export declare function useGameIframeSDK(options: UseGameIframeSDKOptions): UseGameIframeSDKReturn;
|
|
export default useGameIframeSDK;
|
|
//# sourceMappingURL=useGameIframeSDK.d.ts.map
|