export type postMessageType = 'paypal_modal' | 'game_tournament'; export type minusTicketType = 'auth' | 'revive'; export interface SDK { setCallAPI(value: boolean): void; getEnv(): string; getUserTicket(): number; getTopScore(): number; getLatestScore(): number; getGameScore(): number; getUserId(): string; getGameTime(): number | null; getTicketNeedToContinue(): number; init(buyTicketCallBack: () => any, thisArg?: any): void; updateScore(score: number): void; authenticate(): Promise; checkGameScoreTicket(): Promise; buyMoreTicket(): void; postScoreToServer(): void; minusTicket(type: minusTicketType): Promise; callPayPalModal(): void; canRelive(): boolean; spinGacha(id: string): Promise; getLeaderBoard(): Promise; } export enum Env { development, staging, production, } export interface PlayerInfo { userId: string; displayName: string; score: number; } export interface Reward { id: string; name: string; description: string; quantity: number; thumbnailLink: string; } enum InitState { UnInitialized, Initialized, } export default class P4PSDK { private static _initState: InitState = InitState.UnInitialized; private static _sdk: SDK; //#region PUBLIC METHOD public static getEnv(): Env { return Env[this._sdk?.getEnv() as keyof typeof Env]; } public static getUserTicket(): number { return this._sdk?.getUserTicket(); } public static getTopScore(): number { return this._sdk?.getTopScore(); } public static getLatestScore(): number { return this._sdk?.getLatestScore(); } public static getGameScore(): number { return this._sdk?.getGameScore(); } public static getUserId(): string { return this._sdk.getUserId(); } public static getGameTime(): number | null { return this._sdk.getGameTime(); } public static getTicketNeedToContinue(): number { return this._sdk?.getTicketNeedToContinue(); } private static async loadSDK(): Promise { return new Promise((resolve, reject) => { const tag = document.createElement('script'); tag.type = 'module'; tag.src = 'https://firebasestorage.googleapis.com/v0/b/play-now-1aef8.appspot.com/o/SDK%2Fsdk.js?alt=media'; tag.async = true; tag.onload = async () => { console.log('P4P SDK loaded'); this._sdk = (window as any).P4P.SDK; resolve(); }; tag.onerror = (e) => { console.error('Failed to load P4PSDK JS. Please check your internet connection.'); reject(e); }; document.head.appendChild(tag); }); } public static setCallAPI(value: boolean) { this._sdk?.setCallAPI(value); } public static async init(buyTicketCallBack: () => any, thisArg?: any): Promise { if (this._initState == InitState.Initialized) return; await this.loadSDK(); this._sdk.init(buyTicketCallBack, thisArg); this._initState = InitState.Initialized; } public static updateScore(score: number) { this._sdk?.updateScore(score); } public static async authenticate(): Promise { return this._sdk?.authenticate(); } public static async checkGameScoreTicket(): Promise { return this._sdk?.checkGameScoreTicket(); } public static buyMoreTicket() { this._sdk?.buyMoreTicket(); } public static async postScoreToServer() { this._sdk?.postScoreToServer(); } public static async minusTicket(type: minusTicketType): Promise { return this._sdk?.minusTicket(type); } public static callPayPalModal() { this._sdk?.callPayPalModal(); } public static canRelive(): boolean { return this._sdk?.canRelive(); } public static async spinGacha(id: string): Promise { return this._sdk?.spinGacha(id); } public static async getLeaderBoard(): Promise { return this._sdk?.getLeaderBoard(); } //#endregion }