ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.31
Committed: Mon Jun 2 12:39:20 2003 UTC (21 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.30: +35 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package game::goclock;
2
3 use Time::HiRes ();
4
5 use KGS::Constants;
6
7 use base gtk::widget;
8
9 sub new {
10 my $class = shift;
11 my $self = $class->SUPER::new(@_);
12
13 $self->{widget} = new Gtk2::Label;
14
15 $self->{set} = sub { };
16 $self->{format} = sub { "ERROR" };
17
18 $self;
19 }
20
21 sub append_text {
22 my ($self, $text) = @_;
23
24 $self->{buffer}->insert ($self->{buffer}->get_end_iter, $text);
25 }
26
27 sub format_time {
28 my ($time) = @_;
29
30 $time > 60*60
31 ? sprintf "%d:%02d:%02d", $time / (60 * 60), $time / 60 % 60, $time % 60
32 : sprintf "%d:%02d", $time / 60 % 60, $time % 60;
33 }
34
35 sub set_rules {
36 my ($self, $timesys, $main, $interval, $count) = @_;
37
38 if ($timesys == TIMESYS_ABSOLUTE) {
39 $self->{set} = sub { $self->{time} = $_[0] };
40 $self->{format} = sub { format_time $_[0] };
41
42 } elsif ($timesys == TIMESYS_BYO_YOMI) {
43 my $low = $interval * $count;
44
45 $self->{set} = sub { $self->{time} = $_[0] };
46
47 $self->{format} = sub {
48 if ($_[0] > $low) {
49 format_time $_[0] - $low;
50 } else {
51 sprintf "%s (%d)", (format_time int ($_[0] % $interval) || $interval), $_[0] / $interval;
52 }
53 };
54
55 } elsif ($timesys == TIMESYS_CANADIAN) {
56 my $low = $interval;
57
58 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
59
60 $self->{format} = sub {
61 if ($_[0] > $low) {
62 format_time $_[0] - $low;
63 } else {
64 sprintf "%s / %d", (format_time int($_[0] % $interval) || $interval), $self->{moves};
65 }
66 };
67
68 } else {
69 # none, or unknown
70 $self->{set} = sub { };
71 $self->{format} = sub { "---" }
72 }
73 }
74
75 sub refresh {
76 my ($self, $timestamp) = @_;
77 my $timer = $self->{time} + $self->{start} - $timestamp;
78 $self->{widget}->set_text ($self->{format}->($timer));
79
80 $timer - int $timer;
81 }
82
83 sub set_time {
84 my ($self, $time) = @_;
85
86 # we ignore requests to re-set the time of a running clock.
87 # this is the easiest way to ensure that commentary etc.
88 # doesn't re-set the clock. yes, this is frickle design,
89 # but I think the protoocl is to blame here, which gives
90 # very little time information. (cgoban2 also has had quite
91 # a lot of small time update problems...)
92 unless ($self->{timeout}) {
93 $self->{set}->($time->[0], $time->[1]);
94 $self->refresh ($self->{start});
95 }
96 }
97
98 sub start {
99 my ($self) = @_;
100
101 return if $self->{timeout};
102
103 # this is correct, since we assume the last message triggered a start
104 $self->{start} = $KGS::Protocol::NOW;
105
106 my $timeout; $timeout = sub {
107 # -100 means we run the timer a bit earlier to avoid 10.99 => 10s roundings.
108 # we "could" cheat by precalculating the time, but I feel uneasy about both
109 # ways to cheat.
110 my $next = int ($self->refresh (Time::HiRes::time) * 1000) - 100;
111 $next += 1000 if $next < 0;
112 $self->{timeout} = add Glib::Timeout $next, $timeout;
113 0;
114 };
115
116 $timeout->();
117 }
118
119 sub stop {
120 my ($self) = @_;
121
122 remove Glib::Source delete $self->{timeout} if $self->{timeout};
123 }
124
125 sub destroy {
126 my ($self) = @_;
127 $self->stop;
128 $self->SUPER::destroy;
129 }
130
131 package game::userpanel;
132
133 use base gtk::widget;
134
135 sub new {
136 my $class = shift;
137 my $self = $class->SUPER::new(@_);
138
139 $self->{widget} = new Gtk2::HBox;
140
141 $self->{widget}->add (my $vbox = new Gtk2::VBox);
142
143 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
144 $vbox->add ($self->{info} = new Gtk2::Label "");
145 $vbox->add (($self->{clock} = new game::goclock)->widget);
146
147 $self;
148 }
149
150 sub set_rules {
151 my ($self, $rules) = @_;
152
153 if ($self->{name}->get_text ne $rules->{player}[$self->{colour}]) {
154 $self->{name}->set_text ($rules->{player}[$self->{colour}]);
155
156 # the big picture...
157 appwin::userpic ($rules->{player}[$self->{colour}], sub {
158 $self->{widget}->add (gtk::image_from_data $_[0]) if $_[0];
159 $self->{widget}->show_all;
160 # undef => show sth. funny
161 });
162 }
163
164 $self->{clock}->set_rules (@{$rules->{rules}}{qw(timesys time interval count)});
165 }
166
167 sub set_state {
168 my ($self, $captures, $timer, $running) = @_;
169
170 $self->{clock}->stop unless $running;
171 $self->{clock}->set_time ($timer);
172 $self->{clock}->start if $running;
173
174 $self->{info}->set_text ("$captures pris.");
175 }
176
177 package game;
178
179 use KGS::Constants;
180 use KGS::Game::Board;
181
182 use base KGS::Listener::Game;
183 use base KGS::Game;
184
185 use base gtk::widget;
186
187 use POSIX qw(ceil);
188
189 sub new {
190 my $self = shift;
191 $self = $self->SUPER::new(@_);
192
193 $self->listen($self->{conn});
194
195 $self->{window} = new Gtk2::Window 'toplevel';
196 my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
197 $self->{window}->set_title("KGS Game $title");
198 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500];
199
200 $self->{window}->signal_connect(delete_event => sub {
201 $self->part;
202 $self->destroy;
203 1;
204 });
205
206 $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
207 gtk::state $self->{hpane}, "game::hpane", undef, position => 500;
208
209 $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 1);
210
211 $self->{boardbox} = new Gtk2::VBox;
212
213 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
214
215 # challenge
216
217 $self->{challenge} = new challenge channel => $self->{channel};
218
219 # board box (aspect/canvas)
220
221 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
222
223 {
224 $frame->add(my $vbox = new Gtk2::VBox);
225 $vbox->add($self->{title} = new Gtk2::Label $title);
226
227 $self->{moveadj} = new Gtk2::Adjustment 0, 0, 0, 1, 1, 0;
228
229 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj});
230 $scale->set_draw_value (0);
231 $scale->set_digits (0);
232
233 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
234 }
235
236 $self->{boardbox}->pack_start((my $aspect_frame = new Gtk2::AspectFrame "", 0.5, 0.5, 1, 0), 1, 1, 0);
237 $aspect_frame->set (border_width => 0, shadow_type => 'none', label_xalign => 0.5);
238 $self->{board_label} = $aspect_frame->get_label_widget;
239
240 $aspect_frame->add($self->{canvas} = new Gtk2::DrawingArea);
241 $self->{canvas}->double_buffered (0) if $::config->{conserve_memory};
242
243 $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
244 $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
245
246 # RIGHT PANE
247
248 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0);
249 $self->{hpane}->set(position_set => 1);
250 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
251
252 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
253 $sw->set_policy("automatic", "always");
254
255 $sw->add(($self->{userlist} = new userlist)->widget);
256
257 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
258
259 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
260 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget);
261 $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget);
262
263 $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0);
264
265 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
266 $self->{entry}->signal_connect(activate => sub {
267 my $text = $self->{entry}->get_text;
268 $self->say($text) if $text =~ /\S/;
269 $self->{entry}->set_text("");
270 });
271
272 $self->event_update_game;
273 $self;
274 }
275
276 sub event_update_users {
277 my ($self, $add, $update, $remove) = @_;
278
279 $self->{userlist}->update ($add, $update, $remove);
280 }
281
282 sub join {
283 my ($self) = @_;
284 return if $self->{joined};
285
286 $self->SUPER::join;
287
288 $self->{window}->show_all;
289 }
290
291 sub part {
292 my ($self) = @_;
293
294 $self->SUPER::part;
295 $self->destroy;
296 }
297
298 sub configure_event {
299 my ($widget, $event, $self) = @_;
300 delete $self->{stack};
301 delete $self->{pixbuf};
302 delete $self->{board_shown};
303 delete $self->{background};
304 $self->repaint_board;
305 0;
306 }
307
308 sub expose_event {
309 my ($widget, $event, $self) = @_;
310
311 $self->{pixbuf} or return;
312
313 my $area = $event->area;
314
315 $self->redraw ($area);
316
317 0;
318 }
319
320 # something Gtk2 fixed
321 sub INTERP_NEAREST (){ 'nearest' }
322 sub INTERP_TILES (){ 'tiles' }
323 sub INTERP_BILINEAR (){ 'bilinear' }
324 sub INTERP_HYPER (){ 'hyper' }
325
326 sub new_pixbuf {
327 my ($w, $h, $alpha, $fill) = @_;
328
329 my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
330 $pixbuf->fill ($fill) if defined $fill;
331
332 $pixbuf;
333 }
334
335 sub scale_pixbuf {
336 my ($src, $w, $h, $mode) = @_;
337
338 my $dst = new_pixbuf $w, $h, 1;
339
340 $src->scale(
341 $dst, 0, 0, $w, $h, 0, 0,
342 $w / $src->get_width, $h / $src->get_height,
343 $mode,
344 );
345
346 $dst;
347 }
348
349 # create a stack of stones
350 sub create_stack {
351 my ($self, $mark, $size, $rand) = @_;
352
353 my $shadow = $size * 0.05;
354
355 my $c = \$self->{stack}{$mark};
356 unless ($$c) {
357 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
358 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
359
360 # zeroeth the shadow
361 if ($mark & (MARK_B | MARK_W)) {
362 # the -0.5's are a mystery to me
363 $::shadow_img->composite (
364 $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
365 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
366 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
367 );
368 }
369
370 # first the big stones (handicap stones different for effect)
371 for ([MARK_B, $mark & MARK_MOVE ? 255 : 192],
372 [MARK_W, $mark & MARK_MOVE ? 255 : 192],
373 [MARK_GRAY_B, 128],
374 [MARK_GRAY_W, 128]) {
375 my ($mask, $alpha) = @$_;
376 if ($mark & $mask) {
377 $stone->composite (
378 $base, 0, 0, $size, $size, 0, 0,
379 $size / $stone->get_width, $size / $stone->get_height,
380 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
381 );
382 }
383 }
384
385 # then the small stones
386 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
387 [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
388 my ($mask, $img) = @$_;
389 if ($mark & $mask) {
390 $img->composite (
391 $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2,
392 $size / $img->get_width / 2, $size / $img->get_height / 2,
393 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224
394 );
395 }
396 }
397
398 # and lastly any markers
399 my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
400
401 for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
402 [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
403 [MARK_SQUARE, $::square_img[$dark_bg]]) {
404 my ($mask, $img) = @$_;
405 if ($mark & $mask) {
406 $img->composite (
407 $base, 0, 0, $size, $size, 0, 0,
408 $size / $img->get_width, $size / $img->get_height,
409 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
410 );
411 }
412 }
413
414 push @$$c, $base;
415 }
416 }
417
418 $$c->[$rand % @$$c];
419 }
420
421 sub pixbuf_text {
422 my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
423
424 my @c = grep $_,
425 map $::font[$colour][$::fontmap{$_}],
426 split //, $text;
427
428 if (@c) {
429 my $spacing = $height * 0.1;
430 my $s = $height / List::Util::max map $_->get_height, @c;
431 my $W = List::Util::sum map $_->get_width, @c;
432
433 $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
434 $y -= $height * 0.5;
435
436 for (@c) {
437 my $w = $_->get_width * $s;
438 # +2 == don't fight the rounding
439 $_->composite ($pixbuf,
440 $x, $y, $w+2, $height+2, $x, $y, $s, $s,
441 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
442
443 $x += $w + $spacing;
444 }
445 }
446 }
447
448 sub pixbuf_rect {
449 my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
450 # we fake lines by... a horrible method :/
451 my $colour_pb = new_pixbuf 1, 1, 0, $colour;
452 $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
453 INTERP_NEAREST, $alpha);
454 }
455
456 sub repaint_board {
457 my ($self) = @_;
458 my $canvas = $self->{canvas};
459 my $expose_area = undef;
460
461 return $expose_area unless $self->{board};
462
463 my ($w, $h) = ($canvas->allocation->values)[2,3];
464
465 die "FATAL: board aspect ratio != 1" unless $w == $h;
466
467 my $s = $w;
468
469 return unless $s >= 200;
470
471 my $size = $self->{size};
472
473 # we leave enough space for the shadows.. I like smaller stones, and we
474 # do no need to do the nifty recursive screen updates that goban2 does
475 my $border = int ($s / ($size + 3) * 0.5);
476 my $s2 = $s - $border * 2;
477 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
478 my $ofs = int ($edge / 2);
479
480 my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
481
482 my $pixbuf;
483
484 my $oldboard;
485
486 if ($self->{background}) {
487 if ($oldboard = $self->{board_shown}) {
488 $pixbuf = $self->{pixbuf};
489 } else {
490 $pixbuf = $self->{background}->copy;
491 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
492 }
493 } else {
494 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
495
496 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
497
498 if ($s < $bw && $s < $bh) {
499 $pixbuf = new_pixbuf $s, $s, 0;
500 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
501 } else {
502 $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES;
503 }
504
505 my $linew = int ($s / 40 / $size);
506
507 # ornamental border... we have time to waste :/
508 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255;
509 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255;
510 pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
511 pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
512
513 for my $i (1 .. $size) {
514 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
515 pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
516
517 # 38 max, but we allow a bit more
518 my $label = (qw(- A B C D E F G H J K L M N O P Q R S T U V W X Y Z
519 AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ))[$i];
520
521 pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
522 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
523 pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
524 pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
525
526 $a++;
527 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
528 }
529
530 # hoshi points
531 my $hoshi = sub {
532 my ($x, $y) = @_;
533 my $hs = int ($edge / 4) | 1;
534 $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2;
535
536 # we use the shadow mask... not perfect, but I want to finish this
537 $::shadow_img->composite ($pixbuf,
538 $x, $y, $hs + 1, $hs + 1, $x, $y,
539 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
540 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
541 };
542
543 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
544 $hoshi->($h1, $h1);
545 $hoshi->($size - $h1 + 1, $h1);
546 $hoshi->($h1, $size - $h1 + 1);
547 $hoshi->($size - $h1 + 1, $size - $h1 + 1);
548
549 if ($size % 2) { # on odd boards, also the remaining 5
550 my $h2 = ($size + 1) / 2;
551 if ($size > 10) {
552 $hoshi->($h1, $h2);
553 $hoshi->($size - $h1 + 1, $h2);
554 $hoshi->($h2, $size - $h1 + 1);
555 $hoshi->($h2, $h1);
556 }
557 # the tengen
558 $hoshi->($h2, $h2);
559 }
560
561 unless ($::config->{conserve_memory} > 1) {
562 $self->{background} = $pixbuf;
563 $pixbuf = $pixbuf->copy;
564 }
565 }
566
567 $self->{pixbuf} = $pixbuf;
568
569 # hoshi-points(!)#d#
570 # caching of empty board gfx(!)#d#
571
572 for my $x (1 .. $size) {
573 for my $y (1 .. $size) {
574 my $rand = ($x ^ $y ^ 0x5555);
575
576 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
577
578 if ($::config->{randomize}) {
579 $dx += ($rand % 7) - 3;
580 $dy += ($rand / 3 % 7) - 3;
581 }
582
583 my $shadow = $edge * 0.05;
584 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
585
586 my $mark = $self->{board}{board}[$x-1][$y-1];
587 my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
588
589 if ($oldboard) {
590 next if $old == $mark; # no change
591
592 $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
593 $expose_area = $expose_area
594 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
595 : new Gtk2::Gdk::Rectangle @$area;
596 }
597
598 if ($mark) {
599 my $pb = $self->create_stack($mark, $edge, $rand);
600
601 $pb->composite ($pixbuf, @$area,
602 $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
603
604 # labels are handled here because they are quite rare
605 if ($mark & MARK_LABEL) {
606 my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
607
608 if ($white) {
609 pixbuf_text $pixbuf, 0,
610 $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
611 $self->{board}{label}[$x-1][$y-1];
612 }
613 pixbuf_text $pixbuf, $white,
614 $k[$x], $k[$y], $ofs * 0.7,
615 $self->{board}{label}[$x-1][$y-1];
616 }
617 }
618 }
619 }
620
621 $self->{board_shown} = Storable::dclone $self->{board};
622 #d# save
623 #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
624
625 $expose_area;
626 }
627
628 sub redraw {
629 my ($self, $area) = @_;
630
631 if ($area && $self->{pixbuf}) {
632 my ($x, $y, $w, $h) = $area->values;
633
634 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
635 $x, $y, $x, $y, $w, $h,
636 "normal", 0, 0);
637 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
638 $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
639 }
640 }
641
642 sub update_board {
643 my ($self) = @_;
644 return unless $self->{path};
645
646 my $move = int $self->{moveadj}->get_value;
647
648 my $running = $move == @{$self->{path}};
649
650 $self->{board_label}->set_text ("Move $move");
651
652 $self->{board} = new KGS::Game::Board $self->{size};
653 $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
654
655 $self->{userpanel}[WHITE]->set_state ($self->{board}{captures}[WHITE],
656 $self->{board}{timer}[WHITE],
657 $running && $self->{board}{last} == BLACK);
658 $self->{userpanel}[BLACK]->set_state ($self->{board}{captures}[BLACK],
659 $self->{board}{timer}[BLACK],
660 $running && $self->{board}{last} == WHITE);
661
662 $self->redraw ($self->repaint_board);
663 }
664
665 sub event_update_tree {
666 my ($self) = @_;
667
668 $self->{path} = $self->get_path;
669
670 $self->{userpanel}[WHITE]->set_rules ($self->{path}[0]); # should be onload only
671 $self->{userpanel}[BLACK]->set_rules ($self->{path}[0]); # should be onload only
672
673 if ($self->{moveadj}) {
674 my $upper = $self->{moveadj}->upper;
675 my $pos = $self->{moveadj}->get_value;
676
677 $self->{moveadj}->upper (scalar @{$self->{path}});
678
679 $self->{moveadj}->changed;
680 if ($pos == $upper) {
681 $self->{moveadj}->set_value (scalar @{$self->{path}});
682 } else {
683 $self->{moveadj}->value_changed;
684 }
685 }
686 }
687
688 sub event_update_comments {
689 my ($self, $node, $comment, $newnode) = @_;
690 $self->SUPER::event_update_comments($node, $comment, $newnode);
691
692 my $text;
693
694 $text .= "\n<header>Move <move>$node->{move}</move>, <header>Node <node>$node->{id}</node></header>"
695 if $newnode;
696
697 for (split /\n/, $comment) {
698 $text .= "\n";
699 if ($_ =~ s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
700 $text .= "<user>" . (util::toxml $1) . "</user>:";
701 }
702 $text .= $_;
703 }
704
705 $self->{text}->append_text ($text);
706 }
707
708 sub event_join {
709 my ($self) = @_;
710 $self->SUPER::event_join;
711 }
712
713 sub event_part {
714 my ($self) = @_;
715 $self->SUPER::event_part;
716 }
717
718 sub event_move {
719 my ($self, $pass) = @_;
720 sound::play 1, $pass ? "pass" : "move";
721 }
722
723 sub event_update_game {
724 my ($self) = @_;
725 $self->SUPER::event_update_game;
726 warn "GAME UPDATE ".join (":", %$self);
727 warn "SAVED ".$self->is_saved;
728 warn "SCORED ".$self->is_scored;
729 warn "ADJ ".$self->is_adjourned;
730 warn "VALID ".$self->is_valid;
731 warn "MOVES ".$self->moves;
732 warn "TYPE ".$self->type;
733
734 $self->{left}->remove ($_) for $self->{left}->get_children;
735 if ($self->is_valid) {
736 $self->{left}->add ($self->{boardbox});
737 (delete $self->{challenge})->destroy if $self->{challenge};
738 } else {
739 $self->{left}->add ($self->{challenge});
740 }
741 $self->{left}->show_all;
742 }
743
744 sub destroy {
745 my ($self) = @_;
746 (delete $self->{userpanel}[WHITE])->destroy if $self->{userpanel}[WHITE];
747 (delete $self->{userpanel}[BLACK])->destroy if $self->{userpanel}[BLACK];
748 $self->SUPER::destroy;
749 delete $appwin::gamelist->{game}{$self->{channel}};
750 }
751
752 1;
753