1 require 'interaction/match'
2 require 'ics/icsplayer'
6 # Handler for ICS games
13 def initialize(user, protocol)
18 protocol.add_observer(self)
21 def on_creating_game(data)
22 match = Match.new(data[:game],
25 @matches[data[:number]] = [match, data[:icsapi]]
28 def on_style12(style12)
29 match, icsapi = @matches[style12.game_number]
30 return if match == nil
33 puts "match.index = #{match.index}"
34 puts "move index = #{style12.move_index}"
35 puts "current state = #{match.state}"
36 if match.index < style12.move_index
37 # last_move = icsapi.parse_verbose(style12.last_move, match.state)
38 move = match.game.serializer.new(:compact).deserialize(style12.last_move_san, match.state)
39 # if last_move != move
40 # warn "[server inconsistency] " +
41 # "SAN for last move is different from verbose notation"
44 match.move(nil, move, style12.state)
46 warn "Received invalid move from ICS: #{style12.last_move_san}"
50 rel = style12.relation
52 turns = [state.turn, state.opposite_turn(state.turn)]
53 @user.color, opponent_color =
54 if rel == Style12::Relation::MY_MOVE
59 opponent = ICSPlayer.new(
60 lambda {|msg| @protocol.connection.send_text(msg) },
62 match.game.serializer.new(:compact))
65 match.register(opponent)
70 raise "couldn't start match" unless match.started?