## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+require 'shogi_server/league/floodgate'
+require 'observer'
+
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
- attr_reader :players, :black, :white
+ 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.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(/^/, "\'"))
+ log(@game.board.to_s.gsub(/^/, "\'").chomp)
end
- def notify_monitor(type)
- @game.each_monitor do |monitor|
- monitor.write_safe(sprintf("##[MONITOR][%s] %s\n", @game.game_id, type))
- end
- end
end
class GameResultWin < GameResult
black_result = "lose"
white_result = "win"
end
- log("'summary:%s:%s %s:%s %s\n" % [type,
- @black.name, black_result,
- @white.name, white_result])
+ log("'summary:%s:%s %s:%s %s" % [type,
+ @black.name, black_result,
+ @white.name, white_result])
end
end
def process
@winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
@loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
- log("%%TORYO\n")
+ log("%TORYO")
log_summary("abnormal")
- notify_monitor("%%TORYO")
+ @result_type = "%TORYO"
+ notify
end
end
@winner.write_safe("#TIME_UP\n#WIN\n")
@loser.write_safe( "#TIME_UP\n#LOSE\n")
log_summary("time up")
- notify_monitor("#TIME_UP")
+ @result_type = "#TIME_UP"
+ notify
end
end
def process
@winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
@loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n")
- log("%%KACHI\n")
+ log("%KACHI")
log_summary("kachi")
- notify_monitor("%%KACHI")
+ @result_type = "%KACHI"
+ notify
end
end
def process
@winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
@loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
- log("%%KACHI\n")
+ log("%KACHI")
log_summary("illegal kachi")
- notify_monitor("%%KACHI")
+ @result_type = "%KACHI"
+ notify
end
end
@winner.write_safe("#ILLEGAL_MOVE\n#WIN\n")
@loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n")
log_summary(@cause)
- notify_monitor("#ILLEGAL_MOVE")
+ @result_type = "#ILLEGAL_MOVE"
+ notify
end
end
end
end
-class GameResultOuteKaihiMoreWin < GameResultWin
+class GameResultOuteKaihiMoreWin < GameResultIllegalWin
def initialize(game, winner, loser)
super(game, winner, loser, "oute_kaihimore")
end
class GameResultOutoriWin < GameResultWin
def initialize(game, winner, loser)
- super(game, winner, loser, "outori")
+ super(game, winner, loser)
end
end
-class GameReulstToryoWin < GameResultWin
+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\n")
+ log("%TORYO")
log_summary("toryo")
- notify_monitor("%%TORYO")
+ @result_type = "%TORYO"
+ notify
end
end
@winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
@loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n")
log_summary("oute_sennichite")
- notify_monitor("#OUTE_SENNICHITE")
+ @result_type = "#OUTE_SENNICHITE"
+ notify
end
end
def log_summary(type)
log_board
- log("'summary:%s:%s draw:%s draw\n" % [type, @black.name, @white.name])
+ log("'summary:%s:%s draw:%s draw" % [type, @black.name, @white.name])
end
end
player.write_safe("#SENNICHITE\n#DRAW\n")
end
log_summary("sennichite")
- notify_monitor("#SENNICHITE")
+ @result_type = "#SENNICHITE"
+ notify
end
end
@gote.status = "agree_waiting"
@game_id = sprintf("%s+%s+%s+%s+%s",
- LEAGUE.event, @game_name,
+ $league.event, @game_name,
@sente.name, @gote.name, issue_current_time)
now = Time.now
- log_dir_name = File.join(LEAGUE.dir,
+ log_dir_name = File.join($league.dir,
now.strftime("%Y"),
now.strftime("%m"),
now.strftime("%d"))
FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
@logfile = File.join(log_dir_name, @game_id + ".csa")
- LEAGUE.games[@game_id] = self
+ $league.games[@game_id] = self
log_message(sprintf("game created %s", @game_id))
end
def kill(killer)
- if ["agree_waiting", "start_waiting"].include?(@sente.status)
- reject(killer.name)
- elsif (@current_player == killer)
+ [@sente, @gote].each do |player|
+ if ["agree_waiting", "start_waiting"].include?(player.status)
+ reject(killer.name)
+ return # return from this method
+ end
+ end
+
+ if (@current_player == killer)
result = GameResultAbnormalWin.new(self, @next_player, @current_player)
result.process
finish
@gote = nil
@current_player = nil
@next_player = nil
- LEAGUE.games.delete(@game_id)
+ $league.games.delete(@game_id)
end
# class Game
elsif (move_status == :kachi_lose)
result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
elsif (move_status == :toryo)
- result = GameReulstToryoWin.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)