mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-10 00:56:06 +00:00
Y8 API
This commit is contained in:
64
assets/Plugins/Y8/Scripts/Y8.ts
Normal file
64
assets/Plugins/Y8/Scripts/Y8.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Component, _decorator } from "cc";
|
||||
import { Y8API } from "./Y8def";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Y8")
|
||||
export class Y8 extends Component {
|
||||
@property(String) private appId = "ENTER APP ID";
|
||||
|
||||
public init(): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
// callback after Y8 script is loaded
|
||||
window.idAsyncInit = (): void => {
|
||||
// wait for app connection
|
||||
ID.Event.subscribe("id.init", resolve);
|
||||
ID.init({ appId: this.appId });
|
||||
};
|
||||
|
||||
// Load the script
|
||||
(function (d, s, id): void {
|
||||
const fjs = d.getElementsByTagName(s)[0];
|
||||
if (d.getElementById(id)) {
|
||||
return;
|
||||
}
|
||||
const js: HTMLScriptElement = <HTMLScriptElement>d.createElement(s);
|
||||
js.id = id;
|
||||
js.src = document.location.protocol == "https:" ? "https://cdn.y8.com/api/sdk.js" : "http://cdn.y8.com/api/sdk.js";
|
||||
fjs.parentNode.insertBefore(js, fjs);
|
||||
})(document, "script", "id-jssdk");
|
||||
});
|
||||
}
|
||||
|
||||
public login(): Promise<Y8API.Authorization> {
|
||||
console.log("Logging in");
|
||||
return new Promise<Y8API.Authorization>((resolve) => {
|
||||
ID.login((response: Y8API.Authorization) => {
|
||||
resolve(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async tryAutoLogin(): Promise<void> {
|
||||
const auth = await this.getLoginStatus();
|
||||
console.log(auth);
|
||||
if (auth?.status == "not_linked" || auth?.status == "uncomplete") {
|
||||
await this.login();
|
||||
}
|
||||
}
|
||||
|
||||
public async isLoggedIn(): Promise<boolean> {
|
||||
const auth = await this.getLoginStatus();
|
||||
return auth?.success == true;
|
||||
}
|
||||
|
||||
public sendCustomEvent(name: string, data?: string | number): void {
|
||||
ID.Analytics.custom_event(name, data);
|
||||
}
|
||||
|
||||
private getLoginStatus(): Promise<Y8API.Authorization> {
|
||||
return new Promise((resolve) => {
|
||||
ID.getLoginStatus(resolve);
|
||||
});
|
||||
}
|
||||
}
|
9
assets/Plugins/Y8/Scripts/Y8.ts.meta
Normal file
9
assets/Plugins/Y8/Scripts/Y8.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "02815b81-6c1e-4a09-8ae4-f4bcb375745a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
39
assets/Plugins/Y8/Scripts/Y8def.ts
Normal file
39
assets/Plugins/Y8/Scripts/Y8def.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
declare global {
|
||||
const ID: Y8API.ID;
|
||||
interface Window {
|
||||
idAsyncInit: () => void;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Y8API {
|
||||
export interface ID {
|
||||
init: (appInfo: { appId: string }) => void;
|
||||
getLoginStatus: (callback: (response?: Authorization) => void, skipCache?: boolean) => void;
|
||||
login: (callback: (response?: Authorization) => void) => void;
|
||||
register: (callback: (response?: Authorization) => void) => void;
|
||||
Event: Event;
|
||||
Analytics: Analytics;
|
||||
}
|
||||
|
||||
export interface Event {
|
||||
subscribe: (eventName: string, callback: (response?: any) => void) => void;
|
||||
}
|
||||
|
||||
export interface Analytics {
|
||||
custom_event: (name: string, data?: string | number) => void;
|
||||
}
|
||||
|
||||
export interface Authorization {
|
||||
status: string;
|
||||
success?: boolean;
|
||||
authResponse: {
|
||||
state: string;
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
scope: string;
|
||||
redirect_uri: string;
|
||||
details: any;
|
||||
};
|
||||
}
|
||||
}
|
9
assets/Plugins/Y8/Scripts/Y8def.ts.meta
Normal file
9
assets/Plugins/Y8/Scripts/Y8def.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "445aa0c8-388e-4665-aeee-0a51681e77e4",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user