Added Hunt (hangman) game.

This commit is contained in:
Niels Simenon 2022-10-30 04:21:32 +01:00
parent cb2fa362d6
commit b4add9e11d
9 changed files with 1970 additions and 3 deletions

1728
assets/markov-data.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,23 @@ module.exports = {
greeting: 'Hi, I am aisha, your game host!',
usernamePrefix: '@',
channels: ['GamesNight'],
games: ['mash', 'trivia', 'letters', 'duck', '8ball', 'riddle', 'dice', 'rock-paper-scissors', 'ping', 'say', 'kill', 'uptime', 'help'],
games: [
'mash',
'trivia',
'letters',
'hunt',
'8ball',
'riddle',
'dice',
'parrot',
'rock-paper-scissors',
'duck',
'ping',
'say',
'kill',
'uptime',
'help',
],
schatColors: {
white: 'white',
blue: 'var(--message-40)',
@ -65,4 +81,10 @@ module.exports = {
maxRolls: 10,
maxFaces: 1000,
},
hangman: {
minLength: 5,
maxLength: 9,
guesses: [2, 3, 4, 5, 6, 7], // by word length, last number repeats for longer words
// guesses: 6, // fixed
},
};

20
package-lock.json generated
View File

@ -20,6 +20,7 @@
"irc-upd": "^0.11.0",
"jsdom": "^18.1.0",
"linkify-it": "^3.0.3",
"markov-strings": "^3.0.1",
"simple-node-logger": "^21.8.12",
"string-similarity": "^4.0.4",
"tensify": "^0.0.4",
@ -2581,6 +2582,17 @@
"node": ">=10"
}
},
"node_modules/markov-strings": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/markov-strings/-/markov-strings-3.0.1.tgz",
"integrity": "sha512-/Cf6m7iIK1JWtvOemZ9eRUpKvebm+m6uXMTzszOJrRO3rkmdCqoS/EO9d4/3aFPnYEIHkopcF4C7W+k1zTDjnw==",
"dependencies": {
"lodash": "^4.17.20"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
@ -5822,6 +5834,14 @@
"yallist": "^4.0.0"
}
},
"markov-strings": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/markov-strings/-/markov-strings-3.0.1.tgz",
"integrity": "sha512-/Cf6m7iIK1JWtvOemZ9eRUpKvebm+m6uXMTzszOJrRO3rkmdCqoS/EO9d4/3aFPnYEIHkopcF4C7W+k1zTDjnw==",
"requires": {
"lodash": "^4.17.20"
}
},
"mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",

View File

@ -29,6 +29,7 @@
"irc-upd": "^0.11.0",
"jsdom": "^18.1.0",
"linkify-it": "^3.0.3",
"markov-strings": "^3.0.1",
"simple-node-logger": "^21.8.12",
"string-similarity": "^4.0.4",
"tensify": "^0.0.4",

163
src/games/hunt.js Normal file
View File

