Merge pull request #9 from H4CK3R-01/load_card_from_server_and_display_it

Get card data from server and display it
This commit is contained in:
H4CK3R-01 2021-06-08 17:34:20 +02:00 committed by GitHub
commit d47456c184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 38 deletions

View File

@ -98,7 +98,7 @@ function generate_log_message(room, user, type, message) {
function getRandomCard(difficulty) {
let filtered_cards = cards.filter(card => {
return card.diffuculty === difficulty;
return card.difficulty === difficulty;
});
return shuffleAnswers(filtered_cards[Math.floor(Math.random() * filtered_cards.length)]);

View File

@ -1,69 +1,69 @@
[
{
"id": 1,
"diffuculty": 1,
"difficulty": 1,
"question": "Was?",
"answers": [
{
"answer_a": "A",
"text": "A",
"status": false
},
{
"answer_b": "B",
"text": "B",
"status": true
},
{
"answer_c": "C",
"text": "C",
"status": false
},
{
"answer_d": "D",
"text": "D",
"status": false
}
]
},
{
"id": 2,
"diffuculty": 2,
"difficulty": 2,
"question": "Wie?",
"answers": [
{
"answer_a": "A",
"text": "A",
"status": false
},
{
"answer_b": "B",
"text": "B",
"status": true
},
{
"answer_c": "C",
"text": "C",
"status": false
},
{
"answer_d": "D",
"text": "D",
"status": false
}
]
},
{
"id": 3,
"diffuculty": 3,
"difficulty": 3,
"question": "Wo?",
"answers": [
{
"answer_a": "A",
"text": "A",
"status": false
},
{
"answer_b": "B",
"text": "B",
"status": true
},
{
"answer_c": "C",
"text": "C",
"status": false
},
{
"answer_d": "D",
"text": "D",
"status": false
}
]

View File

@ -1,4 +1,4 @@
function Button(default_color, hover_color, select_color, width, height, x, y, text, button_is_answer, click) {
function Button(default_color, hover_color, select_color, width, height, x, y, text, status, button_is_answer, click) {
this.graphics = new PIXI.Graphics();
this.default_color = default_color;
this.hover_color = hover_color;
@ -7,6 +7,7 @@ function Button(default_color, hover_color, select_color, width, height, x, y, t
this.x = x;
this.y = y;
this.text = text;
this.status = status;
this.button_is_answer = button_is_answer;
this.click = click;
this.selected = false;

View File

@ -1,11 +1,14 @@
function Card(game_board_size, s, a1, a2, a3, a4, right_answer, d) {
function Card(game_board_size, s, a1, a2, a3, a4, d) {
this.card = new PIXI.Graphics();
this.s = s;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
this.right_answer = right_answer;
if (a1.status) this.right_answer = this.a1.text;
if (a2.status) this.right_answer = this.a2.text;
if (a3.status) this.right_answer = this.a3.text;
if (a4.status) this.right_answer = this.a4.text;
this.d = d;
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;
@ -45,27 +48,27 @@ function Card(game_board_size, s, a1, a2, a3, a4, right_answer, d) {
this.card.addChild(basicText);
// Answers
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 4, this.a1, true, function () {
select_answer(0);
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, 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);
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 3, this.a2, true, function () {
select_answer(1);
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, 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);
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 2, this.a3, true, function () {
select_answer(2);
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, 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);
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 1, this.a4, true, function () {
select_answer(3);
this.buttons.push(new Button(0xffffff, 0xcccccc, 0xff00ff, 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);
}));
this.buttons.forEach(button => this.card.addChild(button.getButton()));
// 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", false, function () {
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, false, function () {
if (_this.right_answer === answer) {
console.log("Richtig")
} else {
@ -79,10 +82,9 @@ function Card(game_board_size, s, a1, a2, a3, a4, right_answer, d) {
}
function select_answer(id) {
console.log(id)
function select_answer(id, text) {
_this.buttons.forEach(button => button.unSelectButton());
_this.buttons[id].selectButton();
answer = id;
answer = text;
}
}

View File

@ -71,8 +71,7 @@ function start_game() {
let cards_1 = generate_card_stack(PIXI.Sprite.from('/img/card_stack.png'), 3, 3, function () {
if (!show_card) {
console.log("1");
new Card(game_board_size, "Ein Bäcker möchte eine neue Filiale eröffnen. Wie sollte er das Budget einteilen?", "a1", "a2", "a3", "a4", 1, 1).showCard();
show_card = true;
socket.emit('get card', 1);
}
});
app.stage.addChild(cards_1);
@ -80,8 +79,7 @@ function start_game() {
let cards_2 = generate_card_stack(PIXI.Sprite.from('/img/card_stack.png'), 5, 3, function () {
if (!show_card) {
console.log("2");
new Card(game_board_size, "Ein Bäcker möchte eine neue Filiale eröffnen. Wie sollte er das Budget einteilen?", "a1", "a2", "a3", "a4", 1, 1).showCard();
show_card = true;
socket.emit('get card', 2);
}
});
app.stage.addChild(cards_2);
@ -89,8 +87,7 @@ function start_game() {
let cards_3 = generate_card_stack(PIXI.Sprite.from('/img/card_stack.png'), 7, 3, function () {
if (!show_card) {
console.log("3");
new Card(game_board_size, "Ein Bäcker möchte eine neue Filiale eröffnen. Wie sollte er das Budget einteilen?", "a1", "a2", "a3", "a4", 1, 1).showCard();
show_card = true;
socket.emit('get card', 3);
}
});
app.stage.addChild(cards_3);
@ -100,7 +97,11 @@ function start_game() {
});
socket.on('card', function (data) {
console.log(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();
show_card = true;
});
resize();