From 2987945602329f907deec2b0ea064949e3a7c6cd Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Sun, 27 Jun 2010 22:57:34 +0100
Subject: [PATCH] Match now accepts moves from a controller.
Fixes scratch game examination.
---
lib/controller.rb | 8 ++++++++
lib/interaction/match.rb | 17 ++++++++++++++---
lib/plugins/ics/lib/match_handler.rb | 8 ++++----
lib/plugins/ics/lib/match_helper.rb | 36 +++++++++++++++++++++---------------
4 files changed, 47 insertions(+), 22 deletions(-)
diff --git a/lib/controller.rb b/lib/controller.rb
index 30d5f8c..8eff8d5 100644
--- a/lib/controller.rb
+++ b/lib/controller.rb
@@ -296,6 +296,14 @@ class Controller
@controlled[player.color] = player
end
+ def controls?(x)
+ return true if x == self
+ @controlled.each do |c, p|
+ return true if x == p
+ end
+ false
+ end
+
def color=(value)
@match.close if @match
@match = nil
diff --git a/lib/interaction/match.rb b/lib/interaction/match.rb
index 2a298db..f46d2d4 100644
--- a/lib/interaction/match.rb
+++ b/lib/interaction/match.rb
@@ -16,6 +16,10 @@ module Player
def inspect
"<#{name}:#{self.class.name}>"
end
+
+ def controls?(x)
+ x == self
+ end
end
class Match
@@ -81,7 +85,7 @@ class Match
end
return false unless @history
- # if the match is non-editable, jump to the last move
+ # if the match is non-editable, jump to the last move
unless editable?
@history.go_to_last
end
@@ -90,7 +94,7 @@ class Match
if player == nil
player = current_player
else
- return false unless @players.has_key?(player)
+ return false unless is_playing?(player)
end
validate = @game.validator.new(@history.state)
@@ -99,7 +103,7 @@ class Match
warn "Invalid move from #{player.name}: #{move}"
return false
end
-
+
old_state = @history.state
state = old_state.dup
state.perform! move
@@ -255,4 +259,11 @@ class Match
method => event_args.merge(:awaiting_server => awaiting_server)
end
end
+
+ def is_playing?(player)
+ @players.each_key do |p|
+ return true if player.controls?(p)
+ end
+ false
+ end
end
diff --git a/lib/plugins/ics/lib/match_handler.rb b/lib/plugins/ics/lib/match_handler.rb
index d6118b7..718b635 100644
--- a/lib/plugins/ics/lib/match_handler.rb
+++ b/lib/plugins/ics/lib/match_handler.rb
@@ -27,6 +27,7 @@ class MatchHandler
include Observer
attr_reader :matches
+ attr_reader :protocol
#
# Create a match handler for the ICS connection associated to protocol.
@@ -47,7 +48,7 @@ class MatchHandler
#
def on_creating_game(data)
helper = MatchHelper.get(data[:helper] || :default)
- match_info = helper.create_match(data)
+ match_info = helper.create_match(self, data)
@matches[match_info[:number]] = match_info
end
@@ -102,13 +103,12 @@ class MatchHandler
end
match_info = @matches[style12.game_number]
if match_info and match_info[:match] and match_info[:match].closed?
- @matches.delete(style12.game_number)
- helper.close_match(@protocol, match_info)
+ helper.close_match(self, match_info)
return
end
# update match using helper and save it back to the @matches array
- match_info = helper.get_match(@protocol, match_info, style12)
+ match_info = helper.get_match(self, match_info, style12)
@matches[style12.game_number] = match_info
return unless match_info
diff --git a/lib/plugins/ics/lib/match_helper.rb b/lib/plugins/ics/lib/match_helper.rb
index e021cc1..f977f1d 100644
--- a/lib/plugins/ics/lib/match_helper.rb
+++ b/lib/plugins/ics/lib/match_helper.rb
@@ -58,15 +58,15 @@ module MatchHelper
#
# Create a new match instance.
#
- def create_match(match_info)
+ def create_match(handler, match_info)
raise "not implemented"
end
#
# Close a match.
#
- def close_match(protocol, match_info)
- raise "not implemented"
+ def close_match(handler, match_info)
+ handler.matches.delete(match_info[:game_number])
end
#
@@ -162,16 +162,18 @@ class DefaultMatchHelper
user
end
- def create_match(match_info)
+ def create_match(handler, match_info)
match = Match.new(match_info[:game],
:kind => :ics,
:editable => false,
:time_running => true)
+ match.on(:close) { close_match(handler, match_info) }
match_info.merge(:match => match)
end
- def close_match(protocol, match_info)
- protocol.connection.send_text("resign")
+ def close_match(handler, match_info)
+ super(handler, match_info)
+ handler.protocol.connection.send_text("resign")
end
end
@@ -203,31 +205,33 @@ class ExaminingMatchHelper
end
end
- def get_match(protocol, match_info, style12)
+ def get_match(handler, match_info, style12)
if match_info.nil?
# Examined games on ics have no header, so we have to be prepared to
# create a new match on the fly at this point.
# Create an editable Game.dummy match for the moment.
- match_info = create_match(style12.match_info)
+ match_info = create_match(handler, style12.match_info)
# We want to change the game type at some point, so request the game
# movelist to the server.
- protocol.connection.send_text('moves')
+ handler.protocol.connection.send_text('moves')
end
match_info
end
- def create_match(match_info)
+ def create_match(handler, match_info)
match = Match.new(Game.dummy,
:kind => :ics,
:editable => true,
:navigable => true)
+ match.on(:close) { close_match(handler, match_info) }
match_info.merge(:match => match)
end
- def close_match(protocol, match_info)
- protocol.connection.send_text("unexamine")
+ def close_match(handler, match_info)
+ super(handler, match_info)
+ handler.protocol.connection.send_text("unexamine")
end
end
@@ -249,16 +253,18 @@ class ObservingMatchHelper
player
end
- def create_match(match_info)
+ def create_match(handler, match_info)
match = Match.new(Game.dummy,
:kind => :ics,
:editable => false,
:navigable => false)
+ match.on(:close) { close_match(handler, match_info) }
match_info.merge(:match => match)
end
- def close_match(protocol, match_info)
- protocol.connection.send_text("unobserve #{match_info[:number]}")
+ def close_match(handler, match_info)
+ super(handler, match_info)
+ handler.protocol.connection.send_text("unobserve #{match_info[:number]}")
end
def get_user(view, match_info)
--
2.11.4.GIT