Merge pull request #34 from H4CK3R-01/extract_classes_from_main_file
Extracted classes from server.js
This commit is contained in:
commit
b156c8566b
33
Webservice/Game.js
Normal file
33
Webservice/Game.js
Normal file
@ -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;
|
19
Webservice/Hunter.js
Normal file
19
Webservice/Hunter.js
Normal file
@ -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;
|
16
Webservice/Player.js
Normal file
16
Webservice/Player.js
Normal file
@ -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;
|
@ -1,3 +1,6 @@
|
|||||||
|
const Player = require('./Player');
|
||||||
|
const Game = require('./Game');
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const {instrument} = require("@socket.io/admin-ui");
|
const {instrument} = require("@socket.io/admin-ui");
|
||||||
@ -7,69 +10,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'));
|
||||||
|
|
||||||
class Player {
|
let gameState = {};
|
||||||
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 port = 5000;
|
let port = 5000;
|
||||||
server.listen(port, function () {
|
server.listen(port, function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user