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.
247 lines
6.1 KiB
JavaScript
247 lines
6.1 KiB
JavaScript
const roomMod = require('./roomMod');
|
|
const gameMod = require('./gameMod');
|
|
|
|
// Variables for server overall
|
|
let numRooms = 0;
|
|
const maxRooms = 3;
|
|
const maxPlayersPerRoom = 2;
|
|
global.maxPlayersPerRoom = maxPlayersPerRoom;
|
|
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: global.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'] = {};
|
|
|
|
global.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 = global.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 global.roomData[roomId]['playerIds']){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function startGame(roomId){
|
|
|
|
console.log('>> Room: ' + roomId + ': Requesting game...');
|
|
let people = global.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
|
|
|
|
// Not sure how to catch errors for these await alls
|
|
// TODO: Look into error handling for await alls
|
|
const [itemData] =
|
|
await Promise.all([
|
|
roomMod.roomGeneration(roomId),
|
|
]);
|
|
|
|
|
|
// data is the 'itemData' not all the roomData
|
|
response.success = true;
|
|
response.message = itemData;
|
|
|
|
// Each player then gets sent the roomGeneration stuff
|
|
// TODO:They should recieve different data based on what they can see/interact
|
|
|
|
for (const clientId of clients) {
|
|
|
|
const clientSocket = global.io.sockets.sockets.get(clientId);
|
|
console.log('>> responseStartGame: '+clientSocket.playerId);
|
|
|
|
// TODO: TESTING STUFF, REMOVE WHEN SORTED
|
|
let playerIdTemp = roomData[roomId].playerOrder[clientSocket.playerId];
|
|
//let message = 'You are player: '+playerIdTemp;
|
|
//global.socketAlert(clientSocket.id, message, 'alert');
|
|
|
|
// This is the data being sent, add each individual players Id/turnorder/whatever I call it
|
|
response.message['playerId'] = playerIdTemp;
|
|
|
|
// JANK TODO: better this.
|
|
// Ids are 0,1. Max players is 2, so can use modulo for now (while it's only ever 2 players)
|
|
// to set the opponent stuff. With the data passed this can probably be done better, but not sure
|
|
// at the mo. so to get progress, we're janking it in
|
|
// player 1+1 = 2 %= 0, player 0+1 = 1 %= 1
|
|
response.message['opponentId'] = (playerIdTemp+1)%maxPlayersPerRoom;
|
|
|
|
// Emit the itemData to client socket (TODO: only emit what each player should see/recieve)
|
|
global.io.to(clientSocket.id).emit('responseStartGame', response);
|
|
|
|
}
|
|
|
|
// Start game gameMod?
|
|
gameMod.gameStart(roomId);
|
|
|
|
}
|
|
|
|
// TODO: Need a 'leave room'/disconnect
|
|
|
|
module.exports = {
|
|
requestRooms
|
|
,requestJoinRoom
|
|
,requestCreateRoom
|
|
};
|