Merge branch 'feature/serverSide' into develop
commit
579848c84c
@ -0,0 +1,99 @@
|
|||||||
|
// A seperate list for components, so they're easy to recall
|
||||||
|
|
||||||
|
// Done as object, so it can be added to different rooms with components = NEW component?
|
||||||
|
const component = {
|
||||||
|
|
||||||
|
// Entity Stuff
|
||||||
|
//item : [],
|
||||||
|
//itemCount : 0,
|
||||||
|
|
||||||
|
roomId : null,
|
||||||
|
turn : 0,
|
||||||
|
playerTurn : 0,
|
||||||
|
|
||||||
|
// Card Stuff
|
||||||
|
cardData : {},
|
||||||
|
cardFace : {},
|
||||||
|
cardStatus : {
|
||||||
|
tapped : {},
|
||||||
|
attacking : {},
|
||||||
|
inspected : {},
|
||||||
|
},
|
||||||
|
cardAttack : {},
|
||||||
|
cardColours : {}, // Replace with colour
|
||||||
|
colour : {
|
||||||
|
white : {}, // entityId, amountOfColour
|
||||||
|
blue : {},
|
||||||
|
red : {},
|
||||||
|
},
|
||||||
|
cardManaColour : {},
|
||||||
|
cardEffect : {}, // TODO: Split this into effect, trigger, step, targets.
|
||||||
|
cardCost : {},
|
||||||
|
cardSprite : {}, // id, position in spritesheet [0,4] e.g.
|
||||||
|
//cardPlayer = {},
|
||||||
|
|
||||||
|
// Deck Stuff?
|
||||||
|
deckIn : {},
|
||||||
|
deckData : {},
|
||||||
|
|
||||||
|
// UI (so users know what's been targetted)
|
||||||
|
selected : {},
|
||||||
|
selectable : {},
|
||||||
|
|
||||||
|
// Effect (break it up?)
|
||||||
|
effect : {},
|
||||||
|
effectTrigger : {},
|
||||||
|
// etc, etc.
|
||||||
|
|
||||||
|
// Board Elements
|
||||||
|
// loop component.shield for shield items
|
||||||
|
boardElement : {},
|
||||||
|
// Replace with following
|
||||||
|
realDeck : {},
|
||||||
|
inDeck : {},
|
||||||
|
hand : {},
|
||||||
|
board : {},
|
||||||
|
shield : {},
|
||||||
|
mana : {},
|
||||||
|
grave : {},
|
||||||
|
void : {},
|
||||||
|
|
||||||
|
//
|
||||||
|
listPosition : {},
|
||||||
|
// position (clientside)
|
||||||
|
// size (clientside)
|
||||||
|
|
||||||
|
// Passives
|
||||||
|
// component.passive.flight ?
|
||||||
|
passive : {
|
||||||
|
flight : {},
|
||||||
|
reach : {},
|
||||||
|
taunt : {},
|
||||||
|
},
|
||||||
|
|
||||||
|
type : {
|
||||||
|
unit : {},
|
||||||
|
spell : {},
|
||||||
|
token : {},
|
||||||
|
},
|
||||||
|
|
||||||
|
classes : {
|
||||||
|
orc : {},
|
||||||
|
human : {},
|
||||||
|
spirit : {},
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// For front-end
|
||||||
|
// position, size
|
||||||
|
|
||||||
|
// These should be used as such (Not 100% as yet)
|
||||||
|
// For onBoard()... for player()... for passive.flight()...
|
||||||
|
// Check the board, items belonging to playerX, for flight passive?
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
component
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
// For anything related to the actual game itself (kinda)
|
||||||
|
// this will be split into different bits, but should be what manages a rooms
|
||||||
|
// game states, and alladat
|
||||||
|
// Basically here to prevent circular dependencies (where I can)
|
||||||
|
|
||||||
|
|
||||||
|
// PlayerId is using array 0,1,2 for now, not the actual id
|
||||||
|
// actual Id would be better, but the player should be passed correctly
|
||||||
|
// from the client. They can edit data, but server-side validation SHOULD prevent
|
||||||
|
// in the future
|
||||||
|
function passTurn(roomId, playerId){
|
||||||
|
|
||||||
|
// Check playerId (not correct atm) before doing the stuff, to verify the user
|
||||||
|
// IS the user, and in the room
|
||||||
|
|
||||||
|
// Test alert so that different data/emits can be sent per-player
|
||||||
|
global.socketAlert(roomData[roomId].playerData[0].socketId, 'Pass', 'alert');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
passTurn
|
||||||
|
};
|
||||||
|
|
||||||
@ -0,0 +1,229 @@
|
|||||||
|
const roomMod = require('./roomMod');
|
||||||
|
|
||||||
|
// Variables for server overall
|
||||||
|
let numRooms = 0;
|
||||||
|
const maxRooms = 3;
|
||||||
|
const maxPlayersPerRoom = 2;
|
||||||
|
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
|
||||||
|
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 message = 'You are player: '+roomData[roomId].playerOrder[clientSocket.playerId];
|
||||||
|
global.socketAlert(clientSocket.id, message, 'alert');
|
||||||
|
|
||||||
|
// Emit the itemData to client socket (TODO: only emit what each player should see/recieve)
|
||||||
|
global.io.to(clientSocket.id).emit('responseStartGame', response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Need a 'leave room'/disconnect
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
requestRooms
|
||||||
|
,requestJoinRoom
|
||||||
|
,requestCreateRoom
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue