Add joinRoom functionality

Players can join room with independant sockets
When a room is 'filled' the game will start
develop
Nathan Steel 1 year ago
parent 71b3451bf7
commit 840d263eb4

@ -1272,12 +1272,6 @@ let board = new Board;
// Everything beyond initialisation shouldn't be required so no need to wait // Everything beyond initialisation shouldn't be required so no need to wait
// At least I think, for now. May need more stuff in future // At least I think, for now. May need more stuff in future
// Request the deck(s) from server/db
// When the page loads (for now) start the game
requestStartGame();
// This will trigger an emit to and on from the server will trigger loadBoard();
// public/main.js // socket.on('responseStartGame', function (data) {
function loadBoard(data) { function loadBoard(data) {
console.log(data); console.log(data);

@ -115,12 +115,6 @@
</div> </div>
<hr>
<div>
<button onclick="requestStartGame()">(Re)start game</button>
<button onclick="requestRoomGeneration()">RequestRoomGeneration</button>
</div>
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<script src="/cards.js"></script><!-- Temp until DB --> <script src="/cards.js"></script><!-- Temp until DB -->
<script src="/shapes.js"></script> <script src="/shapes.js"></script>

@ -140,7 +140,6 @@ socket.on('responseJoinRoom', function (name) {
if (name != 'error') { if (name != 'error') {
room = name; room = name;
console.log('<< Room Response: ' + name); console.log('<< Room Response: ' + name);
alert('Commented out draw board, for UI stuff');
//board.drawBoard(); //board.drawBoard();
} else { } else {
socket.disconnect(); socket.disconnect();
@ -164,11 +163,8 @@ socket.on('responseGetCards', function (data) {
responseGetCards(data); responseGetCards(data);
}); });
// Testing getting cards from server/DB // When game starts, loadBoard to add data for clients
function requestStartGame(){ // and display the starting board
console.log('+ requestStartGame');
socket.emit('requestStartGame');
}
function responseStartGame(data){ function responseStartGame(data){
if(!data.success){ if(!data.success){
alert(data.message); alert(data.message);
@ -186,25 +182,3 @@ socket.on('responseStartGame', function (data) {
loadBoard(data.message); loadBoard(data.message);
}); });
// More experimenty
function requestRoomGeneration(){
console.log('+ requestRoomGeneration');
socket.emit('requestRoomGeneration');
}
function responseRoomGeneration(data){
if(!data.success){
alert(data.message);
return false;
}
return data;
}
socket.on('responseRoomGeneration', function (data) {
console.log('<< responseRoomGeneration');
responseStartGame(data);
if(data.success !== true){
alert('Err with responseRoomGeneration. '+data.message);
}
// Pass only the data to loadBoard
loadBoard(data.message);
});

@ -41,52 +41,11 @@ for (let roomId = 1; roomId <= numRoomsToPreGen; roomId++) {
createRoom(roomId); createRoom(roomId);
} }
// For testing to see console logs
roomMod.roomGeneration(2);
//cardGen.requestDeck();
function requestStartGame(socket){
response = {success: false, message: 'Failed requestStartGame() server.js'};
cardGen.requestDeck().then(data => {
response.success = true;
response.message = data;
io.to(socket.id).emit('responseStartGame', response);
})
.catch(err => {
response.message = err;
io.to(socket.id).emit('responseStartGame', err);
});
}
function requestRoomGeneration(socket){
response = {success: false, message: 'Failed requestRoomGeneration() server.js'};
roomMod.roomGeneration().then(data => {
response.success = true;
response.message = data;
io.to(socket.id).emit('responseRoomGeneration', response);
})
.catch(err => {
response.message = err;
io.to(socket.id).emit('responseRoomGeneration', err);
});
}
function onConnection(socket){ function onConnection(socket){
console.log('+ User connected'); console.log('+ User connected');
console.log(''); console.log('');
// New testing fella (working afaik)
// TODO: request specific deckId/playerId (and multiples, i.e. get 6 decks at same
// time, based on deckId/playerId combo. Maybe pass as array [deckId, playerId],[deck
socket.on('requestStartGame', function() {
requestStartGame(socket);
});
socket.on('requestRoomGeneration', function() {
requestRoomGeneration(socket);
});
socket.on('requestRooms', function(filter) { socket.on('requestRooms', function(filter) {
requestRooms(socket, filter); requestRooms(socket, filter);
}); });
@ -175,6 +134,7 @@ function createRoom(roomId = false, dump = true){
room['timeout'] = {}; room['timeout'] = {};
room['timeout']['s'] = 10; room['timeout']['s'] = 10;
room['people'] = 0; room['people'] = 0;
room['playerIds'] = {};
//room['deck'] = []; //room['deck'] = [];
//room['turn'] = 0; //room['turn'] = 0;
@ -210,39 +170,160 @@ function createRoom(roomId = false, dump = true){
// TODO: break into requestJoinRoom, and JoinRoom? // TODO: break into requestJoinRoom, and JoinRoom?
// Maybe not needed here? As won't be done via backend? // Maybe not needed here? As won't be done via backend?
// TODO: Need a 'leave room'/disconnect
function requestJoinRoom(socket, playerName, roomId){ function requestJoinRoom(socket, playerName, roomId){
console.log('+ requestJoinRoom recieved'); console.log('+ requestJoinRoom recieved');
socket.playerName = playerName;
for (let i = 1; i <= numRooms; i++) { let room = roomData[roomId];
let name = 'Room_' + i;
let people = roomData[i]['people']; // Add socket for playerName so that players in room get
// responses, etc. from the room (something like that)
// Add player to socket object, so it can be referred to
// as each socket is individual to a player, but rooms need to
// emit to multiple players. These .player can be used in loops
console.log('- people: '+people); // https://socket.io/docs/v4/rooms/
console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom); // https://stackoverflow.com/a/18096649
socket.playerId = playerName;
if (true || people < maxPlayersPerRoom) { if(room === undefined){
io.to(socket.id).emit('responseRoom', 'error');
console.log('>> Room does not exist');
}
let roomName = 'Room_' + roomId;
let people = room['people'];
// people = io.sockets.adapter.rooms[roomId].length; // This gets the sockets in the room (i.e. the players)
//console.log(util.inspect(io.sockets.adapter.rooms, true, 4, true))
//console.log('- people(socket: '+io.sockets.adapter.rooms[player]+')');
//console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom);
if(isUserInRoom(playerName, roomId)){
console.log('Already in room');
return false;
}
if (people < maxPlayersPerRoom) {
people = roomData[i]['people'] += 1; // Update people in room count
socket.join(name); people = room['people'] += 1;
console.log('>> User ' + socket.playerName +
' connected on ' + name + ' (' + (people) + '/' + maxPlayersPerRoom + ')');
io.to(socket.id).emit('responseJoinRoom', name); // Add playerId to room (playerName for now while Ids don't exist TODO)
room['playerIds'][playerName] = playerName;
// https://socket.io/docs/v4/rooms/
// https://stackoverflow.com/a/25028953
socket.join(roomId);
// https://stackoverflow.com/a/25028953
//console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true))
/*
let clients = io.sockets.adapter.rooms.get(roomId);
let numClients = clients ? clients.size : 0;
for (const clientId of clients) {
//this is the socket of each client in the room.
const clientSocket = io.sockets.sockets.get(clientId);
console.log(clientSocket.playerId); // The playerId set beggining of func.
}
*/
if (people >= maxPlayersPerRoom) { console.log('>> User ' + playerName +
console.log('- starting game'); ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')');
//startGame(name);
}
return; // Joined room (emit to the player that just joined)
io.to(socket.id).emit('responseJoinRoom', roomName);
if (people >= maxPlayersPerRoom) {
console.log('- starting game');
// startGame for room
startGame(roomId);
} }
}
}
// Will need to be different to playerName in future (in case dupes)
// would use playerId TODO
function isUserInRoom(playerName, roomId){
if(playerName in roomData[roomId]['playerIds']){
return true;
}
return false;
}
function startGame(roomId){
console.log('>> Room: ' + roomId + ': Requesting game...');
let people = roomData[roomId].players;
/*
try {
//people = io.sockets.adapter.rooms.get(roomId).size;
} catch (e) {
console.log('>> Room: ' + roomId + ': No people here...');
return;
}
*/
// For now, if there's 2 people only. Will need changing for
// 3v1, 5v1, 2v2, etc...
let response = {success: false, message: 'Failed requestStartGame() server.js'};
if(people < maxPlayersPerRoom){
console.log('Too few people');
}
console.log('>> Room: ' + roomId + ': Starting');
// https://stackoverflow.com/a/25028953
//console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true))
let clients = io.sockets.adapter.rooms.get(roomId);
/*
for (const clientId of clients) {
//this is the socket of each client in the room.
const clientSocket = io.sockets.sockets.get(clientId);
console.log(clientSocket.playerId); // The playerId set in requestJoin
// Just so I know how to access stuff in future.
console.log(roomData[roomId].playerIds[clientSocket.playerId] + 'is in the game');
} }
*/
// This should return the deck data, etc. for each client
// ideally only returning the items that the user can/should
// see i.e. shouldn't give them the inDeck card list just a counter
// shouldn't have opponent card data/their hand shouldn't be flipped
roomMod.roomGeneration().then(data => {
response.success = true;
response.message = data;
// Each player then gets the roomGeneration stuff
for (const clientId of clients) {
const clientSocket = io.sockets.sockets.get(clientId);
console.log('>> responseStartGame: '+clientSocket.playerId);
// Emit to client socket
io.to(clientSocket.id).emit('responseStartGame', response);
}
})
.catch(err => {
response.message = err;
// Each player then gets the error message
for (const clientId of clients) {
const clientSocket = io.sockets.sockets.get(clientId);
// Emit to client socket
io.to(clientSocket.id).emit('responseStartGame', err);
}
});
}
io.to(socket.id).emit('responseRoom', 'error'); // Then do functions like this?
console.log('>> Rooms exceeded'); function shuffleDeck(roomId, playerId){
} }

Loading…
Cancel
Save