Return match_info in ExaminingMatchHelper#get_match.
[kaya.git] / lib / plugins / ics / lib / match_handler.rb
blobd975c10c80ffee5343b080e967d51b262a2ed428
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 require 'interaction/match'
9 require_bundle 'ics', 'icsplayer'
10 require_bundle 'ics', 'match_helper'
12 module ICS
15 # Handler for ICS games.
16
17 # Responds to ICS protocol events creating and updating matches.
18 # Matches are stored in the @matches instance variable. It is possible to
19 # have more than one match running on ICS because of the observe feature.
20
21 class MatchHandler
22   include Observer
23   
24   attr_reader :matches
25   
26   def initialize(user, protocol)
27     @protocol = protocol
28     @matches = { }
29     @user = user
30     
31     protocol.add_observer(self)
32   end
33   
34   def on_creating_game(data)
35     match = Match.new(data[:game], 
36         :kind => :ics,
37         :editable => false,
38         :time_running => true)
39     match_info = data.merge(
40       :type => :played,
41       :match => match)
42     @matches[data[:number]] = match_info
43   end
44   
45   def on_end_game(data)
46     match_info = @matches.delete(data[:game_number])
47     if match_info
48       match = match_info[:match]
49       match.close(data[:result], data[:message])
50     end
51   end
52   
53   def on_end_examination(number)
54     on_end_game(:game_number => number,
55                 :result => '',
56                 :message => '')
57   end
58   
59   def on_examination_revert(data)
60     match_info = @matches[data[:game_number]]
61     if match_info
62       match_info[:about_to_revert_to] = data[:index]
63     end
64   end
65   
66   def on_style12(style12)
67     # retrieve match and helper
68     helper = MatchHelper.create(style12)
69     if helper.nil?
70       warn "Unsupported style12. Skipping"
71       return
72     end
73     match_info = @matches[style12.game_number]
74     
75     # update match using helper and save it back to the @matches array
76     match_info = helper.get_match(@protocol, match_info, style12)
77     @matches[style12.game_number] = match_info
78     
79     return unless match_info
80     match = match_info[:match]
81     
82     if match.started?
83       match_info[:icsplayer].on_style12(style12)
84     else
85       helper.start(@protocol, @user, match, match_info, style12)
86     end
87   end
88 end
90 end