Commit 379b7204 authored by 18신대성's avatar 18신대성

서버 안정화

parent 5020b7bf
var GameServer = GameServer || {}; var GameServer = GameServer || {};
GameServer.Phase = {READY: 0, COUNT: -1, START: 1, MAIN: 2, MUSIC: 3}; GameServer.Phase = {READY: 0, COUNT: -1, START: 1, MAIN: 2, MUSIC: 3};
GameServer.connectCount = 0;
GameServer.disconnectCount = 0;
GameServer.currentPlayer = []; GameServer.currentPlayer = [];
GameServer.playingRoom = []; GameServer.playingRoom = [];
......
...@@ -36,6 +36,7 @@ io.on('connection', function(socket) ...@@ -36,6 +36,7 @@ io.on('connection', function(socket)
str: 'your number is ' + socket.playerData.id, str: 'your number is ' + socket.playerData.id,
num: socket.playerData.id num: socket.playerData.id
}); });
GameServer.connectCount++;
}); });
socket.on('setNickname', function(msg) // string new_nickname socket.on('setNickname', function(msg) // string new_nickname
...@@ -80,7 +81,7 @@ io.on('connection', function(socket) ...@@ -80,7 +81,7 @@ io.on('connection', function(socket)
socket.playerData.currentRoom.announceToRoom('attackMode', socket.playerData.id); socket.playerData.currentRoom.announceToRoom('attackMode', socket.playerData.id);
} }
} }
catch (e) {console.log('[ERR] error catched on setPlayerTyping')} catch (e) {console.error('[ERR] error catched on setPlayerTyping')}
}); });
socket.on('endCount', function() socket.on('endCount', function()
...@@ -137,51 +138,60 @@ io.on('connection', function(socket) ...@@ -137,51 +138,60 @@ io.on('connection', function(socket)
socket.on('disconnect', function(reason) socket.on('disconnect', function(reason)
{ {
GameServer.disconnectCount++;
let data = socket.playerData; let data = socket.playerData;
if (typeof data.id === undefined) if (typeof data === undefined)
{ {
console.log('[ERROR] data.id is undefined'); console.error('[ERROR] data is undefined');
console.log(GameServer.currentPlayer); console.table(GameServer.currentPlayer);
} }
else // data.id is not undefined else // data.id is not undefined
{ {
console.log('['+ data.id +'] client disconnected, reason: ' + reason); disconnectUser(data, reason);
let idxToDel = GameServer.currentPlayer.findIndex(function(element) }
{ const connectDiff = GameServer.connectCount - GameServer.disconnectCount;
return element.playerData.id === data.id; const playerCount = GameServer.currentPlayer.length
}); console.log({ connectDiff, playerCount });
if (idxToDel != -1) });
});
var disconnectUser = function(data, reason)
{
console.log('['+ data.id +'] client disconnected, reason: ' + reason);
let idxToDel = GameServer.currentPlayer.findIndex(function(element)
{
return element.playerData.id === data.id;
});
if (idxToDel != -1)
{
// 룸에서도 제거
if (data.currentRoom != null)
{
if (data.currentRoom.currentPhase === GameServer.Phase.READY || data.currentRoom.currentPhase === GameServer.Phase.COUNT)
{ {
// 룸에서도 제거 data.currentRoom.exitRoom(data.id);
if (data.currentRoom != null) if (data.currentRoom.aliveCount < data.currentRoom.startCount)
{ {
if (data.currentRoom.currentPhase === GameServer.Phase.READY || data.currentRoom.currentPhase === GameServer.Phase.COUNT) data.currentRoom.announceToRoom('setRoomCount',
{ {
data.currentRoom.exitRoom(data.id); isEnable: false, endTime: 0, playerCount: data.currentRoom.currentPlayer.length,
if (data.currentRoom.aliveCount < data.currentRoom.startCount) isEnter: false, player: data.playingData
{ });
data.currentRoom.announceToRoom('setRoomCount', data.currentRoom.currentPhase = GameServer.Phase.READY;
{
isEnable: false, endTime: 0, playerCount: data.currentRoom.currentPlayer.length,
isEnter: false, player: data.playingData
});
data.currentRoom.currentPhase = GameServer.Phase.READY;
}
else data.currentRoom.announceToRoom('setRoomCount',
{
isEnable: true, endTime: data.currentRoom.endTime, playerCount: data.currentRoom.currentPlayer.length,
isEnter: false, player: data.playingData
});
}
else if (data.playingData.isAlive)
{
data.playingData.defeat();
data.currentRoom.announceToRoom('userDisconnect', data.playingData);
}
} }
GameServer.currentPlayer.splice(idxToDel, 1); else data.currentRoom.announceToRoom('setRoomCount',
{
isEnable: true, endTime: data.currentRoom.endTime, playerCount: data.currentRoom.currentPlayer.length,
isEnter: false, player: data.playingData
});
}
else if (data.playingData.isAlive)
{
data.playingData.defeat();
data.currentRoom.announceToRoom('userDisconnect', data.playingData);
} }
console.log('['+ data.id +'] disconnect complete');
} }
}); GameServer.currentPlayer.splice(idxToDel, 1);
}); }
\ No newline at end of file console.log('['+ data.id +'] disconnect complete');
}
\ No newline at end of file
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