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

162 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-06-12 04:48:19 -07:00
export type postMessageType = 'paypal_modal' | 'game_tournament';
export type minusTicketType = 'auth' | 'revive';
export interface SDK {
2024-06-18 02:59:16 -07:00
setCallAPI(value: boolean): void;
2024-06-19 23:37:55 -07:00
getEnv(): string;
2024-06-12 04:48:19 -07:00
getUserTicket(): number;
getTopScore(): number;
getLatestScore(): number;
getGameScore(): number;
2024-06-18 02:59:16 -07:00
getUserId(): string;
getGameTime(): number | null;
2024-06-12 04:48:19 -07:00
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>;
2024-06-18 02:59:16 -07:00
getLeaderBoard(): Promise<PlayerInfo[]>;
}
2024-06-19 23:37:55 -07:00
export enum Env {
development,
staging,
production,
}
2024-06-18 02:59:16 -07:00
export interface PlayerInfo {
userId: string;
displayName: string;
score: number;
2024-06-12 04:48:19 -07:00
}
export interface Reward {
id: string;
name: string;
description: string;
2024-06-19 23:37:55 -07:00
quantity: number;
thumbnailLink: string;
2024-06-12 04:48:19 -07:00
}
enum InitState {
UnInitialized,
Initialized,
}
export default class P4PSDK {
private static _initState: InitState = InitState.UnInitialized;
private static _sdk: SDK;
//#region PUBLIC METHOD
2024-06-19 23:37:55 -07:00
public static getEnv(): Env {
return Env[this._sdk?.getEnv() as keyof typeof Env];
}
2024-06-12 04:48:19 -07:00
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();
}
2024-06-18 02:59:16 -07:00
public static getUserId(): string {
return this._sdk.getUserId();
}
public static getGameTime(): number | null {
return this._sdk.getGameTime();
}
2024-06-12 04:48:19 -07:00
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';
2024-06-18 02:59:16 -07:00
tag.src = 'https://firebasestorage.googleapis.com/v0/b/play-now-1aef8.appspot.com/o/SDK%2Fsdk.js?alt=media';
2024-06-12 04:48:19 -07:00
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);
});
}
2024-06-18 02:59:16 -07:00
public static setCallAPI(value: boolean) {
this._sdk?.setCallAPI(value);
}
2024-06-12 04:48:19 -07:00
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);
}
2024-06-18 02:59:16 -07:00
public static async getLeaderBoard(): Promise<PlayerInfo[]> {
return this._sdk?.getLeaderBoard();
}
2024-06-12 04:48:19 -07:00
//#endregion
}