1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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 require 'interaction/match'
9 require 'ics/icsplayer'
13 # Handler for ICS games
20 def initialize(user, protocol)
25 protocol.add_observer(self)
28 def on_creating_game(data)
29 match = Match.new(data[:game],
32 @matches[data[:number]] = [match, data]
36 entry = @matches.delete(data[:game_number])
39 match.close(data[:result], data[:message])
43 def on_style12(style12)
44 match, match_info = @matches[style12.game_number]
45 return if match == nil
48 match.update_time(style12.time)
49 if match.index < style12.move_index
50 # last_move = icsapi.parse_verbose(style12.last_move, match.state)
51 move = match.game.serializer.new(:compact).deserialize(style12.last_move_san, match.state)
53 match.move(nil, move, :state => style12.state)
55 warn "Received invalid move from ICS: #{style12.last_move_san}"
59 rel = style12.relation
61 turns = [state.turn, state.opposite_turn(state.turn)]
62 @user.color, opponent_color =
63 if rel == Style12::Relation::MY_MOVE
68 opponent = ICSPlayer.new(
69 lambda {|msg| @protocol.connection.send_text(msg) },
71 match.game.serializer.new(:compact),
72 match_info[opponent_color][:name])
73 @user.name = match_info[@user.color][:name]
76 match.register(opponent)
81 raise "couldn't start match" unless match.started?
85 match.update_time(style12.time)