###################################################################### # # WScrabbleWinner - An Rbot plugin that always wins WScrabble :) # Copyright (c) 2010 Rob la Lau (rob@nerdstock.org) # # This program is free software: you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License # as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # The GNU General Public License can be found at . # ###################################################################### ###################################################################### # Rbot: http://ruby-rbot.org/ # WScrabble: http://wiz0u.free.fr/wscrabble/index_en.php # WScrabbleWinner: http://nerdstock.org/rbot_wsw # # To have this plugin do anything, make sure it has a dictionary. # See the website for instructions. ###################################################################### ###################################################################### # CAVEAT EMPTOR # # If WScrabble and WScrabbleWinner are not using the same dictionaries # it may happen that WScrabbleWinner gives a word that WScrabble # doesn't recognize, or that WScrabble has a word that is longer than # the longest word WScrabbleWinner found. ###################################################################### # And now: on with the show... class WScrabbleWinnerPlugin < Plugin Config.register Config::BooleanValue.new('wscrabblewinner.enabled', :default => false, :desc => _('Enable WScrabbleWinner?') ) Config.register Config::ArrayValue.new('wscrabblewinner.listen', :default => ['#Scrabble,Scrabble,nl,[Getrokken letters]'], :desc => _('List of channels (,,,).') ) def help(plugin, topic="") case topic when "play" sprintf(_('%s => join in the game in the current channel'), 'wscrabblewinner play') when "shutup" sprintf(_('%s => quit playing in the current channel'), 'wscrabblewinner shutup') else sprintf(_("This plugin was written to always win a game of WScrabble. See topics 'play' and 'shutup' for more info, or go to %s"), @wswurl) end end def initialize super @wswurl = 'http://nerdstock.org/rbot_wsw' Dir.mkdir("#{@bot.botclass}/wscrabblewinner") unless File.exist?("#{@bot.botclass}/wscrabblewinner") if Dir.entries("#{@bot.botclass}/wscrabblewinner").length <= 2 warning "No WScrabbleWinner dictionaries installed in #{@bot.botclass}/wscrabblewinner/. See #{@wswurl} for more info." end @listen = {} @bot.config['wscrabblewinner.listen'].each {|lstn| lstn = lstn.split(',', 4) @listen[lstn[0].downcase] = { lstn[1].downcase => { lstn[3] => lstn[2] } } } if ! @registry.has_key?(:shutup) @registry[:shutup] = Array.new end end def unreplied(m) return unless @bot.config['wscrabblewinner.enabled'] == true dchannel = m.channel.to_s.downcase return if @registry[:shutup].include?(dchannel) if @listen.has_key?(dchannel) @lc = @listen[dchannel] dsourcenick = m.sourcenick.to_s.downcase if @lc.has_key?(dsourcenick) @lcn = @lc[dsourcenick] @lcn.each {|key, val| k = Regexp.escape(key) if m.message =~ /^#{k} - ([A-Z\s]+) - .*/ @letters = $1.strip.downcase.split(/\s/) jletters = @letters.join('') if @letters.length == 12 if File.exist?("#{@bot.botclass}/wscrabblewinner/#{val}") words = File.readlines("#{@bot.botclass}/wscrabblewinner/#{val}").grep(/^[#{jletters}]+$/) words.sort! {|a, b| b.length <=> a.length } lcount = [] letters.each {|l| lcount.push("w.downcase.count('#{l}') <= " + jletters.count(l).to_s) } word = words.detect {|w| eval lcount.join(' and ') } if word != nil @bot.say m.channel, word end break end end end } end end end def shutup(m, params) return unless @bot.config['wscrabblewinner.enabled'] == true shutup = @registry[:shutup] shutup.push(m.channel.to_s.downcase) shutup.uniq! @registry[:shutup] = shutup m.okay end def play(m, params) if @bot.config['wscrabblewinner.enabled'] != true m.reply(_('Plugin is disabled.')) return end shutup = @registry[:shutup] idx = shutup.index(m.channel.to_s.downcase) if idx == nil m.reply(_("I'm doing the best I can...")) else slc = shutup.slice!(idx) @registry[:shutup] = shutup m.reply(_("Let's play %s!") % ['WScrabble']) ###TODO # wsw could say '!play' if there's currently no game running. # (But this would require unreplied() to be rewritten # to register whether a game is running and what the letters are.) end end def languages(m, params) languages = [] Dir.entries("#{@bot.botclass}/wscrabblewinner").each {|f| if f != '.' and f != '..' languages.push(f) end } if languages.length == 0 m.reply(_("No languages installed. See %s for instructions.") % [@wswurl], :overlong => :split) else m.reply(_("Installed languages: %s") % [languages.join(', ')], :overlong => :split) end end end plugin = WScrabbleWinnerPlugin.new plugin.map 'wsw shutup', :action => 'shutup' plugin.map 'wsw play', :action => 'play' plugin.map 'wsw languages', :action => 'languages'