diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-11-17 16:37:57 +0100 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-11-17 16:37:57 +0100 |
commit | 62ca2eda0b2d42f79d06f85e0ce041be64055ad2 (patch) | |
tree | 35659b4bbff10e2f6803ecd267d7e0c9a8af9f42 /data | |
parent | a7cefe6c664ca438fc3785186e814738a390b9f3 (diff) |
azgame plugin: support autoadding words to wordlist
When the game is started with a wordlist it's possible to specify a
language to use for existence of words. When a word is not found in the
wordlist, it's checked against the given language check function, and if
present it's added to a specific file (autoadd-#{language}), which is
also used on load to choose the word to pick.
Diffstat (limited to 'data')
-rw-r--r-- | data/rbot/plugins/games/azgame.rb | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/data/rbot/plugins/games/azgame.rb b/data/rbot/plugins/games/azgame.rb index dd0dfe0d..6fb3f670 100644 --- a/data/rbot/plugins/games/azgame.rb +++ b/data/rbot/plugins/games/azgame.rb @@ -36,7 +36,29 @@ class AzGame return "%s -- %s" % self end if @rules[:list] - @check = Proc.new { |w| @rules[:list].include?(w) } + @check_method = "is_#{@rules[:addlang]}?" + # trick: if addlang was not in rules, this will be is_? which is + # not a method of the plugin + if @check_method and not @plugin.respond_to? @check_method + @check_method = nil + end + @check = Proc.new do |w| + wl = @rules[:list].include?(w) + if !wl and @check_method + if wl = @plugin.send(@check_method, w) + debug "adding #{w} to #{@rules[:addfile]}" + begin + File.open(@rules[:addfile], "a") do |f| + f.puts w + end + rescue Exception => e + error "failed to add #{w} to #{@rules[:addfile]}" + error e + end + end + end + wl + end else @check_method = "is_#{@lang}?" @check = Proc.new { |w| @plugin.send(@check_method, w) } @@ -129,19 +151,30 @@ class AzGamePlugin < Plugin } @wordlist_base = "#{@bot.botclass}/azgame/wordlist-" + @autoadd_base = "#{@bot.botclass}/azgame/autoadd-" end - def initialize_wordlist(lang) + def initialize_wordlist(params) + lang = params[:lang] + addlang = params[:addlang] wordlist = @wordlist_base + lang.to_s + autoadd = @autoadd_base + addlang.to_s if File.exist?(wordlist) # wordlists are assumed to be UTF-8, but we need to strip the BOM, if present - words = File.readlines(wordlist).map {|line| line.sub("\xef\xbb\xbf",'').strip}.uniq.sort + words = File.readlines(wordlist).map {|line| line.sub("\xef\xbb\xbf",'').strip} + if addlang and File.exist?(autoadd) + word += File.readlines(autoadd).map {|line| line.sub("\xef\xbb\xbf",'').strip} + end + words.uniq! + words.sort! if(words.length >= 4) # something to guess rules = { :good => /^\S+$/, :list => words, :first => words[0], :last => words[-1], + :addlang => addlang, + :addfile => autoadd, :listener => /^\S+$/ } debug "#{lang} wordlist loaded, #{rules[:list].length} lines; first word: #{rules[:first]}, last word: #{rules[:last]}" @@ -247,7 +280,7 @@ class AzGamePlugin < Plugin end m.reply _("got it!") @games[k] = AzGame.new(self, lang, @rules[lang], word) - elsif !@rules.has_key?(lang) and rules = initialize_wordlist(lang) + elsif !@rules.has_key?(lang) and rules = initialize_wordlist(params) word = random_pick_wordlist(rules) if word.empty? m.reply _("couldn't think of anything ...") @@ -584,5 +617,5 @@ plugin = AzGamePlugin.new plugin.map 'az [:lang] word :cmd *params', :action=>'wordlist', :defaults => { :lang => nil, :cmd => 'count', :params => [] }, :auth_path => '!az::edit!' plugin.map 'az cancel', :action=>'stop_game', :private => false plugin.map 'az check :word', :action => 'manual_word_check', :private => false -plugin.map 'az [play] [:lang]', :action=>'start_game', :private => false, :defaults => { :lang => nil } +plugin.map 'az [play] [:lang] [autoadd :addlang]', :action=>'start_game', :private => false, :defaults => { :lang => nil, :addlang => nil } |