- Added multiplayer functions

- Only active user can roll the dice
- Only active user can select answers
- Card will be destroyed if active user selects answer
This commit is contained in:
2021-06-10 14:05:01 +02:00
parent d47ca1be66
commit c4a8c84db2
4 changed files with 78 additions and 33 deletions

View File

@@ -6,6 +6,13 @@ let {Server} = require("socket.io");
let io = new Server(server);
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/cards.json'));
let gameState = {
players: [],
positions: [],
whosNext: 0,
started: false
};
let port = 5000;
server.listen(port, function () {
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
@@ -19,17 +26,23 @@ io.on('connection', socket => {
let addedUser = false;
socket.on('add user', function (data) {
socket.username = data.username;
socket.room = data.room_name;
if (gameState['players'].length < 4 && !gameState['started']) {
socket.username = data.username;
socket.room = data.room_name;
addedUser = true;
gameState['players'].push(socket.username);
gameState['positions'].push(1);
addedUser = true;
socket.emit('login');
socket.join(socket.room);
socket.emit('login');
socket.join(socket.room);
socket.broadcast.to(socket.room).emit('user joined', socket.username);
socket.broadcast.to(socket.room).emit('user joined', socket.username);
generate_log_message(socket.room, socket.username, "JOINED", "");
generate_log_message(socket.room, socket.username, "JOINED", "");
} else {
// TODO
}
});
socket.on('new message', function (data) {
@@ -44,6 +57,12 @@ io.on('connection', socket => {
socket.on('disconnect', function () {
if (addedUser) {
socket.broadcast.to(socket.room).emit('user left', socket.username);
let index = gameState['players'].indexOf(socket.username);
if (index > -1) {
gameState['players'].splice(index, 1);
gameState['positions'].splice(index, 1);
}
}
generate_log_message(socket.room, socket.username, "LEFT", "");
@@ -52,18 +71,32 @@ io.on('connection', socket => {
// Game
socket.on('roll dice', function () {
let sides = 3;
let randomNumber = Math.floor(Math.random() * sides) + 1;
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
gameState['started'] = true;
let sides = 3;
let randomNumber = Math.floor(Math.random() * sides) + 1;
io.in(socket.room).emit('dice', randomNumber);
io.in(socket.room).emit('dice', randomNumber);
generate_log_message(socket.room, socket.username, "DICE", randomNumber);
generate_log_message(socket.room, socket.username, "DICE", randomNumber);
} else {
// TODO
}
});
socket.on('get card', function (difficulty) {
io.in(socket.room).emit('card', getRandomCard(difficulty));
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)});
generate_log_message(socket.room, socket.username, "CARD", difficulty);
generate_log_message(socket.room, socket.username, "CARD", difficulty);
} else {
// TODO
}
});
socket.on('card finished', function (difficulty) {
gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty;
io.in(socket.room).emit('card destroyed');
});
});
@@ -115,5 +148,5 @@ function shuffleAnswers(card) {
function pad(width, string, padding) {
if (string === undefined || string === null) return pad(width, " ", " ");
return (width <= string.length) ? string : pad(width, string + padding, padding)
return (width <= string.length) ? string : pad(width, string + padding, padding);
}