]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/azgame.rb
azgame: thread word check so that online checking doesn't block
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / azgame.rb
index 04efb810f0a7b023668d693a80561b6f56ed8add..12cdf724196d8f9566403d3cf793603d67c5ef65 100644 (file)
@@ -45,15 +45,17 @@ class AzGame
     return [:ignore, @range] if w == @range.first or w == @range.last\r
     return [:noexist, @range] unless @plugin.send("is_#{@lang}?", w)\r
     debug "we like it"\r
-    if w < @word\r
+    if w < @word and w > @range.first\r
       @range.first.replace(w)\r
-    else\r
+      return [:in, @range]\r
+    elsif w > @word and w < @range.last\r
       @range.last.replace(w)\r
+      return [:in, @range]\r
     end\r
-    return [:in, @range]\r
+    return [:out, @range]\r
   end\r
 \r
-# TODO scoring: base score is t = ceil(100*exp(-(n-1)^2/50))+p for n attempts\r
+# TODO scoring: base score is t = ceil(100*exp(-((n-1)^2)/(50^2)))+p for n attempts\r
 #               done by p players; players that didn't win but contributed\r
 #               with a attempts will get t*a/n points\r
 \r
@@ -62,16 +64,16 @@ class AzGame
   def score\r
     n = @total_tries\r
     p = @tries.keys.length\r
-    t = (100*exp(-(n-1)**2/50**2)).ceil + p\r
+    t = (100*exp(-((n-1)**2)/(50.0**2))).ceil + p\r
     debug "Total score: #{t}"\r
     ret = Hash.new\r
     @tries.each { |k, a|\r
-      ret[k] = [t*a/n, "%d %s" % [a, a > 1 ? "tries" : "try"]]\r
+      ret[k] = [t*a/n, n_("%{count} try", "%{count} tries", a) % {:count => a}]\r
     }\r
     if @winner\r
       debug "replacing winner score of %d with %d" % [ret[@winner].first, t]\r
       tries = ret[@winner].last\r
-      ret[@winner] = [t, "winner, #{tries}"]\r
+      ret[@winner] = [t, _("winner, %{tries}") % {:tries => tries}]\r
     end\r
     return ret.sort_by { |h| h.last.first }.reverse\r
   end\r
@@ -147,33 +149,36 @@ class AzGamePlugin < Plugin
   end\r
 \r
   def word_check(m, k, word)\r
-    isit = @games[k].check(word)\r
-    case isit.first\r
-    when :bingo\r
-      m.reply _("%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, %{bold}%{player}%{bold}!") % {:bold => Bold, :underline => Underline, :word => word, :player => m.sourcenick}\r
-      @games[k].total_tries += 1\r
-      @games[k].tries[m.source] += 1\r
-      @games[k].winner = m.source\r
-      ar = @games[k].score.inject([]) { |res, kv|\r
-        res.push("%s: %d (%s)" % kv.flatten)\r
-      }\r
-      m.reply _("The game was won after %{tries} tries. Scores for this game:    %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')}\r
-      @games.delete(k)\r
-    when :out\r
-      m.reply _("%{word} is not in the range %{bold}%{range}%{bold}") % {:word => word, :bold => Bold, :range => isit.last} if m.address?\r
-    when :noexist\r
-      m.reply _("%{word} doesn't exist or is not acceptable for the game") % {:word => word}\r
-      @games[k].total_failed += 1\r
-      @games[k].failed[m.source] += 1\r
-    when :in\r
-      m.reply _("close, but no cigar. New range: %{bold}%{range}%{bold}") % {:bold => Bold, :range => isit.last}\r
-      @games[k].total_tries += 1\r
-      @games[k].tries[m.source] += 1\r
-    when :ignore\r
-      m.reply _("%{word} is already one of the range extrema: %{range}") % {:word => word, :range => isit.last} if m.address?\r
-    else\r
-      m.reply _("hm, something went wrong while verifying %{word}")\r
-    end\r
+    # Not really safe ... what happens\r
+    Thread.new {\r
+      isit = @games[k].check(word)\r
+      case isit.first\r
+      when :bingo\r
+        m.reply _("%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, %{bold}%{player}%{bold}!") % {:bold => Bold, :underline => Underline, :word => word, :player => m.sourcenick}\r
+        @games[k].total_tries += 1\r
+        @games[k].tries[m.source] += 1\r
+        @games[k].winner = m.source\r
+        ar = @games[k].score.inject([]) { |res, kv|\r
+          res.push("%s: %d (%s)" % kv.flatten)\r
+        }\r
+        m.reply _("The game was won after %{tries} tries. Scores for this game:    %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')}\r
+        @games.delete(k)\r
+      when :out\r
+        m.reply _("%{word} is not in the range %{bold}%{range}%{bold}") % {:word => word, :bold => Bold, :range => isit.last} if m.address?\r
+      when :noexist\r
+        m.reply _("%{word} doesn't exist or is not acceptable for the game") % {:word => word}\r
+        @games[k].total_failed += 1\r
+        @games[k].failed[m.source] += 1\r
+      when :in\r
+        m.reply _("close, but no cigar. New range: %{bold}%{range}%{bold}") % {:bold => Bold, :range => isit.last}\r
+        @games[k].total_tries += 1\r
+        @games[k].tries[m.source] += 1\r
+      when :ignore\r
+        m.reply _("%{word} is already one of the range extrema: %{range}") % {:word => word, :range => isit.last} if m.address?\r
+      else\r
+        m.reply _("hm, something went wrong while verifying %{word}")\r
+      end\r
+    }\r
   end\r
 \r
   def manual_word_check(m, params)\r
@@ -232,7 +237,7 @@ class AzGamePlugin < Plugin
     else\r
       f_tr = @games[k].total_failed\r
       if f_tr > 0\r
-        tr_msg = _(" (after %{total_tries} and %{invalid_tries}") %\r
+        tr_msg = _(" (after %{total_tries} and %{invalid_tries})") %\r
            { :total_tries => n_("%{count} try", "%{count} tries", tr) %\r
                              {:count => tr},\r
              :invalid_tries => n_("%{count} invalid try", "%{count} invalid tries", tr) %\r