]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
azgame: thread word check so that online checking doesn't block
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 29 Oct 2007 23:17:53 +0000 (23:17 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 29 Oct 2007 23:17:53 +0000 (23:17 +0000)
data/rbot/plugins/games/azgame.rb

index 7a4aa2560b98c0f4cd45254ad3fb7ded8e74168a..12cdf724196d8f9566403d3cf793603d67c5ef65 100644 (file)
@@ -45,12 +45,14 @@ 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^2)))+p for n attempts\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