From bc6d8392948aa9ee0abfcfd37cb9c9c23bfcd37a Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Thu, 10 Jun 2021 10:09:16 +0200 Subject: [PATCH 1/7] add player and hunter class --- public/index.html | 1 + public/js/game.js | 3 ++- public/js/player.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 public/js/player.js diff --git a/public/index.html b/public/index.html index b30a019..8cffa3f 100644 --- a/public/index.html +++ b/public/index.html @@ -56,6 +56,7 @@ + diff --git a/public/js/game.js b/public/js/game.js index 5c79357..d6261d5 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -6,9 +6,10 @@ 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 = [1, 1, 1, 1]; +let player_array = createPlayers(4); let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00]; let player_sprite_array = []; +let hunter = new Hunter(); let answer = null; let show_card = false; diff --git a/public/js/player.js b/public/js/player.js new file mode 100644 index 0000000..d1e8ce0 --- /dev/null +++ b/public/js/player.js @@ -0,0 +1,39 @@ +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 From dd34403a3e8d033e1c66cae8318e3c7d949eb69a Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Thu, 10 Jun 2021 10:21:45 +0200 Subject: [PATCH 2/7] hunter now kills players behind itself --- public/js/player.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/js/player.js b/public/js/player.js index d1e8ce0..ca43054 100644 --- a/public/js/player.js +++ b/public/js/player.js @@ -31,7 +31,7 @@ class Hunter { hunt(players) { for (let i = 0; i < players.length; i++) { - if (players[i].position === this.position) { + if (players[i].position <= this.position) { players[i].alive = false; } } From 10e69084bcbc3fed69a8fa41b8c9c76cc81d45ca Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Tue, 15 Jun 2021 17:37:42 +0200 Subject: [PATCH 3/7] reorder sprites --- public/js/game.js | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/public/js/game.js b/public/js/game.js index d6261d5..e8f34c4 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -35,31 +35,22 @@ let rolled_number_text = new PIXI.Text("", rolled_number_style); // fields let sprites = [ - // First row - new Sprite(1, 1), - new Sprite(3, 1), - new Sprite(5, 1), - new Sprite(7, 1), - new Sprite(9, 1), - - // Second row - new Sprite(1, 3), - new Sprite(9, 3), - - // Third row - new Sprite(1, 5), - new Sprite(9, 5), - - // Fourth row - new Sprite(1, 7), + new Sprite(9, 9), new Sprite(9, 7), - - // Fifth row + new Sprite(9, 5), + new Sprite(9, 3), + new Sprite(9, 1), + new Sprite(7, 1), + new Sprite(5, 1), + new Sprite(3, 1), + new Sprite(1, 1), + new Sprite(1, 3), + new Sprite(1, 5), + new Sprite(1, 7), new Sprite(1, 9), new Sprite(3, 9), new Sprite(5, 9), - new Sprite(7, 9), - new Sprite(9, 9), + new Sprite(7, 9) ]; function start_game() { @@ -74,7 +65,6 @@ function start_game() { sprites.forEach(sprite => app.stage.addChild(sprite.getSprite())); - // Red border let red_border = generate_red_border(new PIXI.Graphics()); app.stage.addChild(red_border); From c243138b87e18982c57a3cb81ef4e7c0c26fb00e Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Tue, 15 Jun 2021 18:18:16 +0200 Subject: [PATCH 4/7] 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 From 4674720cfb02989c758c74e719137b8c42defdd4 Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Tue, 15 Jun 2021 18:38:55 +0200 Subject: [PATCH 5/7] fix players moving when answers are wrong --- Webservice/server.js | 4 ++-- public/js/Card.js | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Webservice/server.js b/Webservice/server.js index 6fbe195..37d6424 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -154,8 +154,8 @@ io.on('connection', socket => { } }); - socket.on('card finished', function (difficulty) { - gameState['players'][gameState['whosNext']].move(difficulty); //TODO: stop players from moving when answer is wrong + socket.on('card finished', function (difficulty, answerIsCorrect) { + if (answerIsCorrect) gameState['players'][gameState['whosNext']].move(difficulty); gameState['whosNext'] += 1; if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0; io.in(socket.room).emit('card destroyed'); diff --git a/public/js/Card.js b/public/js/Card.js index 7e7b8e2..2fd7f81 100644 --- a/public/js/Card.js +++ b/public/js/Card.js @@ -79,16 +79,17 @@ function Card(game_board_size, s, a1, a2, a3, a4, d, your_turn) { // OK-Button this.card.addChild(new Button(0xffffff, 0xcccccc, 0xffffff, this.card_width - 40, 100, this.card_x + 20, this.card_y + this.card_height - 120, "OK", null, function () { if (answer !== null) { - if (_this.right_answer === answer) { + if (_this.right_answer === answer) { //TODO: do this in backend instead to prevent cheating console.log("Richtig"); + socket.emit('card finished', d, true); } else { console.log("Falsch"); + socket.emit('card finished', d, false); } show_card = false; answer = null; diced = false; rolled_number = null; - socket.emit('card finished', d); } else { alert("Bitte wähle eine Antwortmöglichkeit aus"); } From f022f17186b82a66b5abdacc0734a182090aa75f Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Wed, 16 Jun 2021 00:06:00 +0200 Subject: [PATCH 6/7] implement hunter --- Webservice/server.js | 65 +++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/Webservice/server.js b/Webservice/server.js index 37d6424..d12b7be 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -11,7 +11,7 @@ class Player { constructor(socketUsername) { this.socketUsername = socketUsername; this.position = 0; - this.alive = true; + this.isAlive = true; } move(amount) { this.position += amount; @@ -28,20 +28,43 @@ class Hunter { 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; + hunt(playerArray) { + for (let i = 0; i < playerArray.length; i++) { + if (playerArray[i].position <= this.position) { + playerArray[i].isAlive = false; } } } } + let gameState = { players: [], whosNext: 0, - started: false + started: false, + round: 0, + hunter: new Hunter() }; +function finish_turn() { + // move on to next player; skip dead players + do { + gameState.whosNext++; + if (gameState.whosNext === gameState.players.length) { + gameState.whosNext = 0; + gameState.round++; + } + } while (!gameState.players[gameState.whosNext].isAlive); + // kill players with hunter + if (gameState.round >= 5) { + gameState.hunter.move(1); + gameState.hunter.hunt(gameState.players); + } + // check if all players are dead + if (!gameState.players.some(player => player.isAlive === true)) { + // todo: end game (all players are dead) + } +} + let port = 5000; server.listen(port, function () { generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port); @@ -67,17 +90,16 @@ if (process.env.WEBSOCKET_MONITOR_USERNAME && process.env.WEBSOCKET_MONITOR_PASS // Serve static files (html, css, js) app.use(express.static(__dirname + '/../public')); - // Websockets io.on('connection', socket => { let addedUser = false; socket.on('add user', function (data) { - if (gameState['players'].length < 4 && !gameState['started']) { + if (gameState.players.length < 4 && !gameState.started) { socket.username = data.username; socket.room = data.room_name; - gameState['players'].push(new Player(socket.username)); + gameState.players.push(new Player(socket.username)); addedUser = true; socket.emit('login'); @@ -104,23 +126,23 @@ io.on('connection', socket => { if (addedUser) { socket.broadcast.to(socket.room).emit('user left', socket.username); let index = -1; - for (let i = 0; i < gameState['players'].length; i++) { - if (gameState['players'][i].socketUsername === socket.username) { + 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.players.splice(index, 1); } socket.leave(socket.room); - if (gameState['players'].length === 0) { - gameState['players'] = []; - gameState['whosNext'] = 0; - gameState['started'] = false; + if (gameState.players.length === 0) { + gameState.players = []; + gameState.whosNext = 0; + gameState.started = false; } } @@ -131,8 +153,8 @@ io.on('connection', socket => { // Game socket.on('roll dice', function () { - if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) { - gameState['started'] = true; + if (gameState.players[gameState.whosNext].socketUsername === socket.username) { + gameState.started = true; let sides = 3; let randomNumber = Math.floor(Math.random() * sides) + 1; @@ -145,7 +167,7 @@ io.on('connection', socket => { }); socket.on('get card', function (difficulty) { - if (gameState['players'][gameState['whosNext']].socketUsername === 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); @@ -155,10 +177,9 @@ io.on('connection', socket => { }); socket.on('card finished', function (difficulty, answerIsCorrect) { - if (answerIsCorrect) gameState['players'][gameState['whosNext']].move(difficulty); - gameState['whosNext'] += 1; - if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0; + if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty); io.in(socket.room).emit('card destroyed'); + finish_turn(); }); }); From d60bbbcf7d8421cf243f49b9efad3f3e28e923d7 Mon Sep 17 00:00:00 2001 From: Thorsten Rausch Date: Wed, 16 Jun 2021 10:44:21 +0200 Subject: [PATCH 7/7] make gameState a class --- Webservice/server.js | 58 ++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/Webservice/server.js b/Webservice/server.js index d12b7be..6fd8612 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -13,6 +13,7 @@ class Player { this.position = 0; this.isAlive = true; } + move(amount) { this.position += amount; if (this.position === 15) { @@ -25,9 +26,11 @@ 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) { @@ -37,33 +40,37 @@ class Hunter { } } -let gameState = { - players: [], - whosNext: 0, - started: false, - round: 0, - hunter: new Hunter() -}; - -function finish_turn() { - // move on to next player; skip dead players - do { - gameState.whosNext++; - if (gameState.whosNext === gameState.players.length) { - gameState.whosNext = 0; - gameState.round++; - } - } while (!gameState.players[gameState.whosNext].isAlive); - // kill players with hunter - if (gameState.round >= 5) { - gameState.hunter.move(1); - gameState.hunter.hunt(gameState.players); +class Game { + constructor() { + this.players = []; + this.whosNext = 0; + this.started = false; + this.round = 0; + this.hunter = new Hunter() } - // check if all players are dead - if (!gameState.players.some(player => player.isAlive === true)) { - // todo: end game (all players are dead) + + 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) + } } } +// todo: instantiate this for individual rooms +let gameState = new Game(); let port = 5000; server.listen(port, function () { @@ -149,7 +156,6 @@ io.on('connection', socket => { generate_log_message(socket.room, socket.username, "LEFT", ""); }); - // Game socket.on('roll dice', function () { @@ -179,7 +185,7 @@ io.on('connection', socket => { socket.on('card finished', function (difficulty, answerIsCorrect) { if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty); io.in(socket.room).emit('card destroyed'); - finish_turn(); + gameState.finish_turn(); }); });