## $Id$
## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
-## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
+## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
require 'shogi_server/league/floodgate'
require 'shogi_server/game_result'
+require 'shogi_server/time_clock'
require 'shogi_server/util'
module ShogiServer # for a namespace
end
end
+ TimeControlParams = Struct.new(:total_time, :byoyomi, :fischer, :stop_watch, :error)
- # options may or may not have follwing keys: :sente_time, :gote_time; they
- # are used for %%FORK command to set remaining times at the restart.
+ # Parse time related parts of a game name.
#
- def initialize(game_name, player0, player1, board, options={})
+ # Return an array of a total allotted time, byoyomi seconds and fischer
+ # seconds; if it fails to parse, return nil.
+ def Game.parse_time(game_name)
+ ret = TimeControlParams.new(0, 0, 0, false, false)
+
+ if (game_name =~ /-(\d+)-(\d+F?)$/)
+ ret[:total_time] = $1.to_i
+ tmp = $2
+ if tmp =~ /^0\d/
+ ret[:stop_watch] = true
+ end
+ if tmp[-1] == "F"
+ ret[:fischer] = tmp.chop.to_i
+ else
+ ret[:byoyomi] = tmp.to_i
+ end
+ return ret
+ end
+
+ ret[:error] = true
+ return ret
+ end
+
+
+ def initialize(game_name, player0, player1, board)
@monitors = Array::new # array of MonitorHandler*
@game_name = game_name
- if (@game_name =~ /-(\d+)-(\d+)$/)
- @total_time = $1.to_i
- @byoyomi = $2.to_i
- end
+ time_map = Game.parse_time @game_name
+ @total_time = time_map[:total_time]
+ @byoyomi = time_map[:byoyomi]
+ @fischer = time_map[:fischer]
+ @time_clock = TimeClock::factory(board.least_time_per_move, @game_name)
if (player0.sente)
@sente, @gote = player0, player1
@sente.game = self
@gote.game = self
- @last_move = @board.initial_moves.empty? ? "" : "%s,T1" % [@board.initial_moves.last]
+ @last_move = ""
+ unless @board.initial_moves.empty?
+ last_move = @board.initial_moves.last
+ case last_move
+ when Array
+ @last_move = last_move.join(",")
+ when String
+ @last_move = "%s,T1" % [last_move]
+ end
+ end
@current_turn = @board.initial_moves.size
@sente.status = "agree_waiting"
$league.games[@game_id] = self
log_message(sprintf("game created %s", @game_id))
+ log_message(" " + @time_clock.to_s)
@start_time = nil
@fh = open(@logfile, "w")
@fh.sync = true
@result = nil
- @options = options.dup
-
propose
end
- attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
+ attr_accessor :game_name, :total_time, :byoyomi, :fischer, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors, :time_clock
attr_accessor :last_move, :current_turn
attr_reader :result, :prepared_time
return nil
end
- finish_flag = true
@end_time = end_time
- t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
-
+ finish_flag = true
move_status = nil
- if ((@current_player.mytime - t <= -@byoyomi) &&
- ((@total_time > 0) || (@byoyomi > 0)))
+
+ if (@time_clock.timeout?(@current_player, @start_time, @end_time))
status = :timeout
elsif (str == :timeout)
return false # time isn't expired. players aren't swapped. continue game
else
- @current_player.mytime -= t
- if (@current_player.mytime < 0)
- @current_player.mytime = 0
- end
-
+ t = @time_clock.process_time(@current_player, @start_time, @end_time)
+ log_info(sprintf("+++ DEBUG %s consumed %d sec. remain %d sec",
+ @current_player.name, t, @current_player.mytime))
move_status = @board.handle_one_move(str, @sente == @current_player)
# log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
elsif (move_status == :oute_kaihimore)
# the current player losed
@result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
+ elsif (move_status == :max_moves)
+ @result = GameResultMaxMovesDraw.new(self, @current_player, @next_player)
else
finish_flag = false
end
@gote.status = "game"
@sente.write_safe(sprintf("START:%s\n", @game_id))
@gote.write_safe(sprintf("START:%s\n", @game_id))
- @sente.mytime = @options[:sente_time] || @total_time
- @gote.mytime = @options[:gote_time] || @total_time
+ @sente.mytime = @total_time
+ @gote.mytime = @total_time
@start_time = Time.now
end
@fh.puts("V2")
@fh.puts("N+#{@sente.name}")
@fh.puts("N-#{@gote.name}")
+ @fh.puts("'Max_Moves:#{@board.max_moves}")
+ @fh.puts("'Least_Time_Per_Move:#{@board.least_time_per_move}")
+ if @fischer > 0
+ @fh.puts("'Increment:#{@fischer}")
+ end
@fh.puts("$EVENT:#{@game_id}")
@sente.write_safe(propose_message("+"))
black_name = @sente.rated? ? @sente.player_id : @sente.name
white_name = @gote.rated? ? @gote.player_id : @gote.name
@fh.puts("'rating:%s:%s" % [black_name, white_name])
+ if @sente.rated? && @sente.rate != 0
+ @fh.puts("'black_rate:%s:%s" % [@sente.player_id, @sente.rate])
+ end
+ if @gote.rated? && @gote.rate != 0
+ @fh.puts("'white_rate:%s:%s" % [@gote.player_id, @gote.rate])
+ end
end
unless @board.initial_moves.empty?
@fh.puts "'buoy game starting with %d moves" % [@board.initial_moves.size]
@board.initial_moves.each do |move|
- @fh.puts move
- @fh.puts "T1"
+ case move
+ when Array
+ @fh.puts move[0]
+ @fh.puts move[1]
+ when String
+ @fh.puts move
+ @fh.puts "T1"
+ end
end
end
end
def show()
str0 = <<EOM
BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
Protocol_Mode:Server
Format:Shogi 1.0
Declaration:Jishogi 1.1
Name-:#{@gote.name}
Rematch_On_Draw:NO
To_Move:+
+Max_Moves:#{@board.max_moves}
BEGIN Time
-Time_Unit:1sec
+Time_Unit:#{@time_clock.time_unit}
Total_Time:#{@total_time}
Byoyomi:#{@byoyomi}
-Least_Time_Per_Move:#{Least_Time_Per_Move}
+Increment:#{@fischer}
+Least_Time_Per_Move:#{@board.least_time_per_move}
Remaining_Time+:#{@sente.mytime}
Remaining_Time-:#{@gote.mytime}
Last_Move:#{@last_move}
end
def propose_message(sg_flag)
- time = @total_time
- if @options[:sente_time] && sg_flag == "+"
- time = @options[:sente_time]
- elsif @options[:gote_time] && sg_flag == "-"
- time = @options[:gote_time]
- end
-
str = <<EOM
BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
Protocol_Mode:Server
Format:Shogi 1.0
Declaration:Jishogi 1.1
Your_Turn:#{sg_flag}
Rematch_On_Draw:NO
To_Move:#{@board.teban ? "+" : "-"}
+Max_Moves:#{@board.max_moves}
BEGIN Time
-Time_Unit:1sec
-Total_Time:#{time}
+Time_Unit:#{@time_clock.time_unit}
+Total_Time:#{@total_time}
Byoyomi:#{@byoyomi}
-Least_Time_Per_Move:#{Least_Time_Per_Move}
+Increment:#{@fischer}
+Least_Time_Per_Move:#{@board.least_time_per_move}
END Time
BEGIN Position
#{@board.initial_string.chomp}
-#{@board.initial_moves.collect {|m| m + ",T1"}.join("\n")}
+#{@board.initial_moves.collect do |m|
+ case m
+ when Array
+ m.join(",")
+ when String
+ m + ",T1"
+ end
+end.join("\n")}
END Position
END Game_Summary
EOM
return false
end
- # Read the .csa file and returns an array of moves.
- # ex. ["+7776FU", "-3334FU"]
+ # Read the .csa file and returns an array of moves and times.
+ # ex. [["+7776FU","T2"], ["-3334FU","T5"]]
#
def read_moves
ret = []
IO.foreach(@logfile) do |line|
if /^[\+\-]\d{4}[A-Z]{2}/ =~ line
- ret << line.chomp
+ ret << [line.chomp]
+ elsif /^T\d*/ =~ line
+ ret[-1] << line.chomp
end
end
return ret