237 lines
7.2 KiB
TypeScript
237 lines
7.2 KiB
TypeScript
import { _decorator } from 'cc';
|
|
import * as CryptoES from 'crypto-es';
|
|
import { GameManager } from '../Manager/GameManager';
|
|
import Utilities from '../Utilities';
|
|
import Singleton from '../Singleton';
|
|
export let CryptoESDefault = CryptoES.default;
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
window.addEventListener('message', (data) => {
|
|
const { data: res } = data;
|
|
const objectRes = Utilities.getJson(res);
|
|
if (objectRes) {
|
|
const { type, value } = objectRes;
|
|
if (type === 'newTicket') {
|
|
BEConnector.instance.numberTicket += value;
|
|
GameManager.instance.gameRelive();
|
|
}
|
|
}
|
|
});
|
|
|
|
const faketSocre = [
|
|
{
|
|
userNickName: 'Poorgodzz',
|
|
score: 1785,
|
|
},
|
|
{
|
|
userNickName: 'a3',
|
|
score: 172,
|
|
},
|
|
{
|
|
userNickName: 'LeonNeilson',
|
|
score: 150,
|
|
},
|
|
{
|
|
userNickName: 'Promo_',
|
|
score: 142,
|
|
},
|
|
{
|
|
userNickName: 'H2O',
|
|
score: 90,
|
|
},
|
|
{
|
|
userNickName: 'lmao',
|
|
score: 30,
|
|
},
|
|
{
|
|
userNickName: 'Hellooo',
|
|
score: 14,
|
|
},
|
|
];
|
|
|
|
@ccclass('BEConnector')
|
|
export default class BEConnector extends Singleton<BEConnector>('BEConnector') {
|
|
private token: string;
|
|
private skinId: string;
|
|
private tournamentId: string;
|
|
private key: string;
|
|
private deviceInfo: string;
|
|
|
|
// Ticket info
|
|
public numberTicket: number;
|
|
public maxScore: number;
|
|
public currentScore: number;
|
|
public topScores: [] = [];
|
|
private mileStone: string;
|
|
|
|
private gameURL: string = '';
|
|
|
|
constructor() {
|
|
super();
|
|
this.getGameData();
|
|
}
|
|
|
|
public 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 async getInfo() {
|
|
try {
|
|
const res = await fetch(`${this.gameURL}/promotions/detail/${this.tournamentId}`);
|
|
const json = await res.json();
|
|
this.topScores = json.tScores;
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
public async authenticate() {
|
|
await fetch(
|
|
`${this.gameURL}/promotions/authenticate-tournament?token=${this.token}&tournamentId=${this.tournamentId}&skinId=${this.skinId}&deviceInfo=${this.deviceInfo}`,
|
|
)
|
|
.then((response) => {
|
|
if (response.ok) {
|
|
return response.json();
|
|
}
|
|
})
|
|
.then((data) => {
|
|
if (data.ResultCode == 1) {
|
|
this.key = data.Data.Key;
|
|
console.log('authen success', this.key);
|
|
} else {
|
|
throw new Error('');
|
|
}
|
|
})
|
|
.catch((err) => console.log('authen failed'));
|
|
}
|
|
|
|
public ticketMinus(type: 'auth' | 'revive') {
|
|
let numberTicket = type === 'auth' ? 1 : this.getTicketCanBeMinus();
|
|
let dataEncrypted: string = this.getDataEncrypted({ type: type, total: numberTicket });
|
|
|
|
fetch(`${this.gameURL}/promotions/ticket-minus/${this.tournamentId}/${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 }),
|
|
}).then(() => {
|
|
this.numberTicket -= numberTicket;
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
return closestMilestone;
|
|
}
|
|
|
|
public async checkGameScoreTicket() {
|
|
let totalScore: number = GameManager.instance.score;
|
|
let dataEncrypted: string = this.getDataEncrypted({ score: totalScore, ticket: this.getTicketCanBeMinus() });
|
|
|
|
await fetch(`${this.gameURL}/promotions/check-game-score-ticket/${this.tournamentId}/${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 }),
|
|
});
|
|
}
|
|
|
|
public postMessage() {
|
|
let totalScore: number = GameManager.instance.score;
|
|
window.parent.postMessage(
|
|
JSON.stringify({
|
|
error: false,
|
|
message: 'Hello World',
|
|
score: totalScore,
|
|
type: 'paypal_modal',
|
|
}),
|
|
'*',
|
|
);
|
|
}
|
|
|
|
public postScoreToServer(score: number) {
|
|
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 }),
|
|
},
|
|
).catch((err) => console.log(err));
|
|
console.log('send score to server: ' + score);
|
|
|
|
window.parent.postMessage(
|
|
JSON.stringify({
|
|
error: false,
|
|
message: 'Hello World',
|
|
score: score + this.currentScore,
|
|
type: 'game_tournament',
|
|
}),
|
|
'*',
|
|
);
|
|
}
|
|
|
|
private getDataEncrypted(data: any): string {
|
|
return CryptoESDefault.AES.encrypt(JSON.stringify(data), this.key, {
|
|
iv: CryptoESDefault.enc.Utf8.parse('16'),
|
|
mode: CryptoESDefault.mode.CBC,
|
|
padding: CryptoESDefault.pad.Pkcs7,
|
|
}).toString();
|
|
}
|
|
|
|
public getTicketCanBeMinus() {
|
|
if (!this.mileStone) return 0;
|
|
let mileStone = JSON.parse(this.mileStone);
|
|
let currentScore = GameManager.instance.score;
|
|
let total = this.calculatingTicketToContinue(mileStone, currentScore);
|
|
return total;
|
|
}
|
|
|
|
public 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',
|
|
};
|