diff --git a/Webservice/server.js b/Webservice/server.js index e93be02..0898cfb 100644 --- a/Webservice/server.js +++ b/Webservice/server.js @@ -6,6 +6,13 @@ let {Server} = require("socket.io"); let io = new Server(server); let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/cards.json')); +let gameState = { + players: [], + positions: [], + whosNext: 0, + started: false +}; + let port = 5000; server.listen(port, function () { generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port); @@ -19,17 +26,23 @@ io.on('connection', socket => { let addedUser = false; socket.on('add user', function (data) { - socket.username = data.username; - socket.room = data.room_name; + if (gameState['players'].length < 4 && !gameState['started']) { + socket.username = data.username; + socket.room = data.room_name; - addedUser = true; + gameState['players'].push(socket.username); + gameState['positions'].push(1); + addedUser = true; - socket.emit('login'); - socket.join(socket.room); + socket.emit('login'); + socket.join(socket.room); - socket.broadcast.to(socket.room).emit('user joined', socket.username); + socket.broadcast.to(socket.room).emit('user joined', socket.username); - generate_log_message(socket.room, socket.username, "JOINED", ""); + generate_log_message(socket.room, socket.username, "JOINED", ""); + } else { + // TODO + } }); socket.on('new message', function (data) { @@ -44,6 +57,12 @@ 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); + + if (index > -1) { + gameState['players'].splice(index, 1); + gameState['positions'].splice(index, 1); + } } generate_log_message(socket.room, socket.username, "LEFT", ""); @@ -52,18 +71,32 @@ io.on('connection', socket => { // Game socket.on('roll dice', function () { - let sides = 3; - let randomNumber = Math.floor(Math.random() * sides) + 1; + if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) { + gameState['started'] = true; + let sides = 3; + let randomNumber = Math.floor(Math.random() * sides) + 1; - io.in(socket.room).emit('dice', randomNumber); + io.in(socket.room).emit('dice', randomNumber); - generate_log_message(socket.room, socket.username, "DICE", randomNumber); + generate_log_message(socket.room, socket.username, "DICE", randomNumber); + } else { + // TODO + } }); socket.on('get card', function (difficulty) { - io.in(socket.room).emit('card', getRandomCard(difficulty)); + if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) { + 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); + } else { + // TODO + } + }); + + socket.on('card finished', function (difficulty) { + gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty; + io.in(socket.room).emit('card destroyed'); }); }); @@ -115,5 +148,5 @@ function shuffleAnswers(card) { function pad(width, string, padding) { if (string === undefined || string === null) return pad(width, " ", " "); - return (width <= string.length) ? string : pad(width, string + padding, padding) + return (width <= string.length) ? string : pad(width, string + padding, padding); } \ No newline at end of file diff --git a/public/js/Button.js b/public/js/Button.js index 163e4bd..d7ad35d 100644 --- a/public/js/Button.js +++ b/public/js/Button.js @@ -56,13 +56,6 @@ function Button(default_color, hover_color, select_color, width, height, x, y, t this.graphics.buttonMode = true; this.graphics.defaultCursor = 'pointer'; this.graphics.on('click', function () { - if (_this.button_is_answer) { - if (_this.selected === true) { - _this.unSelectButton(); - } else { - _this.selectButton(); - } - } click(); }); this.graphics.on('mouseover', function () { diff --git a/public/js/Card.js b/public/js/Card.js index 45442d3..1e21d0d 100644 --- a/public/js/Card.js +++ b/public/js/Card.js @@ -1,4 +1,4 @@ -function Card(game_board_size, s, a1, a2, a3, a4, d) { +function Card(game_board_size, s, a1, a2, a3, a4, d, your_turn) { this.card = new PIXI.Graphics(); this.s = s; this.a1 = a1; @@ -10,6 +10,7 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) { if (a3.status) this.right_answer = this.a3.text; if (a4.status) this.right_answer = this.a4.text; this.d = d; + this.your_turn = your_turn; this.card_x = game_board_size * 0.25 + 2.5; this.card_y = game_board_size / 2 - game_board_size * 0.72 / 2 + 2.5; this.card_height = game_board_size * 0.72; @@ -49,19 +50,27 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) { // Answers this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 4, this.a1.text, this.a1.status, true, function () { - select_answer(0, _this.a1.text); + if (this.your_turn) { + select_answer(0, _this.a1.text); + } })); this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 3, this.a2.text, this.a2.status, true, function () { - select_answer(1, _this.a2.text); + if (this.your_turn) { + select_answer(1, _this.a2.text); + } })); this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 2, this.a3.text, this.a3.status, true, function () { - select_answer(2, _this.a3.text); + if (this.your_turn) { + select_answer(2, _this.a3.text); + } })); this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 1, this.a4.text, this.a4.status, true, function () { - select_answer(3, _this.a4.text); + if (this.your_turn) { + select_answer(3, _this.a4.text); + } })); this.buttons.forEach(button => this.card.addChild(button.getButton())); @@ -81,7 +90,7 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) { rolled_number = null; rolled_number_text.destroy(); border_card_stack.clear(); - _this.card.destroy(); + socket.emit('card finished', d); } else { alert("Bitte wähle eine Antwortmöglichkeit aus"); } @@ -90,6 +99,11 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) { app.stage.addChild(this.card); }; + this.destroyCard = function () { + if (this.card !== null) { + this.card.destroy(); + } + }; function select_answer(id, text) { _this.buttons.forEach(button => button.unSelectButton()); diff --git a/public/js/game.js b/public/js/game.js index d46caa8..7ff968d 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -9,7 +9,7 @@ let curr_player = 1; let player_array = [1, 1, 1, 1]; let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00]; let player_sprite_array = []; - +let card; let answer = null; let show_card = false; let diced = false; @@ -156,17 +156,22 @@ function start_game() { rolled_number_text.x = sprite_size * 7 - sprite_size * 0.2 + dice.width / 2 - rolled_number_text.width / 2; rolled_number_text.y = sprite_size * 6 - sprite_size * 0.2; app.stage.addChild(rolled_number_text); - }); socket.on('card', function (data) { - let q = data.question; - let a = data.answers; - let d = data.difficulty; - new Card(game_board_size, q, a[0], a[1], a[2], a[3], d).showCard(); + let u = data.username; + let q = data.card.question; + let a = data.card.answers; + let d = data.card.difficulty; + card = new Card(game_board_size, q, a[0], a[1], a[2], a[3], d, u === username); + card.showCard(); show_card = true; }); + socket.on('card destroyed', function () { + card.destroyCard(); + }); + resize(); }