add methods to Game to access players by name
This commit is contained in:
		@@ -1,26 +1,33 @@
 | 
				
			|||||||
 | 
					const Player = require('./Player');
 | 
				
			||||||
const Hunter = require("./Hunter");
 | 
					const Hunter = require("./Hunter");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Game {
 | 
					class Game {
 | 
				
			||||||
    constructor() {
 | 
					    constructor() {
 | 
				
			||||||
        this.players = [];
 | 
					        this.players = [];
 | 
				
			||||||
        this.whosNext = 0;
 | 
					        this.currentPlayerIndex = 0;
 | 
				
			||||||
        this.started = false;
 | 
					        this.started = false;
 | 
				
			||||||
        this.round = 0;
 | 
					        this.round = 0;
 | 
				
			||||||
        this.hunter = new Hunter();
 | 
					        this.hunter = new Hunter();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    finish_turn() {
 | 
					    finish_turn() {
 | 
				
			||||||
 | 
					        let move_to_next_round = false;
 | 
				
			||||||
        // move on to next player; skip dead players
 | 
					        // move on to next player; skip dead players
 | 
				
			||||||
        do {
 | 
					        do {
 | 
				
			||||||
            this.whosNext++;
 | 
					            this.currentPlayerIndex++;
 | 
				
			||||||
            if (this.whosNext === this.players.length) {
 | 
					            if (this.currentPlayerIndex >= this.players.length) {
 | 
				
			||||||
                this.whosNext = 0;
 | 
					                this.currentPlayerIndex = 0;
 | 
				
			||||||
                this.round++;
 | 
					                move_to_next_round = true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } while (!this.players[this.whosNext].isAlive);
 | 
					        } while (!this.players[this.currentPlayerIndex].isAlive); // skip dead players
 | 
				
			||||||
 | 
					        this.finish_round();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    finish_round() {
 | 
				
			||||||
 | 
					        this.round++;
 | 
				
			||||||
        // kill players with hunter
 | 
					        // kill players with hunter
 | 
				
			||||||
        if (this.round >= 5) {
 | 
					        if (this.round >= 5) {
 | 
				
			||||||
            this.hunter.move(1);
 | 
					            this.hunter.move_by(1);
 | 
				
			||||||
            this.hunter.hunt(this.players);
 | 
					            this.hunter.hunt(this.players);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // check if all players are dead
 | 
					        // check if all players are dead
 | 
				
			||||||
@@ -28,6 +35,29 @@ class Game {
 | 
				
			|||||||
            // todo: end game (all players are dead)
 | 
					            // todo: end game (all players are dead)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    add_player(name) {
 | 
				
			||||||
 | 
					        this.players.push(new Player(name));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    remove_player(name) {
 | 
				
			||||||
 | 
					        let index = this.get_player_index(name);
 | 
				
			||||||
 | 
					        if (index !== -1) this.players.splice(index, 1);
 | 
				
			||||||
 | 
					        if (this.currentPlayerIndex === index) this.finish_turn(); // if current player leaves: move on to next
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    current_player_is(name) {
 | 
				
			||||||
 | 
					        return this.players[this.currentPlayerIndex].name === name;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_player_index(name) {
 | 
				
			||||||
 | 
					        return this.players.findIndex(player => player.name === name);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    move_player(name, amount) {
 | 
				
			||||||
 | 
					        let index = this.get_player_index(name);
 | 
				
			||||||
 | 
					        this.players[index].move_by(amount);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = Game;
 | 
					module.exports = Game;
 | 
				
			||||||
@@ -3,7 +3,7 @@ class Hunter {
 | 
				
			|||||||
        this.position = 0;
 | 
					        this.position = 0;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    move(amount) {
 | 
					    move_by(amount) {
 | 
				
			||||||
        this.position += amount;
 | 
					        this.position += amount;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,13 +1,14 @@
 | 
				
			|||||||
class Player {
 | 
					class Player {
 | 
				
			||||||
    constructor(socketUsername) {
 | 
					    constructor(name) {
 | 
				
			||||||
        this.socketUsername = socketUsername;
 | 
					        this.name = name;
 | 
				
			||||||
        this.position = 0;
 | 
					        this.position = 0;
 | 
				
			||||||
        this.isAlive = true;
 | 
					        this.isAlive = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    move(amount) {
 | 
					    move_by(amount) {
 | 
				
			||||||
        this.position += amount;
 | 
					        this.position += amount;
 | 
				
			||||||
        if (this.position === 15) {
 | 
					        //todo: move by 1 only on the last 3 fields
 | 
				
			||||||
 | 
					        if (this.position >= 16) {
 | 
				
			||||||
            // todo: win
 | 
					            // todo: win
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,3 @@
 | 
				
			|||||||
const Player = require('./Player');
 | 
					 | 
				
			||||||
const Game = require('./Game');
 | 
					const Game = require('./Game');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const express = require('express');
 | 
					const express = require('express');
 | 
				
			||||||
@@ -50,7 +49,7 @@ io.on('connection', socket => {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (gameState[socket.room].players.length < 4 && !gameState[socket.room].started) {
 | 
					        if (gameState[socket.room].players.length < 4 && !gameState[socket.room].started) {
 | 
				
			||||||
            gameState[socket.room].players.push(new Player(socket.username));
 | 
					            gameState[socket.room].add_player(socket.username);
 | 
				
			||||||
            addedUser = true;
 | 
					            addedUser = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            socket.emit('login');
 | 
					            socket.emit('login');
 | 
				
			||||||
@@ -77,38 +76,21 @@ io.on('connection', socket => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    socket.on('disconnect', function () {
 | 
					    socket.on('disconnect', function () {
 | 
				
			||||||
        if (gameState[socket.room] !== undefined && addedUser) {
 | 
					        if (gameState[socket.room] !== undefined && addedUser) {
 | 
				
			||||||
            socket.broadcast.to(socket.room).emit('user left', socket.username);
 | 
					            gameState.remove_player(socket.username);
 | 
				
			||||||
            let index = -1;
 | 
					 | 
				
			||||||
            for (let i = 0; i < gameState[socket.room].players.length; i++) {
 | 
					 | 
				
			||||||
                if (gameState[socket.room].players[i].socketUsername === socket.username) {
 | 
					 | 
				
			||||||
                    index = i;
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (index > -1) {
 | 
					 | 
				
			||||||
                gameState[socket.room].players.splice(index, 1);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            socket.leave(socket.room);
 | 
					            socket.leave(socket.room);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (gameState[socket.room].players.length === 0) {
 | 
					            if (gameState[socket.room].players.length === 0) delete gameState[socket.room];
 | 
				
			||||||
                delete gameState[socket.room];
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        generate_log_message(socket.room, socket.username, "LEFT", "");
 | 
					        generate_log_message(socket.room, socket.username, "LEFT", "");
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Game
 | 
					    // Game
 | 
				
			||||||
    socket.on('roll dice', function () {
 | 
					    socket.on('roll dice', function () {
 | 
				
			||||||
        if (gameState[socket.room] !== undefined && addedUser) {
 | 
					        if (gameState[socket.room] !== undefined && addedUser) {
 | 
				
			||||||
            if(gameState[socket.room].players[gameState[socket.room].whosNext] === undefined) {
 | 
					            if (gameState[socket.room].current_player_is(socket.username)) {
 | 
				
			||||||
                console.log(gameState[socket.room].players)
 | 
					 | 
				
			||||||
                console.log(gameState[socket.room].whosNext)
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (gameState[socket.room].players[gameState[socket.room].whosNext].socketUsername === socket.username) {
 | 
					 | 
				
			||||||
                gameState[socket.room].started = true;
 | 
					                gameState[socket.room].started = true;
 | 
				
			||||||
                let sides = 3;
 | 
					                let sides = 3;
 | 
				
			||||||
                let randomNumber = Math.floor(Math.random() * sides) + 1;
 | 
					                let randomNumber = Math.floor(Math.random() * sides) + 1;
 | 
				
			||||||
@@ -124,7 +106,7 @@ io.on('connection', socket => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    socket.on('get card', function (difficulty) {
 | 
					    socket.on('get card', function (difficulty) {
 | 
				
			||||||
        if (gameState[socket.room] !== undefined && addedUser) {
 | 
					        if (gameState[socket.room] !== undefined && addedUser) {
 | 
				
			||||||
            if (gameState[socket.room].players[gameState[socket.room].whosNext].socketUsername === socket.username) {
 | 
					            if (gameState[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);
 | 
				
			||||||
@@ -136,7 +118,10 @@ io.on('connection', socket => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    socket.on('card finished', function (difficulty, answerIsCorrect) {
 | 
					    socket.on('card finished', function (difficulty, answerIsCorrect) {
 | 
				
			||||||
        if (gameState[socket.room] !== undefined && addedUser) {
 | 
					        if (gameState[socket.room] !== undefined && addedUser) {
 | 
				
			||||||
            if (answerIsCorrect) gameState[socket.room].players[gameState[socket.room].whosNext].move(difficulty);
 | 
					            if (answerIsCorrect) {
 | 
				
			||||||
 | 
					                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();
 | 
					            gameState[socket.room].finish_turn();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user