pinball/assets/_Game/Scripts/P4PSDK/index.ts

125 lines
3.5 KiB
TypeScript

export type postMessageType = 'paypal_modal' | 'game_tournament';
export type minusTicketType = 'auth' | 'revive';
export interface SDK {
getUserTicket(): number;
getTopScore(): number;
getLatestScore(): number;
getGameScore(): number;
getTicketNeedToContinue(): number;
init(buyTicketCallBack: () => any, thisArg?: any): void;
updateScore(score: number): void;
authenticate(): Promise<boolean>;
checkGameScoreTicket(): Promise<boolean>;
buyMoreTicket(): void;
postScoreToServer(): void;
minusTicket(type: minusTicketType): Promise<boolean>;
callPayPalModal(): void;
canRelive(): boolean;
spinGacha(id: string): Promise<Reward>;
}
export interface Reward {
id: string;
name: string;
type: string;
description: string;
}
enum InitState {
UnInitialized,
Initialized,
}
export default class P4PSDK {
private static _initState: InitState = InitState.UnInitialized;
private static _sdk: SDK;
//#region PUBLIC METHOD
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 getTicketNeedToContinue(): number {
return this._sdk?.getTicketNeedToContinue();
}
private static async loadSDK(): Promise<void> {
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&token=dd9de5d9-3c09-40da-81ae-c4efae33a828';
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 async init(buyTicketCallBack: () => any, thisArg?: any): Promise<void> {
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<boolean> {
return this._sdk?.authenticate();
}
public static async checkGameScoreTicket(): Promise<boolean> {
return this._sdk?.checkGameScoreTicket();
}
public static buyMoreTicket() {
this._sdk?.buyMoreTicket();
}
public static async postScoreToServer() {
this._sdk?.postScoreToServer();
}
public static async minusTicket(type: minusTicketType): Promise<boolean> {
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<Reward> {
return this._sdk?.spinGacha(id);
}
//#endregion
}