@ -0,0 +1,163 @@
'use strict';
const config = require('config');
const words = require('../../assets/mash-words.json');
const pickRandom = require('../utils/pick-random');
const style = require('../utils/style');
const games = new Map();
const targets = ['🐣', '🐕', '🐈', '🐢', '🐇', '🐁', '🦔'];
const predators = ['🦅', '🐍', '🦖', '🐅', '🐆', '🐊', '🐉'];
const trees = ['🌴', '🌳', '🌲', '🌵'];
const flowers = ['🌹', '🌻', '🌷', '🌱', '🍄', '🪨'];
function getGuesses(word) {
if (Array.isArray(config.hangman.guesses)) {
return config.hangman.guesses[word.length - 1] || config.hangman.guesses.at(-1);
}
return config.hangman.guesses;
}
function renderBoard(context) {
const game = games.get(context.room.id);
const word = game.partial.map((letter) => letter || '_').join(' ').toUpperCase();
const track = `${game.flower}${game.target}${game.track.slice(0, getGuesses(game.word) - game.progress).join('')}${game.predator}`;
context.sendMessage(`${style.bold(style.code(word))} ${track}`, context.room.id);
}
function getOutcome(target, predator) {
return pickRandom([
`${target} and ${predator} will now have to spend time together`,
`${target} will now have to listen to boring stories from ${predator}`,
`${target} must now endure a horrible tickle attack from ${predator}`,
`${target} and ${predator} are now going to waste an entire day arguing which one of them has more legs`,
`${predator} is out of breath now`,
`${target} will now have to explain why they never answer any texts from ${predator}`,
]);
}
function progress(context) {
const game = games.get(context.room.id);
game.progress += 1;
if (game.progress > getGuesses(game.word)) {
context.sendMessage(`The word ${style.bold(game.word)} was not guessed. ${getOutcome(game.target, game.predator)}.`, context.room.id);
games.delete(context.room.id);
return;
}
renderBoard(context);
}
function playLetter(playedLetter, context) {
const game = games.get(context.room.id);
if (game.partial.includes(playedLetter)) {
return;
}
if (!game.word.includes(playedLetter)) {
progress(context);
return;
}
game.partial = game.partial.map((letter, index) => letter
|| (game.word[index] === playedLetter && game.word[index])
|| null);
renderBoard(context);
if (game.partial.every((letter) => letter !== null)) {
context.sendMessage(style.bold('The word was completed!'), context.room.id);
games.delete(context.room.id);
}
}
function playWord(word, context) {
const game = games.get(context.room.id);
if (word === game.word) {
context.sendMessage(`The word ${style.answer(word)} was guessed, ${style.username(context.user.prefixedUsername)}, who gets a point!`, context.room.id);
context.setPoints(context.user);
games.delete(context.room.id);
return;
}
context.sendMessage('That\'s not it', context.room.id);
progress(context);
}
function stop(context) {
const game = games.get(context.room.id);
if (!game) {
context.sendMessage(`There is no game going on, ${style.username(context.user.prefixedUsername)}. You can start one with ${config.prefix}hunt!`, context.room.id);
return;
}
games.delete(context.room.id);
context.sendMessage(`The game was stopped by ${style.username(context.user.prefixedUsername)}. The word was ${style.bold(game.word)} with ${getGuesses(game.word) - game.progress} guesses remaining.`, context.room.id);
}
function onCommand(args, context) {
if (context.subcommand === 'stop') {
stop(context);
return;
}
const minLength = Number(args[0]) || config.hangman.minLength;
const maxLength = Number(args[0]) || config.hangman.maxLength;
const playableWords = Array.from({ length: (maxLength - minLength) + 1 }, (value, index) => Object.values(words[index + minLength] || {})).flat(2);
const word = pickRandom(playableWords);
if (!word) {
context.sendMessage('Sorry, I was not able to find a word to play.', context.room.id);
return;
}
context.logger.info(`Hangman played ${word.word} with ${getGuesses(word)} guesses`);
games.set(context.room.id, {
word: word.word,
progress: 0,
defintiions: word.definitions,
partial: word.word.split('').map(() => null),
flower: pickRandom(flowers),
target: pickRandom(targets),
predator: pickRandom(predators),
track: Array.from({ length: getGuesses(word) }, (value, index) => (index % 2 ? pickRandom(trees) : '')), // Em Space to ensure proper spacing in SChat
});
renderBoard(context);
}
function onMessage(message, context) {
if (games.has(context.room.id)) {
const letter = message.body.match(/^\s*\w\s*$/)?.[0];
const word = message.body.match(/^\s*\w{2,}\s*$/)?.[0];
if (letter) {
playLetter(letter.toLowerCase().trim(), context);
return;
}
if (word) {
playWord(word.toLowerCase().trim(), context);
}
}
}
module.exports = {
onCommand,
onMessage,
commands: ['hangman'],
};

20
src/games/parrot.js Normal file
View File

@ -0,0 +1,20 @@
'use strict';
const Markov = require('markov-strings').default;
const sentences = require('../../assets/markov-data.json');
const markov = new Markov();
markov.addData(sentences);
function onCommand(args, context) {
const result = markov.generate({ maxTries: 100 });
console.log(result);
context.sendMessage(result.string, context.room.id);
}
module.exports = {
onCommand,
};

View File

@ -124,7 +124,7 @@ async function getGames(bot, identifier) {
}
};
const setGamePoints = (userId, score, options) => setPoints(identifier, key, userId, score, options);
const setGamePoints = (userId, score = 1, options) => setPoints(identifier, key, userId, score, options);
const curatedGame = {
...game,

View File

@ -3,6 +3,10 @@
const crypto = require('crypto');
function pickRandom(array) {
if (array.length === 0) {
return null;
}
return array[crypto.randomInt(0, array.length)];
}

View File

@ -37,7 +37,7 @@ function shieldMethods(methods) {
return Object.fromEntries(Object.entries(methods).map(([key, method]) => [key, curate(method)]));
}
module.exports = (() => {
const styleMethods = (() => {
if (config.platform === 'irc') {
return shieldMethods({
...styles,
@ -66,3 +66,12 @@ module.exports = (() => {
return null;
})();
const expandedMethods = {
...styleMethods,
username: (value) => styleMethods.bold(styleMethods.cyan(value)),
time: (value) => styleMethods.bold(styleMethods.green(value)),
answer: (value) => styleMethods.bold(styleMethods.yellow(value)),
};
module.exports = expandedMethods;