Merge pull request #32 from H4CK3R-01/logic

Logic
This commit is contained in:
Florian Kaiser 2021-06-16 11:00:52 +02:00 committed by GitHub
commit 35da8d56bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
let gameState = {
players: [],
positions: [],
whosNext: 0,
started: false
};
class Player {
constructor(socketUsername) {
this.socketUsername = socketUsername;
this.position = 0;
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;
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)
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(socket.username);
gameState['positions'].push(1);
gameState.players.push(new Player(socket.username));
addedUser = true;
socket.emit('login');
@ -76,31 +132,35 @@ 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);
gameState.players.splice(index, 1);
}
socket.leave(socket.room);
if (gameState['players'].length === 0) {
gameState['players'] = [];
gameState['positions'] = [];
gameState['whosNext'] = 0;
gameState['started'] = false;
if (gameState.players.length === 0) {
gameState.players = [];
gameState.whosNext = 0;
gameState.started = false;
}
}
generate_log_message(socket.room, socket.username, "LEFT", "");
});
// Game
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 randomNumber = Math.floor(Math.random() * sides) + 1;
@ -113,7 +173,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);
@ -122,11 +182,10 @@ io.on('connection', socket => {
}
});
socket.on('card finished', function (difficulty) {
gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty;
gameState['whosNext'] += 1;
if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0;
socket.on('card finished', function (difficulty, answerIsCorrect) {
if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty);
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
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");
}

View File

@ -5,10 +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 = [1, 1, 1, 1];
let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
let player_sprite_array = [];
let card;
let answer = null;
let show_card = false;
@ -34,31 +30,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() {
@ -73,7 +60,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);