Commit e65a4739 authored by Vũ Gia Vương's avatar Vũ Gia Vương

fix call end game when back in web browser, add matchId,eventId

parent 15f154ec
......@@ -19,18 +19,14 @@ let tokenTest = 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTI1LCJ1c2VybmFtZSI6IlFIU18wMUpEVk
+ 'kZBIjpmYWxzZSwibGFzdExvZ2luVGltZSI6IjIwMjUtMDgtMDhUMTQ6MTc6MDcuNzczMTEyMjUzIiwicmVmZXJlbmNlQ29kZSI6IklRVEtUQzRkbGk4NiIsImdlbmRlciI6Ik1BTEUiLCJ2dG1BY2NvdW50'
+ 'SWQiOiIwMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRvYiI6IjE5OTktMDItMTciLCJjb2RlIjoiMTIzNDU2NzYiLCJzdWIiOiIxMjUiLCJpYXQiOjE3NTQ2Mzc0MjcsImV4cCI6MTc2MzI3NzQyN30.'
+ 'Wf7PDX2rgqRPajkTL8oHHeYFwZrLpTKmuBO8Va9OGMs';
let timeStart = 0;
let userId = 0;
let matchId = '';
let eventId = null;
// let timeStart = 0;
function startGameApiCtrl(data, user) {
return __awaiter(this, void 0, void 0, function* () {
try {
timeStart = new Date().getTime();
userId = (Math.random() * 100 >> 0) + 10;
matchId = `${userId}-${GAME_CODE}-${timeStart}`;
user.timeStart = new Date().getTime();
user.matchId = `${user.id}-${GAME_CODE}-${user.timeStart}`;
data = {
matchId,
matchId: user.matchId,
gameCode: GAME_CODE,
};
const result = yield callApi(URL_START_GAME, 'POST', data, user.token);
......@@ -44,7 +40,7 @@ function startGameApiCtrl(data, user) {
function endGameApiCtrl(user) {
return __awaiter(this, void 0, void 0, function* () {
try {
const playedSeconds = (new Date().getTime() - timeStart) / 1e3;
const playedSeconds = (new Date().getTime() - user.timeStart) / 1e3;
const details = user.history.map(h => ({
level: h.tower,
score: h.score,
......@@ -52,9 +48,9 @@ function endGameApiCtrl(user) {
timePlayed: h.timePlayed / 1e3,
}));
const params = {
matchId,
matchId: user.matchId,
gameCode: GAME_CODE,
eventId,
eventId: user.eventId,
playedSeconds,
score: user.totalScore,
gameLevel: user.towerNumber - 1,
......@@ -85,6 +81,12 @@ function callApi(url, method, data, token) {
console.log('error callApi: ', res, 'data: ', data);
}
const result = yield res.json();
console.log('-----------');
console.log('url', url);
console.log('token', token);
console.log('data', data);
console.log('result', result);
console.log('-----------');
return (result === null || result === void 0 ? void 0 : result.code) == 'success';
}
catch (error) {
......
......@@ -19,6 +19,8 @@ class User {
this.nextBlock = new v2_1.default(0, 0);
this.screenPos = new v2_1.default(0, 0);
this.position = new v2_1.default(0, 0);
this.matchId = '';
this.eventId = '';
this.timeStart = 0;
this.isStartGame = true;
this.history = [];
......@@ -32,6 +34,8 @@ class User {
this.score = user.score || 0;
this.totalScore = user.totalScore || 0;
this.combo = user.combo || 0;
this.matchId = user.matchId || '';
this.eventId = user.eventId || '';
this.timeStart = user.timeStart || 0;
this.isStartGame = user.isStartGame || true;
this.history = ((_a = user.history) === null || _a === void 0 ? void 0 : _a.map(h => new History(h.timeStart, h.timePlayed, h.totalScore, h.tower, h.score))) || [];
......
import { Socket } from "socket.io";
import User, { IDataPassTower, IDataSpawnTower, IRequestPassTower } from "../Model/User";
import User, { IDataPassTower, IDataSpawnTower, IDataStartGame, IRequestPassTower } from "../Model/User";
import { endGameApiCtrl, startGameApiCtrl } from "./networkCtrl";
import v2 from "../Model/v2";
import CONFIG from "../Config/config";
......@@ -14,6 +14,9 @@ const RATE_BLACK_LIST = 0.15;
export async function endGameCtrl(user: User) {
try {
if (!user.isStartGame) {
return;
}
await endGameApiCtrl(user);
user.reset();
} catch (error) {
......@@ -21,8 +24,9 @@ export async function endGameCtrl(user: User) {
}
}
export async function startGameCtrl(socket: Socket, data: any, user: User) {
export async function startGameCtrl(socket: Socket, data: IDataStartGame, user: User) {
try {
user.eventId = data?.eventId || '';
const result = await startGameApiCtrl(data, user);
if (result && user.isStartGame) {
......@@ -132,7 +136,7 @@ export async function passTowerCtrl(socket: Socket, data: IRequestPassTower, use
addBlockCtrl(socket, user);
} else {
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
await endGameApiCtrl(user);
await endGameCtrl(user);
}
} catch (error) {
console.log('error', error)
......
import User from "../Model/User";
import User, { IDataStartGame } from "../Model/User";
const URL = process.env.URL || 'https://dev.gamee.vn/api/web';
const URL_START_GAME = `${URL}/game/start`;
......@@ -8,19 +8,15 @@ let tokenTest = 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTI1LCJ1c2VybmFtZSI6IlFIU18wMUpEVk
+ 'kZBIjpmYWxzZSwibGFzdExvZ2luVGltZSI6IjIwMjUtMDgtMDhUMTQ6MTc6MDcuNzczMTEyMjUzIiwicmVmZXJlbmNlQ29kZSI6IklRVEtUQzRkbGk4NiIsImdlbmRlciI6Ik1BTEUiLCJ2dG1BY2NvdW50'
+ 'SWQiOiIwMUpEVkZHOE5DUEdBNEtFRUhLWDA4RUJWOCIsImRvYiI6IjE5OTktMDItMTciLCJjb2RlIjoiMTIzNDU2NzYiLCJzdWIiOiIxMjUiLCJpYXQiOjE3NTQ2Mzc0MjcsImV4cCI6MTc2MzI3NzQyN30.'
+ 'Wf7PDX2rgqRPajkTL8oHHeYFwZrLpTKmuBO8Va9OGMs';
let timeStart = 0;
let userId = 0;
let matchId = '';
let eventId: string | null = null;
// let timeStart = 0;
export async function startGameApiCtrl(data: any, user: User) {
try {
timeStart = new Date().getTime();
userId = (Math.random() * 100 >> 0) + 10;
matchId = `${userId}-${GAME_CODE}-${timeStart}`;
user.timeStart = new Date().getTime();
user.matchId = `${user.id}-${GAME_CODE}-${user.timeStart}`;
data = {
matchId,
matchId: user.matchId,
gameCode: GAME_CODE,
};
const result = await callApi(URL_START_GAME, 'POST', data, user.token);
......@@ -32,7 +28,7 @@ export async function startGameApiCtrl(data: any, user: User) {
export async function endGameApiCtrl(user: User) {
try {
const playedSeconds = (new Date().getTime() - timeStart) / 1e3;
const playedSeconds = (new Date().getTime() - user.timeStart) / 1e3;
const details = user.history.map(h => ({
level: h.tower,
score: h.score,
......@@ -41,9 +37,9 @@ export async function endGameApiCtrl(user: User) {
}));
const params = {
matchId,
matchId: user.matchId,
gameCode: GAME_CODE,
eventId,
eventId: user.eventId,
playedSeconds,
score: user.totalScore,
gameLevel: user.towerNumber - 1,
......@@ -73,6 +69,13 @@ async function callApi(url: string, method: string, data: any, token: string) {
console.log('error callApi: ', res, 'data: ', data);
}
const result = await res.json();
console.log('-----------')
console.log('url', url)
console.log('token', token)
console.log('data', data)
console.log('result', result)
console.log('-----------')
return result?.code == 'success';
} catch (error) {
console.log('error callApi: ', error);
......
......@@ -16,6 +16,8 @@ class User {
public screenPos: v2 = new v2(0, 0);
public position: v2 = new v2(0, 0);
public matchId: string = '';
public eventId: string = '';
public timeStart: number = 0;
public isStartGame: boolean = true;
public history: History[] = [];
......@@ -33,6 +35,8 @@ class User {
this.totalScore = user.totalScore || 0;
this.combo = user.combo || 0;
this.matchId = user.matchId || '';
this.eventId = user.eventId || '';
this.timeStart = user.timeStart || 0;
this.isStartGame = user.isStartGame || true;
this.history = user.history?.map(h => new History(h.timeStart, h.timePlayed, h.totalScore, h.tower, h.score)) || [];
......@@ -111,4 +115,8 @@ export interface IQuery {
userId?: string
}
export interface IDataStartGame {
eventId?: string
}
export default User;
\ No newline at end of file
import { Server, Socket } from "socket.io";
import User, { IRequestPassTower } from "./Model/User";
import User, { IDataStartGame, IRequestPassTower } from "./Model/User";
import { redis } from ".";
import { endGameCtrl, getHistory, passTowerCtrl, startGameCtrl } from "./Controller/gameCtrl";
import CONFIG from "./Config/config";
......@@ -48,7 +48,7 @@ export async function setupSocket(io: Server) {
connectedUserCtrl();
}
socket.on(CONFIG.EVT.START_GAME, (data: string) => startGameCtrl(socket, data, user));
socket.on(CONFIG.EVT.START_GAME, (data: IDataStartGame) => startGameCtrl(socket, data, user));
socket.on(CONFIG.EVT.PASS_TOWER, async (data: IRequestPassTower) => await passTowerCtrl(socket, data, user));
socket.on(CONFIG.EVT.HISTORY, async () => await getHistory(socket, user));
socket.on(CONFIG.EVT.END_GAME, () => endGameCtrl(user));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment