3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 require 'shogi_server/command'
22 module ShogiServer # for a namespace
33 @last_game_win = false
40 # Idetifier of the player in the rating system
41 attr_accessor :player_id
46 # Password of the player, which does not include a trip
47 attr_accessor :password
49 # Score in the rating sysem
52 # Estimated rate for unrated player (rate == 0)
53 # But this value is not persisted and cleared when player logs off.
54 attr_accessor :estimated_rate
56 # Number of games for win and loss in the rating system
57 attr_accessor :win, :loss
59 # Group in the rating system
60 attr_accessor :rating_group
62 # Last timestamp when the rate was modified
63 attr_accessor :modified_at
65 # Whether win the previous game or not
66 attr_accessor :last_game_win
68 # true for Sente; false for Gote
72 attr_accessor :game_name
75 return [%r!_human$!, %r!_human@!].any? do |re|
85 @modified_at || Time.now
91 @modified_at = Time.now
100 return @last_game_win
105 simple_name = @name.gsub(/@.*?$/, '')
106 "%s+%s" % [simple_name, @trip[0..8]]
113 # Parses str in the LOGIN command, sets up @player_id and @trip
115 def set_password(str)
116 if str && !str.empty?
117 @password = str.strip
118 @player_id = "%s+%s" % [@name, Digest::MD5.hexdigest(@password)]
120 @player_id = @password = nil
124 def set_sente_from_str(str)
126 when "+" then @sente = true
127 when "-" then @sente = false
136 class Player < BasicPlayer
137 WRITE_THREAD_WATCH_INTERVAL = 20 # sec
138 def initialize(str, socket, eol=nil)
141 @status = "connected" # game_waiting -> agree_waiting -> start_waiting -> game -> finished
143 @protocol = nil # CSA or x1
144 @eol = eol || "\m" # favorite eol code
146 @mytime = 0 # set in start method also
148 @main_thread = Thread::current
149 @write_queue = ShogiServer::TimeoutQueue.new(WRITE_THREAD_WATCH_INTERVAL)
154 attr_accessor :socket, :status
155 attr_accessor :protocol, :eol, :game, :mytime
156 attr_accessor :main_thread
157 attr_reader :socket_buffer
159 def setup_logger(dir)
160 log_file = File.join(dir, "%s.log" % [simple_player_id])
161 @player_logger = Logger.new(log_file, 'daily')
162 @player_logger.formatter = ShogiServer::Formatter.new
163 @player_logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO
164 @player_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
167 def log(level, direction, message)
168 return unless @player_logger
172 str = "IN: %s" % [str]
174 str = "OUT: %s" % [str]
176 str = "UNKNOWN DIRECTION: %s %s" % [direction, str]
180 @player_logger.debug(str)
182 @player_logger.info(str)
184 @player_logger.warn(str)
186 @player_logger.error(str)
188 @player_logger.debug("UNKNOWN LEVEL: %s %s" % [level, str])
190 rescue Exception => ex
191 log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
195 log_message(sprintf("user %s killed", @name))
200 Thread::kill(@main_thread) if @main_thread
201 Thread::kill(@write_thread) if @write_thread
205 if (@status != "finished")
207 log_message(sprintf("user %s finish", @name))
209 log_debug("Terminating %s's write thread..." % [@name])
210 if @write_thread && @write_thread.alive?
212 Thread.pass # help the write_thread to terminate
216 log_message(sprintf("user %s finish failed", @name))
221 def start_write_thread
222 @write_thread = Thread.start do
224 while !@socket.closed?
226 str = @write_queue.deq
228 log_debug("%s's write thread terminated" % [@name])
232 log_debug("%s's write queue timed out. Try again..." % [@name])
236 if r = select(nil, [@socket], nil, 20)
237 r[1].first.write(str)
238 log(:info, :out, str)
240 log_error("Gave a try to send a message to #{@name}, but it timed out.")
243 rescue Exception => ex
244 log_error("Failed to send a message to #{@name}. #{ex.class}: #{ex.message}\t#{ex.backtrace[0]}")
248 log_error("%s's socket closed." % [@name]) if @socket.closed?
249 log_message("At least %d messages are not sent to the client." %
250 [@write_queue.get_messages.size])
255 # Wait for the write thread to finish.
256 # This method should be called just before this instance will be freed.
258 def wait_write_thread_finish(msec=1000)
259 while msec > 0 && @write_thread && @write_thread.alive?
260 sleep 0.1; msec -= 0.1
262 @player_logger.close if @player_logger
266 # Note that sending a message is included in the giant lock.
269 @write_queue.enq(str)
273 if ["game_waiting", "start_waiting", "agree_waiting", "game"].include?(status)
275 return sprintf("%s %s %s %s +", rated? ? @player_id : @name, @protocol, @status, @game_name)
276 elsif (@sente == false)
277 return sprintf("%s %s %s %s -", rated? ? @player_id : @name, @protocol, @status, @game_name)
278 elsif (@sente == nil)
279 return sprintf("%s %s %s %s *", rated? ? @player_id : @name, @protocol, @status, @game_name)
282 return sprintf("%s %s %s", rated? ? @player_id : @name, @protocol, @status)
286 def run(csa_1st_str=nil)
287 while ( csa_1st_str ||
288 str = gets_safe(@socket, (@socket_buffer.empty? ? Default_Timeout : 1)) )
290 log(:info, :in, str) if str && str.instance_of?(String)
293 if !@write_thread.alive?
294 log_error("%s's write thread is dead. Aborting..." % [@name])
297 if (@game && @game.turn?(self))
298 @socket_buffer << str
299 str = @socket_buffer.shift
301 log_debug("%s (%s)" % [str, @socket_buffer.map {|a| String === a ? a.strip : a }.join(",")])
308 if (@status == "finished")
311 str.chomp! if (str.class == String) # may be strip! ?
313 delay = Time.now - time
315 log_warning("Detected a long delay: %.2f sec" % [delay])
317 cmd = ShogiServer::Command.factory(str, self, time)
325 log_error("Detected a wrong return value for %s" % [cmd])
332 log_warning("%s's socket was suddenly closed" % [@name])