Logic #32

Merged
thorsten-rausch merged 9 commits from logic into main 2021-06-16 09:00:52 +00:00
3 changed files with 100 additions and 54 deletions

View File

@ -7,12 +7,70 @@ 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 = { class Player {
players: [], constructor(socketUsername) {
positions: [], this.socketUsername = socketUsername;
whosNext: 0, this.position = 0;
started: false this.isAlive = true;
}; }
move(amount) {
this.position += amount;
if (this.position === 15) {
// todo: win
}
}
}
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) {
playerArray[i].isAlive = false;
}
}
}
}
class Game {
constructor() {
this.players = [];
this.whosNext = 0;
this.started = false;
this.round = 0;
this.hunter = new Hunter()
}
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; let port = 5000;
server.listen(port, function () { server.listen(port, function () {
@ -39,18 +97,16 @@ if (process.env.WEBSOCKET_MONITOR_USERNAME && process.env.WEBSOCKET_MONITOR_PASS
// Serve static files (html, css, js) // Serve static files (html, css, js)
app.use(express.static(__dirname + '/../public')); app.use(express.static(__dirname + '/../public'));
// Websockets // Websockets
io.on('connection', socket => { 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']) { 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.players.push(new Player(socket.username));
gameState['positions'].push(1);
addedUser = true; addedUser = true;
socket.emit('login'); socket.emit('login');
@ -76,31 +132,35 @@ 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); 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) { if (index > -1) {
gameState['players'].splice(index, 1); gameState.players.splice(index, 1);
gameState['positions'].splice(index, 1);
} }
socket.leave(socket.room); socket.leave(socket.room);
if (gameState['players'].length === 0) { if (gameState.players.length === 0) {
gameState['players'] = []; gameState.players = [];
gameState['positions'] = []; gameState.whosNext = 0;
gameState['whosNext'] = 0; gameState.started = false;
gameState['started'] = false;
} }
} }
generate_log_message(socket.room, socket.username, "LEFT", ""); generate_log_message(socket.room, socket.username, "LEFT", "");
}); });
// Game // Game
socket.on('roll dice', function () { socket.on('roll dice', function () {
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
gameState['started'] = true; if (gameState.players[gameState.whosNext].socketUsername === 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;
@ -113,7 +173,7 @@ io.on('connection', socket => {
}); });
socket.on('get card', function (difficulty) { 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)}); 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);
@ -122,11 +182,10 @@ io.on('connection', socket => {
} }
}); });
socket.on('card finished', function (difficulty) { socket.on('card finished', function (difficulty, answerIsCorrect) {
gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty; 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'); io.in(socket.room).emit('card destroyed');
gameState.finish_turn();
}); });
}); });

View File

@ -79,16 +79,17 @@ function Card(game_board_size, s, a1, a2, a3, a4, d, your_turn) {
// OK-Button // 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 () { 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 (answer !== null) {
if (_this.right_answer === answer) { if (_this.right_answer === answer) { //TODO: do this in backend instead to prevent cheating
console.log("Richtig"); console.log("Richtig");
socket.emit('card finished', d, true);
} else { } else {
console.log("Falsch"); console.log("Falsch");
socket.emit('card finished', d, false);
} }
show_card = false; show_card = false;
answer = null; answer = null;
diced = false; diced = false;
rolled_number = null; rolled_number = null;
socket.emit('card finished', d);
} else { } else {
alert("Bitte wähle eine Antwortmöglichkeit aus"); alert("Bitte wähle eine Antwortmöglichkeit aus");
} }

View File

@ -5,10 +5,6 @@
dice.svg: https://www.svgrepo.com/download/198836/gambler-casino.svg 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 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_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
let player_sprite_array = [];
let card; let card;
let answer = null; let answer = null;
let show_card = false; let show_card = false;
@ -34,31 +30,22 @@ let rolled_number_text = new PIXI.Text("", rolled_number_style);
// fields // fields
let sprites = [ let sprites = [
// First row new Sprite(9, 9),
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, 7), new Sprite(9, 7),
new Sprite(9, 5),
// Fifth row 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(1, 9),
new Sprite(3, 9), new Sprite(3, 9),
new Sprite(5, 9), new Sprite(5, 9),
new Sprite(7, 9), new Sprite(7, 9)
new Sprite(9, 9),
]; ];
function start_game() { function start_game() {
@ -73,7 +60,6 @@ function start_game() {
sprites.forEach(sprite => app.stage.addChild(sprite.getSprite())); sprites.forEach(sprite => app.stage.addChild(sprite.getSprite()));
// Red border // Red border
let red_border = generate_red_border(new PIXI.Graphics()); let red_border = generate_red_border(new PIXI.Graphics());
app.stage.addChild(red_border); app.stage.addChild(red_border);