From 4ca95aa1aaf7854a0fc136c8be767729924aac26 Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 12 Jan 2015 04:30:31 +0000 Subject: [PATCH] wordcrimes initial --- modules/wordcrimes/config.json | 26 ++++++++++ modules/wordcrimes/wordcrimes.js | 83 ++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 modules/wordcrimes/config.json create mode 100644 modules/wordcrimes/wordcrimes.js diff --git a/modules/wordcrimes/config.json b/modules/wordcrimes/config.json new file mode 100644 index 0000000..4c768eb --- /dev/null +++ b/modules/wordcrimes/config.json @@ -0,0 +1,26 @@ +{ + "dict": "/usr/share/dict/british-english", + "puzzles": [ + { + "type": "start", + "part": "en" + }, + { + "type": "start", + "part": "re" + }, + { + "type": "end", + "part": "tain" + }, + { + "type": "end", + "part": "ter" + }, + { + "type": "start", + "part": "under" + } + ], + "allowed_chans": [ "#realitree" ] +} diff --git a/modules/wordcrimes/wordcrimes.js b/modules/wordcrimes/wordcrimes.js new file mode 100644 index 0000000..919e8f5 --- /dev/null +++ b/modules/wordcrimes/wordcrimes.js @@ -0,0 +1,83 @@ +/** + * Module Name: wordcrimes + * Description: wordgame and ting + */ +var fs = require('fs'), + _ = require('underscore')._; + +var wordcrimes = function(dbot) { + this.game = null; + + this.commands = { + '~startgame': function(event) { + if(!this.game && _.include(this.config.allowed_chans, event.channel.name)) { + event.reply('WORD GAME STARTING IN 5 SECONDS'); + setTimeout(function() { + var puzzle = this.puzzles[_.random(0, this.puzzles.length -1)], + solutions = _.filter(this.dict, function(word) { + if(puzzle.type === 'start') { + return word.match('^' + puzzle.part); + } else if(puzzle.type === 'end') { + return word.match(puzzle.part + '$'); + } + }); + + this.game = { + 'puzzle': puzzle, + 'solutions': solutions, + 'channel': event.channel.name, + 'found': [], + 'scores': {} + }; + + if(puzzle.type === 'start') { + event.reply('NAME ALL THE WORDS YOU CAN THINK OF THAT START WITH ' + puzzle.part); + } else if(puzzle.type === 'end') { + event.reply('NAME ALL THE WORDS YOU CAN THINK OF THAT END IN ' + puzzle.part); + } + + setTimeout(function() { + if(!_.isNull(this.game)) { + if(this.game.found.length > 0) { + var winner = _.invert(this.game.scores)[_.max(this.game.scores)]; + event.reply('GAME OVER. THE WINNER IS ' + winner.toUpperCase() + ' WITH ' + this.game.scores[winner]); + event.reply(this.game.solutions.length - this.game.found.length + ' solutions remained.'); + } else { + event.reply('NO SOLUTIONS FOUND. YOU ARE ALL RUBBISH.'); + } + this.game = null; + } + }.bind(this), 40000); + }.bind(this), 5000); + } + } + }; + + this.listener = function(event) { + if(!_.isNull(this.game) && this.game.channel === event.channel.name) { + if(_.include(this.game.solutions, event.message) && !_.include(this.game.found, event.message)) { + if(!_.has(this.game.scores, event.user)) this.game.scores[event.user] = 0; + this.game.scores[event.user]++; + this.game.found.push(event.message); + event.reply(event.user + ': ' + event.message.toUpperCase() + ' IS CORRECT. ' + + this.game.found.length + '/' + this.game.solutions.length + ' WORDS FOUND'); + + if(this.game.found.length === this.game.solutions.length) { + var winner = _.invert(this.game.scores)[_.max(this.game.scores)]; + event.reply('ALL WORDS FOUND. THE WINNER IS ' + winner.toUpperCase() + ' WITH ' + this.game.scores[winner]); + this.game = null; + } + } + } + }.bind(this); + this.on = 'PRIVMSG'; + + this.onLoad = function() { + this.puzzles = this.config.puzzles; + this.dict = fs.readFileSync(this.config.dict).toString().split('\n'); + }.bind(this); +}; + +exports.fetch = function(dbot) { + return new wordcrimes(dbot); +};