]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Initial attempt at scoring in azgame. scoring isn't kept yet, and it's calculated...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 25 Jan 2007 23:08:50 +0000 (23:08 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 25 Jan 2007 23:08:50 +0000 (23:08 +0000)
data/rbot/plugins/azgame.rb

index 9d2cbe9510fe6988ec7ff352d36df7737f217ba5..9721d8b8b77c3ed398bb5cb2c6dacc7a6c80b5c0 100644 (file)
@@ -6,9 +6,6 @@
 # (C) 2006 Giuseppe Bilotta\r
 #\r
 # TODO allow manual addition of words\r
-# TODO scoring: base score is t = ceil(100*exp(-(n-1)^2/50^))+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
 AZ_RULES = {\r
   :italian => {\r
@@ -30,11 +27,15 @@ AZ_RULES = {
 class AzGame\r
 \r
   attr_reader :range, :word\r
+  attr_accessor :tries, :total_tries, :winner\r
   def initialize(plugin, lang, word)\r
     @plugin = plugin\r
     @lang = lang.to_sym\r
     @word = word.downcase\r
     @range = [AZ_RULES[lang][:first].dup, AZ_RULES[lang][:last].dup]\r
+    @total_tries = 0\r
+    @tries = Hash.new(0)\r
+    @winner = nil\r
     def @range.to_s\r
       return "%s -- %s" % self\r
     end\r
@@ -56,6 +57,28 @@ class AzGame
     return [:in, @range]\r
   end\r
 \r
+# TODO scoring: base score is t = ceil(100*exp(-(n-1)^2/50))+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
+  include Math\r
+\r
+  def score\r
+    n = @total_tries\r
+    p = @tries.keys.length\r
+    t = (100*exp(-(n-1)**2/50**2)).ceil + p\r
+    debug "Total score: #{t}"\r
+    ret = Hash.new\r
+    @tries.each { |k, a|\r
+      ret[k] = [t*a/n, "%d tries" % a]\r
+    }\r
+    if @winner\r
+      debug "replacing winner score of %d with %d" % [ret[@winner].first, t]\r
+      ret[@winner] = [t, "winner"]\r
+    end\r
+    return ret.sort_by { |h| h.last.first }\r
+  end\r
+\r
 end\r
 \r
 class AzGamePlugin < Plugin\r
@@ -96,6 +119,13 @@ class AzGamePlugin < Plugin
     case isit.first\r
     when :bingo\r
       m.reply "#{Bold}BINGO!#{Bold}: the word was #{Underline}#{word}#{Underline}. Congrats, #{Bold}#{m.sourcenick}#{Bold}!"\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 #{@games[k].total_tries} tries. Scores for this game:    #{ar.join('; ')}"\r
       @games.delete(k)\r
     when :out\r
       m.reply "#{word} is not in the range #{Bold}#{isit.last}#{Bold}" if m.address?\r
@@ -103,6 +133,8 @@ class AzGamePlugin < Plugin
       m.reply "#{word} doesn't exist or is not acceptable for the game"\r
     when :in\r
       m.reply "close, but no cigar. New range: #{Bold}#{isit.last}#{Bold}"\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: #{isit.last}" if m.address?\r
     else\r
@@ -129,6 +161,10 @@ class AzGamePlugin < Plugin
     k = m.channel.downcase.to_s # to_sym?\r
     if @games.key?(k)\r
       m.reply "the word in #{Bold}#{@games[k].range}#{Bold} was:   #{Bold}#{@games[k].word}"\r
+      ar = @games[k].score.inject([]) { |res, kv|\r
+        res.push("%s: %d (%s)" % kv.flatten)\r
+      }\r
+      m.reply "The game was cancelled after #{@games[k].total_tries} tries. Scores for this game would have been:    #{ar.join('; ')}"\r
       @games.delete(k)\r
     else\r
       m.reply "no A-Z game running in this channel ..."\r
@@ -155,7 +191,8 @@ class AzGamePlugin < Plugin
       m.reply "got it!"\r
       @games[k] = AzGame.new(self, lang, word)\r
     end\r
-    m.reply "A-Z: #{Bold}#{@games[k].range}#{Bold}"\r
+    tr = @games[k].total_tries\r
+    m.reply "A-Z: #{Bold}#{@games[k].range}#{Bold}" + (tr > 0 ? "(after #{tr} tries)" : "")\r
     return\r
   end\r
 \r