- Added multiplayer functions #27
@ -7,6 +7,13 @@ 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'));
|
||||||
|
|
||||||
|
let gameState = {
|
||||||
|
players: [],
|
||||||
|
positions: [],
|
||||||
|
whosNext: 0,
|
||||||
|
started: false
|
||||||
|
};
|
||||||
|
|
||||||
let port = 5000;
|
let port = 5000;
|
||||||
server.listen(port, function () {
|
server.listen(port, function () {
|
||||||
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
|
generate_log_message("MAIN", "Server", 'RUNNING', "PORT " + port);
|
||||||
@ -38,9 +45,12 @@ io.on('connection', socket => {
|
|||||||
let addedUser = false;
|
let addedUser = false;
|
||||||
|
|
||||||
socket.on('add user', function (data) {
|
socket.on('add user', function (data) {
|
||||||
|
if (gameState['players'].length < 4 && !gameState['started']) {
|
||||||
socket.username = data.username;
|
socket.username = data.username;
|
||||||
socket.room = data.room_name;
|
socket.room = data.room_name;
|
||||||
|
|
||||||
|
gameState['players'].push(socket.username);
|
||||||
|
gameState['positions'].push(1);
|
||||||
addedUser = true;
|
addedUser = true;
|
||||||
|
|
||||||
socket.emit('login');
|
socket.emit('login');
|
||||||
@ -49,6 +59,9 @@ io.on('connection', socket => {
|
|||||||
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) {
|
socket.on('new message', function (data) {
|
||||||
@ -63,6 +76,21 @@ io.on('connection', socket => {
|
|||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', function () {
|
||||||
if (addedUser) {
|
if (addedUser) {
|
||||||
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.leave(socket.room);
|
||||||
|
|
||||||
|
if (gameState['players'].length === 0) {
|
||||||
|
gameState['players'] = [];
|
||||||
|
gameState['positions'] = [];
|
||||||
|
gameState['whosNext'] = 0;
|
||||||
|
gameState['started'] = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_log_message(socket.room, socket.username, "LEFT", "");
|
generate_log_message(socket.room, socket.username, "LEFT", "");
|
||||||
@ -71,18 +99,32 @@ io.on('connection', socket => {
|
|||||||
|
|
||||||
// Game
|
// Game
|
||||||
socket.on('roll dice', function () {
|
socket.on('roll dice', function () {
|
||||||
|
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
|
||||||
|
gameState['started'] = true;
|
||||||
let sides = 3;
|
let sides = 3;
|
||||||
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
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) {
|
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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -56,13 +56,6 @@ function Button(default_color, hover_color, select_color, width, height, x, y, t
|
|||||||
this.graphics.buttonMode = true;
|
this.graphics.buttonMode = true;
|
||||||
this.graphics.defaultCursor = 'pointer';
|
this.graphics.defaultCursor = 'pointer';
|
||||||
this.graphics.on('pointerdown', function () {
|
this.graphics.on('pointerdown', function () {
|
||||||
if (_this.button_is_answer) {
|
|
||||||
if (_this.selected === true) {
|
|
||||||
_this.unSelectButton();
|
|
||||||
} else {
|
|
||||||
_this.selectButton();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
click();
|
click();
|
||||||
});
|
});
|
||||||
this.graphics.on('mouseover', function () {
|
this.graphics.on('mouseover', function () {
|
||||||
|
@ -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.card = new PIXI.Graphics();
|
||||||
this.s = s;
|
this.s = s;
|
||||||
this.a1 = a1;
|
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 (a3.status) this.right_answer = this.a3.text;
|
||||||
if (a4.status) this.right_answer = this.a4.text;
|
if (a4.status) this.right_answer = this.a4.text;
|
||||||
this.d = d;
|
this.d = d;
|
||||||
|
this.your_turn = your_turn;
|
||||||
this.card_x = game_board_size * 0.25 + 2.5;
|
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_y = game_board_size / 2 - game_board_size * 0.72 / 2 + 2.5;
|
||||||
this.card_height = game_board_size * 0.72;
|
this.card_height = game_board_size * 0.72;
|
||||||
@ -49,19 +50,27 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
|
|||||||
|
|
||||||
// Answers
|
// 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 () {
|
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 () {
|
||||||
|
if (_this.your_turn) {
|
||||||
select_answer(0, _this.a1.text);
|
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 () {
|
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 () {
|
||||||
|
if (_this.your_turn) {
|
||||||
select_answer(1, _this.a2.text);
|
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 () {
|
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 () {
|
||||||
|
if (_this.your_turn) {
|
||||||
select_answer(2, _this.a3.text);
|
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 () {
|
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 () {
|
||||||
|
if (_this.your_turn) {
|
||||||
select_answer(3, _this.a4.text);
|
select_answer(3, _this.a4.text);
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.buttons.forEach(button => this.card.addChild(button.getButton()));
|
this.buttons.forEach(button => this.card.addChild(button.getButton()));
|
||||||
@ -79,9 +88,7 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
|
|||||||
answer = null;
|
answer = null;
|
||||||
diced = false;
|
diced = false;
|
||||||
rolled_number = null;
|
rolled_number = null;
|
||||||
rolled_number_text.destroy();
|
socket.emit('card finished', d);
|
||||||
border_card_stack.clear();
|
|
||||||
_this.card.destroy();
|
|
||||||
} else {
|
} else {
|
||||||
alert("Bitte wähle eine Antwortmöglichkeit aus");
|
alert("Bitte wähle eine Antwortmöglichkeit aus");
|
||||||
}
|
}
|
||||||
@ -90,6 +97,11 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
|
|||||||
app.stage.addChild(this.card);
|
app.stage.addChild(this.card);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.destroyCard = function () {
|
||||||
|
if (this.card !== null) {
|
||||||
|
this.card.destroy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function select_answer(id, text) {
|
function select_answer(id, text) {
|
||||||
_this.buttons.forEach(button => button.unSelectButton());
|
_this.buttons.forEach(button => button.unSelectButton());
|
||||||
|
@ -9,7 +9,7 @@ let curr_player = 1;
|
|||||||
let player_array = [1, 1, 1, 1];
|
let player_array = [1, 1, 1, 1];
|
||||||
let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
|
let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
|
||||||
let player_sprite_array = [];
|
let player_sprite_array = [];
|
||||||
|
let card;
|
||||||
let answer = null;
|
let answer = null;
|
||||||
let show_card = false;
|
let show_card = false;
|
||||||
let diced = false;
|
let diced = false;
|
||||||
@ -156,17 +156,24 @@ 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.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;
|
rolled_number_text.y = sprite_size * 6 - sprite_size * 0.2;
|
||||||
app.stage.addChild(rolled_number_text);
|
app.stage.addChild(rolled_number_text);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('card', function (data) {
|
socket.on('card', function (data) {
|
||||||
let q = data.question;
|
let u = data.username;
|
||||||
let a = data.answers;
|
let q = data.card.question;
|
||||||
let d = data.difficulty;
|
let a = data.card.answers;
|
||||||
new Card(game_board_size, q, a[0], a[1], a[2], a[3], d).showCard();
|
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;
|
show_card = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('card destroyed', function () {
|
||||||
|
card.destroyCard();
|
||||||
|
rolled_number_text.destroy();
|
||||||
|
border_card_stack.clear();
|
||||||
|
});
|
||||||
|
|
||||||
resize();
|
resize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user