From c243138b87e18982c57a3cb81ef4e7c0c26fb00e Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Tue, 15 Jun 2021 18:18:16 +0200 Subject: [PATCH] implement player class with player movement --- Webservice/server.js | 50 ++++++++++++++++++++++++++++++++++++-------- public/index.html | 1 - public/js/game.js | 6 ------ public/js/player.js | 39 ---------------------------------- 4 files changed, 41 insertions(+), 55 deletions(-) delete mode 100644 public/js/player.js diff --git a/Webservice/server.js b/Webservice/server.js index cc4ec85..6fbe195 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -7,9 +7,37 @@ 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.alive = true; + } + move(amount) { + this.position += amount; + if (this.position === 15) { + // todo: win + } + } +} + +class Hunter { + constructor() { + this.position = 0; + } + move(amount) { + this.position += amount; + } + hunt(players) { + for (let i = 0; i < players.length; i++) { + if (players[i].position <= this.position) { + players[i].alive = false; + } + } + } +} let gameState = { players: [], - positions: [], whosNext: 0, started: false }; @@ -49,8 +77,7 @@ io.on('connection', socket => { socket.username = data.username; socket.room = data.room_name; - gameState['players'].push(socket.username); - gameState['positions'].push(1); + gameState['players'].push(new Player(socket.username)); addedUser = true; socket.emit('login'); @@ -76,18 +103,22 @@ io.on('connection', socket => { socket.on('disconnect', function () { if (addedUser) { socket.broadcast.to(socket.room).emit('user left', socket.username); - let index = gameState['players'].indexOf(socket.username); + let index = -1; + for (let i = 0; i < gameState['players'].length; i++) { + if (gameState['players'][i].socketUsername === socket.username) { + index = i; + break; + } + } if (index > -1) { gameState['players'].splice(index, 1); - gameState['positions'].splice(index, 1); } socket.leave(socket.room); if (gameState['players'].length === 0) { gameState['players'] = []; - gameState['positions'] = []; gameState['whosNext'] = 0; gameState['started'] = false; } @@ -99,7 +130,8 @@ io.on('connection', socket => { // Game socket.on('roll dice', function () { - if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) { + + if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) { gameState['started'] = true; let sides = 3; let randomNumber = Math.floor(Math.random() * sides) + 1; @@ -113,7 +145,7 @@ io.on('connection', socket => { }); socket.on('get card', function (difficulty) { - if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) { + if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) { io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)}); generate_log_message(socket.room, socket.username, "CARD", difficulty); @@ -123,7 +155,7 @@ io.on('connection', socket => { }); socket.on('card finished', function (difficulty) { - gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty; + gameState['players'][gameState['whosNext']].move(difficulty); //TODO: stop players from moving when answer is wrong gameState['whosNext'] += 1; if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0; io.in(socket.room).emit('card destroyed'); diff --git a/public/index.html b/public/index.html index 11c0cc3..75d4ffa 100644 --- a/public/index.html +++ b/public/index.html @@ -59,7 +59,6 @@ - diff --git a/public/js/game.js b/public/js/game.js index c0eb0ea..73d025f 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -5,12 +5,6 @@ dice.svg: https://www.svgrepo.com/download/198836/gambler-casino.svg sprite.jpg: https://media.istockphoto.com/photos/gray-granite-stone-texture-seamless-square-background-tile-ready-picture-id1096464726 */ -let curr_player = 1; -let player_array = createPlayers(4); -let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00]; -let player_sprite_array = []; -let hunter = new Hunter(); - let card; let answer = null; let show_card = false; diff --git a/public/js/player.js b/public/js/player.js deleted file mode 100644 index ca43054..0000000 --- a/public/js/player.js +++ /dev/null @@ -1,39 +0,0 @@ -function createPlayers(amount) { - let players = new Array(amount); - for (let i = 0; i < amount; i++) { - players[i] = new Player(); - } - return players; -} - -class Player { - constructor() { - this.position = 0; - this.alive = true; - } - - move(amount) { - this.position += amount; - if (this.position === 15) { - // todo: win - } - } -} - -class Hunter { - constructor() { - this.position = 0; - } - - move(amount) { - this.position += amount; - } - - hunt(players) { - for (let i = 0; i < players.length; i++) { - if (players[i].position <= this.position) { - players[i].alive = false; - } - } - } -} \ No newline at end of file