Move all non-socket requests from server.js
parent
840d263eb4
commit
c04b7f0b6c
@ -0,0 +1,226 @@
|
||||
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, io, filter){
|
||||
console.log('+ requestRooms recieved');
|
||||
console.log('- filter: '+filter);
|
||||
|
||||
let response = getRooms(filter, dump = true);
|
||||
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, io, playerName){
|
||||
console.log('+ createRoom recieved');
|
||||
console.log('- requested by: '+playerName);
|
||||
|
||||
response = createRoom(roomId = false, dump = true);
|
||||
io.to(socket.id).emit('returnCreateRoom', response);
|
||||
|
||||
if(response.success){
|
||||
let response = getRooms(filter = 'all', dump = true);
|
||||
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, io, 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)
|
||||
io.to(socket.id).emit('responseRoom', response);
|
||||
|
||||
if (people >= maxPlayersPerRoom) {
|
||||
console.log('- starting game');
|
||||
// startGame for room
|
||||
startGame(roomId, io);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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, io){
|
||||
|
||||
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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// TODO: Need a 'leave room'/disconnect
|
||||
|
||||
module.exports = {
|
||||
requestRooms
|
||||
,requestJoinRoom
|
||||
,requestCreateRoom
|
||||
};
|
||||
Loading…
Reference in New Issue