ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
(Generate patch)

Comparing kgsueme/kgsueme/game.pl (file contents):
Revision 1.66 by pcg, Sat Jun 28 16:44:56 2003 UTC vs.
Revision 1.107 by root, Mon May 31 02:04:40 2004 UTC

1use utf8; 1use utf8;
2
3use Scalar::Util ();
2 4
3package game::goclock; 5package game::goclock;
4 6
5# Lo and Behold! I admit it! The rounding stuff etc.. in goclock 7# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
6# is completely borked. 8# is completely borked.
7 9
8use Time::HiRes (); 10use Time::HiRes ();
9 11
10use KGS::Constants; 12use KGS::Constants;
11 13
12use base gtk::widget; 14use Glib::Object::Subclass
15 Gtk2::Label;
13 16
14sub new { 17sub INIT_INSTANCE {
15 my $class = shift; 18 my $self = shift;
16 my $self = $class->SUPER::new(@_);
17 19
18 $self->{widget} = new Gtk2::Label; 20 $self->signal_connect (destroy => sub { $_[0]->stop });
19 21
20 $self->{set} = sub { }; 22 $self->{set} = sub { };
21 $self->{format} = sub { "ERROR" }; 23 $self->{format} = sub { "???" };
24}
22 25
26sub FINALIZE_INSTANCE {
27 my $self = shift;
28
23 $self; 29 $self->stop;
24} 30}
25 31
26sub configure { 32sub configure {
27 my ($self, $timesys, $main, $interval, $count) = @_; 33 my ($self, $timesys, $main, $interval, $count) = @_;
28 34
29 if ($timesys == TIMESYS_ABSOLUTE) { 35 if ($timesys == TIMESYS_ABSOLUTE) {
30 $self->{set} = sub { $self->{time} = $_[0] }; 36 $self->{format} = sub {
31 $self->{format} = sub { util::format_time $_[0] }; 37 if ($_[0] <= 0) {
38 "TIMEOUT";
39 } else {
40 util::format_time $_[0];
41 }
42 };
32 43
33 } elsif ($timesys == TIMESYS_BYO_YOMI) { 44 } elsif ($timesys == TIMESYS_BYO_YOMI) {
34 my $low = $interval * $count; 45 my $low = $interval * $count;
35 46
36 $self->{set} = sub { $self->{time} = $_[0] };
37
38 $self->{format} = sub { 47 $self->{format} = sub {
48 if ($_[0] <= 0) {
49 "TIMEOUT";
39 if ($_[0] > $low) { 50 } elsif ($_[0] > $low) {
40 util::format_time $_[0] - $low; 51 util::format_time $_[0] - $low;
41 } else { 52 } else {
42 sprintf "%s (%d)", 53 sprintf "%s (%d)",
43 util::format_time int (($_[0] - 1) % $interval + 1), 54 util::format_time int (($_[0] - 1) % $interval + 1),
44 ($_[0] - 1) / $interval; 55 ($_[0] - 1) / $interval;
45 } 56 }
46 }; 57 };
47 58
48 } elsif ($timesys == TIMESYS_CANADIAN) { 59 } elsif ($timesys == TIMESYS_CANADIAN) {
49 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
50
51 $self->{format} = sub { 60 $self->{format} = sub {
61 if ($_[0] <= 0) {
62 "TIMEOUT";
52 if (!$self->{moves}) { 63 } elsif (!$self->{moves}) {
53 util::format_time $_[0] - $low; 64 util::format_time $_[0] - $low;
54 } else { 65 } else {
55 my $time = int (($_[0] - 1) % $interval + 1); 66 my $time = int (($_[0] - 1) % $interval + 1);
56 67
57 sprintf "%s/%d =%d", 68 sprintf "%s/%d =%d",
58 util::format_time $time, 69 util::format_time $time,
59 $self->{moves}, 70 $self->{moves},
71 $self->{moves} > 1
60 $time / ($self->{moves} || 1); 72 ? $time / $self->{moves}
61 73 : $interval;
62 } 74 }
63 }; 75 };
64 76
65 } else { 77 } else {
66 # none, or unknown 78 # none, or unknown
67 $self->{set} = sub { };
68 $self->{format} = sub { "---" } 79 $self->{format} = sub { "-" }
69 } 80 }
70} 81}
71 82
72sub refresh { 83sub refresh {
73 my ($self, $timestamp) = @_; 84 my ($self, $timestamp) = @_;
74 my $timer = $self->{time} + $self->{start} - $timestamp; 85 my $timer = $self->{time} + $self->{start} - $timestamp;
75 86
76 # we round the timer value slightly... the protocol isn't exact anyways, 87 # we round the timer value slightly... the protocol isn't exact anyways,
77 # and this gives smoother timers ;) 88 # and this gives smoother timers ;)
78 my @format = $self->{format}->(int ($timer + 0.4)); 89 my $timer2 = int $timer + 0.4;
90
79 $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4))); 91 $self->set_text ($self->{format}->($timer2));
80 92
81 $timer - int $timer; 93 $timer - int $timer;
82} 94}
83 95
84sub set_time { 96sub set_time {
85 my ($self, $time) = @_; 97 my ($self, $start, $time, $moves) = @_;
86 98
87 # we ignore requests to re-set the time of a running clock. 99 $self->{time} = $time;
88 # this is the easiest way to ensure that commentary etc. 100 $self->{moves} = $moves;
89 # doesn't re-set the clock. yes, this is frickle design, 101
90 # but I think the protocol is to blame here, which gives 102 if ($start) {
91 # very little time information. (cgoban2 also has had quite 103 $self->{start} = $start;
92 # a lot of small time update problems...) 104 $self->start;
93 unless ($self->{timeout}) { 105 } else {
94 $self->{set}->($time->[0], $time->[1]); 106 $self->stop;
95 $self->refresh ($self->{start}); 107 $self->refresh ($self->{start});
96 } 108 }
97} 109}
98 110
99sub start { 111sub start {
100 my ($self, $when) = @_; 112 my ($self) = @_;
101 113
102 $self->stop; 114 $self->stop;
103
104 $self->{start} = $when;
105 115
106 my $timeout; $timeout = sub { 116 my $timeout; $timeout = sub {
107 my $next = $self->refresh (Time::HiRes::time) * 1000; 117 my $next = $self->refresh (Time::HiRes::time) * 1000;
108 $next += 1000 if $next < 0; 118 $next += 1000 if $next < 0;
109 $self->{timeout} = add Glib::Timeout $next, $timeout; 119 $self->{timeout} = add Glib::Timeout $next, $timeout;
117 my ($self) = @_; 127 my ($self) = @_;
118 128
119 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 129 remove Glib::Source delete $self->{timeout} if $self->{timeout};
120} 130}
121 131
122sub destroy {
123 my ($self) = @_;
124 $self->stop;
125 $self->SUPER::destroy;
126}
127
128package game::userpanel; 132package game::userpanel;
129 133
130use base gtk::widget; 134use Glib::Object::Subclass
135 Gtk2::HBox,
136 properties => [
137 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
138 ];
131 139
132sub new { 140sub INIT_INSTANCE {
133 my $class = shift; 141 my ($self) = @_;
134 my $self = $class->SUPER::new(@_);
135 142
136 $self->{widget} = new Gtk2::HBox;
137
138 $self->{widget}->add (my $vbox = new Gtk2::VBox); 143 $self->add (my $vbox = new Gtk2::VBox);
139 144
140 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 145 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
141 $vbox->add ($self->{info} = new Gtk2::Label ""); 146 $vbox->add ($self->{info} = new Gtk2::Label "");
142 $vbox->add (($self->{clock} = new game::goclock)->widget); 147 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
143 148
144 $vbox->add ($self->{imagebox} = new Gtk2::VBox); 149 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
145 150
146 $self; 151 $self;
147} 152}
148 153
149sub configure { 154sub configure {
150 my ($self, $user, $rules) = @_; 155 my ($self, $app, $user, $rules) = @_;
151 156
152 if ($self->{name}->get_text ne $user->as_string) { 157 if ($self->{name}->get_text ne $user->as_string) {
153 $self->{name}->set_text ($user->as_string); 158 $self->{name}->set_text ($user->as_string);
154 159
155 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; 160 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
156 $self->{imagebox}->add (gtk::image_from_data undef); 161 $self->{imagebox}->add (gtk::image_from_data undef);
157 $self->{imagebox}->show_all; 162 $self->{imagebox}->show_all;
158 163
159 if ($user->has_pic) { 164 if ($user->has_pic) {
160 # the big picture... 165 # the big picture...
161 appwin::userpic ($user->{name}, sub { 166 $app->userpic ($user->{name}, sub {
162 return unless $self->{imagebox}; 167 return unless $self->{imagebox};
168
163 if ($_[0]) { 169 if ($_[0]) {
164 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; 170 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
165 $self->{imagebox}->add (gtk::image_from_data $_[0]); 171 $self->{imagebox}->add (gtk::image_from_data $_[0]);
166 $self->{imagebox}->show_all; 172 $self->{imagebox}->show_all;
167 } 173 }
170 } 176 }
171 177
172 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); 178 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
173} 179}
174 180
175sub set_state { 181sub set_captures {
176 my ($self, $captures, $timer, $when) = @_; 182 my ($self, $captures) = @_;
177
178 $self->{clock}->stop unless $when;
179 $self->{clock}->set_time ($timer);
180 $self->{clock}->start ($when) if $when;
181 183
182 $self->{info}->set_text ("$captures pris."); 184 $self->{info}->set_text ("$captures pris.");
183} 185}
184 186
187sub set_timer {
188 my ($self, $start, $time, $moves) = @_;
189
190 $self->{clock}->set_time ($start, $time, $moves);
191}
192
185package game; 193package game;
194
195use Scalar::Util qw(weaken);
186 196
187use KGS::Constants; 197use KGS::Constants;
188use KGS::Game::Board; 198use KGS::Game::Board;
189 199
200use Gtk2::GoBoard;
201use Gtk2::GoBoard::Constants;
202
203use base KGS::Game;
190use base KGS::Listener::Game; 204use base KGS::Listener::Game;
191use base KGS::Game;
192 205
193use base gtk::widget; 206use Glib::Object::Subclass
207 Gtk2::Window;
194 208
195use POSIX qw(ceil); 209use POSIX qw(ceil);
196 210
197sub new { 211sub new {
198 my $self = shift; 212 my ($self, %arg) = @_;
199 $self = $self->SUPER::new(@_); 213 $self = $self->Glib::Object::new;
214 $self->{$_} = delete $arg{$_} for keys %arg;
200 215
201 $self->listen($self->{conn});
202
203 $self->{window} = new Gtk2::Window 'toplevel';
204 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500]; 216 gtk::state $self, "game::window", undef, window_size => [600, 500];
205 217
206 $self->{window}->signal_connect(delete_event => sub { 218 $self->signal_connect (destroy => sub {
207 $self->part; 219 $self->unlisten;
208 $self->destroy; 220 delete $self->{app}{game}{$self->{channel}};
209 1; 221 %{$_[0]} = ();
210 }); 222 });#d#
211 223
212 $self->{window}->add($self->{hpane} = new Gtk2::HPaned); 224 $self->add (my $hpane = new Gtk2::HPaned);
213 gtk::state $self->{hpane}, "game::hpane", undef, position => 500; 225 gtk::state $hpane, "game::hpane", undef, position => 500;
214 226
215 # LEFT PANE 227 # LEFT PANE
216 228
217 $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 0); 229 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
218 230
219 $self->{boardbox} = new Gtk2::VBox; 231 $self->{boardbox} = new Gtk2::VBox;
220 232
221 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); 233 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
222 234
223 # challenge
224
225 $self->{challenge} = new challenge channel => $self->{channel};
226
227 # board box (aspect/canvas) 235 # board box (aspect/canvas)
228 236
229 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 237 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
238
239 # RIGHT PANE
240
241 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
242 $hpane->set (position_set => 1);
243
244 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
230 245
231 { 246 {
232 $frame->add (my $vbox = new Gtk2::VBox); 247 $frame->add (my $vbox = new Gtk2::VBox);
233 $vbox->add ($self->{title} = new Gtk2::Label $title); 248 $vbox->add ($self->{title} = new Gtk2::Label $title);
234 249
240 255
241 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); 256 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
242 $scale->set_draw_value (0); 257 $scale->set_draw_value (0);
243 $scale->set_digits (0); 258 $scale->set_digits (0);
244 259
245 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 260 $self->{moveadj}->signal_connect (value_changed => sub {
261 $self->{showmove} = int $self->{moveadj}->get_value;
262 $self->update_board;
263 });
246 } 264 }
247 265
248 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
249
250 # RIGHT PANE
251
252 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 1, 1);
253 $self->{hpane}->set(position_set => 1);
254 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
255
256 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
257 $sw->set_policy("automatic", "always");
258
259 $sw->add(($self->{userlist} = new userlist)->widget);
260
261 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
262
263 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); 266 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
267
264 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget); 268 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
265 $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget); 269 for COLOUR_WHITE, COLOUR_BLACK;
270
271 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
272
273 $buttonbox->add ($self->{button_pass} =
274 Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0));
275 $self->{button_pass}->signal_connect (clicked => sub {
276 $self->{board_click}->(255, 255) if $self->{board_click};
277 });
278 $buttonbox->add ($self->{button_undo} =
279 Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0));
280 $self->{button_undo}->signal_connect (clicked => sub {
281 $self->send (req_undo => channel => $self->{channel});
282 });
283 $buttonbox->add ($self->{button_resign} =
284 Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0));
285 $self->{button_resign}->signal_connect (clicked => sub {
286 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour});
287 });
266 288
267 $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0); 289 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
268 290
269 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); 291 $self->set_channel ($self->{channel});
270 $self->{entry}->signal_connect(activate => sub {
271 my $text = $self->{entry}->get_text;
272 $self->say($text) if $text =~ /\S/;
273 $self->{entry}->set_text("");
274 });
275 292
276 $self->event_update_game; 293 $self->show_all;
294
277 $self; 295 $self;
296}
297
298sub set_channel {
299 my ($self, $channel) = @_;
300
301 $self->{channel} = $channel;
302
303 if ($self->{channel} > 0) {
304 $self->listen ($self->{conn});
305
306 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1);
307 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1);
308
309 $self->signal_connect (delete_event => sub { $self->part; 1 });
310 $self->{chat}->signal_connect (command => sub {
311 my ($chat, $cmd, $arg) = @_;
312 if ($cmd eq "rsave") {
313 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
314 } else {
315 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
316 }
317 });
318 }
278} 319}
279 320
280sub event_update_users { 321sub event_update_users {
281 my ($self, $add, $update, $remove) = @_; 322 my ($self, $add, $update, $remove) = @_;
282 323
283 $self->{userlist}->update ($add, $update, $remove); 324# $self->{userlist}->update ($add, $update, $remove);
325
326 $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy
327 for @$remove;
328
329 $self->{users_inlay}->refresh;
284 330
285 my %important; 331 my %important;
332 $important{$self->{black}{name}}++;
333 $important{$self->{white}{name}}++;
286 $important{$self->{user1}{name}}++; 334 $important{$self->{owner}{name}}++;
287 $important{$self->{user2}{name}}++;
288 $important{$self->{user3}{name}}++;
289 335
290 if (my @users = grep $important{$_->{name}}, @$add) { 336 if (my @users = grep $important{$_->{name}}, @$add) {
291 $self->{text}->append_text ("\n<header>Joins:</header>"); 337 $self->{chat}->append_text ("\n<header>Joins:</header>");
292 $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; 338 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
293 } 339 }
294 if (my @users = grep $important{$_->{name}}, @$remove) { 340 if (my @users = grep $important{$_->{name}}, @$remove) {
295 $self->{text}->append_text ("\n<header>Parts:</header>"); 341 $self->{chat}->append_text ("\n<header>Parts:</header>");
296 $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; 342 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
297 } 343 }
298
299} 344}
300 345
301sub join { 346sub join {
302 my ($self) = @_; 347 my ($self) = @_;
303 return if $self->{joined}; 348 return if $self->{joined};
304 349
305 $self->SUPER::join; 350 $self->SUPER::join;
306
307 $self->{window}->show_all;
308} 351}
309 352
310sub part { 353sub update_cursor {
354 my ($self) = @_;
355
356 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active;
357
358 delete $self->{board_click};
359
360 if ($self->{teacher} eq $self->{app}{conn}) {
361 #TODO# # teaching mode not implemented
362 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
363 $self->{button_undo}->hide;
364 $self->{button_resign}->hide;
365 $self->{board}->set (cursor => undef);
366
367 } elsif ($running && $self->{colour} != COLOUR_NONE) {
368 # during game
369 $self->{button_undo}->show;
370 $self->{button_resign}->show;
371
372 if ($self->{cur_board}{score}) {
373 # during scoring
374 $self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1);
375 $self->{board}->set (cursor => sub {
376 $_[0] & (MARK_B | MARK_W)
377 ? $_[0] ^ MARK_GRAYED
378 : $_[0];
379 });
380 $self->{board_click} = sub {
381 if ($_[0] == 255) {
382 $self->{button_pass}->sensitive (0);
383 warn sprintf "SEND DONE @{$self->{done}} %x\n", $self->{doneid};#d#
384 $self->done;
385 } else {
386 $self->send (mark_dead =>
387 channel => $self->{channel},
388 x => $_[0],
389 y => $_[1],
390 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED),
391 );
392 }
393 };
394
395 } elsif ($self->{colour} == $self->{whosemove}) {
396 # normal move
397 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
398 $self->{board}->set (cursor => sub {
399 # if is_valid_move oder so#TODO#
400 $_[0] & (MARK_B | MARK_W)
401 ? $_[0]
402 : $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B);
403 });
404 $self->{board_click} = sub {
405 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
406 $self->{board}->set (cursor => undef);
407 delete $self->{board_click};
408 $self->{button_pass}->sensitive (0);
409 };
410 } else {
411 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1);
412 $self->{board}->set (cursor => undef);
413 }
414 } else {
415 $self->{button_undo}->hide;
416 $self->{button_resign}->hide;
417 $self->{button_pass}->hide;
418 $self->{board}->set (cursor => undef);
419 #TODO# # implement coordinate-grabbing
420 }
421}
422
423sub update_timers {
311 my ($self) = @_; 424 my ($self, $timers) = @_;
312 425
313 $self->SUPER::part; 426 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
314 $self->destroy; 427
428 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
429 my $t = $timers->[$colour];
430 $self->{userpanel}[$colour]->set_timer (
431 $running && $colour == $self->{whosemove} && $t->[0],
432 $t->[1] || $self->{rules}{time}
433 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
434 && $self->{rules}{interval} * $self->{rules}{count}),
435 $t->[2] || $self->{rules}{count});
436 }
315} 437}
316 438
317sub update_board { 439sub update_board {
318 my ($self) = @_; 440 my ($self) = @_;
319 return unless $self->{path}; 441 return unless $self->{path};
320 442
321 my $move = int $self->{moveadj}->get_value;
322
323 my $running = $move == @{$self->{path}};
324
325 $self->{board_label}->set_text ("Move " . ($move - 1)); 443 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1));
326 444
327 $self->{cur_board} = new KGS::Game::Board $self->{size}; 445 $self->{cur_board} = new KGS::Game::Board $self->{size};
328 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); 446 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]);
329 447
330 for my $colour (WHITE, BLACK) { 448 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
331 $self->{userpanel}[$colour]->set_state ( 449 for COLOUR_WHITE, COLOUR_BLACK;
332 $self->{cur_board}{captures}[$colour], 450
451 if ($self->{rules}{ruleset} == RULESET_JAPANESE) {
452 if ($self->{curnode}{move} == 0) {
453 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
454 } else {
455 $self->{whosemove} = 1 - $self->{cur_board}{last};
456 }
457 } else {
458 # Chinese, Aga, NZ all have manual placement
459 if ($self->{curnode}{move} < $self->{handicap}) {
460 $self->{whosemove} = COLOUR_BLACK;
461 } elsif ($self->{curnode}{move} == $self->{handicap}) {
462 $self->{whosemove} = COLOUR_WHITE;
463 } else {
464 $self->{whosemove} = 1 - $self->{cur_board}{last};
465 }
466 }
467
468 my $start_time = $self->{rules}{time};
469
470 if ($self->{showmove} == @{$self->{path}}) {
471 $self->{timers} = [
472 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}],
473 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}],
474 ];
475 $self->update_timers ($self->{timers});
476 } else {
477 $self->update_timers ([
333 $self->{cur_board}{timer}[$colour], 478 [0, @{$self->{cur_board}{timer}[0]}],
334 ($running && $self->{lastmove_colour} == !$colour) 479 [0, @{$self->{cur_board}{timer}[1]}],
335 ? $self->{lastmove_time} : 0
336 ); 480 ]);
337 } 481 }
338 482
339 $self->{board}->set_board ($self->{cur_board}); 483 $self->{board}->set_board ($self->{cur_board});
484
485 if ($self->{cur_board}{score}) {
486 $self->{score_inlay} ||= $self->{chat}->new_inlay;
487 $self->{score_inlay}->clear;
488 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
489 . "\n<score>"
490 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
491 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
492 . "</score>");
493 } elsif ($self->{score_inlay}) {
494 (delete $self->{score_inlay})->clear;
495 }
496
497 $self->update_cursor;
340} 498}
341 499
342sub event_update_tree { 500sub event_update_tree {
343 my ($self) = @_; 501 my ($self) = @_;
502
503 (delete $self->{undo_inlay})->clear
504 if $self->{undo_inlay};
344 505
345 $self->{path} = $self->get_path; 506 $self->{path} = $self->get_path;
346 507
347 if ($self->{moveadj}) { 508 if ($self->{moveadj}) {
348 my $upper = $self->{moveadj}->upper; 509 my $upper = $self->{moveadj}->upper;
391 }sgexi; 552 }sgexi;
392 553
393 $text .= $_; 554 $text .= $_;
394 } 555 }
395 556
396 $self->{text}->append_text ($text); 557 $self->{chat}->append_text ($text);
397} 558}
398 559
399sub event_join { 560sub event_join {
400 my ($self) = @_; 561 my ($self) = @_;
562
401 $self->SUPER::event_join; 563 $self->SUPER::event_join (@_);
564 $self->init_tree;
565 $self->event_update_game;
402} 566}
403 567
404sub event_part { 568sub event_part {
405 my ($self) = @_; 569 my ($self) = @_;
570
406 $self->SUPER::event_part; 571 $self->SUPER::event_part;
407 $self->destroy; 572 $self->destroy;
408} 573}
409 574
410sub event_move { 575sub event_move {
411 my ($self, $pass) = @_; 576 my ($self, $pass) = @_;
577
412 sound::play 1, $pass ? "pass" : "move"; 578 sound::play 1, $pass ? "pass" : "move";
413} 579}
414 580
415sub event_update_game { 581sub event_update_game {
416 my ($self) = @_; 582 my ($self) = @_;
583
417 $self->SUPER::event_update_game; 584 $self->SUPER::event_update_game;
418 585
419 my $title = $self->{channel} ? $self->owner->as_string . " " . $self->opponent_string : "Game Window"; 586 return unless $self->{joined};
587
588 $self->{colour} = $self->player_colour ($self->{conn}{name});
589
590 my $title = defined $self->{channel}
591 ? $self->owner->as_string . " " . $self->opponent_string
592 : "Game Window";
420 $self->{window}->set_title("KGS Game $title"); 593 $self->set_title ("KGS Game $title");
421 $self->{title}->set_text ($title); 594 $self->{title}->set_text ($title);
422 595
423 $self->{user}[BLACK] = $self->{user1}; 596 $self->{user}[COLOUR_BLACK] = $self->{black};
424 $self->{user}[WHITE] = $self->{user2}; 597 $self->{user}[COLOUR_WHITE] = $self->{white};
425 598
426 # show board 599 # show board
600 if ($self->is_inprogress) {
601 if (!$self->{boardbox}->parent) {
602 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
603 $self->{left}->add ($self->{boardbox});
604 $self->{board}->signal_connect (button_release => sub {
605 if ($_[1] == 1) {
606 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
607 }
608 });
609 }
610 if (my $ch = delete $self->{challenge}) {
611 $_->{inlay}->destroy for values %$ch;
612 }
613 $self->update_cursor;
614 }
615
616 $self->{left}->show_all;
617
618 $self->{rules_inlay}->refresh;
427 619
428 $self->{left}->remove ($_) for $self->{left}->get_children; 620}
621
622sub draw_setup {
623 my ($self, $inlay) = @_;
624
625 return unless $self->{joined};
626
627 my $rules = $self->{rules};
628
629 my $text = "";
630
631 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
632 if $self->{teacher};
633
634 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
635 if $self->{owner}->is_valid;
636
429 if ($self->is_valid) { 637 if ($self->is_inprogress) {
430 $self->{left}->add ($self->{boardbox});
431 (delete $self->{challenge})->destroy if $self->{challenge};
432 } else {
433 $self->{left}->add ($self->{challenge}->widget);
434 }
435 $self->{left}->show_all;
436
437 # view text
438
439 eval { #d#
440 my @ga;
441 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
442 . " (" . (util::toxml $gameopt{$self->option}) . ")";
443 $ga[1] = "\nFlags:";
444 $ga[1] .= " valid" if $self->is_valid;
445 $ga[1] .= " adjourned" if $self->is_adjourned;
446 $ga[1] .= " scored" if $self->is_scored;
447 $ga[1] .= " saved" if $self->is_saved;
448
449 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid;
450
451 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>" 638 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
452 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>" 639 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
453 if $self->is_valid;
454
455 if ($self->is_valid) {
456 $ga[4] = "\nHandicap: " . $self->{handicap};
457 $ga[5] = "\nKomi: " . $self->{komi};
458 $ga[6] = "\nSize: " . $self->size_string;
459 }
460
461 if ($self->is_scored) {
462 $ga[7] = "\nResult: " . $self->score_string;
463 }
464
465 $text = "\n<infoblock><header>Game Update</header>";
466 for (0..7) {
467 if ($self->{gatext}[$_] ne $ga[$_]) {
468 $text .= $ga[$_];
469 }
470 }
471 $text .= "</infoblock>";
472
473 $self->{gatext} = \@ga;
474 }; 640 }
475 641 $text .= "\nType: " . util::toxml $gametype{$self->type};
476 $self->{text}->append_text ($text);
477}
478
479sub event_update_rules {
480 my ($self, $rules) = @_;
481
482 $self->{userpanel}[$_]->configure ($self->{user}[$_], $rules)
483 for BLACK, WHITE;
484
485 sound::play 3, "gamestart";
486
487 my $text = "\n<header>Game Rules</header>";
488 642
489 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; 643 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
490 644
491 $text .= "\nTime: "; 645 $text .= "\nTime: ";
492 646
494 $text .= "UNLIMITED"; 648 $text .= "UNLIMITED";
495 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { 649 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
496 $text .= util::format_time $rules->{time}; 650 $text .= util::format_time $rules->{time};
497 $text .= " ABS"; 651 $text .= " ABS";
498 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { 652 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
499 $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count}; 653 $text .= util::format_time $rules->{time};
500 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; 654 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
501 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { 655 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
502 $text .= util::format_time $rules->{time}; 656 $text .= util::format_time $rules->{time};
503 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; 657 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
504 } 658 }
505 659
660 $text .= "\nFlags:";
661 $text .= " private" if $self->is_private;
662 $text .= " started" if $self->is_inprogress;
663 $text .= " adjourned" if $self->is_adjourned;
664 $text .= " scored" if $self->is_scored;
665 $text .= " saved" if $self->is_saved;
666
667 if ($self->is_inprogress) {
668 $text .= "\nHandicap: " . $self->{handicap};
669 $text .= "\nKomi: " . $self->{komi};
670 $text .= "\nSize: " . $self->size_string;
671 }
672
673 if ($self->is_scored) {
674 $text .= "\nResult: " . $self->score_string;
675 }
676
506 $self->{text}->append_text ("<infoblock>$text</infoblock>"); 677 $inlay->append_text ("<infoblock>$text</infoblock>");
507}
508 678
509sub inject_resign_game { 679}
680
681sub event_update_rules {
510 my ($self, $msg) = @_; 682 my ($self, $rules) = @_;
683
684 $self->{rules} = $rules;
685
686 if ($self->{user}) {
687 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
688 for COLOUR_BLACK, COLOUR_WHITE;
689 }
690
691 sound::play 3, "gamestart";
692 $self->{rules_inlay}->refresh;
693}
694
695sub event_resign_game {
696 my ($self, $player) = @_;
511 697
512 sound::play 3, "resign"; 698 sound::play 3, "resign";
513
514 $self->{text}->append_text ("\n<infoblock><header>Resign</header>" 699 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
515 . "\n<user>" 700 . "\n<user>"
516 . (util::toxml $self->{user}[$msg->{player}]->as_string) 701 . (util::toxml $self->{user}[$msg->{player}]->as_string)
517 . "</user> resigned.</infoblock>"); 702 . "</user> resigned.</infoblock>");
518} 703}
519 704
705sub event_out_of_time {
706 my ($self, $player) = @_;
707
708 sound::play 3, "timewin";
709 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>"
710 . "\n<user>"
711 . (util::toxml $self->{user}[$msg->{player}]->as_string)
712 . "</user> ran out of time and lost.</infoblock>");
713}
714
715sub event_done {
716 my ($self) = @_;
717
718 warn sprintf "EVENT DONE @{$self->{done}} %x\n", $self->{doneid};#d#
719
720 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) {
721 sound::play 2, "ring" unless $inlay->{count};
722 $self->{chat}->append_text ("\n<infoblock><header>Press Done</header>"
723 . "\nYour opponent pressed done. Now it's up to you.");
724 }
725 if ($self->{doneid} & 0x80000000) {
726 sound::play 2, "warning" unless $inlay->{count};
727 $self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>"
728 . "\nYour opponent changed the board..");
729 }
730
731 $self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]);
732
733 $self->{chat}->set_end;
734}
735
520sub inject_final_result { 736sub inject_final_result {
521 my ($self, $msg) = @_; 737 my ($self, $msg) = @_;
522 738
523 $self->{text}->append_text ("<infoblock>\n<header>Game Over</header>" 739 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
524 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) 740 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
525 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) 741 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
526 . "</infoblock>" 742 . "</infoblock>"
527 ); 743 );
528} 744}
529 745
530sub destroy { 746sub inject_set_gametime {
747 my ($self, $msg) = @_;
748
749 $self->{timers} = [
750 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
751 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
752 ];
753
754 $self->update_timers ($self->{timers})
755 if $self->{showmove} == @{$self->{path}};
756}
757
758sub inject_req_undo {
759 my ($self, $msg) = @_;
760
761 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay;
762 return if $inlay->{ignore};
763
764 sound::play 2, "warning" unless $inlay->{count};
765 $inlay->{count}++;
766
767 $inlay->clear;
768 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n");
769 $inlay->append_button ("Grant", sub {
770 (delete $self->{undo_inlay})->clear;
771 $self->send (grant_undo => channel => $self->{channel});
772 });
773 $inlay->append_button ("Ignore", sub {
774 $inlay->clear;
775 $inlay->{ignore} = 1;
776 # but leave inlay, so further undo requests get counted
777 });
778
779 $self->{chat}->set_end;
780}
781
782sub inject_new_game {
783 my ($self, $msg) = @_;
784
785 if ($msg->{cid} != $self->{cid}) {
786 $self->part;
787 warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d#
788 }
789
790 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>");
791 delete $self->{cid};
792}
793
794sub draw_challenge {
531 my ($self) = @_; 795 my ($self, $id) = @_;
532 $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy 796
533 for BLACK, WHITE; 797 my $info = $self->{challenge}{$id};
534 $self->SUPER::destroy; 798 my $inlay = $info->{inlay};
535 delete $appwin::gamelist->{game}{$self->{channel}}; 799 my $rules = $info->{rules};
800
801 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;;
802 my $opponent = $as_black ? $info->{white} : $info->{black};
803
804 my ($size, $time, $interval, $count, $type);
805
806 if (!$self->{channel}) {
807 $inlay->append_text ("\nNotes: ");
808 $inlay->append_entry (\$info->{notes}, 20, "");
809 $inlay->append_text ("\nGlobal Offer: ");
810 $inlay->append_optionmenu (\$info->{flags},
811 0 => "No",
812 2 => "Yes",
813 );
814 } else {
815 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes});
816 }
817
818 $inlay->append_text ("\nType: ");
819 $type = $inlay->append_optionmenu (
820 \$info->{gametype},
821 GAMETYPE_DEMONSTRATION , "Demonstration (not yet)",
822 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)",
823 GAMETYPE_TEACHING , "Teaching (not yet)",
824 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)",
825 GAMETYPE_SIMUL , "Simul (not yet!)",
826 GAMETYPE_FREE , "Free",
827 GAMETYPE_RATED , "Rated",
828 sub {
829 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
830 },
831 );
832
833 if ($self->{channel}) {
834 $inlay->append_text ("\nMy Colour: ");
835 $inlay->append_optionmenu (
836 \$as_black,
837 0 => "White",
838 1 => "Black",
839 sub {
840 if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) {
841 ($info->{black}, $info->{white}) = ($info->{white}, $info->{black});
842 }
843 }
844 );
845 }
846
847 $inlay->append_text ("\nRuleset: ");
848 $inlay->append_optionmenu (
849 \$info->{rules}{ruleset},
850 RULESET_JAPANESE , "Japanese",
851 RULESET_CHINESE , "Chinese",
852 RULESET_AGA , "AGA",
853 RULESET_NEW_ZEALAND, "New Zealand",
854 );
855
856 $inlay->append_text ("\nSize: ");
857 $size = $inlay->append_optionmenu (
858 \$info->{rules}{size},
859 (9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38),
860 sub {
861 $type->set_history (5) # reset to free
862 if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED;
863 },
864 );
865
866 if ($self->{channel}) {
867 $inlay->append_text ("\nHandicap: ");
868 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
869
870 $inlay->append_text ("\nKomi: ");
871 $inlay->append_entry (\$info->{rules}{komi}, 5);
872 }
873
874 $inlay->append_text ("\nTimesys: ");
875 $inlay->append_optionmenu (
876 \$info->{rules}{timesys},
877 &TIMESYS_NONE => "None",
878 &TIMESYS_ABSOLUTE => "Absolute",
879 &TIMESYS_BYO_YOMI => "Byo Yomi",
880 &TIMESYS_CANADIAN => "Canadian",
881 sub {
882 my ($new) = @_;
883
884 if ($new eq TIMESYS_NONE) {
885 $time->hide;
886 $interval->hide;
887 $count->hide;
888 } else {
889 $time->show;
890 $time->set_text ($self->{app}{defaults}{time});
891 if ($new eq TIMESYS_ABSOLUTE) {
892 $interval->hide;
893 $count->hide;
894 } else {
895 $interval->show;
896 $count->show;
897 if ($new eq TIMESYS_BYO_YOMI) {
898 $interval->set_text ($self->{app}{defaults}{byo_time});
899 $count->set_text ($self->{app}{defaults}{byo_period});
900 } elsif ($new eq TIMESYS_CANADIAN) {
901 $interval->set_text ($self->{app}{defaults}{can_time});
902 $count->set_text ($self->{app}{defaults}{can_period});
903 }
904 }
905 }
906 }
907 );
908
909 $inlay->append_text ("\nMain Time: ");
910 $time = $inlay->append_entry (\$info->{rules}{time}, 5);
911 $inlay->append_text ("\nInterval: ");
912 $interval = $inlay->append_entry (\$info->{rules}{interval}, 3);
913 $inlay->append_text ("\nPeriods/Stones: ");
914 $count = $inlay->append_entry (\$info->{rules}{count}, 2);
915
916 $inlay->append_text ("\n");
917
918 if (!$self->{channel}) {
919 $inlay->append_button ("Create Challenge", sub {
920 $inlay->clear;
921 $self->{cid} = $self->{conn}->alloc_clientid;
922 $self->send (new_game =>
923 channel => delete $self->{roomid},
924 gametype => $info->{gametype},
925 cid => $self->{cid},
926 flags => $info->{flags},
927 rules => $info->{rules},
928 notes => $info->{notes},
929 );
930 });
931 } else {
932 $inlay->append_button ("OK", sub {
933 $inlay->clear;
934 $self->send (challenge =>
935 channel => $self->{channel},
936 black => $info->{black},
937 white => $info->{white},
938 gametype => $info->{gametype},
939 cid => $info->{cid},
940 rules => $info->{rules},
941 );
942 });
943 if (exists $self->{challenge}{""}) {
944 $inlay->append_button ("Reject", sub {
945 $inlay->clear;
946 $self->send (reject_challenge =>
947 channel => $self->{channel},
948 name => $opponent->{name},
949 gametype => $info->{gametype},
950 cid => $info->{cid},
951 rules => $info->{rules},
952 );
953 });
954 }
955 }
956}
957
958sub draw_users {
959 my ($self, $inlay) = @_;
960
961 for (sort keys %{$self->{users}}) {
962 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
963 }
964}
965
966sub event_challenge {
967 my ($self, $info) = @_;
968
969 my $as_black = $info->{black}->{name} eq $self->{conn}{name};
970 my $opponent = $as_black ? $info->{white} : $info->{black};
971
972 my $id = $opponent->{name};
973
974 $self->{challenge}{$id} = $info;
975 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay (
976 exists $self->{challenge}{""}
977 ? "Challenge from $opponent->{name}"
978 : "Challenge to $opponent->{name}",
979 sub {
980 $self->{challenge}{$id}{inlay} = $_[0];
981 $self->draw_challenge ($id);
982 },
983 !exists $self->{challenge}{""} # only open when not offerer
984 );
536} 985}
537 986
5381; 9871;
539 988

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines