implement hunter
This commit is contained in:
parent
4674720cfb
commit
f022f17186
@ -11,7 +11,7 @@ class Player {
|
|||||||
constructor(socketUsername) {
|
constructor(socketUsername) {
|
||||||
this.socketUsername = socketUsername;
|
this.socketUsername = socketUsername;
|
||||||
this.position = 0;
|
this.position = 0;
|
||||||
this.alive = true;
|
this.isAlive = true;
|
||||||
}
|
}
|
||||||
move(amount) {
|
move(amount) {
|
||||||
this.position += amount;
|
this.position += amount;
|
||||||
@ -28,20 +28,43 @@ class Hunter {
|
|||||||
move(amount) {
|
move(amount) {
|
||||||
this.position += amount;
|
this.position += amount;
|
||||||
}
|
}
|
||||||
hunt(players) {
|
hunt(playerArray) {
|
||||||
for (let i = 0; i < players.length; i++) {
|
for (let i = 0; i < playerArray.length; i++) {
|
||||||
if (players[i].position <= this.position) {
|
if (playerArray[i].position <= this.position) {
|
||||||
players[i].alive = false;
|
playerArray[i].isAlive = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let gameState = {
|
let gameState = {
|
||||||
players: [],
|
players: [],
|
||||||
whosNext: 0,
|
whosNext: 0,
|
||||||
started: false
|
started: false,
|
||||||
|
round: 0,
|
||||||
|
hunter: new Hunter()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function finish_turn() {
|
||||||
|
// move on to next player; skip dead players
|
||||||
|
do {
|
||||||
|
gameState.whosNext++;
|
||||||
|
if (gameState.whosNext === gameState.players.length) {
|
||||||
|
gameState.whosNext = 0;
|
||||||
|
gameState.round++;
|
||||||
|
}
|
||||||
|
} while (!gameState.players[gameState.whosNext].isAlive);
|
||||||
|
// kill players with hunter
|
||||||
|
if (gameState.round >= 5) {
|
||||||
|
gameState.hunter.move(1);
|
||||||
|
gameState.hunter.hunt(gameState.players);
|
||||||
|
}
|
||||||
|
// check if all players are dead
|
||||||
|
if (!gameState.players.some(player => player.isAlive === true)) {
|
||||||
|
// todo: end game (all players are dead)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let port = 5000;
|
let port = 5000;
|
||||||
server.listen(port, function () {
|
server.listen(port, function () {
|
||||||
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
|
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
|
||||||
@ -67,17 +90,16 @@ if (process.env.WEBSOCKET_MONITOR_USERNAME && process.env.WEBSOCKET_MONITOR_PASS
|
|||||||
// Serve static files (html, css, js)
|
// Serve static files (html, css, js)
|
||||||
app.use(express.static(__dirname + '/../public'));
|
app.use(express.static(__dirname + '/../public'));
|
||||||
|
|
||||||
|
|
||||||
// Websockets
|
// Websockets
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
let addedUser = false;
|
let addedUser = false;
|
||||||
|
|
||||||
socket.on('add user', function (data) {
|
socket.on('add user', function (data) {
|
||||||
if (gameState['players'].length < 4 && !gameState['started']) {
|
if (gameState.players.length < 4 && !gameState.started) {
|
||||||
socket.username = data.username;
|
socket.username = data.username;
|
||||||
socket.room = data.room_name;
|
socket.room = data.room_name;
|
||||||
|
|
||||||
gameState['players'].push(new Player(socket.username));
|
gameState.players.push(new Player(socket.username));
|
||||||
addedUser = true;
|
addedUser = true;
|
||||||
|
|
||||||
socket.emit('login');
|
socket.emit('login');
|
||||||
@ -104,23 +126,23 @@ io.on('connection', socket => {
|
|||||||
if (addedUser) {
|
if (addedUser) {
|
||||||
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
||||||
let index = -1;
|
let index = -1;
|
||||||
for (let i = 0; i < gameState['players'].length; i++) {
|
for (let i = 0; i < gameState.players.length; i++) {
|
||||||
if (gameState['players'][i].socketUsername === socket.username) {
|
if (gameState.players[i].socketUsername === socket.username) {
|
||||||
index = i;
|
index = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
gameState['players'].splice(index, 1);
|
gameState.players.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
|
|
||||||
if (gameState['players'].length === 0) {
|
if (gameState.players.length === 0) {
|
||||||
gameState['players'] = [];
|
gameState.players = [];
|
||||||
gameState['whosNext'] = 0;
|
gameState.whosNext = 0;
|
||||||
gameState['started'] = false;
|
gameState.started = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,8 +153,8 @@ io.on('connection', socket => {
|
|||||||
// Game
|
// Game
|
||||||
socket.on('roll dice', function () {
|
socket.on('roll dice', function () {
|
||||||
|
|
||||||
if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) {
|
if (gameState.players[gameState.whosNext].socketUsername === socket.username) {
|
||||||
gameState['started'] = true;
|
gameState.started = true;
|
||||||
let sides = 3;
|
let sides = 3;
|
||||||
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
||||||
|
|
||||||
@ -145,7 +167,7 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('get card', function (difficulty) {
|
socket.on('get card', function (difficulty) {
|
||||||
if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) {
|
if (gameState.players[gameState.whosNext].socketUsername === 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);
|
||||||
@ -155,10 +177,9 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
||||||
if (answerIsCorrect) gameState['players'][gameState['whosNext']].move(difficulty);
|
if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty);
|
||||||
gameState['whosNext'] += 1;
|
|
||||||
if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0;
|
|
||||||
io.in(socket.room).emit('card destroyed');
|
io.in(socket.room).emit('card destroyed');
|
||||||
|
finish_turn();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user