pinball/assets/_Game/Scripts/API/BEConnector.ts

190 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-02-27 18:19:33 -08:00
import { _decorator } from 'cc';
2024-02-28 03:25:11 -08:00
import * as CryptoES from 'crypto-es';
2024-03-06 01:28:01 -08:00
import { GameManager } from '../Manager/GameManager';
2024-02-27 18:19:33 -08:00
export let CryptoESDefault = CryptoES.default;
const { ccclass, property } = _decorator;
@ccclass('BEConnector')
export default class BEConnector {
public static _instance: BEConnector = null;
private token: string;
private skinId: string;
private tournamentId: string;
private key: string;
private deviceInfo: string;
// Ticket infors
public numberTicket: number;
public maxScore: number;
private mileStone: string;
2024-02-28 03:25:11 -08:00
private gameURL: string = '';
2024-02-27 18:19:33 -08:00
public static get instance() {
if (null == this._instance) {
this._instance = new BEConnector();
}
return this._instance;
}
constructor() {
this.getGameData();
}
public getGameData() {
let url = new URLSearchParams(window.location.search);
2024-02-28 03:25:11 -08:00
this.token = url.get('token');
this.skinId = url.get('skinId');
this.tournamentId = url.get('tournamentId');
this.deviceInfo = url.get('deviceInfo');
2024-02-27 18:19:33 -08:00
this.numberTicket = parseInt(url.get('numberTicket'));
this.maxScore = parseInt(url.get('maxScore'));
2024-02-28 03:25:11 -08:00
this.mileStone = url.get('mileStone');
2024-02-27 18:19:33 -08:00
this.gameURL = ENV_CONFIG[url.get('env')];
}
public async authenticate() {
2024-02-28 03:25:11 -08:00
await fetch(
`${this.gameURL}/promotions/authenticate-tournament?token=${this.token}&tournamentId=${this.tournamentId}&skinId=${this.skinId}&deviceInfo=${this.deviceInfo}`,
)
2024-02-27 18:19:33 -08:00
.then((response) => {
if (response.ok) {
return response.json();
}
})
2024-02-28 03:25:11 -08:00
.then((data) => {
2024-02-27 18:19:33 -08:00
if (data.ResultCode == 1) {
this.key = data.Data.Key;
2024-02-28 03:25:11 -08:00
console.log('authen success', this.key);
} else {
throw new Error('');
2024-02-27 18:19:33 -08:00
}
})
2024-02-28 03:25:11 -08:00
.catch((err) => console.log('authen failed'));
2024-02-27 18:19:33 -08:00
}
2024-02-28 03:25:11 -08:00
public ticketMinus(type: 'auth' | 'revive') {
let numberTicket = type === 'auth' ? 1 : this.getTicketCanBeMinus();
2024-02-27 18:19:33 -08:00
let dataEncrypted: string = this.getDataEncrypted({ type: type, total: numberTicket });
fetch(`${this.gameURL}/promotions/ticket-minus/${this.tournamentId}/${this.skinId}?cocos=1`, {
headers: {
2024-02-28 03:25:11 -08:00
Accept: 'application/json',
2024-02-27 18:19:33 -08:00
'Content-Type': 'application/json',
'x-access-refactor-token': this.token,
},
2024-02-28 03:25:11 -08:00
method: 'POST',
body: JSON.stringify({ data: dataEncrypted }),
}).then(() => {
this.numberTicket -= numberTicket;
});
2024-02-27 18:19:33 -08:00
}
public calculatingTicketToContinue(scoreRange: object, yourScore: number) {
let closestMilestone: number = 0;
for (const milestone in scoreRange) {
if (parseInt(milestone) <= yourScore) {
closestMilestone = scoreRange[milestone];
}
}
if (!closestMilestone) {
const minValue = Math.min(...Object.values(scoreRange));
closestMilestone = minValue;
}
2024-02-28 03:25:11 -08:00
return closestMilestone;
2024-02-27 18:19:33 -08:00
}
public async checkGameScoreTicket() {
2024-03-06 01:28:01 -08:00
let totalScore: number = GameManager.instance.score;
2024-02-28 03:25:11 -08:00
let dataEncrypted: string = this.getDataEncrypted({ score: totalScore, ticket: this.getTicketCanBeMinus() });
2024-02-27 18:19:33 -08:00
await fetch(`${this.gameURL}/promotions/check-game-score-ticket/${this.tournamentId}/${this.skinId}?cocos=1`, {
headers: {
2024-02-28 03:25:11 -08:00
Accept: 'application/json',
2024-02-27 18:19:33 -08:00
'Content-Type': 'application/json',
'x-access-refactor-token': this.token,
},
2024-02-28 03:25:11 -08:00
method: 'POST',
body: JSON.stringify({ data: dataEncrypted }),
});
2024-02-27 18:19:33 -08:00
}
public postMessage() {
2024-03-06 01:28:01 -08:00
let totalScore: number = GameManager.instance.score;
2024-02-27 18:19:33 -08:00
// window.parent.postMessage(
// JSON.stringify({
// error: false,
// message: "Hello World",
// score: totalScore,
// type: "paypal_modal",
// }),
// "*"
// );
setTimeout(() => {
2024-02-28 03:25:11 -08:00
BEConnector.instance.numberTicket += 5;
2024-03-06 01:28:01 -08:00
GameManager.instance.onRevive();
2024-02-27 18:19:33 -08:00
}, 2000);
}
public postScoreToServer(score: number) {
2024-02-28 03:25:11 -08:00
let dataEncrypted: string = this.getDataEncrypted({
Score: score,
TournamentId: this.tournamentId,
SkinId: this.skinId,
});
fetch(
`${this.gameURL}/promotions/store-score-tournament?tournamentId=${this.tournamentId}&skinId=${this.skinId}&cocos=1`,
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-access-refactor-token': this.token,
},
method: 'POST',
body: JSON.stringify({ data: dataEncrypted }),
2024-02-27 18:19:33 -08:00
},
2024-02-28 03:25:11 -08:00
).catch((err) => console.log(err));
console.log('send score to server: ' + score);
2024-02-27 18:19:33 -08:00
window.parent.postMessage(
JSON.stringify({
error: false,
2024-02-28 03:25:11 -08:00
message: 'Hello World',
2024-02-27 18:19:33 -08:00
score: score,
2024-02-28 03:25:11 -08:00
type: 'game_tournament',
2024-02-27 18:19:33 -08:00
}),
2024-02-28 03:25:11 -08:00
'*',
2024-02-27 18:19:33 -08:00
);
}
private getDataEncrypted(data: any): string {
return CryptoESDefault.AES.encrypt(JSON.stringify(data), this.key, {
iv: CryptoESDefault.enc.Utf8.parse('16'),
mode: CryptoESDefault.mode.CBC,
2024-02-28 03:25:11 -08:00
padding: CryptoESDefault.pad.Pkcs7,
2024-02-27 18:19:33 -08:00
}).toString();
}
public getTicketCanBeMinus() {
let mileStone = JSON.parse(this.mileStone);
2024-03-06 01:28:01 -08:00
let currentScore = GameManager.instance.score;
2024-02-27 18:19:33 -08:00
let total = this.calculatingTicketToContinue(mileStone, currentScore);
return total;
}
public canRelive() {
return this.numberTicket > this.getTicketCanBeMinus();
}
}
const ENV_CONFIG = {
2024-02-28 03:25:11 -08:00
development: 'http://192.168.1.144:3009/api',
staging: 'https://api.play4promote.com/api',
production: 'https://api.play4promo.com/api',
};