Don't overwrite pools when receiving updates from ICS.
[kaya.git] / lib / plugins / ics / lib / icsapi.rb
blob7e5a946e80f78830e641e711db8cb5ad489de6be
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 module ICS
10 class ICSApi
11   PIECES = {
12     'r' => :rook,
13     'n' => :knight,
14     'b' => :bishop,
15     'q' => :queen,
16     'k' => :king,
17     'p' => :pawn }
19   def initialize(game)
20     @game = game
21   end
23   def new_state(opts)
24     state = @game.state.new
25     state.turn = opts[:turn]
26     state.en_passant_square = 
27       if opts[:en_passant] != -1
28         Point.new(opts[:en_passant], 
29                   state.turn == :white ? 
30                   state.board.size.y - 3 : 2)
31       end
32     state.castling_rights.cancel_king(:white) unless opts[:wk_castling]
33     state.castling_rights.cancel_queen(:white) unless opts[:wq_castling]
34     state.castling_rights.cancel_king(:black) unless opts[:bk_castling]
35     state.castling_rights.cancel_queen(:black) unless opts[:bq_castling]
36     state
37   end
39   def new_piece(p)
40     return nil if p == '-'
41     color = p =~ /[a-z]/ ? :black : :white
42     type = PIECES[p.downcase]
43     @game.piece.new(color, type)
44   end
45   
46   def same_state(state1, state2)
47     (state1.board == state2.board).tap{|x| puts "boards differ" unless x } &&
48     (state1.turn == state2.turn).tap{|x| puts "turns differ" unless x }
49   end
50   
51   def amend_state(state1, state2)
52     state1.dup.tap do |result|
53       result.board = state2.board.dup
54       result.turn = state2.turn
55     end
56   end
57 end
59 end