]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/azgame.rb
refactor: wordlist shouldn't use bot singleton #35
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / azgame.rb
index edff909368372d50ad3b17f6005752fc2b682cc1..b4dbabc1b73a03573c8883112b89c3e8246119df 100644 (file)
@@ -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) }
@@ -123,25 +145,34 @@ class AzGamePlugin < Plugin
       :good => /(?:singular )?noun|verb|adj/,
       :first => 'abacus',
       :last => 'zuni',
-      :url => "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st",
+      :url => "http://www.chambers.co.uk/search.php?query=%s&title=21st",
       :listener => /^[a-z]+$/
     },
     }
 
-    @wordlist_base = "#{@bot.botclass}/azgame/wordlist-"
+    @autoadd_base = datafile "autoadd-"
   end
 
-  def initialize_wordlist(lang)
-    wordlist = @wordlist_base + lang.to_s
-    if File.exist?(wordlist)
+  def initialize_wordlist(params)
+    lang = params[:lang]
+    addlang = params[:addlang]
+    autoadd = @autoadd_base + addlang.to_s
+    if Wordlist.exist?(@bot, lang)
       # 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
+      words = Wordlist.get(@bot, lang)
+      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 +278,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 ...")
@@ -324,10 +355,10 @@ class AzGamePlugin < Plugin
       if wc[word].key?(:when)
         tr = _("%{word} learned from %{user} on %{date}") % {:word => word, :user => wc[word][:who], :date => wc[word][:when]}
       else
-        tr = _("%{word} learned from %{user}") % {:word => word, :user => wc[word][:who]} 
+        tr = _("%{word} learned from %{user}") % {:word => word, :user => wc[word][:who]}
       end
       m.reply tr
-    when :delete 
+    when :delete
       if pars.empty?
         m.reply _("provide a word")
         return
@@ -529,6 +560,7 @@ class AzGamePlugin < Plugin
         debug "getting random word from dictionary, matching #{random}"
         p = @bot.httputil.get(rules[:url] % CGI.escape(random))
         debug p
+        raise 'unable to get search results' if not p.match /id="fullsearchresults"/i
         lemmi = Array.new
         good = rules[:good]
         # We look for a lemma composed by a single word and of length at least two
@@ -567,9 +599,8 @@ class AzGamePlugin < Plugin
     when 'play'
       return _("az => start a game if none is running, show the current word range otherwise; you can say 'az <language>' if you want to play in a language different from the current bot default")
     end
-    offset = @wordlist_base.length
     langs = @rules.keys
-    wls = Dir.glob(@wordlist_base + "*").map { |f| f[offset,f.length].intern rescue nil }.compact - langs
+    wls = Wordlist.list(@bot)
     return [
       _("az topics: play, rules, cancel, manage, check"),
       _("available languages: %{langs}") % { :langs => langs.join(", ") },
@@ -584,5 +615,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 }