OSDN Git Service

Fix a bug. Now it can show %%RATING even if it has no rated player.
[shogi-server/shogi-server.git] / shogi-server
index ae4c873..3b93486 100755 (executable)
@@ -117,17 +117,6 @@ class League
     return nil
   end
   
-  def save
-    @db.transaction do
-      @players.each_value do |p|
-        next unless p.id
-        @db[p.id] = {'name' => p.name, 
-                     'rate' => p.rate,
-                     'modified_at' => p.modified_at}
-      end
-    end
-  end
-
   def load(player)
     hash = search(player.id)
     if hash
@@ -162,7 +151,6 @@ class League
   end
 end
 
-#class RatedPlayer < Struct.new(:id, :name, :rate); end
 
 class RatedPlayer
   # Idetifier of the player in the rating system
@@ -184,6 +172,10 @@ class RatedPlayer
       @modified_at = Time.now
     end
   end
+
+  def rated?
+    @id != nil
+  end
 end
 
 class Player < RatedPlayer
@@ -268,7 +260,7 @@ class Player < RatedPlayer
     str.chomp!
     (login, @name, @password, ext) = str.split
     @name, trip = @name.split(",") # used by rating
-    @id = trip ? Digest::MD5.hexdigest("#{@name},#{@trip}") : nil
+    @id = trip ? "%s+%s" % [@name, Digest::MD5.hexdigest(trip)] : nil
     if (ext)
       @protocol = "x1"
     else
@@ -370,9 +362,10 @@ class Player < RatedPlayer
         when /^%%RATING/
           players = LEAGUE.rated_players
           players.sort {|a,b| b.rate <=> a.rate}.each do |p|
