2024-04-26 00:48:20 -07:00
|
|
|
import CryptoES from 'crypto-es';
|
|
|
|
import { get, post } from './HttpRequest';
|
2024-02-27 18:19:33 -08:00
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
export default class BEConnector {
|
|
|
|
private static token: string;
|
|
|
|
private static skinId: string;
|
|
|
|
private static tournamentId: string;
|
|
|
|
private static key: string;
|
|
|
|
private static deviceInfo: string;
|
2024-02-27 18:19:33 -08:00
|
|
|
|
2024-03-29 04:24:58 -07:00
|
|
|
// Ticket info
|
2024-04-26 00:48:20 -07:00
|
|
|
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() {
|
2024-02-27 18:19:33 -08:00
|
|
|
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
|
|
|
|
2024-03-29 04:24:58 -07:00
|
|
|
this.numberTicket = parseInt(url.get('numberTicket')) || 0;
|
2024-04-04 21:43:09 -07:00
|
|
|
this.maxScore = parseInt(url.get('maxScore')) || 0;
|
2024-03-26 00:28:59 -07:00
|
|
|
this.currentScore = parseInt(url.get('currentScore')) || 0;
|
2024-03-29 04:24:58 -07:00
|
|
|
this.mileStone = url.get('mileStone') || '';
|
2024-02-27 18:19:33 -08:00
|
|
|
this.gameURL = ENV_CONFIG[url.get('env')];
|
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static async authenticate() {
|
2024-03-29 04:24:58 -07:00
|
|
|
try {
|
2024-04-26 00:48:20 -07:00
|
|
|
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');
|
|
|
|
}
|
2024-03-29 04:24:58 -07:00
|
|
|
} catch (error) {
|
2024-04-26 00:48:20 -07:00
|
|
|
console.log('Authenticate failed', error);
|
2024-03-29 04:24:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
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,
|
|
|
|
);
|
2024-02-27 18:19:33 -08:00
|
|
|
|
2024-02-28 03:25:11 -08:00
|
|
|
this.numberTicket -= numberTicket;
|
2024-05-02 02:55:47 -07:00
|
|
|
return this.numberTicket;
|
2024-04-26 00:48:20 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static calculatingTicketToContinue(scoreRange: object, yourScore: number) {
|
2024-02-27 18:19:33 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static async checkGameScoreTicket() {
|
|
|
|
const totalScore: number = this.gameScore;
|
|
|
|
const dataEncrypted: string = this.getDataEncrypted({
|
2024-04-05 01:09:19 -07:00
|
|
|
score: totalScore,
|
|
|
|
ticket: this.getTicketCanBeMinus(),
|
|
|
|
});
|
2024-04-26 00:48:20 -07:00
|
|
|
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);
|
|
|
|
}
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static postMessage() {
|
2024-04-05 01:09:19 -07:00
|
|
|
let totalScore: number = this.gameScore + this.currentScore;
|
2024-03-26 00:28:59 -07:00
|
|
|
window.parent.postMessage(
|
|
|
|
JSON.stringify({
|
|
|
|
error: false,
|
|
|
|
message: 'Hello World',
|
|
|
|
score: totalScore,
|
|
|
|
type: 'paypal_modal',
|
|
|
|
}),
|
|
|
|
'*',
|
|
|
|
);
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static async postScoreToServer() {
|
|
|
|
const dataEncrypted: string = this.getDataEncrypted({
|
2024-04-05 01:09:19 -07:00
|
|
|
Score: this.gameScore,
|
2024-02-28 03:25:11 -08:00
|
|
|
TournamentId: this.tournamentId,
|
|
|
|
SkinId: this.skinId,
|
|
|
|
});
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
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);
|
|
|
|
}
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
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,
|
2024-02-27 18:19:33 -08:00
|
|
|
}).toString();
|
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static getTicketCanBeMinus() {
|
2024-03-29 04:24:58 -07:00
|
|
|
if (!this.mileStone) return 0;
|
2024-02-27 18:19:33 -08:00
|
|
|
let mileStone = JSON.parse(this.mileStone);
|
2024-04-05 01:09:19 -07:00
|
|
|
let currentScore = this.gameScore;
|
2024-02-27 18:19:33 -08:00
|
|
|
let total = this.calculatingTicketToContinue(mileStone, currentScore);
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2024-04-26 00:48:20 -07:00
|
|
|
public static canRelive() {
|
2024-02-27 18:19:33 -08:00
|
|
|
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',
|
|
|
|
};
|