implemented hunter

This commit is contained in:
fabianthome
2021-07-12 13:18:31 +02:00
parent 6c14f8619a
commit 58e9cd7115
5 changed files with 48 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
const Player = require('./Player');
const Hunter = require("./Hunter");
const Hunter = require('./Hunter');
class Game {
@@ -93,15 +93,15 @@ class Game {
}
}
getPlayerNames(){
getPlayerNames() {
return this.playerNames;
}
addPlayerName(playerName){
addPlayerName(playerName) {
this.playerNames.push(playerName);
}
removePlayerName(playerName){
removePlayerName(playerName) {
this.playerNames.splice(this.playerNames.indexOf(playerName), 1)
}
}

View File

@@ -4,13 +4,17 @@ class Hunter {
this.isAlive = false;
}
getPosition() {
return this.position;
}
move_by(amount) {
this.position += amount;
}
hunt(playerArray) {
for (let i = 0; i < playerArray.length; i++) {
if (playerArray[i].position <= this.position) {
if (playerArray[i].position <= this.position - 1) {
playerArray[i].isAlive = false;
}
}

View File

@@ -2,10 +2,10 @@ const Game = require('./Game');
const express = require('express');
const fs = require('fs');
const {instrument} = require("@socket.io/admin-ui");
const { instrument } = require("@socket.io/admin-ui");
const app = express();
const server = require('http').createServer(app);
const {Server} = require("socket.io");
const { Server } = require("socket.io");
const io = new Server(server);
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
@@ -111,7 +111,6 @@ io.on('connection', socket => {
} else {
io.to(socket.id).emit('error', 'It\'s not your turn');
}
});
socket.on('get card', function (difficulty) {
@@ -119,7 +118,7 @@ io.on('connection', socket => {
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);
} else {
@@ -147,16 +146,18 @@ io.on('connection', socket => {
io.in(socket.room).emit('player moved', {
"next_player": game[socket.room].players[game[socket.room].currentPlayerIndex].name,
"player": index,
"position": position
"position": position,
"state": game[socket.room].currentStatus,
});
io.in(socket.room).emit('update Hunter', game[socket.room].hunter.getPosition());
switch (game[socket.room].currentStatus) {
case Game.STATUS.IS_WON:
//TODO show clients the winner
//game[socket.room].winnerIndex
console.log("FINISHED");
break;
case Game.STATUS.IS_DRAW:
//TODO show clients that nobody wins
console.log("DRAW");
break;
default:
break;