1 | package room; |
1 | package room; |
2 | |
2 | |
3 | use KGS::Constants; |
3 | use KGS::Constants; |
4 | |
4 | |
5 | use base KGS::Listener::Room; |
5 | use base KGS::Listener::Room; |
6 | use base gtk::widget; |
6 | |
|
|
7 | use Glib::Object::Subclass |
|
|
8 | Gtk2::Frame; |
7 | |
9 | |
8 | sub new { |
10 | sub new { |
9 | my $self = shift; |
11 | my ($self, %arg) = @_; |
10 | $self = $self->SUPER::new(@_); |
12 | $self = $self->Glib::Object::new; |
|
|
13 | $self->{$_} = delete $arg{$_} for keys %arg; |
11 | |
14 | |
12 | $self->listen($self->{conn}, qw(msg_room:)); |
|
|
13 | |
|
|
14 | $self->{window} = new Gtk2::Window 'toplevel'; |
|
|
15 | $self->{window}->set_title("KGS Room $self->{name}"); |
|
|
16 | gtk::state $self->{window}, "room::window", $self->{name}, window_size => [600, 400]; |
|
|
17 | |
|
|
18 | $self->{window}->signal_connect(delete_event => sub { $self->part; 1 }); |
|
|
19 | |
|
|
20 | $self->{window}->add($self->{hpane} = new Gtk2::HPaned); |
|
|
21 | $self->{hpane}->set(position_set => 1); |
|
|
22 | gtk::state $self->{hpane}, "room::hpane", $self->{name}, position => 200; |
|
|
23 | |
|
|
24 | $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
|
|
25 | |
|
|
26 | $vbox->add(($self->{text} = new gtk::text)->widget); |
|
|
27 | |
|
|
28 | $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); |
|
|
29 | $self->{entry}->signal_connect(activate => sub { |
15 | $self->signal_connect (destroy => sub { |
30 | my $text = $self->{entry}->get_text; |
16 | delete $::config->{rooms}{$self->{channel}}; |
31 | $self->say($text) if $text =~ /\S/; |
17 | delete $self->{app}{room}{$self->{channel}}; |
32 | $self->{entry}->set_text(""); |
18 | (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; |
|
|
19 | $self->unlisten; |
|
|
20 | %{$_[0]} = (); |
33 | }); |
21 | }); |
34 | |
22 | |
|
|
23 | $self->listen ($self->{conn}, qw(msg_room:)); |
|
|
24 | |
|
|
25 | $self->signal_connect (delete_event => sub { $self->part; 1 }); |
|
|
26 | |
|
|
27 | $self->add (my $hbox = new Gtk2::HBox); |
|
|
28 | |
|
|
29 | $hbox->pack_start ((my $vbox = new Gtk2::VBox), 1, 1, 0); |
|
|
30 | |
|
|
31 | $vbox->add ($self->{chat} = new chat); |
|
|
32 | |
|
|
33 | $self->{chat}->signal_connect(command => sub { |
|
|
34 | my ($chat, $cmd, $arg) = @_; |
|
|
35 | $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, room => $self); |
|
|
36 | }); |
|
|
37 | |
|
|
38 | $hbox->pack_start ((my $vbox = new Gtk2::VBox), 0, 1, 0); |
|
|
39 | |
|
|
40 | $vbox->pack_start ((my $button = new_with_label Gtk2::Button "Leave"), 0, 1, 0); |
|
|
41 | $button->signal_connect (clicked => sub { $self->part }); |
|
|
42 | |
|
|
43 | $vbox->pack_start ((my $button = new_with_label Gtk2::Button "New Game"), 0, 1, 0); |
|
|
44 | $button->signal_connect (clicked => sub { $self->new_game }); |
|
|
45 | |
35 | $self->{hpane}->pack2((my $sw = new Gtk2::ScrolledWindow), 0, 1); |
46 | $vbox->pack_start ((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0); |
36 | $sw->set_policy("automatic", "always"); |
47 | $sw->set_policy("automatic", "always"); |
37 | |
48 | |
38 | $sw->add(($self->{userlist} = new userlist)->widget); |
49 | $sw->add ($self->{userlist} = new userlist); |
39 | |
50 | |
40 | $self; |
51 | $self; |
41 | } |
52 | } |
42 | |
53 | |
43 | sub join { |
54 | sub FINALIZE_INSTANCE { print "FIN room\n" } # never called MEMLEAK #d#TODO# |
44 | my ($self) = @_; |
|
|
45 | $self->SUPER::join; |
|
|
46 | |
|
|
47 | $self->{window}->show_all; |
|
|
48 | } |
|
|
49 | |
55 | |
50 | sub part { |
56 | sub part { |
51 | my ($self) = @_; |
57 | my ($self) = @_; |
52 | $self->SUPER::part; |
58 | $self->SUPER::part; |
53 | |
59 | |
54 | $self->destroy; # yeaha |
60 | $self->hide_all; |
55 | } |
61 | } |
56 | |
62 | |
57 | sub inject_msg_room { |
63 | sub inject_msg_room { |
58 | my ($self, $msg) = @_; |
64 | my ($self, $msg) = @_; |
59 | |
65 | |
60 | # secret typoe ;-) |
66 | # secret typoe ;-) |
61 | $self->{text}->append_text("\n<header><user>" . (util::toxml $msg->{name}) |
67 | $self->{chat}->append_text ("\n<header><user>" . (util::toxml $msg->{name}) |
62 | . "</user>: </header>" . (util::toxml $msg->{message})); |
68 | . "</user>: </header>" . (util::toxml $msg->{message})); |
63 | } |
69 | } |
64 | |
70 | |
65 | sub event_update_users { |
71 | sub event_update_users { |
66 | my ($self, $add, $update, $remove) = @_; |
72 | my ($self, $add, $update, $remove) = @_; |
67 | |
73 | |
… | |
… | |
69 | } |
75 | } |
70 | |
76 | |
71 | sub event_update_games { |
77 | sub event_update_games { |
72 | my ($self, $add, $update, $remove) = @_; |
78 | my ($self, $add, $update, $remove) = @_; |
73 | |
79 | |
74 | $appwin::gamelist->update ($self, $add, $update, $remove); |
80 | $self->{app}{gamelist}->update ($self, $add, $update, $remove); |
|
|
81 | |
|
|
82 | # try to identify any new games assigned to us. stupid protocol |
|
|
83 | # first updates the game, joins you and THEN tells you that |
|
|
84 | # which of the games you asked for this is. |
|
|
85 | |
|
|
86 | for (@$add) { |
|
|
87 | if (($_->{black}{name} eq $self->{conn}{name} |
|
|
88 | || $_->{white}{name} eq $self->{conn}{name} |
|
|
89 | || $_->{owner}{name} eq $self->{conn}{name}) |
|
|
90 | && (my $game = shift @{$self->{new_game}})) { |
|
|
91 | $game->inject_upd_game ({ game => $_ }); |
|
|
92 | $game->set_channel ($game->{channel}); |
|
|
93 | } |
|
|
94 | } |
75 | } |
95 | } |
76 | |
96 | |
77 | sub event_join { |
97 | sub event_join { |
78 | my ($self) = @_; |
98 | my ($self) = @_; |
79 | $self->SUPER::event_join; |
99 | $self->SUPER::event_join; |
… | |
… | |
83 | # mysteriously enough, we have to request game updates manually |
103 | # mysteriously enough, we have to request game updates manually |
84 | $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub { |
104 | $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub { |
85 | $self->req_games; |
105 | $self->req_games; |
86 | 1; |
106 | 1; |
87 | }; |
107 | }; |
|
|
108 | |
|
|
109 | $self->show_all; |
88 | } |
110 | } |
89 | |
111 | |
90 | sub event_part { |
112 | sub event_part { |
91 | my ($self) = @_; |
113 | my ($self) = @_; |
92 | |
114 | |
93 | delete $::config->{rooms}{$self->{channel}}; |
|
|
94 | delete $appwin::self->{roomlist}{room}{$self->{channel}}; |
|
|
95 | (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; |
|
|
96 | $self->unlisten; |
|
|
97 | |
|
|
98 | $self->SUPER::event_part; |
115 | $self->SUPER::event_part; |
|
|
116 | $self->destroy; |
99 | } |
117 | } |
100 | |
118 | |
101 | sub event_update_roominfo { |
119 | sub event_update_roominfo { |
102 | my ($self) = @_; |
120 | my ($self) = @_; |
103 | |
121 | |
104 | $self->{text}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n" |
122 | $self->{chat}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n" |
105 | . "<description>" . (util::toxml $self->{description}) . "</description>\n"); |
123 | . "<description>" . (util::toxml $self->{description}) . "</description>\n"); |
106 | } |
124 | } |
107 | |
125 | |
108 | sub destroy { |
126 | sub new_game { |
109 | my ($self) = @_; |
127 | my ($self) = @_; |
110 | |
128 | |
111 | $self->event_part; |
129 | my $d = $self->{app}{defaults}; |
112 | |
130 | |
113 | $self->SUPER::destroy; |
131 | my $game = new game conn => $self->{conn}, app => $self->{app}, roomid => $self->{channel}; |
|
|
132 | $game->{challenge}{""} = { |
|
|
133 | gametype => $d->{gametype}, |
|
|
134 | flags => 0, |
|
|
135 | notes => $d->{stones}, |
|
|
136 | rules => { |
|
|
137 | ruleset => $d->{ruleset}, |
|
|
138 | size => $d->{size}, |
|
|
139 | timesys => $d->{timesys}, |
|
|
140 | time => $d->{time}, |
|
|
141 | interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time}, |
|
|
142 | count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones}, |
|
|
143 | }, |
|
|
144 | |
|
|
145 | inlay => $game->{chat}->new_inlay, |
|
|
146 | }; |
|
|
147 | $game->draw_challenge (""); |
|
|
148 | $game->show_all; |
|
|
149 | |
|
|
150 | push @{$self->{new_game}}, $game; |
114 | } |
151 | } |
115 | |
152 | |
116 | 1; |
153 | 1; |
117 | |
154 | |