Commit 1090e19e authored by 18신대성's avatar 18신대성 Committed by 18류지석

룸에서 퇴장시 그 룸에서 유저를 삭제함

parent 026e9ec6
var GameServer = GameServer || {}; var GameServer = GameServer || {};
GameServer.Phase = {READY: 0, START: 1, MAIN: 2, MUSIC: 3}; GameServer.Phase = {READY: 0, START: 1, MAIN: 2, MUSIC: 3};
GameServer.startCount = 1; GameServer.startCount = 2;
GameServer.currentPlayer = []; GameServer.currentPlayer = [];
GameServer.playingRoom = []; GameServer.playingRoom = [];
...@@ -42,15 +42,22 @@ GameServer.makeRoom = function() ...@@ -42,15 +42,22 @@ GameServer.makeRoom = function()
minPlayerTyping: 0 minPlayerTyping: 0
} }
this.playingRoom.push(roomOption); this.playingRoom.push(roomOption);
console.log('[SERVER] new room made, roomCount: ' + this.playingRoom.length); console.log('[SERVER] new room #'+roomOption.roomNum+' made, roomCount: ' + this.playingRoom.length);
return this.playingRoom.length - 1; return this.playingRoom.length - 1;
} }
GameServer.findRoomIndex = function(roomNum)
{
return GameServer.playingRoom.findIndex(function(element)
{
return element.roomNum === roomNum;
});
}
GameServer.enterRoom = function(roomIdx, playerData) GameServer.enterRoom = function(roomIdx, playerData)
{ {
this.playingRoom[roomIdx].currentPlayer.push(playerData); this.playingRoom[roomIdx].currentPlayer.push(playerData);
playerData.currentRoom = this.playingRoom[roomIdx]; playerData.currentRoom = this.playingRoom[roomIdx];
console.log('[' + playerData.id + '] entered to room #' + this.playingRoom[roomIdx].roomNum); console.log('[' + playerData.id + '] entered to room #' + this.playingRoom[roomIdx].roomNum);
if (this.playingRoom[roomIdx].currentPlayer.length > this.startCount) GameServer.startRoom(roomIdx); if (this.playingRoom[roomIdx].currentPlayer.length >= this.startCount && this.playingRoom[roomIdx].Phase != GameServer.Phase.START) GameServer.startRoom(roomIdx);
return this.playingRoom[roomIdx]; return this.playingRoom[roomIdx];
} }
GameServer.enterEmptyRoom = function(playerData) GameServer.enterEmptyRoom = function(playerData)
...@@ -73,11 +80,19 @@ GameServer.enterEmptyRoom = function(playerData) ...@@ -73,11 +80,19 @@ GameServer.enterEmptyRoom = function(playerData)
GameServer.startRoom = function(roomIdx) GameServer.startRoom = function(roomIdx)
{ {
this.playingRoom[roomIdx].Phase = this.Phase.START; this.playingRoom[roomIdx].Phase = this.Phase.START;
this.playingRoom[roomIdx].currentPlayer.forEach(element => { console.log('[ROOM#'+this.playingRoom[roomIdx].roomNum+'] Game Start');
element.socketId.emit('phaseChange', this.Phase.START); this.anounceToRoom(roomIdx, 'phaseChange', this.Phase.START);
element.socketId.emit('startGame'); this.anounceToRoom(roomIdx, 'startGame');
// 데이터 동기화 // 데이터 동기화도
}
GameServer.anounceToRoom = function(roomIdx, message, data = null)
{
this.playingRoom[roomIdx].currentPlayer.forEach(element =>
{
element.socketId.emit(message, data);
}); });
} }
// 데이터 동기화 함수 만들기
// 동기화할것: 유저리스트(id - nickname 쌍)
module.exports = GameServer; module.exports = GameServer;
\ No newline at end of file
...@@ -18,4 +18,8 @@ socket.on('phaseChange', function(msg) // number Phase ...@@ -18,4 +18,8 @@ socket.on('phaseChange', function(msg) // number Phase
socket.on('startGame', function() socket.on('startGame', function()
{ {
game.scene.start('gameScene'); game.scene.start('gameScene');
});
socket.on('userDisconnect', function(msg) // {num id, str nickname}
{
console.log(msg.id + ' / ' + msg.nickname + ' disconnected');
}); });
\ No newline at end of file
...@@ -26,7 +26,7 @@ io.on('connection', function(socket) ...@@ -26,7 +26,7 @@ io.on('connection', function(socket)
id: GameServer.getPlayerNumber(), id: GameServer.getPlayerNumber(),
nickname: '게스트', nickname: '게스트',
socketId: socket, socketId: socket,
currnetRoom: null, currentRoom: null,
playerTyping: 0 playerTyping: 0
} }
...@@ -56,17 +56,31 @@ io.on('connection', function(socket) ...@@ -56,17 +56,31 @@ io.on('connection', function(socket)
socket.on('disconnect', function(reason) socket.on('disconnect', function(reason)
{ {
var idxToDel = GameServer.currentPlayer.findIndex(function(element) let idxToDel = GameServer.currentPlayer.findIndex(function(element)
{ {
return element.id === socket.playerData.id; return element.id === socket.playerData.id;
} });
);
if (idxToDel != -1) if (idxToDel != -1)
{ {
console.log('['+ socket.playerData.id +'] client disconnected, reason: ' + reason); console.log('['+ socket.playerData.id +'] client disconnected, reason: ' + reason);
GameServer.currentPlayer.splice(idxToDel, 1); GameServer.currentPlayer.splice(idxToDel, 1);
// 룸에서도 제거 // 룸에서도 제거
// 모두에게 삭제했다고 보내기 if (socket.playerData.currentRoom != null)
{
GameServer.anounceToRoom(GameServer.findRoomIndex(socket.playerData.currentRoom.roomNum), 'userDisconnect',
{
id: socket.playerData.id,
nickname: socket.playerData.nickname
});
let _idxToDel = socket.playerData.currentRoom.currentPlayer.findIndex(function(element)
{
return element.id === socket.playerData.id;
});
if (idxToDel != -1)
{
socket.playerData.currentRoom.currentPlayer.splice(_idxToDel, 1);
}
}
} }
}); });
}); });
\ 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