Skip to content

Commit

Permalink
#12 Finished Server2 for Sprint 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelEwinger committed May 2, 2022
1 parent ed936f1 commit 8181326
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
5 changes: 2 additions & 3 deletions backend/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
lobby = room;
})

socket.on('test2', (msg) =>{
displayMessage("Game starts " + msg);

socket.on('startGame', (room) =>{
displayMessage("Game starts " + room);
})


Expand Down
2 changes: 1 addition & 1 deletion backend/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function checkIfRoomIsReady(room, socket){
}

function sendGameStarts(room){
io.in(room).emit("test2", "Hello");
io.in(room).emit("startGame", "Hello");
}

async function validateRoom(room, socket) {
Expand Down
56 changes: 45 additions & 11 deletions backend/server/server2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const { v4: uuidv4 } = require('uuid');
const rooms = {};



/**
* Check if a private Room was send
* @param socket A connected socket.io socket
* @param room Name of the room
* @param io
*/
function validateRoom(socket, room, io){
if(room === ''){
let roomID = searchEmptyRooms();
Expand All @@ -33,7 +38,10 @@ function validateRoom(socket, room, io){
}



/**
* Will create a new Room and returns the room
* @returns {{ready: number, id: (*|string), sockets: *[], status: boolean}}
*/
function createRoom () {
const room = {
id: uuidv4(), // generate a unique id for the new room, that way we don't need to deal with duplicates.
Expand All @@ -47,7 +55,12 @@ function createRoom () {
return room;
}

/**
* Will search for Empty Rooms and returns a room
* @returns {room}
*/
function searchEmptyRooms(){
console.log("Search")
for (const id in rooms) {
const room = rooms[id];
if(room.sockets.length < 4 && room.status === false){
Expand All @@ -59,21 +72,27 @@ function searchEmptyRooms(){
* Will connect a socket to a specified room
* @param socket A connected socket.io socket
* @param room An object that represents a room from the `rooms` instance variable object
* @param io
*/
function joinRoom(socket, room, io) {
room.sockets.push(socket.id);
socket.join(room.id);
console.log(socket.id, "Joined", room.id);
io.in(room.id).emit('join-room', room.id, room.sockets)
io.in(room.id).emit('join-room', room.id, room.sockets);


if(room.sockets.length === 4){
room.status = true;
console.log(room.id + " is full")
io.in(room.id).emit('startGame', room.id, room.sockets);
createRoom();
}
}

/**
* Will make the socket leave any rooms that it is a part of
* @param socket A connected socket.io socket
*/
function leaveRooms(socket){
const roomsToDelete = [];
// check to see if the socket is in the current room
Expand All @@ -99,6 +118,27 @@ function leaveRooms(socket){
}
}

/**
*
* @param socket
* @param room
* @param io
*/
function increaseReadyCounterForRoom(socket, room, io){
if(rooms[room] !== undefined){
rooms[room].ready++;
if(rooms[room].ready === rooms[room].sockets.length && rooms[room].sockets.length >= 2){
rooms[room].status = true;
io.in(room).emit('startGame', room, rooms[room]);
console.log("Game starts " + room)
}
}else{
io.to(socket).emit('error', "Couldn't find Room");
}
}



app.get('/', (req, res) =>{
res.write(`<h1>Socket IO Start on Port : ${PORT}</h1>`)
res.end();
Expand All @@ -122,7 +162,8 @@ io.on('connection', (socket) => {

});

socket.on("readyForGame", (room, socket) =>{
socket.on('readyForGame', (room) =>{
increaseReadyCounterForRoom(socket, room, io);

});

Expand All @@ -138,13 +179,6 @@ io.on('connection', (socket) => {
});









/*
instrument(io, {
auth: false
Expand Down
22 changes: 21 additions & 1 deletion backend/server/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,28 @@ function create(){




/*
createRoom();
check();
console.log(rooms)
*/

const room = {
id: uuidv4(),// generate a unique id for the new room, that way we don't need to deal with duplicates.
sockets: [],
status: false
};
rooms[room.id] = room;
let find = 'room.id';

const room2 = {
id: uuidv4(),// generate a unique id for the new room, that way we don't need to deal with duplicates.
sockets: [],
status: false
};
rooms[room2.id] = room2;

//console.log(rooms)

console.log(rooms[find])

0 comments on commit 8181326

Please sign in to comment.