X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fgames%2Fazgame.rb;h=495173b34fdd89c6a7f6f4cf72ef8ca0446c5278;hb=959bc65b4852e313fbc51d0f8d097b670324b1d2;hp=7a4aa2560b98c0f4cd45254ad3fb7ded8e74168a;hpb=38fe66ef163b584656f5728d3db3a2e3ea9fd59d;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/games/azgame.rb b/data/rbot/plugins/games/azgame.rb index 7a4aa256..495173b3 100644 --- a/data/rbot/plugins/games/azgame.rb +++ b/data/rbot/plugins/games/azgame.rb @@ -40,17 +40,26 @@ class AzGame def check(word) w = word.downcase debug "checking #{w} for #{@word} in #{@range}" + # Since we're called threaded, bail out early if a winner + # was assigned already + return [:ignore, nil] if @winner return [:bingo, nil] if w == @word return [:out, @range] if w < @range.first or w > @range.last return [:ignore, @range] if w == @range.first or w == @range.last + # This is potentially slow (for languages that check online) return [:noexist, @range] unless @plugin.send("is_#{@lang}?", w) debug "we like it" - if w < @word + # Check again if there was a winner in the mean time, + # and bail out if there was + return [:ignore, nil] if @winner + if w < @word and w > @range.first @range.first.replace(w) - else + return [:in, @range] + elsif w > @word and w < @range.last @range.last.replace(w) + return [:in, @range] end - return [:in, @range] + return [:out, @range] end # TODO scoring: base score is t = ceil(100*exp(-((n-1)^2)/(50^2)))+p for n attempts @@ -112,7 +121,7 @@ class AzGamePlugin < Plugin :listener => /^[a-z]+$/ }, } - + japanese_wordlist = "#{@bot.botclass}/azgame/wordlist-japanese" if File.exist?(japanese_wordlist) words = File.readlines(japanese_wordlist) \ @@ -147,33 +156,40 @@ class AzGamePlugin < Plugin end def word_check(m, k, word) - isit = @games[k].check(word) - case isit.first - when :bingo - m.reply _("%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, %{bold}%{player}%{bold}!") % {:bold => Bold, :underline => Underline, :word => word, :player => m.sourcenick} - @games[k].total_tries += 1 - @games[k].tries[m.source] += 1 - @games[k].winner = m.source - ar = @games[k].score.inject([]) { |res, kv| - res.push("%s: %d (%s)" % kv.flatten) - } - m.reply _("The game was won after %{tries} tries. Scores for this game: %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')} - @games.delete(k) - when :out - m.reply _("%{word} is not in the range %{bold}%{range}%{bold}") % {:word => word, :bold => Bold, :range => isit.last} if m.address? - when :noexist - m.reply _("%{word} doesn't exist or is not acceptable for the game") % {:word => word} - @games[k].total_failed += 1 - @games[k].failed[m.source] += 1 - when :in - m.reply _("close, but no cigar. New range: %{bold}%{range}%{bold}") % {:bold => Bold, :range => isit.last} - @games[k].total_tries += 1 - @games[k].tries[m.source] += 1 - when :ignore - m.reply _("%{word} is already one of the range extrema: %{range}") % {:word => word, :range => isit.last} if m.address? - else - m.reply _("hm, something went wrong while verifying %{word}") - end + # Not really safe ... what happens + Thread.new { + isit = @games[k].check(word) + case isit.first + when :bingo + m.reply _("%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, %{bold}%{player}%{bold}!") % {:bold => Bold, :underline => Underline, :word => word, :player => m.sourcenick} + @games[k].total_tries += 1 + @games[k].tries[m.source] += 1 + @games[k].winner = m.source + ar = @games[k].score.inject([]) { |res, kv| + res.push("%s: %d (%s)" % kv.flatten) + } + m.reply _("The game was won after %{tries} tries. Scores for this game: %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')} + @games.delete(k) + when :out + m.reply _("%{word} is not in the range %{bold}%{range}%{bold}") % {:word => word, :bold => Bold, :range => isit.last} if m.address? + when :noexist + # bail out early if the game was won in the mean time + return if !@games[k] or @games[k].winner + m.reply _("%{word} doesn't exist or is not acceptable for the game") % {:word => word} + @games[k].total_failed += 1 + @games[k].failed[m.source] += 1 + when :in + # bail out early if the game was won in the mean time + return if !@games[k] or @games[k].winner + m.reply _("close, but no cigar. New range: %{bold}%{range}%{bold}") % {:bold => Bold, :range => isit.last} + @games[k].total_tries += 1 + @games[k].tries[m.source] += 1 + when :ignore + m.reply _("%{word} is already one of the range extrema: %{range}") % {:word => word, :range => isit.last} if m.address? + else + m.reply _("hm, something went wrong while verifying %{word}") + end + } end def manual_word_check(m, params) @@ -238,7 +254,7 @@ class AzGamePlugin < Plugin :invalid_tries => n_("%{count} invalid try", "%{count} invalid tries", tr) % {:count => f_tr} } else - tr_msg = _(" (after %{total_tries}") % + tr_msg = _(" (after %{total_tries})") % { :total_tries => n_("%{count} try", "%{count} tries", tr) % {:count => tr}} end @@ -323,12 +339,12 @@ class AzGamePlugin < Plugin def is_japanese?(word) @rules[:japanese][:list].include?(word) end - + # return integer between min and max, inclusive def rand_between(min, max) rand(max - min + 1) + min end - + def random_pick_japanese(min=nil, max=nil) rules = @rules[:japanese] min = rules[:first] if min.nil_or_empty? @@ -351,7 +367,7 @@ class AzGamePlugin < Plugin wc = @wordcache[:italian] return true if wc.key?(word.to_sym) rules = @rules[:italian] - p = @bot.httputil.get(rules[:wapurl] % word) + p = @bot.httputil.get(rules[:wapurl] % word, :open_timeout => 60, :read_timeout => 60) if not p error "could not connect!" return false