Refactoring: use Match to handle interactions.
[kaya.git] / lib / mainwindow.rb
blob2361354054579d60c13e3b9fe1828e4b107dc4c1
1 require 'qtutils'
2 require 'board/board'
3 require 'board/pool'
4 require 'board/table'
5 require 'board/scene'
6 require 'history'
7 require 'controller'
9 require 'interaction/match'
11 require 'ics/protocol'
12 require 'ics/match_handler'
13 require 'ics/connection'
14 require 'console'
16 class MainWindow < KDE::XmlGuiWindow
17   include ActionHandler
19   def initialize(loader, game)
20     super nil
21     
22     @loader = loader
23     @default_game = game
24     
25     load_board(game)
26     
27     setup_actions
28     setupGUI
29   end
31 private
33   def setup_actions
34     std_action(:open_new) { new_game(@default_game) }
35     std_action :quit, :slot => :close
36     regular_action :back, :icon => 'go-previous', 
37                           :text => KDE.i18n("&Back") do
38       @controller.back
39     end
40     regular_action :forward, :icon => 'go-next', 
41                              :text => KDE.i18n("&Forward") do
42       @controller.forward
43     end
44     regular_action :connect, :icon => 'network-connect',
45                              :text => KDE.i18n("&Connect to ICS") do
46       connect_to_ics
47     end
48     regular_action :disconnect, :icon => 'network-disconnect',
49                                 :text => KDE.i18n("&Disconnect from ICS") do
50       puts "disconnect"
51     end
52                   
53   end
54   
55   def load_board(game)
56     scene = Scene.new
57     table = Table.new scene, @loader, self
58     @controller = Controller.new(table)
59     new_game(game)
60     
61     self.central_widget = table
62   end
63   
64   def connect_to_ics
65     protocol = ICS::Protocol.new(:debug)
66     c = ICS::Connection.new('freechess.org', 23)
67     protocol.add_observer ICS::AuthModule.new(c, 'ujjajja', '')
68     protocol.add_observer ICS::StartupModule.new(c)
69     protocol.link_to c
70     
71     console = Console.new(nil)
72     console.show
74     protocol.observe :text do |text|
75       console.append(text)
76     end
78     console.observe :input do |text|
79       c.send_text text
80     end
82     handler = ICS::MatchHandler.new(@controller, protocol)
84     protocol.observe :creating_game do |data|
85       puts "CREATING GAME: #{data.inspect}"
86     end
88     c.start
89   end
90   
91   def new_game(game)
92     @controller.color = game.players.first
93     opponents = game.players[1..-1].map do |color|
94       DummyPlayer.new(color)
95     end
96     opponents.each do |p| 
97       @controller.add_controlled_player(p)
98     end
100     match = Match.new(game)
101     @controller.controlled.values.each do |p|
102       match.register(p)
103     end
104     @controller.controlled.values.each do |p|
105       match.start(p)
106     end
107     
108     @controller.reset(match)
109   end
112 class DummyPlayer
113   include Observer
114   
115   attr_reader :color
116   
117   def initialize(color)
118     @color = color
119   end