You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cardGame/rooms.js

227 lines
5.2 KiB
JavaScript

const roomMod = require('./roomMod');
// Data for rooms only atm
let data = {};
let roomData = {};
// Variables for server overall
let numRooms = 0;
const maxRooms = 3;
const maxPlayersPerRoom = 1;
const maxSpectatorsPerRoom = 0;
function requestRooms(socket, filter){
console.log('+ requestRooms recieved');
console.log('- filter: '+filter);
let response = getRooms(filter, dump = true);
global.io.to(socket.id).emit('returnRooms', response);
console.log('');
}
function getRooms(filter = 'all', dump = false){
console.log('+ getRooms');
let response = {
random: 'randomStuff',
roomData: roomData,
};
if(dump){
console.log(response);
console.log('');
}
return response;
}
function requestCreateRoom(socket, playerName){
console.log('+ createRoom recieved');
console.log('- requested by: '+playerName);
response = createRoom(roomId = false, dump = true);
global.io.to(socket.id).emit('returnCreateRoom', response);
if(response.success){
let response = getRooms(filter = 'all', dump = true);
global.io.to(socket.id).emit('returnRooms', response);
}
console.log('');
}
function createRoom(roomId = false, dump = true){
let roomName = false;
if(roomId == false){
roomId = numRooms + 1;
}
console.log(roomId);
let response = {
success: false,
message: 'No idea bossman'
};
// Max room limit reached
console.log(numRooms);
console.log(maxRooms);
if(numRooms >= maxRooms){
console.log('- Room limit reached');
response = {
success: false,
message: 'No space '+numRooms+' out of '+maxRooms+' created.'
};
// Create room
}else{
console.log('- Creating room')
let room = {};
room['id'] = roomId;
room['name'] = 'Room:'+room['id'];
roomName = room['name'];
room['password'] = '';
room['timeout'] = {};
room['timeout']['s'] = 10;
room['people'] = 0;
room['playerIds'] = {};
roomData[roomId] = room;
numRooms = numRooms + 1;
response = {
success: true,
message: 'Room Created: '+roomName,
};
}
if(dump){
console.log(response);
console.log('');
}
return response;
}
function requestJoinRoom(socket, playerName, roomId){
console.log('+ requestJoinRoom recieved');
let room = roomData[roomId];
// https://stackoverflow.com/a/18096649
socket.playerId = playerName;
if(room === undefined){
console.log('>> Room does not exist');
return 'error';
}
let roomName = 'Room_' + roomId;
let people = room['people'];
if(isUserInRoom(playerName, roomId)){
console.log('>> Already in room');
return 'already in room';
}
if (people < maxPlayersPerRoom) {
// Update people in room count
people = room['people'] += 1;
// 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);
console.log('>> User ' + playerName +
' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')');
// Joined room (emit to the player that just joined)
global.io.to(socket.id).emit('responseRoom', response);
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 = global.io.sockets.adapter.rooms.get(roomId);
// 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(roomId).then(data => {
response.success = true;
response.message = data;
// Each player then gets the roomGeneration stuff
for (const clientId of clients) {
const clientSocket = global.io.sockets.sockets.get(clientId);
console.log('>> responseStartGame: '+clientSocket.playerId);
// Emit to client socket
global.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 = global.io.sockets.sockets.get(clientId);
// Emit to client socket
global.io.to(clientSocket.id).emit('responseStartGame', err);
}
});
}
// TODO: Need a 'leave room'/disconnect
module.exports = {
requestRooms
,requestJoinRoom
,requestCreateRoom
};