-class Game
- def initialize(game_name, player0, player1)
- @monitors = Array::new
- @game_name = game_name
- if (@game_name =~ /-(\d+)-(\d+)$/)
- @total_time = $1.to_i
- @byoyomi = $2.to_i
- end
-
- if (player0.sente)
- @sente = player0
- @gote = player1
- else
- @sente = player1
- @gote = player0
- end
- @current_player = @sente
- @next_player = @gote
-
- @sente.game = self
- @gote.game = self
-
- @last_move = ""
- @current_turn = 0
-
- @sente.status = "agree_waiting"
- @gote.status = "agree_waiting"
- @id = sprintf("%s+%s+%s+%s+%s",
- LEAGUE.event, @game_name, @sente.name, @gote.name,
- Time::new.strftime("%Y%m%d%H%M%S"))
-
- LEAGUE.games[@id] = self
-
-
- log_message(sprintf("game created %s", @id))
-
- @logfile = @id + ".csa"
- @board = Board::new
- @board.initial
- @start_time = nil
- @fh = nil
-
- propose
- end
- attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :id, :board, :current_player, :next_player, :fh, :monitors
- attr_accessor :last_move, :current_turn
-
- def monitoron(monitor)
- @monitors.delete(monitor)
- @monitors.push(monitor)
- end
-
- def monitoroff(monitor)
- @monitors.delete(monitor)
- end
-
- def reject(rejector)
- @sente.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
- @gote.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
- finish
- end
-
- def kill(killer)
- if ((@sente.status == "agree_waiting") || (@sente.status == "start_waiting"))
- reject(killer.name)
- elsif (@current_player == killer)
- abnormal_lose()
- finish
- end
- end
-
- def finish
- log_message(sprintf("game finished %s", @id))
- @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
- @fh.close
-
- @sente.game = nil
- @gote.game = nil
- @sente.status = "connected"
- @gote.status = "connected"
-
- if (@current_player.protocol == "CSA")
- @current_player.finish
- end
- if (@next_player.protocol == "CSA")
- @next_player.finish
- end
- @monitors = Array::new
- @sente = nil
- @gote = nil
- @current_player = nil
- @next_player = nil
- LEAGUE.games.delete(@id)
- end
-
- def handle_one_move(str, player)
- finish_flag = true
- if (@current_player == player)
- @end_time = Time::new
- t = (@end_time - @start_time).ceil
- t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
-
- move_status = nil
- if ((@current_player.mytime - t <= -@byoyomi) && ((@total_time > 0) || (@byoyomi > 0)))
- status = :timeout
- elsif (str == :timeout)
- return false # time isn't expired. players aren't swapped. continue game
- else
- @current_player.mytime = @current_player.mytime - t
- if (@current_player.mytime < 0)
- @current_player.mytime = 0
- end
-
-# begin
- move_status = @board.handle_one_move(str)
-# rescue
-# log_error("handle_one_move raise exception for #{str}")
-# move_status = :illegal
-# end
-
- if ((move_status == :illegal) || (move_status == :uchifuzme) || (move_status == :oute_kaihimore))
- @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
- else
- if ((move_status == :normal) || (move_status == :outori) || (move_status == :sennichite) || (move_status == :oute_sennichite))
- @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 = @current_turn + 1
- end
-
- @monitors.each do |monitor|
- monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@id}] "))
- monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @id))
- end
- end
- end
-
- if (@next_player.status != "game") # rival is logout or disconnected
- abnormal_win()
- elsif (status == :timeout)
- timeout_lose()
- elsif (move_status == :illegal)
- illegal_lose()
- elsif (move_status == :kachi_win)
- kachi_win()
- elsif (move_status == :kachi_lose)
- kachi_lose()
- elsif (move_status == :toryo)
- toryo_lose()
- elsif (move_status == :outori)
- outori_win()
- elsif (move_status == :sennichite)
- sennichite_draw()
- elsif (move_status == :oute_sennichite)
- oute_sennichite_lose()
- elsif (move_status == :uchifuzume)
- uchifuzume_lose()
- elsif (move_status == :oute_kaihimore)
- oute_kaihimore_lose()
- else
- finish_flag = false
- end
- finish() if finish_flag
- (@current_player, @next_player) = [@next_player, @current_player]
- @start_time = Time::new
- return finish_flag
- end
- end
-
- def abnormal_win
- @current_player.status = "connected"
- @next_player.status = "connected"
- @current_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
- @next_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
- @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)
- @monitors.each do |monitor|
- monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
- end
- end
-
- def abnormal_lose
- @current_player.status = "connected"
- @next_player.status = "connected"
- @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
- @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
- @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)
- @monitors.each do |monitor|
- monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
- end
- end