From 53d6809baa0c56995f133ab9f6ac506c166ca53e Mon Sep 17 00:00:00 2001 From: H4CK3R-01 Date: Thu, 17 Jun 2021 08:45:45 +0200 Subject: [PATCH] Extracted classes from server.js --- Webservice/Game.js | 33 +++++++++++++++++++++ Webservice/Hunter.js | 19 ++++++++++++ Webservice/Player.js | 16 ++++++++++ Webservice/server.js | 69 ++++---------------------------------------- 4 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 Webservice/Game.js create mode 100644 Webservice/Hunter.js create mode 100644 Webservice/Player.js diff --git a/Webservice/Game.js b/Webservice/Game.js new file mode 100644 index 0000000..2cb500d --- /dev/null +++ b/Webservice/Game.js @@ -0,0 +1,33 @@ +const Hunter = require("./Hunter"); + +class Game { + constructor() { + this.players = []; + this.whosNext = 0; + this.started = false; + this.round = 0; + this.hunter = new Hunter(); + } + + finish_turn() { + // move on to next player; skip dead players + do { + this.whosNext++; + if (this.whosNext === this.players.length) { + this.whosNext = 0; + this.round++; + } + } while (!this.players[this.whosNext].isAlive); + // kill players with hunter + if (this.round >= 5) { + this.hunter.move(1); + this.hunter.hunt(this.players); + } + // check if all players are dead + if (!this.players.some(player => player.isAlive === true)) { + // todo: end game (all players are dead) + } + } +} + +module.exports = Game; \ No newline at end of file diff --git a/Webservice/Hunter.js b/Webservice/Hunter.js new file mode 100644 index 0000000..19a59ee --- /dev/null +++ b/Webservice/Hunter.js @@ -0,0 +1,19 @@ +class Hunter { + constructor() { + this.position = 0; + } + + move(amount) { + this.position += amount; + } + + hunt(playerArray) { + for (let i = 0; i < playerArray.length; i++) { + if (playerArray[i].position <= this.position) { + playerArray[i].isAlive = false; + } + } + } +} + +module.exports = Hunter; \ No newline at end of file diff --git a/Webservice/Player.js b/Webservice/Player.js new file mode 100644 index 0000000..1de9a1b --- /dev/null +++ b/Webservice/Player.js @@ -0,0 +1,16 @@ +class Player { + constructor(socketUsername) { + this.socketUsername = socketUsername; + this.position = 0; + this.isAlive = true; + } + + move(amount) { + this.position += amount; + if (this.position === 15) { + // todo: win + } + } +} + +module.exports = Player; \ No newline at end of file diff --git a/Webservice/server.js b/Webservice/server.js index 4748e2d..0e7d735 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -1,3 +1,6 @@ +const Player = require('./Player'); +const Game = require('./Game'); + const express = require('express'); const fs = require('fs'); const {instrument} = require("@socket.io/admin-ui"); @@ -7,69 +10,7 @@ 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')); -class Player { - constructor(socketUsername) { - this.socketUsername = socketUsername; - this.position = 0; - this.isAlive = true; - } - - move(amount) { - this.position += amount; - if (this.position === 15) { - // todo: win - } - } -} - -class Hunter { - constructor() { - this.position = 0; - } - - move(amount) { - this.position += amount; - } - - hunt(playerArray) { - for (let i = 0; i < playerArray.length; i++) { - if (playerArray[i].position <= this.position) { - playerArray[i].isAlive = false; - } - } - } -} - -class Game { - constructor() { - this.players = []; - this.whosNext = 0; - this.started = false; - this.round = 0; - this.hunter = new Hunter() - } - - finish_turn() { - // move on to next player; skip dead players - do { - this.whosNext++; - if (this.whosNext === this.players.length) { - this.whosNext = 0; - this.round++; - } - } while (!gameState.players[gameState.whosNext].isAlive); - // kill players with hunter - if (this.round >= 5) { - this.hunter.move(1); - this.hunter.hunt(this.players); - } - // check if all players are dead - if (!this.players.some(player => player.isAlive === true)) { - // todo: end game (all players are dead) - } - } -} -let gameState = {} +let gameState = {}; let port = 5000; server.listen(port, function () { @@ -104,7 +45,7 @@ io.on('connection', socket => { socket.username = data.username; socket.room = data.room_name; - if(gameState[socket.room] === undefined) { + if (gameState[socket.room] === undefined) { gameState[socket.room] = new Game(); } -- 2.45.2