End conditions #40
@ -2,44 +2,56 @@ const Player = require('./Player');
|
|||||||
const Hunter = require("./Hunter");
|
const Hunter = require("./Hunter");
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
|
|
||||||
|
static MAX_PLAYERS = 4;
|
||||||
|
static WIN_POSITION = 16;
|
||||||
|
static STATUS = {
|
||||||
|
SETTING_UP: 0,
|
||||||
|
ONGOING: 1,
|
||||||
|
IS_DRAW: 2,
|
||||||
|
IS_WON: 3
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.currentStatus = Game.STATUS.SETTING_UP;
|
||||||
this.players = [];
|
this.players = [];
|
||||||
this.currentPlayerIndex = 0;
|
this.currentPlayerIndex = 0;
|
||||||
this.started = false;
|
this.winnerIndex = 0;
|
||||||
this.round = 0;
|
this.round = 0;
|
||||||
this.hunter = new Hunter();
|
this.hunter = new Hunter();
|
||||||
}
|
}
|
||||||
|
|
||||||
finish_turn() {
|
finish_turn() {
|
||||||
let move_to_next_round = false;
|
this.update_game_status();
|
||||||
// move on to next player; skip dead players
|
|
||||||
do {
|
|
||||||
if (this.players.length === 0) break;
|
|
||||||
|
|
||||||
|
if (this.currentStatus !== Game.STATUS.ONGOING) return;
|
||||||
|
if (!this.players.some(player => player.isAlive === true)) return;
|
||||||
|
|
||||||
|
let roundIsOver = false;
|
||||||
|
do {
|
||||||
this.currentPlayerIndex++;
|
this.currentPlayerIndex++;
|
||||||
if (this.currentPlayerIndex >= this.players.length) {
|
if (this.currentPlayerIndex >= this.players.length) {
|
||||||
this.currentPlayerIndex = 0;
|
this.currentPlayerIndex = 0;
|
||||||
move_to_next_round = true;
|
roundIsOver = true;
|
||||||
}
|
}
|
||||||
} while (!this.players[this.currentPlayerIndex].isAlive); // skip dead players
|
} while (!this.players[this.currentPlayerIndex].isAlive); // skip dead players
|
||||||
this.finish_round();
|
|
||||||
|
if (roundIsOver) this.#finish_round();
|
||||||
}
|
}
|
||||||
|
|
||||||
finish_round() {
|
#finish_round() {
|
||||||
this.round++;
|
this.round++;
|
||||||
// kill players with hunter
|
|
||||||
if (this.round >= 5) {
|
if (this.round >= 5) {
|
||||||
this.hunter.move_by(1);
|
this.hunter.move_by(1);
|
||||||
this.hunter.hunt(this.players);
|
this.hunter.hunt(this.players);
|
||||||
}
|
}
|
||||||
// check if all players are dead
|
this.update_game_status();
|
||||||
if (!this.players.some(player => player.isAlive === true)) {
|
|
||||||
// todo: end game (all players are dead)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_player(name) {
|
add_player(name) {
|
||||||
this.players.push(new Player(name));
|
let canAddPlayer = this.players.length < Game.MAX_PLAYERS;
|
||||||
|
if (canAddPlayer) this.players.push(new Player(name));
|
||||||
|
return canAddPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_player(name) {
|
remove_player(name) {
|
||||||
@ -52,6 +64,7 @@ class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
current_player_is(name) {
|
current_player_is(name) {
|
||||||
|
if (this.players[this.currentPlayerIndex] === undefined) return false;
|
||||||
return this.players[this.currentPlayerIndex].name === name;
|
return this.players[this.currentPlayerIndex].name === name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +74,19 @@ class Game {
|
|||||||
|
|
||||||
move_player(name, amount) {
|
move_player(name, amount) {
|
||||||
let index = this.get_player_index(name);
|
let index = this.get_player_index(name);
|
||||||
|
if (index === -1) return;
|
||||||
this.players[index].move_by(amount);
|
this.players[index].move_by(amount);
|
||||||
|
this.update_game_status();
|
||||||
|
}
|
||||||
|
|
||||||
|
update_game_status() {
|
||||||
|
if (!this.players.some(player => player.isAlive === true)) this.currentStatus = Game.STATUS.IS_DRAW;
|
||||||
|
|
||||||
|
let index = this.players.findIndex(player => player.position >= Game.WIN_POSITION);
|
||||||
|
if (index !== -1) {
|
||||||
|
this.currentStatus = Game.STATUS.IS_WON;
|
||||||
|
this.winnerIndex = index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,10 +7,6 @@ class Player {
|
|||||||
|
|
||||||
move_by(amount) {
|
move_by(amount) {
|
||||||
this.position += amount;
|
this.position += amount;
|
||||||
//todo: move by 1 only on the last 3 fields
|
|
||||||
if (this.position >= 16) {
|
|
||||||
// todo: win
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ const {Server} = require("socket.io");
|
|||||||
const io = new Server(server);
|
const io = new Server(server);
|
||||||
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
|
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
|
||||||
|
|
||||||
let gameState = {};
|
let game = {};
|
||||||
|
|
||||||
let port = 5000;
|
let port = 5000;
|
||||||
server.listen(port, function () {
|
server.listen(port, function () {
|
||||||
@ -44,12 +44,12 @@ io.on('connection', socket => {
|
|||||||
socket.username = data.username;
|
socket.username = data.username;
|
||||||
socket.room = data.room_name;
|
socket.room = data.room_name;
|
||||||
|
|
||||||
if (gameState[socket.room] === undefined) {
|
if (game[socket.room] === undefined) {
|
||||||
gameState[socket.room] = new Game();
|
game[socket.room] = new Game();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gameState[socket.room].players.length < 4 && !gameState[socket.room].started) {
|
if (game[socket.room].add_player(socket.username)) {
|
||||||
gameState[socket.room].add_player(socket.username);
|
|
||||||
addedUser = true;
|
addedUser = true;
|
||||||
|
|
||||||
socket.emit('login');
|
socket.emit('login');
|
||||||
@ -64,7 +64,7 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('new message', function (data) {
|
socket.on('new message', function (data) {
|
||||||
if (gameState[socket.room] !== undefined && addedUser) {
|
if (game[socket.room] !== undefined && addedUser) {
|
||||||
socket.broadcast.to(socket.room).emit('new message', {
|
socket.broadcast.to(socket.room).emit('new message', {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
message: data
|
message: data
|
||||||
@ -75,15 +75,15 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', function () {
|
||||||
if (gameState[socket.room] !== undefined && addedUser) {
|
if (game[socket.room] !== undefined && addedUser) {
|
||||||
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
||||||
gameState[socket.room].remove_player(socket.username);
|
game[socket.room].remove_player(socket.username);
|
||||||
|
|
||||||
// TODO Close card if card is opened and active player left
|
// TODO Close card if card is opened and active player left
|
||||||
|
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
|
|
||||||
if (gameState[socket.room].players.length === 0) delete gameState[socket.room];
|
if (game[socket.room].players.length === 0) delete game[socket.room];
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_log_message(socket.room, socket.username, "LEFT", "");
|
generate_log_message(socket.room, socket.username, "LEFT", "");
|
||||||
@ -91,9 +91,10 @@ io.on('connection', socket => {
|
|||||||
|
|
||||||
// Game
|
// Game
|
||||||
socket.on('roll dice', function () {
|
socket.on('roll dice', function () {
|
||||||
if (gameState[socket.room] !== undefined && addedUser) {
|
if (game[socket.room] === undefined || !addedUser) return;
|
||||||
if (gameState[socket.room].current_player_is(socket.username)) {
|
|
||||||
gameState[socket.room].started = true;
|
if (game[socket.room].current_player_is(socket.username)) {
|
||||||
|
game[socket.room].currentStatus = Game.STATUS.ONGOING;
|
||||||
let sides = 3;
|
let sides = 3;
|
||||||
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
||||||
|
|
||||||
@ -103,39 +104,55 @@ io.on('connection', socket => {
|
|||||||
} else {
|
} else {
|
||||||
io.to(socket.id).emit('error', 'It\'s not your turn');
|
io.to(socket.id).emit('error', 'It\'s not your turn');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('get card', function (difficulty) {
|
socket.on('get card', function (difficulty) {
|
||||||
if (gameState[socket.room] !== undefined && addedUser) {
|
if (game[socket.room] === undefined || !addedUser) return;
|
||||||
if (gameState[socket.room].current_player_is(socket.username)) {
|
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
|
||||||
|
|
||||||
|
if (game[socket.room].current_player_is(socket.username)) {
|
||||||
io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)});
|
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 {
|
} else {
|
||||||
io.to(socket.id).emit('error', 'It\'s not your turn');
|
io.to(socket.id).emit('error', 'It\'s not your turn');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
||||||
if (gameState[socket.room] !== undefined && addedUser) {
|
if (game[socket.room] === undefined || !addedUser) return;
|
||||||
if (answerIsCorrect) {
|
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
|
||||||
gameState[socket.room].move_player(socket.username, difficulty);
|
|
||||||
generate_log_message(socket.room, socket.username, "MOVE", difficulty);
|
|
||||||
}
|
|
||||||
io.in(socket.room).emit('card destroyed');
|
io.in(socket.room).emit('card destroyed');
|
||||||
gameState[socket.room].finish_turn();
|
|
||||||
|
|
||||||
let index = gameState[socket.room].get_player_index(socket.username);
|
if (answerIsCorrect) {
|
||||||
let next_player = gameState[socket.room].players[gameState[socket.room].currentPlayerIndex].name;
|
game[socket.room].move_player(socket.username, difficulty);
|
||||||
|
generate_log_message(socket.room, socket.username, "MOVE", difficulty);
|
||||||
|
|
||||||
|
let index = game[socket.room].get_player_index(socket.username);
|
||||||
|
let next_player = game[socket.room].players[game[socket.room].currentPlayerIndex].name;
|
||||||
io.in(socket.room).emit('player moved', {
|
io.in(socket.room).emit('player moved', {
|
||||||
"next_player": next_player,
|
"next_player": next_player,
|
||||||
"player": index,
|
"player": index,
|
||||||
"position": gameState[socket.room].players[index].position
|
"position": game[socket.room].players[index].position
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game[socket.room].finish_turn();
|
||||||
|
|
||||||
|
switch (game[socket.room].currentStatus) {
|
||||||
|
case Game.STATUS.IS_WON:
|
||||||
|
//TODO show clients the winner
|
||||||
|
//game[socket.room].winnerIndex
|
||||||
|
break;
|
||||||
|
case Game.STATUS.IS_DRAW:
|
||||||
|
//TODO show clients that nobody wins
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user