Check if card was used already #67

Merged
H4CK3R-01 merged 1 commits from disable_duplicate_questions into main 2021-07-12 12:41:04 +00:00
2 changed files with 14 additions and 3 deletions

View File

@ -20,6 +20,7 @@ class Game {
this.round = 0; this.round = 0;
this.hunter = new Hunter(); this.hunter = new Hunter();
this.playerNames = []; this.playerNames = [];
this.used_cards = [];
} }
finish_turn() { finish_turn() {

View File

@ -122,7 +122,10 @@ io.on('connection', socket => {
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return; if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
if (game[socket.room].current_player_is(socket.username)) { if (game[socket.room].current_player_is(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, socket.room)
});
generate_log_message(socket.room, socket.username, "CARD", difficulty); generate_log_message(socket.room, socket.username, "CARD", difficulty);
} else { } else {
@ -190,11 +193,18 @@ function generate_log_message(room, user, type, message) {
console.info("%s[%s] [%s] [%s]\x1b[0m %s", color, room, user, type, reset_color, message); console.info("%s[%s] [%s] [%s]\x1b[0m %s", color, room, user, type, reset_color, message);
} }
function getRandomCard(difficulty) { function getRandomCard(difficulty, room) {
let filtered_cards = cards.filter(card => { let filtered_cards = cards.filter(card => {
return card.difficulty === difficulty; return card.difficulty === difficulty;
}); });
return shuffleAnswers(filtered_cards[Math.floor(Math.random() * filtered_cards.length)]);
let tmp = filtered_cards[Math.floor(Math.random() * filtered_cards.length)];
while (game[room].used_cards.indexOf(tmp.id) !== -1) {
tmp = filtered_cards[Math.floor(Math.random() * filtered_cards.length)];
}
game[room].used_cards.push(tmp.id);
return shuffleAnswers(tmp);
} }
function shuffleAnswers(card) { function shuffleAnswers(card) {