-            write_safe("%s (%s) \t %4d @%s\n" % 
+            write_safe("##[RATING]%s (%s) \t %4d @%s\n" % 
                        [p.name, p.id, p.rate, p.modified_at.strftime("%Y-%m-%d")])
           end
+          write_safe("##[RATING] +OK\n")
         when /^%%GAME\s*$/
           if ((@status == "connected") || (@status == "game_waiting"))
             @status = "connected"
@@ -1147,7 +1140,7 @@ class Board
     return true
   end
 
-  def handle_one_move(str, sente)
+  def handle_one_move(str, sente=nil)
     if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
       sg = $1
       x0 = $2.to_i
@@ -1156,6 +1149,7 @@ class Board
       y1 = $5.to_i
       name = $6
     elsif (str =~ /^%KACHI/)
+      raise ArgumentError, "sente is null", caller unless sente
       if (good_kachi?(sente))
         return :kachi_win
       else
@@ -1269,66 +1263,35 @@ class Board
   end
 end
 
-#################################################
-# Rating module
-#
-# http://www2.saganet.ne.jp/a-sim/mhp0726.html
-# http://www10.plala.or.jp/greenstone/content1_3.html
-class Rating
-  K = 16
-
-  def new_rate(me, you, win)
-    w = win ? 1 : 0
-    
-    if me == nil && you != nil
-      return (you + w*400).to_i
-    elsif me == nil && you == nil
-      return (1100 + w*400).to_i
-    elsif you == nil
-      return me.to_i
-    end
-    score = me + K*(w - we(me, you))
-    score.to_i
-  end
-
-  private
-
-  # win expectancy
-  def we(me, you)
-    dr = (me - you)
-    1.0 / ( 10**(-1.0*dr/400) + 1 )
-  end
-end
-
-
-
 class GameResult
+  attr_reader :players, :black, :white
+
   def initialize(p1, p2)
     @players = []
     @players << p1
     @players << p2
+    if p1.sente && !p2.sente
+      @black, @white = p1, p2
+    elsif !p1.sente && p2.sente
+      @black, @white = p2, p1
+    else
+      raise "Never reached!"
+    end
   end
 end
 
 class GameResultWin < GameResult
   attr_reader :winner, :loser
-    
+
   def initialize(winner, loser)
     super
     @winner, @loser = winner, loser
-    rate if @winner.id && @loser.id
-  end
-
-  def win?(player)
-    @winner == player
   end
 
-  def rate
-    rating = Rating.new
-    new_winner = rating.new_rate(@winner.rate, @loser.rate,  true)
-    new_loser  = rating.new_rate(@loser.rate,  @winner.rate, false)
-    @winner.rate = new_winner if new_winner
-    @loser.rate  = new_loser  if new_loser
+  def to_s
+    black_name = @black.id || @black.name
+    white_name = @white.id || @white.name
+    "%s:%s" % [black_name, white_name]
   end
 end
 
@@ -1387,6 +1350,10 @@ class Game
   attr_accessor :last_move, :current_turn
   attr_reader   :result
 
+  def rated?
+    @sente.rated? && @gote.rated?
+  end
+
   def monitoron(monitor)
     @monitors.delete(monitor)
     @monitors.push(monitor)
@@ -1433,7 +1400,6 @@ class Game
     @current_player = nil
     @next_player = nil
     LEAGUE.games.delete(@id)
-    LEAGUE.save
   end
 
   def handle_one_move(str, player)
@@ -1516,10 +1482,11 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
     @next_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
-    @result = GameResultWin.new(@current_player, @next_player)
     @fh.printf("%%TORYO\n")
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:abnormal:%s win:%s lose\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@current_player, @next_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
     end
@@ -1530,10 +1497,11 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
     @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.printf("%%TORYO\n")
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:abnormal:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
     end
@@ -1544,9 +1512,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#SENNICHITE\n#DRAW\n")
     @next_player.write_safe("#SENNICHITE\n#DRAW\n")
-    @result = GameResultDraw.new(@current_player, @next_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:sennichite:%s draw:%s draw\n", @current_player.name, @next_player.name)
+    @result = GameResultDraw.new(@current_player, @next_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #SENNICHITE\n", @id))
     end
@@ -1557,9 +1526,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#OUTE_SENNICHITE\n#LOSE\n")
     @next_player.write_safe("#OUTE_SENNICHITE\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:oute_sennichite:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #OUTE_SENNICHITE\n", @id))
     end
@@ -1570,9 +1540,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
     @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:illegal move:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
     end
@@ -1583,9 +1554,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
     @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:uchifuzume:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
     end
@@ -1596,9 +1568,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
     @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:oute_kaihimore:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
     end
@@ -1609,9 +1582,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#TIME_UP\n#LOSE\n")
     @next_player.write_safe("#TIME_UP\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:time up:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #TIME_UP\n", @id))
     end
@@ -1622,10 +1596,11 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
     @next_player.write_safe("%KACHI\n#JISHOGI\n#LOSE\n")
-    @result = GameResultWin.new(@current_player, @next_player)
     @fh.printf("%%KACHI\n")
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:kachi:%s win:%s lose\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@current_player, @next_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] %%KACHI\n", @id))
     end
@@ -1636,10 +1611,11 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
     @next_player.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
-    @result = GameResultWin(@next_player, @current_player)
     @fh.printf("%%KACHI\n")
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:illegal kachi:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] %%KACHI\n", @id))
     end
@@ -1650,10 +1626,11 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
     @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
-    @result = GameResultWin.new(@next_player, @current_player)
     @fh.printf("%%TORYO\n")
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:toryo:%s lose:%s win\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@next_player, @current_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
     end
@@ -1664,9 +1641,10 @@ class Game
     @next_player.status = "connected"
     @current_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
     @next_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
-    @result = GameResultWin.new(@current_player, @next_player)
     @fh.print(@board.to_s.gsub(/^/, "\'"))
     @fh.printf("'summary:outori:%s win:%s lose\n", @current_player.name, @next_player.name)
+    @result = GameResultWin.new(@current_player, @next_player)
+    @fh.printf("'rating:#{@result.to_s}\n") if rated?
     @monitors.each do |monitor|
       monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
     end
@@ -1868,10 +1846,11 @@ def good_game_name?(str)
   end
 end
 
+# TODO This is also checked by good_game_name?().
 def good_identifier?(str)
-  if ( ((str =~ /\A[\w\d_@\-\.]+\z/) || 
-        (str =~ /\A[\w\d_@\-\.]+,[\w\d_@\-\.]+\z/)) &&
-      (str.length < Max_Identifier_Length))
+  if str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
+    return true
+  elsif str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}},[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
     return true
   else
     return false