Improved Letters game shuffle, board layout and minimum word length.

This commit is contained in:
ThePendulum 2022-10-21 19:38:39 +02:00
parent 61af290780
commit 25a196e569
2 changed files with 15 additions and 24 deletions

View File

@ -35,6 +35,6 @@ module.exports = {
}, },
letters: { letters: {
length: 9, length: 9,
timeout: 30, timeout: 60,
}, },
}; };

View File

@ -3,15 +3,18 @@
const config = require('config'); const config = require('config');
const timers = require('timers/promises'); const timers = require('timers/promises');
const shuffle = require('../utils/shuffle'); const crypto = require('crypto');
const style = require('../utils/style'); const style = require('../utils/style');
const getLeaders = require('../utils/get-leaders'); const getLeaders = require('../utils/get-leaders');
const getWordKey = require('../utils/get-word-key'); const getWordKey = require('../utils/get-word-key');
const words = require('../../assets/mash-words.json'); const words = require('../../assets/mash-words.json');
const availableVowels = ['a', 'e', 'i', 'o', 'u']; const availableLetters = {
const availableConsonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']; // Countdown regards y as a consonant vowel: ['a', 'e', 'i', 'o', 'u'],
const types = { v: 'vowels', c: 'consonants' }; consonant: ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'], // Countdown regards y as a consonant
};
const types = { v: 'vowel', c: 'consonant' };
const settings = { ...config.letters }; const settings = { ...config.letters };
const games = new Map(); const games = new Map();
@ -19,28 +22,18 @@ const games = new Map();
function getBoard(context) { function getBoard(context) {
const game = games.get(context.room.id); const game = games.get(context.room.id);
return `${game.word.split('').map((letter) => `${style.grey('[')}${letter.toUpperCase()}${style.grey(']')}`).join('')}${`${style.silver('[')} ${style.silver(']')}`.repeat(config.letters.length - game.word.length)}`; return `${style.grey('[')} ${game.word.split('').concat(Array.from({ length: config.letters.length - game.word.length })).map((letter) => letter?.toUpperCase() || ' ').join(style.grey(' | '))} ${style.grey(']')}`;
} }
function countLetters(word) { function countLetters(word) {
return word.split('').reduce((counts, letter) => ({ ...counts, [letter]: (counts[letter] || 0) + 1 }), {}); return word.split('').reduce((counts, letter) => ({ ...counts, [letter]: (counts[letter] || 0) + 1 }), {});
} }
function shuffleLetters(letters, acc = []) {
const shuffled = shuffle(letters, config.letters.length);
if (acc.length + shuffled.length < config.letters.length) {
return shuffleLetters(letters, [...acc, ...shuffled, ...letters]);
}
return [...acc, ...shuffled].slice(0, config.letters.length);
}
function playWord(rawWord, context) { function playWord(rawWord, context) {
const game = games.get(context.room.id); const game = games.get(context.room.id);
const word = rawWord.trim().toLowerCase(); const word = rawWord.trim().toLowerCase();
if (!word || word.length > game.word.length) { if (!word || word.length < 3 || word.length > game.word.length) {
return; return;
} }
@ -112,11 +105,11 @@ function pickLetters(type, context) {
return; return;
} }
if (type === 'consonants' || type === 'vowels') { if (type === 'consonant' || type === 'vowel') {
game.word = `${game.word}${game[type].pop()}`; game.word = `${game.word}${availableLetters[type][crypto.randomInt(0, availableLetters[type].length)]}`;
} else { } else {
type.toLowerCase().slice(0, config.letters.length - game.word.length).split('').forEach((typeKey) => { type.toLowerCase().slice(0, config.letters.length - game.word.length).split('').forEach((typeKey) => {
game.word = `${game.word}${game[types[typeKey]].pop()}`; game.word = `${game.word}${availableLetters[types[typeKey]][crypto.randomInt(0, availableLetters[types[typeKey]].length)]}`;
}); });
} }
@ -138,8 +131,6 @@ function start(context) {
games.set(context.room.id, { games.set(context.room.id, {
state: 'letters', state: 'letters',
word: '', word: '',
vowels: shuffleLetters(availableVowels),
consonants: shuffleLetters(availableConsonants),
played: new Set(), played: new Set(),
points: {}, points: {},
ac: new AbortController(), // eslint-disable-line no-undef ac: new AbortController(), // eslint-disable-line no-undef
@ -176,12 +167,12 @@ function onMessage(message, context) {
} }
if (/(^|\b)cons?(onant)?($|\b)/i.test(message.body)) { if (/(^|\b)cons?(onant)?($|\b)/i.test(message.body)) {
pickLetters('consonants', context); pickLetters('consonant', context);
return; return;
} }
if (/(^|\b)vow(el)?($|\b)/i.test(message.body)) { if (/(^|\b)vow(el)?($|\b)/i.test(message.body)) {
pickLetters('vowels', context); pickLetters('vowel', context);
return; return;
} }
} }