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