From c42327a24402c3f8ee6557877192d2f3772fd4a4 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Wed, 24 Jun 2009 15:28:41 +0200
Subject: [PATCH] Restored back and forward functionalities.
---
lib/controller.rb | 45 ++++++++++++++++++++++++++++-----------------
lib/ics/match_handler.rb | 4 +++-
lib/interaction/match.rb | 14 +++++++++-----
3 files changed, 40 insertions(+), 23 deletions(-)
diff --git a/lib/controller.rb b/lib/controller.rb
index 99058fd..b78db70 100644
--- a/lib/controller.rb
+++ b/lib/controller.rb
@@ -91,21 +91,21 @@ class Controller
end
end
-# def back
-# state, move = @history.back
-# animate(:back, state, move)
-# @board.highlight(@history.move)
-# rescue History::OutOfBound
-# puts "error: first move"
-# end
-#
-# def forward
-# state, move = @history.forward
-# animate(:forward, state, move)
-# @board.highlight(move)
-# rescue History::OutOfBound
-# puts "error: last move"
-# end
+ def back
+ state, move = @match.history.back
+ animate(:back, state, move)
+ @board.highlight(@match.history.move)
+ rescue History::OutOfBound
+ puts "error: first move"
+ end
+
+ def forward
+ state, move = @match.history.forward
+ animate(:forward, state, move)
+ @board.highlight(move)
+ rescue History::OutOfBound
+ puts "error: last move"
+ end
def animate(direction, state, move, opts = {})
anim = @animator.send(direction, state, move, opts)
@@ -219,10 +219,21 @@ class Controller
end
def movable?(p)
- ! ! @controlled[@match.state.turn]
+ can_play?
end
def droppable?(color, index)
- ! ! @controlled[@match.state.turn]
+ can_play?
+ end
+
+ private
+
+ def can_play?
+ return false unless @controlled[@match.state.turn]
+ if @match.history.current < @match.index
+ @match.editable?
+ else
+ true
+ end
end
end
diff --git a/lib/ics/match_handler.rb b/lib/ics/match_handler.rb
index 4499d34..18d9144 100644
--- a/lib/ics/match_handler.rb
+++ b/lib/ics/match_handler.rb
@@ -19,7 +19,9 @@ class MatchHandler
end
def on_creating_game(data)
- match = Match.new(data[:game], :ics)
+ match = Match.new(data[:game],
+ :kind => :ics,
+ :editable => false)
@matches[data[:number]] = [match, data[:icsapi]]
end
diff --git a/lib/interaction/match.rb b/lib/interaction/match.rb
index 23847f9..90cd31d 100644
--- a/lib/interaction/match.rb
+++ b/lib/interaction/match.rb
@@ -11,12 +11,14 @@ class Match
attr_reader :game
attr_reader :history
attr_reader :kind
+ attr_reader :index
- def initialize(game, kind = :local)
+ def initialize(game, opts = {})
@game = game
@players = { } # player => ready
@history = nil
- @kind = kind
+ @kind = opts[:kind] || :local
+ @editable = opts.fetch(:editable, true)
end
def register(player)
@@ -40,6 +42,7 @@ class Match
state = @game.state.new
state.setup
@history = History.new(state)
+ @index = 0
fire :started
end
@@ -64,6 +67,7 @@ class Match
state = old_state.dup
state.perform! move
@history.add_move(state, move)
+ @index += 1
broadcast player, :move => {
:player => player,
@@ -87,10 +91,10 @@ class Match
@history.state
end
- def index
- @history.current
+ def editable?
+ @editable
end
-
+
private
def broadcast(player, event)
--
2.11.4.GIT