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

update disconnect

parent 5161cb87
......@@ -13,8 +13,9 @@ export function setupSocket(io: Server) {
io.use((socket: Socket, next) => {
const token = socket.handshake.query.token;
console.log('token', token)
users[socket.id] = new User(String(token));
if (!users[socket.id]) {
users[socket.id] = new User(String(token));
}
next();
});
......@@ -24,16 +25,25 @@ export function setupSocket(io: Server) {
socket.on(CONFIG.EVT.START_GAME, (data: string) => startGame(socket, data));
socket.on(CONFIG.EVT.PASS_TOWER, async (data: { nextPos: v2, time: number, distance: number }) => await passTower(socket, data));
// socket.on(CONFIG.EVT.SPAWN_TOWER, async () => addBlock(socket));
socket.on(CONFIG.EVT.HISTORY, async () => getHistory(socket));
socket.on("disconnect", () => console.log(`🔴 Client disconnected: ${socket.id}`));
socket.on("disconnect", () => onDisconnect(socket));
});
}
function onDisconnect(socket: Socket): void {
const user = getUserBySocketId(socket.id);
if (user.isEndGame) {
return;
}
endGameApi(user.totalScore);
user.totalScore = 0;
return console.log(`🔴 Client disconnected: ${socket.id}`);
}
async function getHistory(socket: Socket) {
const user = getUserBySocketId(socket.id);
const response = await socket.emitWithAck('requestHistory', user.history);
socket.emit('requestHistory', user.history);
}
async function startGame(socket: Socket, data: any) {
......@@ -45,7 +55,7 @@ async function startGame(socket: Socket, data: any) {
addBlock(socket);
} else {
console.log('start game fail');
const response = await socket.emitWithAck('requestStartGame', { code: 9999 });
socket.emit('requestStartGame', { code: 9999 });
}
}
......@@ -90,7 +100,7 @@ async function passTower(socket: Socket, data: { nextPos: v2, time: number, dist
user.history.unshift(dataSend);
user.totalScore = 0;
}
const response = await socket.emitWithAck('requestPassTower', dataSend);
socket.emit('requestPassTower', dataSend);
}
async function addBlock(socket: Socket) {
......@@ -114,7 +124,7 @@ async function addBlock(socket: Socket) {
distance2Tower: user.distance2Tower,
towerNumber: user.towerNumber - 1,
};
const response = await socket.emitWithAck('requestSpawnTower', data);
socket.emit('requestSpawnTower', data);
}
function joinRoom(socket: Socket, data: string) {
......
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