OSDN Git Service

mk_rate supports draw games.
[shogi-server/shogi-server.git] / shogi-server
index 6a45917..7c94fae 100755 (executable)
@@ -123,7 +123,7 @@ class League
       # a current user
       player.name = hash['name']
       player.rate = hash['rate']
-      player.modified_at = hash['modified_at']
+      player.modified_at = hash['last_modified']
     end
   end
 
@@ -143,7 +143,7 @@ class League
       end
     end
     return players.collect do |id|
-      p = RatedPlayer.new
+      p = BasicPlayer.new
       p.id = id
       self.load(p)
       p
@@ -152,16 +152,29 @@ class League
 end
 
 
-class RatedPlayer
+class BasicPlayer
   # Idetifier of the player in the rating system
   attr_accessor :id
+
   # Name of the player
   attr_accessor :name
+  
+  # Password of the player, which does not include a trip
+  attr_accessor :password
+
   # Score in the rating sysem
   attr_accessor :rate
+  
   # Last timestamp when the rate was modified
   attr_accessor :modified_at
 
+
+
+  def initialize
+    @name = nil
+    @password = nil
+  end
+
   def modified_at
     @modified_at || Time.now
   end
@@ -176,12 +189,32 @@ class RatedPlayer
   def rated?
     @id != nil
   end
+
+  def simple_id
+    if @trip
+      simple_name = @name.gsub(/@.*?$/, '')
+      "%s+%s" % [simple_name, @trip[0..8]]
+    else
+      @name
+    end
+  end
+
+  ##
+  # Parses str in the LOGIN command, sets up @id and @trip
+  #
+  def set_password(str)
+    if str && !str.empty?
+      @password = str.strip
+      @id   = "%s+%s" % [@name, Digest::MD5.hexdigest(@password)]
+    else
+      @id = @password = nil
+    end
+  end
 end
 
-class Player < RatedPlayer
+class Player < BasicPlayer
   def initialize(str, socket)
-    @name = nil
-    @password = nil
+    super()
     @socket = socket
     @status = "connected"        # game_waiting -> agree_waiting -> start_waiting -> game -> finished
 
@@ -197,7 +230,7 @@ class Player < RatedPlayer
     login(str)
   end
 
-  attr_accessor :password, :socket, :status
+  attr_accessor :socket, :status
   attr_accessor :protocol, :eol, :game, :mytime, :game_name, :sente
   attr_accessor :main_thread, :writer_thread, :write_queue
   
@@ -258,9 +291,8 @@ class Player < RatedPlayer
     str =~ /([\r\n]*)$/
     @eol = $1
     str.chomp!
-    (login, @name, @password, ext) = str.split
-    @name, trip = @name.split(",") # used by rating
-    @id = trip ? "%s+%s" % [@name, Digest::MD5.hexdigest(trip)] : nil
+    (login, @name, password, ext) = str.split
+    set_password(password)
     if (ext)
       @protocol = "x1"
     else
@@ -362,9 +394,13 @@ 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" % 
-                       [p.name, p.id, p.rate, p.modified_at.strftime("%Y-%m-%d")])
+            write_safe("##[RATING] %s \t %4d @%s\n" % 
+                       [p.simple_id, p.rate, p.modified_at.strftime("%Y-%m-%d")])
           end
+          write_safe("##[RATING] +OK\n")
+        when /^%%VERSION/
+          write_safe "##[VERSION] Shogi Server revision #{Revision}\n"
+          write_safe("##[VERSION] +OK\n")
         when /^%%GAME\s*$/
           if ((@status == "connected") || (@status == "game_waiting"))
             @status = "connected"
@@ -1148,7 +1184,7 @@ class Board
       y1 = $5.to_i
       name = $6
     elsif (str =~ /^%KACHI/)
-      raise ArgumentError, "sente is null", caller unless sente
+      raise ArgumentError, "sente is null", caller if sente == nil
       if (good_kachi?(sente))
         return :kachi_win
       else
@@ -1845,12 +1881,9 @@ 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_@\-\.]{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
   end