]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/azgame.rb
remove whitespace
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / azgame.rb
index 2d524305f8214559b19855de04b703149a86d142..572684d912d68c1fc1d5ae2ea8faa16f666b3deb 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) }
@@ -127,18 +149,30 @@ class AzGamePlugin < Plugin
       :listener => /^[a-z]+$/
     },
     }
+
+    @autoadd_base = datafile "autoadd-"
   end
 
-  def initialize_wordlist(lang)
-    wordlist = "#{@bot.botclass}/azgame/wordlist-#{lang}"
-    if File.exist?(wordlist)
-      words = File.readlines(wordlist).map {|line| line.strip}.uniq
+  def initialize_wordlist(params)
+    lang = params[:lang]
+    addlang = params[:addlang]
+    autoadd = @autoadd_base + addlang.to_s
+    if Wordlist.exist?(lang)
+      # wordlists are assumed to be UTF-8, but we need to strip the BOM, if present
+      words = Wordlist.get(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]}"
@@ -244,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 ...")
@@ -321,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
@@ -564,7 +598,14 @@ 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
-    return _("az topics: play, rules, cancel, manage, check")
+    langs = @rules.keys
+    wls = Wordlist.list
+    return [
+      _("az topics: play, rules, cancel, manage, check"),
+      _("available languages: %{langs}") % { :langs => langs.join(", ") },
+      wls.empty? ? nil : _("available wordlists: %{wls}") % { :wls => wls.join(", ") },
+    ].compact.join(". ")
+
   end
 
 end
@@ -573,5 +614,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 }