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) { ...@@ -13,8 +13,9 @@ export function setupSocket(io: Server) {
io.use((socket: Socket, next) => { io.use((socket: Socket, next) => {
const token = socket.handshake.query.token; const token = socket.handshake.query.token;
console.log('token', token) console.log('token', token)
if (!users[socket.id]) {
users[socket.id] = new User(String(token)); users[socket.id] = new User(String(token));
}
next(); next();
}); });
...@@ -24,16 +25,25 @@ export function setupSocket(io: Server) { ...@@ -24,16 +25,25 @@ export function setupSocket(io: Server) {
socket.on(CONFIG.EVT.START_GAME, (data: string) => startGame(socket, data)); 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.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(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) { async function getHistory(socket: Socket) {
const user = getUserBySocketId(socket.id); 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) { async function startGame(socket: Socket, data: any) {
...@@ -45,7 +55,7 @@ async function startGame(socket: Socket, data: any) { ...@@ -45,7 +55,7 @@ async function startGame(socket: Socket, data: any) {
addBlock(socket); addBlock(socket);
} else { } else {
console.log('start game fail'); 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 ...@@ -90,7 +100,7 @@ async function passTower(socket: Socket, data: { nextPos: v2, time: number, dist
user.history.unshift(dataSend); user.history.unshift(dataSend);
user.totalScore = 0; user.totalScore = 0;
} }
const response = await socket.emitWithAck('requestPassTower', dataSend); socket.emit('requestPassTower', dataSend);
} }
async function addBlock(socket: Socket) { async function addBlock(socket: Socket) {
...@@ -114,7 +124,7 @@ async function addBlock(socket: Socket) { ...@@ -114,7 +124,7 @@ async function addBlock(socket: Socket) {
distance2Tower: user.distance2Tower, distance2Tower: user.distance2Tower,
towerNumber: user.towerNumber - 1, towerNumber: user.towerNumber - 1,
}; };
const response = await socket.emitWithAck('requestSpawnTower', data); socket.emit('requestSpawnTower', data);
} }
function joinRoom(socket: Socket, data: string) { 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