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

168 lines
5.6 KiB
TypeScript

import CryptoES from 'crypto-es';
import { get, post } from './HttpRequest';
export default class BEConnector {
private static token: string;
private static skinId: string;
private static tournamentId: string;
private static key: string;
private static deviceInfo: string;
// Ticket info
public static numberTicket: number;
public static maxScore: number;
public static currentScore: number;
public static topScores: [] = [];
private static mileStone: string;
public static gameScore: number = 0;
private static gameURL: string = '';
public static getGameData() {
let url = new URLSearchParams(window.location.search);
this.token = url.get('token');
this.skinId = url.get('skinId');
this.tournamentId = url.get('tournamentId');
this.deviceInfo = url.get('deviceInfo');
this.numberTicket = parseInt(url.get('numberTicket')) || 0;
this.maxScore = parseInt(url.get('maxScore')) || 0;
this.currentScore = parseInt(url.get('currentScore')) || 0;
this.mileStone = url.get('mileStone') || '';
this.gameURL = ENV_CONFIG[url.get('env')];
}
public static async authenticate() {
try {
const res = await get(
`${this.gameURL}/promotions/authenticate-tournament?token=${this.token}&tournamentId=${this.tournamentId}&skinId=${this.skinId}&deviceInfo=${this.deviceInfo}`,
);
const data = await res.json();
if (data.ResultCode == 1) {
this.key = data.Data.Key;
console.log('Authenticate success');
}
} catch (error) {
console.log('Authenticate failed', error);
}
}
public static async ticketMinus(type: 'auth' | 'revive') {
const numberTicket = type === 'auth' ? 1 : this.getTicketCanBeMinus();
const dataEncrypted: string = this.getDataEncrypted({ type: type, total: numberTicket });
const JsonData = JSON.stringify({ data: dataEncrypted });
try {
await post(
`${this.gameURL}/promotions/ticket-minus/${this.tournamentId}/${this.skinId}?cocos=1`,
this.token,
JsonData,
);
this.numberTicket -= numberTicket;
return this.numberTicket;
} catch (error) {
console.log(error);
}
}
public static 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;
}
return closestMilestone;
}
public static async checkGameScoreTicket() {
const totalScore: number = this.gameScore;
const dataEncrypted: string = this.getDataEncrypted({
score: totalScore,
ticket: this.getTicketCanBeMinus(),
});
const data = JSON.stringify({ data: dataEncrypted });
try {
await post(
`${this.gameURL}/promotions/check-game-score-ticket/${this.tournamentId}/${this.skinId}?cocos=1`,
this.token,
data,
);
} catch (error) {
console.log(error);
}
}
public static postMessage() {
let totalScore: number = this.gameScore + this.currentScore;
window.parent.postMessage(
JSON.stringify({
error: false,
message: 'Hello World',
score: totalScore,
type: 'paypal_modal',
}),
'*',
);
}
public static async postScoreToServer() {
const dataEncrypted: string = this.getDataEncrypted({
Score: this.gameScore,
TournamentId: this.tournamentId,
SkinId: this.skinId,
});
const data = JSON.stringify({ data: dataEncrypted });
try {
const res = await post(
`${this.gameURL}/promotions/store-score-tournament?tournamentId=${this.tournamentId}&skinId=${this.skinId}&cocos=1`,
this.token,
data,
);
console.log('send score to server: ' + this.gameScore);
window.parent.postMessage(
JSON.stringify({
error: false,
message: 'Hello World',
score: this.gameScore + this.currentScore,
type: 'game_tournament',
}),
'*',
);
} catch (error) {
console.log(error);
}
}
private static getDataEncrypted(data: any): string {
return CryptoES.AES.encrypt(JSON.stringify(data), this.key, {
iv: CryptoES.enc.Utf8.parse('16'),
mode: CryptoES.mode.CBC,
padding: CryptoES.pad.Pkcs7,
}).toString();
}
public static getTicketCanBeMinus() {
if (!this.mileStone) return 0;
let mileStone = JSON.parse(this.mileStone);
let currentScore = this.gameScore;
let total = this.calculatingTicketToContinue(mileStone, currentScore);
return total;
}
public static canRelive() {
return this.numberTicket > this.getTicketCanBeMinus();
}
}
const ENV_CONFIG = {
development: 'http://192.168.1.144:3009/api',
staging: 'https://api.play4promote.com/api',
production: 'https://api.play4promo.com/api',
};