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