|
|
|
@ -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){
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|