2021-06-17 06:45:45 +00:00
|
|
|
const Game = require('./Game');
|
|
|
|
|
2021-06-10 17:52:32 +00:00
|
|
|
const express = require('express');
|
|
|
|
const fs = require('fs');
|
2021-07-12 11:28:23 +00:00
|
|
|
const {instrument} = require("@socket.io/admin-ui");
|
2021-06-10 17:52:32 +00:00
|
|
|
const app = express();
|
|
|
|
const server = require('http').createServer(app);
|
2021-07-12 11:28:23 +00:00
|
|
|
const {Server} = require("socket.io");
|
2021-06-10 17:52:32 +00:00
|
|
|
const io = new Server(server);
|
2021-06-10 10:45:56 +00:00
|
|
|
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
|
2021-05-26 14:26:05 +00:00
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
let game = {};
|
2021-06-15 22:06:00 +00:00
|
|
|
|
2021-05-26 20:58:00 +00:00
|
|
|
let port = 5000;
|
2021-05-26 14:26:05 +00:00
|
|
|
server.listen(port, function () {
|
2021-05-26 20:58:00 +00:00
|
|
|
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
|
2021-05-26 14:26:05 +00:00
|
|
|
});
|
|
|
|
|
2021-06-10 17:52:32 +00:00
|
|
|
// Monitor websockets
|
2021-06-10 18:27:04 +00:00
|
|
|
if (process.env.WEBSOCKET_MONITOR_USERNAME && process.env.WEBSOCKET_MONITOR_PASSWORD) {
|
|
|
|
instrument(io, {
|
|
|
|
auth: {
|
|
|
|
type: "basic",
|
|
|
|
username: process.env.WEBSOCKET_MONITOR_USERNAME,
|
|
|
|
password: process.env.WEBSOCKET_MONITOR_PASSWORD
|
|
|
|
},
|
|
|
|
serverId: `${require("os").hostname()}#${process.pid}`
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
instrument(io, {
|
|
|
|
auth: false,
|
|
|
|
serverId: `${require("os").hostname()}#${process.pid}`
|
|
|
|
});
|
|
|
|
}
|
2021-05-26 14:26:05 +00:00
|
|
|
|
2021-06-10 17:52:32 +00:00
|
|
|
// Serve static files (html, css, js)
|
2021-05-26 14:26:05 +00:00
|
|
|
app.use(express.static(__dirname + '/../public'));
|
|
|
|
|
2021-06-10 17:52:32 +00:00
|
|
|
// Websockets
|
2021-05-26 20:41:05 +00:00
|
|
|
io.on('connection', socket => {
|
2021-05-26 14:26:05 +00:00
|
|
|
let addedUser = false;
|
|
|
|
|
2021-05-26 20:41:05 +00:00
|
|
|
socket.on('add user', function (data) {
|
2021-06-17 06:35:14 +00:00
|
|
|
socket.username = data.username;
|
|
|
|
socket.room = data.room_name;
|
2021-07-05 14:20:57 +00:00
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] === undefined) {
|
|
|
|
game[socket.room] = new Game();
|
2021-06-17 06:35:14 +00:00
|
|
|
}
|
2021-07-05 14:20:57 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
if (game[socket.room].get_player_index(socket.username) === -1) {
|
|
|
|
if (game[socket.room].add_player(socket.username)) {
|
2021-07-05 14:20:57 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
game[socket.room].addPlayerName(data.username);
|
|
|
|
addedUser = true;
|
2021-07-05 14:20:57 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
socket.emit('login', game[socket.room].get_player_index(socket.username));
|
|
|
|
socket.join(socket.room);
|
|
|
|
io.in(socket.room).emit('updatePlayerNames', game[socket.room].getPlayerNames());
|
2021-05-26 14:26:05 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
if (game[socket.room].players.length === 1) io.to(socket.id).emit('first player');
|
2021-06-21 20:45:58 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
socket.broadcast.to(socket.room).emit('user joined', socket.username);
|
2021-05-26 14:26:05 +00:00
|
|
|
|
2021-07-12 11:41:52 +00:00
|
|
|
generate_log_message(socket.room, socket.username, "JOINED", "");
|
|
|
|
} else {
|
|
|
|
io.to(socket.id).emit('error', 'Game started already or room has too many members');
|
|
|
|
}
|
2021-06-10 12:05:01 +00:00
|
|
|
} else {
|
2021-07-12 11:41:52 +00:00
|
|
|
io.to(socket.id).emit('error', 'Username already exists');
|
2021-06-10 12:05:01 +00:00
|
|
|
}
|
2021-05-26 14:26:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('new message', function (data) {
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] !== undefined && addedUser) {
|
2021-06-17 08:01:11 +00:00
|
|
|
socket.broadcast.to(socket.room).emit('new message', {
|
|
|
|
username: socket.username,
|
|
|
|
message: data
|
|
|
|
});
|
2021-05-26 14:26:05 +00:00
|
|
|
|
2021-06-17 08:01:11 +00:00
|
|
|
generate_log_message(socket.room, socket.username, "MESSAGE", data);
|
|
|
|
}
|
2021-05-26 14:26:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('disconnect', function () {
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] !== undefined && addedUser) {
|
2021-07-05 14:20:57 +00:00
|
|
|
if (game[socket.room].current_player_is(socket.username)) socket.broadcast.to(socket.room).emit('card destroyed');
|
2021-07-01 16:39:23 +00:00
|
|
|
|
|
|
|
game[socket.room].removePlayerName(socket.username);
|
|
|
|
io.in(socket.room).emit('updatePlayerNames', game[socket.room].getPlayerNames());
|
|
|
|
|
2021-06-17 17:22:36 +00:00
|
|
|
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
2021-06-18 11:03:25 +00:00
|
|
|
game[socket.room].remove_player(socket.username);
|
2021-06-14 10:06:49 +00:00
|
|
|
|
|
|
|
socket.leave(socket.room);
|
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room].players.length === 0) delete game[socket.room];
|
2021-05-26 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 20:41:05 +00:00
|
|
|
generate_log_message(socket.room, socket.username, "LEFT", "");
|
2021-05-26 14:26:05 +00:00
|
|
|
});
|
2021-06-08 13:58:32 +00:00
|
|
|
|
|
|
|
// Game
|
|
|
|
socket.on('roll dice', function () {
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] === undefined || !addedUser) return;
|
|
|
|
|
|
|
|
if (game[socket.room].current_player_is(socket.username)) {
|
|
|
|
game[socket.room].currentStatus = Game.STATUS.ONGOING;
|
|
|
|
let sides = 3;
|
|
|
|
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
|
|
|
|
|
|
|
io.in(socket.room).emit('dice', randomNumber);
|
|
|
|
|
|
|
|
generate_log_message(socket.room, socket.username, "DICE", randomNumber);
|
|
|
|
} else {
|
|
|
|
io.to(socket.id).emit('error', 'It\'s not your turn');
|
2021-06-10 12:05:01 +00:00
|
|
|
}
|
2021-06-08 13:58:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('get card', function (difficulty) {
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] === undefined || !addedUser) return;
|
|
|
|
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
|
|
|
|
|
|
|
|
if (game[socket.room].current_player_is(socket.username)) {
|
2021-07-12 11:28:23 +00:00
|
|
|
io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)});
|
2021-06-18 11:03:25 +00:00
|
|
|
|
|
|
|
generate_log_message(socket.room, socket.username, "CARD", difficulty);
|
|
|
|
} else {
|
|
|
|
io.to(socket.id).emit('error', 'It\'s not your turn');
|
2021-06-10 12:05:01 +00:00
|
|
|
}
|
2021-06-18 11:03:25 +00:00
|
|
|
|
2021-06-10 12:05:01 +00:00
|
|
|
});
|
2021-06-08 13:58:32 +00:00
|
|
|
|
2021-06-15 16:38:55 +00:00
|
|
|
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
2021-06-18 11:03:25 +00:00
|
|
|
if (game[socket.room] === undefined || !addedUser) return;
|
|
|
|
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
|
2021-06-18 07:43:40 +00:00
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
io.in(socket.room).emit('card destroyed');
|
2021-06-18 07:43:40 +00:00
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
if (answerIsCorrect) {
|
|
|
|
game[socket.room].move_player(socket.username, difficulty);
|
|
|
|
generate_log_message(socket.room, socket.username, "MOVE", difficulty);
|
2021-06-17 08:01:11 +00:00
|
|
|
}
|
2021-06-18 11:03:25 +00:00
|
|
|
|
2021-06-21 20:45:58 +00:00
|
|
|
let index = game[socket.room].get_player_index(socket.username);
|
|
|
|
let position = game[socket.room].players[index].position;
|
|
|
|
|
2021-06-18 11:03:25 +00:00
|
|
|
game[socket.room].finish_turn();
|
|
|
|
|
2021-06-21 20:45:58 +00:00
|
|
|
io.in(socket.room).emit('player moved', {
|
|
|
|
"next_player": game[socket.room].players[game[socket.room].currentPlayerIndex].name,
|
|
|
|
"player": index,
|
2021-07-12 11:18:31 +00:00
|
|
|
"position": position,
|
|
|
|
"state": game[socket.room].currentStatus,
|
2021-06-21 20:45:58 +00:00
|
|
|
});
|
|
|
|
|
2021-07-12 11:18:31 +00:00
|
|
|
io.in(socket.room).emit('update Hunter', game[socket.room].hunter.getPosition());
|
2021-06-08 13:58:32 +00:00
|
|
|
});
|
2021-05-26 14:26:05 +00:00
|
|
|
});
|
2021-05-26 20:41:05 +00:00
|
|
|
|
|
|
|
function generate_log_message(room, user, type, message) {
|
|
|
|
let color;
|
|
|
|
switch (type) {
|
|
|
|
case 'LEFT':
|
|
|
|
color = '\x1b[31m';
|
|
|
|
break;
|
|
|
|
case 'JOINED':
|
|
|
|
color = '\x1b[32m';
|
|
|
|
break;
|
|
|
|
case 'MESSAGE':
|
|
|
|
color = '\x1b[36m';
|
|
|
|
break;
|
|
|
|
case 'RUNNING':
|
|
|
|
color = '\x1b[35m';
|
|
|
|
break;
|
2021-06-08 13:58:32 +00:00
|
|
|
case 'DICE':
|
|
|
|
color = '\x1b[34m';
|
|
|
|
break;
|
2021-06-18 08:19:03 +00:00
|
|
|
case 'MOVE':
|
|
|
|
color = '\x1b[30m';
|
|
|
|
break;
|
2021-05-26 20:41:05 +00:00
|
|
|
default:
|
|
|
|
color = '\x1b[0m';
|
|
|
|
}
|
|
|
|
room = pad(10, room, ' ').substr(0, 10);
|
|
|
|
user = pad(10, user, ' ').substr(0, 10);
|
|
|
|
type = pad(10, type, ' ').substr(0, 10);
|
|
|
|
|
2021-05-26 20:58:00 +00:00
|
|
|
let reset_color = '\x1b[0m';
|
|
|
|
console.info("%s[%s] [%s] [%s]\x1b[0m %s", color, room, user, type, reset_color, message);
|
2021-05-26 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 13:58:32 +00:00
|
|
|
function getRandomCard(difficulty) {
|
|
|
|
let filtered_cards = cards.filter(card => {
|
2021-06-08 15:33:59 +00:00
|
|
|
return card.difficulty === difficulty;
|
2021-06-08 13:58:32 +00:00
|
|
|
});
|
|
|
|
return shuffleAnswers(filtered_cards[Math.floor(Math.random() * filtered_cards.length)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function shuffleAnswers(card) {
|
|
|
|
for (let i = card.answers.length - 1; i > 0; i--) {
|
|
|
|
let j = Math.floor(Math.random() * (i + 1));
|
|
|
|
[card.answers[i], card.answers[j]] = [card.answers[j], card.answers[i]];
|
|
|
|
}
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-26 20:41:05 +00:00
|
|
|
function pad(width, string, padding) {
|
2021-05-31 09:40:02 +00:00
|
|
|
if (string === undefined || string === null) return pad(width, " ", " ");
|
2021-06-10 12:05:01 +00:00
|
|
|
return (width <= string.length) ? string : pad(width, string + padding, padding);
|
2021-06-17 16:49:31 +00:00
|
|
|
}
|