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

fix disconnect

parent 2cc5435c
......@@ -50,6 +50,7 @@ export async function endGameApi(user: User) {
gameLevel: user.towerNumber - 1,
details
}
console.log('END GAME');
const res = await callApi(URL_END_GAME, 'POST', params);
return res;
......
......@@ -17,7 +17,7 @@ class User {
public position: v2 = new v2(0, 0);
public timeStart: number = 0;
public isEndGame: boolean = true;
public isStartGame: boolean = true;
public history: History[] = [];
constructor(token: string, id: string) {
......@@ -30,7 +30,7 @@ class User {
this.score = 0;
this.totalScore = 0;
this.combo = 0;
this.isEndGame = true;
this.isStartGame = false;
this.history = [];
this.timeStart = 0;
this.towerNumber = 0;
......
......@@ -54,12 +54,8 @@ export function setupSocket(io: Server) {
async function endGame(socket: Socket) {
try {
const user = getUserBySocket(socket);
user.reset();
if (user?.isEndGame) {
return;
}
endGameApi(user);
await endGameApi(user);
users.delete(user.id);
} catch (error) {
console.log('error', error)
}
......@@ -67,7 +63,7 @@ async function endGame(socket: Socket) {
function onDisconnect(socket: Socket): void {
console.log(`🔴 Client disconnected: ${socket.id}`)
endGame(socket);
// endGame(socket);
}
async function getHistory(socket: Socket) {
......@@ -79,10 +75,14 @@ async function startGame(socket: Socket, data: any) {
try {
const user = getUserBySocket(socket);
const result = await startGameApi(data);
// const result = true;
if(result && user.isStartGame) {
user.reset();
}
if (result) {
socket.emit(CONFIG.EVT.REQUEST_START_GAME, true);
user.isEndGame = false;
user.isStartGame = true;
addBlock(socket);
} else {
console.log('start game fail');
......@@ -129,10 +129,6 @@ async function passTower(socket: Socket, data: { distance: number }) {
const heightJump = user.distance2Tower * 0.7;
if (score <= 0) {
user.isEndGame = true;
}
if (score == 2) {
user.combo = Math.min(user.combo + 1, 2);
score *= user.combo;
......@@ -169,7 +165,6 @@ async function passTower(socket: Socket, data: { distance: number }) {
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
await endGameApi(user);
user.reset();
console.log('END');
}
} catch (error) {
console.log('error', error)
......
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