2021-11-01 00:12:17 +00:00
'use strict' ;
const config = require ( 'config' ) ;
2021-11-04 00:51:43 +00:00
2021-11-01 00:12:17 +00:00
const words = require ( '../../assets/mash-words.json' ) ;
2021-11-04 00:51:43 +00:00
2021-11-01 00:12:17 +00:00
let mash = null ;
2021-11-04 02:53:25 +00:00
function getWordKey ( word ) {
return word . split ( '' ) . sort ( ) . join ( '' ) ;
}
2021-11-01 00:12:17 +00:00
function start ( length , context , attempt = 0 ) {
2021-11-04 00:51:43 +00:00
const lengthWords = words [ length ] ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
if ( ! lengthWords ) {
context . sendMessage ( ` No words with ${ length } letters available ` , context . room . id ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
if ( mash ) {
2021-11-04 01:57:32 +00:00
context . sendMessage ( ` The mash ** ${ mash . anagram } ** was not guessed, possible answers: ${ mash . answers . map ( ( answer ) => ` ** ${ answer . word } ** ` ) . join ( ', ' ) } ` , context . room . id ) ;
2021-11-04 00:51:43 +00:00
context . logger . info ( ` Mash ' ${ mash . anagram } ' discarded ` ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
mash = null ;
}
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
const wordEntries = Object . entries ( lengthWords ) ;
const [ key , answers ] = wordEntries [ Math . floor ( Math . random ( ) * wordEntries . length ) ] ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
const anagram = key . split ( '' ) . sort ( ( ) => ( Math . random ( ) > 0.5 ? 1 : - 1 ) ) . join ( '' ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 01:57:32 +00:00
if ( answers . some ( ( answer ) => answer . word === anagram ) ) {
2021-11-04 00:51:43 +00:00
if ( attempt >= 10 ) {
context . sendMessage ( ` Sorry, I did not find a mashable ${ length } -letter word ` ) ;
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
start ( length , context , attempt + 1 ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
mash = { key , anagram , answers } ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
context . sendMessage ( ` Stomp stomp, here's your mash: ** ${ mash . anagram } ** ` , context . room . id ) ;
2021-11-04 01:57:32 +00:00
context . logger . info ( ` Mash started, ' ${ anagram } ' with answers ${ answers . map ( ( answer ) => ` ' ${ answer . word } ' ` ) . join ( ', ' ) } ` ) ;
2021-11-01 00:12:17 +00:00
}
2021-11-18 00:14:35 +00:00
function play ( rawWord , context , shouted ) {
2021-11-06 00:33:52 +00:00
const word = rawWord . toLowerCase ( ) ;
2021-11-04 02:53:25 +00:00
const key = getWordKey ( word ) ;
2021-11-18 00:14:35 +00:00
const answer = mash . answers . find ( ( answerX ) => answerX . word === word ) ;
2021-11-01 00:12:17 +00:00
2021-11-18 00:14:35 +00:00
if ( ! shouted ) {
if ( word . length !== mash . key . length ) {
context . sendMessage ( ` Your answer needs to be ${ mash . key . length } letters, @ ${ context . user . username } ` , context . room . id ) ;
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-18 00:14:35 +00:00
if ( key !== mash . key ) {
context . sendMessage ( ` You are not using the letters in ** ${ mash . anagram } **, @ ${ context . user . username } ` , context . room . id ) ;
return ;
}
if ( word === mash . anagram ) {
context . sendMessage ( ` @ ${ context . user . username } ... :expressionless: ` , context . room . id ) ;
return ;
}
2021-11-04 00:51:43 +00:00
}
2021-11-01 00:12:17 +00:00
2021-11-18 00:14:35 +00:00
if ( answer ) {
const definition = answer . definitions [ 0 ] ? ` : * ${ answer . definitions [ 0 ] . slice ( 0 , 100 ) } ${ mash . answers [ 0 ] . definitions [ 0 ] . length > 100 ? '...*' : '*' } ` : '' ;
2021-11-04 01:57:32 +00:00
2021-11-04 00:51:43 +00:00
context . sendMessage ( mash . answers . length === 1
2021-11-04 01:57:32 +00:00
? ` ** ${ word } ** is the right answer ${ definition } , @ ${ context . user . username } now has ** ${ context . user . points + 1 } ${ context . user . points === 0 ? 'point' : 'points' } **! There were no other options for ** ${ mash . anagram } **. `
2021-11-18 00:14:35 +00:00
: ` ** ${ word } ** is the right answer ${ definition } , @ ${ context . user . username } now has ** ${ context . user . points + 1 } ${ context . user . points === 0 ? 'point' : 'points' } **! Other options for ** ${ mash . anagram } **: ${ mash . answers . filter ( ( answerX ) => answerX . word !== word ) . map ( ( answerX ) => ` * ${ answerX . word } * ` ) . join ( ', ' ) } ` , context . room . id ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
context . logger . info ( ` Mash ' ${ mash . anagram } ' guessed by ' ${ context . user . username } ' with ' ${ word } ' ` ) ;
context . setPoints ( context . user , 1 ) ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
mash = null ;
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
setTimeout ( ( ) => start ( word . length , context ) , 2000 ) ;
}
2021-11-01 00:12:17 +00:00
}
2021-11-04 02:53:25 +00:00
function resolve ( word , context ) {
if ( ! word ) {
context . sendMessage ( ` Please specify an anagram you would like to resolve, @ ${ context . user . username } ` , context . room . id ) ;
return ;
}
const anagram = getWordKey ( word ) ;
const answers = words [ word . length ] ? . [ anagram ] ;
if ( answers ? . length > 1 && answers . some ( ( answer ) => answer . word === word ) ) {
context . sendMessage ( ` ** ${ word } ** is a valid word in itself, and has the following anagrams, @ ${ context . user . username } : ${ answers . filter ( ( answer ) => answer . word !== word ) . map ( ( answer ) => ` * ${ answer . word } * ` ) . join ( ', ' ) } ` , context . room . id ) ;
return ;
}
if ( answers ? . length === 1 && answers [ 0 ] . word === word ) {
context . sendMessage ( ` ** ${ word } ** is a valid word in itself, but has no anagrams, @ ${ context . user . username } ` , context . room . id ) ;
return ;
}
if ( answers ? . length > 0 ) {
context . sendMessage ( ` Anagrams of ** ${ word } **, @ ${ context . user . username } : ${ answers . map ( ( answer ) => ` * ${ answer . word } * ` ) . join ( ', ' ) } ` , context . room . id ) ;
return ;
}
context . sendMessage ( ` No anagrams found for ** ${ word } **, @ ${ context . user . username } ` , context . room . id ) ;
}
function define ( word , context ) {
if ( ! word ) {
context . sendMessage ( ` Please specify word you would like to define, @ ${ context . user . username } ` , context . room . id ) ;
return ;
}
const anagram = getWordKey ( word ) ;
const answers = words [ word . length ] ? . [ anagram ] ;
const answer = answers ? . find ( ( answerX ) => answerX . word === word ) ;
if ( answer && answer . definitions ? . length > 0 ) {
context . sendMessage ( ` ${ word } can be defined as follows, @ ${ context . user . username } : * ${ answer . definitions [ 0 ] } * ` , context . room . id ) ;
return ;
}
context . sendMessage ( ` No definition available for ** ${ word } **, @ ${ context . user . username } ` , context . room . id ) ;
}
2021-11-15 22:57:16 +00:00
function hint ( context ) {
if ( ! mash ) {
context . sendMessage ( ` There is no mash going on right now, @ ${ context . user . username } . Start one with ${ config . prefix } mash {length} ` , context . room . id ) ;
return ;
}
if ( mash . anagram . length <= 3 ) {
context . sendMessage ( ` The mash ** ${ mash . anagram } ** is too short for a hint, @ ${ context . user . username } . ` , context . room . id ) ;
return ;
}
if ( mash . anagram . length === 4 ) {
2021-11-18 00:14:35 +00:00
context . sendMessage ( ` Hints for ** ${ mash . anagram } **, @ ${ context . user . username } : ${ mash . answers . map ( ( answer ) => ` ** ${ answer . word . slice ( 0 , 1 ) } ${ '_ ' . repeat ( answer . word . length - 1 ) . trim ( ) } ** ( ${ answer . definitions [ 0 ] } ) ` ) . join ( ', ' ) } ` , context . room . id ) ;
2021-11-15 22:57:16 +00:00
return ;
}
2021-11-18 00:14:35 +00:00
context . sendMessage ( ` Hints for ** ${ mash . anagram } **, @ ${ context . user . username } : ${ mash . answers . map ( ( answer ) => ` ** ${ answer . word . slice ( 0 , 1 ) } ${ '_ ' . repeat ( answer . word . length - 2 ) } ${ answer . word . slice ( - 1 ) } ** ( ${ answer . definitions [ 0 ] } ) ` ) . join ( ', ' ) } ` , context . room . id ) ;
2021-11-15 22:57:16 +00:00
}
2021-11-01 00:12:17 +00:00
function onCommand ( args , context ) {
2021-11-04 02:53:25 +00:00
const word = args [ 0 ] ;
const length = Number ( word ) ;
if ( [ 'solve' , 'resolve' , 'lookup' ] . includes ( context . subcommand ) ) {
resolve ( word , context ) ;
return ;
}
if ( [ 'define' , 'dict' , 'dictionary' ] . includes ( context . subcommand ) ) {
define ( word , context ) ;
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-15 22:57:16 +00:00
if ( [ 'hint' ] . includes ( context . subcommand ) ) {
hint ( context ) ;
return ;
}
2021-11-04 00:51:43 +00:00
if ( ! Number . isNaN ( length ) ) {
start ( length , context ) ;
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-15 23:12:55 +00:00
if ( ! word && mash ) {
context . sendMessage ( ` The current mash is: ** ${ mash . anagram } ** ` , context . room . id ) ;
2021-11-04 00:51:43 +00:00
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-15 23:12:55 +00:00
if ( ! word || ! mash ) {
context . sendMessage ( ` Start a mash with ${ config . prefix } mash {length} ` , context . room . id ) ;
2021-11-04 00:51:43 +00:00
return ;
}
2021-11-01 00:12:17 +00:00
2021-11-04 00:51:43 +00:00
play ( args [ 0 ] , context ) ;
2021-11-01 00:12:17 +00:00
}
2021-11-18 00:14:35 +00:00
function onMessage ( message , context ) {
2022-10-12 23:46:57 +00:00
if ( mash && context . user ? . id !== config . user . id ) {
2021-11-18 00:14:35 +00:00
play ( message . body , context , true ) ;
}
}
2021-11-01 00:12:17 +00:00
module . exports = {
2021-11-04 00:51:43 +00:00
name : 'Mash' ,
onCommand ,
2021-11-18 00:14:35 +00:00
onMessage ,
2021-11-01 00:12:17 +00:00
} ;