ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.37
Committed: Tue Jun 3 13:47:46 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.36: +51 -28 lines
Log Message:
*** empty log message ***

File Contents

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