X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=blobdiff_plain;f=shogi_server%2Fgame.rb;h=50bdbaaa8c52125069ea4aab1f1adb50b1a34c2b;hp=0134ddd36a29e01d4763e93928327e3182d91e63;hb=ec11b3359a4258d7808036231f959463e8cb608e;hpb=b378ba13c26b11254d3cef81d6d6a26cb824e43c;ds=sidebyside diff --git a/shogi_server/game.rb b/shogi_server/game.rb index 0134ddd..50bdbaa 100644 --- a/shogi_server/game.rb +++ b/shogi_server/game.rb @@ -1,7 +1,7 @@ ## $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 @@ -18,242 +18,12 @@ ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA require 'shogi_server/league/floodgate' -require 'observer' +require 'shogi_server/game_result' +require 'shogi_server/time_clock' +require 'shogi_server/util' module ShogiServer # for a namespace -# MonitorObserver obserers GameResult to send messages to the monotors -# watching the game -# -class MonitorObserver - def update(game_result) - game_result.game.each_monitor do |monitor| - monitor.write_safe("##[MONITOR][%s] %s\n" % [game_result.game.game_id, game_result.result_type]) - end - end -end - -# Base class for a game result -# -class GameResult - include Observable - - # Game object - attr_reader :game - # Array of players - attr_reader :players - # Black player object - attr_reader :black - # White plyer object - attr_reader :white - # Command to send monitors such as '%TORYO' etc... - attr_reader :result_type - - def initialize(game, p1, p2) - @game = game - @players = [p1, p2] - if p1.sente && !p2.sente - @black, @white = p1, p2 - elsif !p1.sente && p2.sente - @black, @white = p2, p1 - else - raise "Never reached!" - end - @players.each do |player| - player.status = "connected" - end - @result_type = "" - - regist_observers - end - - def regist_observers - add_observer MonitorObserver.new - - if League::Floodgate.game_name?(@game.game_name) && - @game.sente.player_id && - @game.gote.player_id && - $options["floodgate-history"] - add_observer League::Floodgate::History.factory - end - end - - def process - raise "Implement me!" - end - - def notify - changed - notify_observers(self) - end - - def log(str) - @game.log_game(str) - end - - def log_board - log(@game.board.to_s.gsub(/^/, "\'").chomp) - end - -end - -class GameResultWin < GameResult - attr_reader :winner, :loser - - def initialize(game, winner, loser) - super - @winner, @loser = winner, loser - @winner.last_game_win = true - @loser.last_game_win = false - end - - def log_summary(type) - log_board - - black_result = white_result = "" - if @black == @winner - black_result = "win" - white_result = "lose" - else - black_result = "lose" - white_result = "win" - end - log("'summary:%s:%s %s:%s %s" % [type, - @black.name, black_result, - @white.name, white_result]) - - end -end - -class GameResultAbnormalWin < GameResultWin - def process - @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n") - @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n") - log("%TORYO") - log_summary("abnormal") - @result_type = "%TORYO" - notify - end -end - -class GameResultTimeoutWin < GameResultWin - def process - @winner.write_safe("#TIME_UP\n#WIN\n") - @loser.write_safe( "#TIME_UP\n#LOSE\n") - log_summary("time up") - @result_type = "#TIME_UP" - notify - end -end - -# A player declares (successful) Kachi -class GameResultKachiWin < GameResultWin - def process - @winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n") - @loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n") - log("%KACHI") - log_summary("kachi") - @result_type = "%KACHI" - notify - end -end - -# A player declares wrong Kachi -class GameResultIllegalKachiWin < GameResultWin - def process - @winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n") - @loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n") - log("%KACHI") - log_summary("illegal kachi") - @result_type = "%KACHI" - notify - end -end - -class GameResultIllegalWin < GameResultWin - def initialize(game, winner, loser, cause) - super(game, winner, loser) - @cause = cause - end - - def process - @winner.write_safe("#ILLEGAL_MOVE\n#WIN\n") - @loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n") - log_summary(@cause) - @result_type = "#ILLEGAL_MOVE" - notify - end -end - -class GameResultIllegalMoveWin < GameResultIllegalWin - def initialize(game, winner, loser) - super(game, winner, loser, "illegal move") - end -end - -class GameResultUchifuzumeWin < GameResultIllegalWin - def initialize(game, winner, loser) - super(game, winner, loser, "uchifuzume") - end -end - -class GameResultOuteKaihiMoreWin < GameResultIllegalWin - def initialize(game, winner, loser) - super(game, winner, loser, "oute_kaihimore") - end -end - -class GameResultOutoriWin < GameResultWin - def initialize(game, winner, loser) - super(game, winner, loser) - end -end - -class GameResultToryoWin < GameResultWin - def process - @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n") - @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n") - log("%TORYO") - log_summary("toryo") - @result_type = "%TORYO" - notify - end -end - -class GameResultOuteSennichiteWin < GameResultWin - def process - @winner.write_safe("#OUTE_SENNICHITE\n#WIN\n") - @loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n") - log_summary("oute_sennichite") - @result_type = "#OUTE_SENNICHITE" - notify - end -end - -class GameResultDraw < GameResult - def initialize(game, p1, p2) - super - p1.last_game_win = false - p2.last_game_win = false - end - - def log_summary(type) - log_board - log("'summary:%s:%s draw:%s draw" % [type, @black.name, @white.name]) - end -end - -class GameResultSennichiteDraw < GameResultDraw - def process - @players.each do |player| - player.write_safe("#SENNICHITE\n#DRAW\n") - end - log_summary("sennichite") - @result_type = "#SENNICHITE" - notify - end -end - class Game # When this duration passes after this object instanciated (i.e. # the agree_waiting or start_waiting state lasts too long), @@ -262,12 +32,46 @@ class Game @@mutex = Mutex.new @@time = 0 - def initialize(game_name, player0, player1) - @monitors = Array::new + + # Decide an actual turn of each player according to their turn preferences. + # p2 is a rival player of the p1 player. + # p1_sente_string must be "*", "+" or "-". + # After this call, the sente value of each player is always true or false, not + # nil. + # + def Game.decide_turns(p1, p1_sente_string, p2) + if ((p1_sente_string == "*") && (p2.sente == nil)) + if (rand(2) == 0) + p1.sente = true + p2.sente = false + else + p1.sente = false + p2.sente = true + end + elsif (p2.sente == true) # rival has higher priority + p1.sente = false + elsif (p2.sente == false) + p1.sente = true + elsif (p1_sente_string == "+") + p1.sente = true + p2.sente = false + elsif (p1_sente_string == "-") + p1.sente = false + p2.sente = true + else + ## never reached + end + 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 + + @time_clock = TimeClock::factory(Least_Time_Per_Move, @game_name) end if (player0.sente) @@ -277,12 +81,26 @@ class Game end @sente.socket_buffer.clear @gote.socket_buffer.clear - @current_player, @next_player = @sente, @gote + @board = board + if @board.teban + @current_player, @next_player = @sente, @gote + else + @current_player, @next_player = @gote, @sente + end @sente.game = self @gote.game = self @last_move = "" - @current_turn = 0 + 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" @gote.status = "agree_waiting" @@ -298,15 +116,14 @@ class Game @prepared_time.strftime("%Y"), @prepared_time.strftime("%m"), @prepared_time.strftime("%d")) - FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name) @logfile = File.join(log_dir_name, @game_id + ".csa") + Mkdir.mkdir_for(@logfile) $league.games[@game_id] = self log_message(sprintf("game created %s", @game_id)) + log_message(" " + @time_clock.to_s) - @board = Board::new - @board.initial @start_time = nil @fh = open(@logfile, "w") @fh.sync = true @@ -314,10 +131,13 @@ class Game 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, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors, :time_clock attr_accessor :last_move, :current_turn attr_reader :result, :prepared_time + # Path of a log file for this game. + attr_reader :logfile + def rated? @sente.rated? && @gote.rated? end @@ -326,18 +146,18 @@ class Game return player.status == "game" && @current_player == player end - def monitoron(monitor) - @monitors.delete(monitor) - @monitors.push(monitor) + def monitoron(monitor_handler) + monitoroff(monitor_handler) + @monitors.push(monitor_handler) end - def monitoroff(monitor) - @monitors.delete(monitor) + def monitoroff(monitor_handler) + @monitors.delete_if {|mon| mon == monitor_handler} end def each_monitor - @monitors.each do |monitor| - yield monitor + @monitors.each do |monitor_handler| + yield monitor_handler end end @@ -364,15 +184,20 @@ class Game end if (@current_player == killer) - result = GameResultAbnormalWin.new(self, @next_player, @current_player) - result.process + @result = GameResultAbnormalWin.new(self, @next_player, @current_player) + @result.process finish end end def finish log_message(sprintf("game finished %s", @game_id)) - @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S")) + + # In a case where a player in agree_waiting or start_waiting status is + # rejected, a GameResult object is not yet instanciated. + # See test/TC_before_agree.rb. + end_time = @result ? @result.end_time : Time.now + @fh.printf("'$END_TIME:%s\n", end_time.strftime("%Y/%m/%d %H:%M:%S")) @fh.close @sente.game = nil @@ -395,7 +220,7 @@ class Game end # class Game - def handle_one_move(str, player) + def handle_one_move(str, player, end_time) unless turn?(player) return false if str == :timeout @@ -406,90 +231,92 @@ class Game return nil end + @end_time = end_time finish_flag = true - @end_time = Time::new - t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max - 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) 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]) if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status) @fh.printf("'ILLEGAL_MOVE(%s)\n", str) else - if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status) + if :toryo != move_status # Thinking time includes network traffic @sente.write_safe(sprintf("%s,T%d\n", str, t)) @gote.write_safe(sprintf("%s,T%d\n", str, t)) @fh.printf("%s\nT%d\n", str, t) @last_move = sprintf("%s,T%d", str, t) @current_turn += 1 - end - @monitors.each do |monitor| - monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] ")) - monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id)) - end - end + @monitors.each do |monitor_handler| + monitor_handler.write_one_move(@game_id, self) + end + end # if + # if move_status is :toryo then a GameResult message will be sent to monitors + end # if end - result = nil + @result = nil if (@next_player.status != "game") # rival is logout or disconnected - result = GameResultAbnormalWin.new(self, @current_player, @next_player) + @result = GameResultAbnormalWin.new(self, @current_player, @next_player) elsif (status == :timeout) # current_player losed - result = GameResultTimeoutWin.new(self, @next_player, @current_player) + @result = GameResultTimeoutWin.new(self, @next_player, @current_player) elsif (move_status == :illegal) - result = GameResultIllegalMoveWin.new(self, @next_player, @current_player) + @result = GameResultIllegalMoveWin.new(self, @next_player, @current_player) elsif (move_status == :kachi_win) - result = GameResultKachiWin.new(self, @current_player, @next_player) + @result = GameResultKachiWin.new(self, @current_player, @next_player) elsif (move_status == :kachi_lose) - result = GameResultIllegalKachiWin.new(self, @next_player, @current_player) + @result = GameResultIllegalKachiWin.new(self, @next_player, @current_player) elsif (move_status == :toryo) - result = GameResultToryoWin.new(self, @next_player, @current_player) + @result = GameResultToryoWin.new(self, @next_player, @current_player) elsif (move_status == :outori) # The current player captures the next player's king - result = GameResultOutoriWin.new(self, @current_player, @next_player) + @result = GameResultOutoriWin.new(self, @current_player, @next_player) elsif (move_status == :oute_sennichite_sente_lose) - result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking + @result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking elsif (move_status == :oute_sennichite_gote_lose) - result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking + @result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking elsif (move_status == :sennichite) - result = GameResultSennichiteDraw.new(self, @current_player, @next_player) + @result = GameResultSennichiteDraw.new(self, @current_player, @next_player) elsif (move_status == :uchifuzume) # the current player losed - result = GameResultUchifuzumeWin.new(self, @next_player, @current_player) + @result = GameResultUchifuzumeWin.new(self, @next_player, @current_player) elsif (move_status == :oute_kaihimore) # the current player losed - result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player) + @result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player) else finish_flag = false end - result.process if result + @result.process if @result finish() if finish_flag @current_player, @next_player = @next_player, @current_player - @start_time = Time::new + @start_time = Time.now return finish_flag end + def is_startable_status? + return (@sente && @gote && + (@sente.status == "start_waiting") && + (@gote.status == "start_waiting")) + end + def start log_message(sprintf("game started %s", @game_id)) + @sente.status = "game" + @gote.status = "game" @sente.write_safe(sprintf("START:%s\n", @game_id)) @gote.write_safe(sprintf("START:%s\n", @game_id)) @sente.mytime = @total_time @gote.mytime = @total_time - @start_time = Time::new + @start_time = Time.now end def propose @@ -501,24 +328,32 @@ class Game @sente.write_safe(propose_message("+")) @gote.write_safe(propose_message("-")) - now = Time::new.strftime("%Y/%m/%d %H:%M:%S") + now = Time.now.strftime("%Y/%m/%d %H:%M:%S") @fh.puts("$START_TIME:#{now}") - @fh.print <