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.102 by pcg, Sun May 30 06:40:21 2004 UTC vs.
Revision 1.114 by root, Tue Jun 1 12:46:54 2004 UTC

1use utf8; 1use utf8;
2 2
3use Scalar::Util (); 3use Scalar::Util ();
4
5### GO CLOCK WIDGET #########################################################
4 6
5package game::goclock; 7package game::goclock;
6 8
7# Lo and Behold! I admit it! The rounding stuff etc.. in goclock 9# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
8# is completely borked. 10# is completely borked.
21 23
22 $self->{set} = sub { }; 24 $self->{set} = sub { };
23 $self->{format} = sub { "???" }; 25 $self->{format} = sub { "???" };
24} 26}
25 27
28sub FINALIZE_INSTANCE {
29 my $self = shift;
30
31 $self->stop;
32}
33
26sub configure { 34sub configure {
27 my ($self, $timesys, $main, $interval, $count) = @_; 35 my ($self, $timesys, $main, $interval, $count) = @_;
28 36
29 if ($timesys == TIMESYS_ABSOLUTE) { 37 if ($timesys == TIMESYS_ABSOLUTE) {
30 $self->{set} = sub { $self->{time} = $_[0] }; 38 $self->{format} = sub {
31 $self->{format} = sub { util::format_time $_[0] }; 39 if ($_[0] < 0) {
40 "TIMEOUT";
41 } else {
42 util::format_time $_[0];
43 }
44 };
32 45
33 } elsif ($timesys == TIMESYS_BYO_YOMI) { 46 } elsif ($timesys == TIMESYS_BYO_YOMI) {
34 my $low = $interval * $count; 47 my $low = $interval * $count;
35 48
36 $self->{set} = sub { $self->{time} = $_[0] };
37
38 $self->{format} = sub { 49 $self->{format} = sub {
50 if ($_[0] < 0) {
51 "TIMEOUT";
39 if ($_[0] > $low) { 52 } elsif ($_[0] > $low) {
40 util::format_time $_[0] - $low; 53 util::format_time $_[0] - $low;
41 } else { 54 } else {
42 sprintf "%s (%d)", 55 sprintf "%s (%d)",
43 util::format_time int (($_[0] - 1) % $interval + 1), 56 util::format_time int (($_[0] - 1) % $interval + 1),
44 ($_[0] - 1) / $interval; 57 ($_[0] - 1) / $interval;
45 } 58 }
46 }; 59 };
47 60
48 } elsif ($timesys == TIMESYS_CANADIAN) { 61 } elsif ($timesys == TIMESYS_CANADIAN) {
49 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
50
51 $self->{format} = sub { 62 $self->{format} = sub {
63 if ($_[0] < 0) {
64 "TIMEOUT";
52 if (!$self->{moves}) { 65 } elsif (!$self->{moves}) {
53 util::format_time $_[0] - $low; 66 util::format_time $_[0] - $low;
54 } else { 67 } else {
55 my $time = int (($_[0] - 1) % $interval + 1); 68 my $time = int (($_[0] - 1) % $interval + 1);
56 69
57 sprintf "%s/%d =%d", 70 sprintf "%s/%d =%d",
63 } 76 }
64 }; 77 };
65 78
66 } else { 79 } else {
67 # none, or unknown 80 # none, or unknown
68 $self->{set} = sub { };
69 $self->{format} = sub { "---" } 81 $self->{format} = sub { "-" }
70 } 82 }
71} 83}
72 84
73sub refresh { 85sub refresh {
74 my ($self, $timestamp) = @_; 86 my ($self, $timestamp) = @_;
76 88
77 # we round the timer value slightly... the protocol isn't exact anyways, 89 # we round the timer value slightly... the protocol isn't exact anyways,
78 # and this gives smoother timers ;) 90 # and this gives smoother timers ;)
79 my $timer2 = int $timer + 0.4; 91 my $timer2 = int $timer + 0.4;
80 92
81 if ($timer2 <= 0) { 93 $self->set_text ($self->{format}->($timer2));
82 $timer2 = 0 if $timer2 < 0; 94
83 $self->set_text ("TIME OVER"); 95 $timer - int $timer;
96}
97
98sub set_time {
99 my ($self, $start, $time, $moves) = @_;
100
101 $self->{time} = $time;
102 $self->{moves} = $moves;
103
104 if ($start) {
105 $self->{start} = $start;
106 $self->start;
84 } else { 107 } else {
85 $self->set_text ($self->{format}->($timer2)); 108 $self->stop;
86 }
87
88 $timer - int $timer;
89}
90
91sub set_time {
92 my ($self, $time, $moves) = @_;
93
94 # we ignore requests to re-set the time of a running clock.
95 # this is the easiest way to ensure that commentary etc.
96 # doesn't re-set the clock. yes, this is frickle design,
97 # but I think the protocol is to blame here, which gives
98 # very little time information. (cgoban2 also has had quite
99 # a lot of small time update problems...)
100 unless ($self->{timeout}) {
101 $self->{set}->($time, $moves);
102 $self->refresh ($self->{start}); 109 $self->refresh ($self->{start});
103 } 110 }
104} 111}
105 112
106sub start { 113sub start {
107 my ($self, $when) = @_; 114 my ($self) = @_;
108 115
109 $self->stop; 116 $self->stop;
110
111 $self->{start} = $when;
112 117
113 my $timeout; $timeout = sub { 118 my $timeout; $timeout = sub {
114 my $next = $self->refresh (Time::HiRes::time) * 1000; 119 my $next = $self->refresh (Time::HiRes::time) * 1000;
115 $next += 1000 if $next < 0; 120 $next += 1000 if $next < 0;
116 $self->{timeout} = add Glib::Timeout $next, $timeout; 121 $self->{timeout} = add Glib::Timeout $next, $timeout;
124 my ($self) = @_; 129 my ($self) = @_;
125 130
126 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 131 remove Glib::Source delete $self->{timeout} if $self->{timeout};
127} 132}
128 133
134### USER PANEL ##############################################################
135
129package game::userpanel; 136package game::userpanel;
130 137
138use KGS::Constants;
139
131use Glib::Object::Subclass 140use Glib::Object::Subclass
132 Gtk2::HBox, 141 Gtk2::Frame,
133 properties => [ 142 properties => [
134 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]), 143 Glib::ParamSpec->IV ("colour", "colour", "User Colour",
144 COLOUR_BLACK, COLOUR_WHITE, COLOUR_BLACK,
145 [qw(construct-only readable writable)]),
135 ]; 146 ];
136 147
137sub INIT_INSTANCE { 148sub INIT_INSTANCE {
138 my ($self) = @_; 149 my ($self) = @_;
139 150
140 $self->add (my $vbox = new Gtk2::VBox); 151 $self->add (my $vbox = new Gtk2::VBox);
141 152
142 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 153 $vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0);
143 $vbox->add ($self->{info} = new Gtk2::Label ""); 154 $vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0);
144 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock}; 155 $vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0);
145 156
146 $vbox->add ($self->{imagebox} = new Gtk2::VBox); 157 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
147 158
148 $self; 159 $self;
149} 160}
180 191
181 $self->{info}->set_text ("$captures pris."); 192 $self->{info}->set_text ("$captures pris.");
182} 193}
183 194
184sub set_timer { 195sub set_timer {
185 my ($self, $when, $time, $moves) = @_; 196 my ($self, $start, $time, $moves) = @_;
186 197
187 $self->{clock}->stop unless $when;
188 $self->{clock}->set_time ($time, $moves); 198 $self->{clock}->set_time ($start, $time, $moves);
189 $self->{clock}->start ($when) if $when;
190} 199}
200
201### GAME WINDOW #############################################################
191 202
192package game; 203package game;
193 204
194use Scalar::Util qw(weaken); 205use Scalar::Util qw(weaken);
195 206
225 236
226 # LEFT PANE 237 # LEFT PANE
227 238
228 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0); 239 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
229 240
230 $self->{boardbox} = new Gtk2::VBox;
231
232 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); 241 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
233 242
234 # board box (aspect/canvas) 243 # board box (aspect/canvas)
235 244
236 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
237
238 # RIGHT PANE 245 # RIGHT PANE
239 246
240 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1); 247 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
241 $hpane->set (position_set => 1); 248 $hpane->set (position_set => 1);
242 249
243 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0); 250 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
244 251
245 { 252 {
246 $frame->add (my $vbox = new Gtk2::VBox); 253 $frame->add (my $vbox = new Gtk2::VBox);
247 $vbox->add ($self->{title} = new Gtk2::Label $title); 254 $vbox->add ($self->{title} = new Gtk2::Label "-");
248 255
249 $vbox->add (my $hbox = new Gtk2::HBox); 256 $vbox->add (my $hbox = new Gtk2::HBox);
250 257
251 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0); 258 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0);
252 259
253 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0; 260 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
254 261
255 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); 262 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
256 $scale->set_draw_value (0); 263 $scale->set_draw_value (0);
257 $scale->set_digits (0); 264 $scale->set_digits (0);
258 265
259 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 266 $self->{moveadj}->signal_connect (value_changed => sub {
267 $self->{showmove} = int $self->{moveadj}->get_value;
268 $self->update_board;
269 });
260 } 270 }
261 271
262 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); 272 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
263 273
264 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) 274 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
312 } 322 }
313 }); 323 });
314 } 324 }
315} 325}
316 326
327### JOIN/LEAVE ##############################################################
328
329sub join {
330 my ($self) = @_;
331 return if $self->{joined};
332
333 $self->SUPER::join;
334}
335
336sub event_join {
337 my ($self) = @_;
338
339 $self->SUPER::event_join (@_);
340 $self->init_tree;
341 $self->event_update_game;
342}
343
344sub event_part {
345 my ($self) = @_;
346
347 $self->SUPER::event_part;
348 $self->destroy;
349}
350
351sub event_quit {
352 my ($self) = @_;
353
354 $self->SUPER::event_quit;
355 $self->destroy;
356}
357
358### USERS ###################################################################
359
360sub draw_users {
361 my ($self, $inlay) = @_;
362
363 for (sort keys %{$self->{users}}) {
364 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
365 }
366}
367
317sub event_update_users { 368sub event_update_users {
318 my ($self, $add, $update, $remove) = @_; 369 my ($self, $add, $update, $remove) = @_;
319 370
320# $self->{userlist}->update ($add, $update, $remove); 371# $self->{userlist}->update ($add, $update, $remove);
321 372
337 $self->{chat}->append_text ("\n<header>Parts:</header>"); 388 $self->{chat}->append_text ("\n<header>Parts:</header>");
338 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; 389 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
339 } 390 }
340} 391}
341 392
342sub join { 393### GAME INFO ###############################################################
394
395sub draw_setup {
343 my ($self) = @_; 396 my ($self, $inlay) = @_;
397
344 return if $self->{joined}; 398 return unless $self->{joined};
345 399
346 $self->SUPER::join; 400 my $rules = $self->{rules};
401
402 my $text = "";
403
404 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
405 if $self->{teacher};
406
407 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
408 if $self->{owner}->is_valid;
409
410 if ($self->is_inprogress) {
411 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
412 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
413 }
414 $text .= "\nType: " . util::toxml $gametype{$self->type};
415
416 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
417
418 $text .= "\nTime: ";
419
420 if ($rules->{timesys} == TIMESYS_NONE) {
421 $text .= "UNLIMITED";
422 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
423 $text .= util::format_time $rules->{time};
424 $text .= " ABS";
425 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
426 $text .= util::format_time $rules->{time};
427 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
428 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
429 $text .= util::format_time $rules->{time};
430 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
431 }
432
433 $text .= "\nFlags:";
434 $text .= " private" if $self->is_private;
435 $text .= " started" if $self->is_inprogress;
436 $text .= " adjourned" if $self->is_adjourned;
437 $text .= " scored" if $self->is_scored;
438 $text .= " saved" if $self->is_saved;
439
440 if ($self->is_inprogress) {
441 $text .= "\nHandicap: " . $self->{handicap};
442 $text .= "\nKomi: " . $self->{komi};
443 $text .= "\nSize: " . $self->size_string;
444 }
445
446 if ($self->is_scored) {
447 $text .= "\nResult: " . $self->score_string;
448 }
449
450 $inlay->append_text ("<infoblock>$text</infoblock>");
451
452}
453
454sub event_update_game {
455 my ($self) = @_;
456
457 $self->SUPER::event_update_game;
458
459 return unless $self->{joined};
460
461 $self->{colour} = $self->player_colour ($self->{conn}{name});
462
463 $self->{user}[COLOUR_BLACK] = $self->{black};
464 $self->{user}[COLOUR_WHITE] = $self->{white};
465
466 # show board
467 if ($self->is_inprogress) {
468 if (!$self->{board}) {
469 $self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
470 $self->{board}->signal_connect (button_release => sub {
471 return unless $self->{cur_board};
472 if ($_[1] == 1) {
473 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
474 }
475 });
476 $self->{board}->show_all;
477 }
478 if (my $ch = delete $self->{challenge}) {
479 $_->{inlay}->destroy for values %$ch;
480 }
481 $self->update_cursor;
482 }
483
484 my $title = defined $self->{channel}
485 ? $self->owner->as_string . " " . $self->opponent_string
486 : "Game Window";
487 $self->set_title ("KGS Game $title");
488 $self->{title}->set_text ($title); # title gets redrawn wrongly
489
490 $self->{rules_inlay}->refresh;
491
492 if (exists $self->{teacher}) {
493 $self->{teacher_inlay} ||= $self->{chat}->new_inlay;
494 $self->{teacher_inlay}->clear;
495 $self->{teacher_inlay}->append_text ("\n<header>Teacher:</header> <user>"
496 . (util::toxml $self->{teacher}) . "</user>");
497 } elsif ($self->{teacher_inlay}) {
498 (delete $self->{teacher_inlay})->clear;
499 }
500
501 $self->update_cursor;
502}
503
504sub event_update_rules {
505 my ($self, $rules) = @_;
506
507 $self->{rules} = $rules;
508
509 if ($self->{user}) {
510 # todo. gets drawn wrongly
511
512 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
513 for COLOUR_BLACK, COLOUR_WHITE;
514 }
515
516 sound::play 3, "gamestart";
517 $self->{rules_inlay}->refresh;
518}
519
520### BOARD DISPLAY ###########################################################
521
522sub update_timers {
523 my ($self, $timers) = @_;
524
525 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
526
527 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
528 my $t = $timers->[$colour];
529 $self->{userpanel}[$colour]->set_timer (
530 $running && $colour == $self->{whosemove} && $t->[0],
531 $t->[1] || $self->{rules}{time}
532 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
533 && $self->{rules}{interval} * $self->{rules}{count}),
534 $t->[2]);
535 }
536}
537
538sub inject_set_gametime {
539 my ($self, $msg) = @_;
540
541 $self->{timers} = [
542 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
543 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
544 ];
545
546 $self->update_timers ($self->{timers})
547 if $self->{showmove} == @{$self->{path}};
347} 548}
348 549
349sub update_cursor { 550sub update_cursor {
350 my ($self) = @_; 551 my ($self) = @_;
351 552
352 my $move = int $self->{moveadj}->get_value; 553 return unless $self->{cur_board};
353 my $cb;
354 554
355 my $running = $move == @{$self->{path}} && $self->is_active; 555 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active;
356 556
357 delete $self->{board_click}; 557 delete $self->{board_click};
358 558
559 $self->{colour} = COLOUR_BLACK;#d#
560 $self->{whosemove} = COLOUR_BLACK;#d#
359 if ($self->{teacher} eq $self->{app}{conn}) { 561 if ($self->{teacher} eq $self->{app}{conn}) {
360 #TODO# # teaching mode not implemented 562 #TODO# # teaching mode not implemented
361 $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1); 563 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
362 $self->{button_undo}->hide; 564 $self->{button_undo}->hide;
363 $self->{button_resign}->hide; 565 $self->{button_resign}->hide;
364 $self->{board}->set (cursor => undef); 566 $self->{board}->set (cursor => undef);
365 567
366 } elsif ($running && $self->{colour} != COLOUR_NONE) { 568 } elsif ($running && $self->{colour} != COLOUR_NONE) {
368 $self->{button_undo}->show; 570 $self->{button_undo}->show;
369 $self->{button_resign}->show; 571 $self->{button_resign}->show;
370 572
371 if ($self->{cur_board}{score}) { 573 if ($self->{cur_board}{score}) {
372 # during scoring 574 # during scoring
373 $self->{button_pass}->set (label => "Done", sensitive => 1, visible => 1); 575 $self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1);
374 $self->{board}->set (cursor => sub { 576 $self->{board}->set (cursor => sub {
375 $_[0] & (MARK_B | MARK_W) 577 $_[0] & (MARK_B | MARK_W)
376 ? $_[0] ^ MARK_GRAYED 578 ? $_[0] ^ MARK_GRAYED
377 : $_[0]; 579 : $_[0];
378 }); 580 });
388 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED), 590 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED),
389 ); 591 );
390 } 592 }
391 }; 593 };
392 594
393 } elsif (1 - $self->{colour} == $self->{cur_board}{last}) { 595 } elsif ($self->{colour} == $self->{whosemove}) {
394 # normal move 596 # normal move
395 $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1); 597 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
396 $self->{board}->set (cursor => sub { 598 $self->{board}->set (cursor => sub {
397 # if is_valid_move oder so#TODO# 599 $self->{cur_board}
398 $_[0] & (MARK_B | MARK_W) 600 && $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
399 ? $_[0] 601 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND)
400 : $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B); 602 ? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B)
603 : $_[0];
401 }); 604 });
402 $self->{board_click} = sub { 605 $self->{board_click} = sub {
606 return unless
607 $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
608 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND);
403 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); 609 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
404 $self->{board}->set (cursor => undef); 610 $self->{board}->set (cursor => undef);
405 delete $self->{board_click}; 611 delete $self->{board_click};
406 $self->{button_pass}->sensitive (0); 612 $self->{button_pass}->sensitive (0);
407 }; 613 };
408 } else { 614 } else {
409 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1); 615 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1);
616 $self->{board}->set (cursor => undef);
410 } 617 }
411 } else { 618 } else {
412 $self->{button_undo}->hide; 619 $self->{button_undo}->hide;
413 $self->{button_resign}->hide; 620 $self->{button_resign}->hide;
414 $self->{button_pass}->hide; 621 $self->{button_pass}->hide;
417 } 624 }
418} 625}
419 626
420sub update_board { 627sub update_board {
421 my ($self) = @_; 628 my ($self) = @_;
629
422 return unless $self->{path}; 630 return unless $self->{path};
423 631
424 my $move = int $self->{moveadj}->get_value; 632 if ($self->{rules}{ruleset} == RULESET_JAPANESE) {
633 if ($self->{curnode}{move} == 0) {
634 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
635 } else {
636 $self->{whosemove} = 1 - $self->{cur_board}{last};
637 }
638 } else {
639 # Chinese, Aga, NZ all have manual placement
640 if ($self->{curnode}{move} < $self->{handicap}) {
641 $self->{whosemove} = COLOUR_BLACK;
642 } elsif ($self->{curnode}{move} == $self->{handicap}) {
643 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
644 } else {
645 $self->{whosemove} = 1 - $self->{cur_board}{last};
646 }
647 }
425 648
426 $self->{board_label}->set_text ("Move " . ($move - 1)); 649 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1));
427 650
428 $self->{cur_board} = new KGS::Game::Board $self->{size}; 651 $self->{cur_board} = new KGS::Game::Board $self->{size};
429 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); 652 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]);
653
654 $self->update_cursor;
430 655
431 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) 656 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
432 for COLOUR_WHITE, COLOUR_BLACK; 657 for COLOUR_WHITE, COLOUR_BLACK;
433 658
659 my $start_time = $self->{rules}{time};
660
661 if ($self->{showmove} == @{$self->{path}}) {
662 $self->{timers} = [
663 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}],
664 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}],
665 ];
666 $self->update_timers ($self->{timers});
667 } else {
668 $self->update_timers ([
669 [0, @{$self->{cur_board}{timer}[0]}],
670 [0, @{$self->{cur_board}{timer}[1]}],
671 ]);
672 }
673
434 $self->{board}->set_board ($self->{cur_board}); 674 $self->{board}->set_board ($self->{cur_board});
435 675
436 $self->update_cursor; 676 if ($self->{cur_board}{score}) {
677 $self->{score_inlay} ||= $self->{chat}->new_inlay;
678 $self->{score_inlay}->clear;
679 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
680 . "\n<score>"
681 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
682 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
683 . "</score>");
684 } elsif ($self->{score_inlay}) {
685 (delete $self->{score_inlay})->clear;
686 }
437} 687}
438 688
439sub event_update_tree { 689sub event_update_tree {
440 my ($self) = @_; 690 my ($self) = @_;
691
692 (delete $self->{undo_inlay})->clear
693 if $self->{undo_inlay};
441 694
442 $self->{path} = $self->get_path; 695 $self->{path} = $self->get_path;
443 696
444 if ($self->{moveadj}) { 697 if ($self->{moveadj}) {
445 my $upper = $self->{moveadj}->upper; 698 my $upper = $self->{moveadj}->upper;
491 } 744 }
492 745
493 $self->{chat}->append_text ($text); 746 $self->{chat}->append_text ($text);
494} 747}
495 748
496sub event_join {
497 my ($self) = @_;
498
499 $self->SUPER::event_join (@_);
500 $self->init_tree;
501 $self->event_update_game;
502}
503
504sub event_part {
505 my ($self) = @_;
506
507 $self->SUPER::event_part;
508 $self->destroy;
509}
510
511sub event_move { 749sub event_move {
512 my ($self, $pass) = @_; 750 my ($self, $pass) = @_;
751
513 sound::play 1, $pass ? "pass" : "move"; 752 sound::play 1, $pass ? "pass" : "move";
514
515 if ($self->{undo_inlay}) {
516 (delete $self->{undo_inlay})->clear;
517 }
518} 753}
519 754
520sub event_update_game { 755### GAMEPLAY EVENTS #########################################################
521 my ($self) = @_;
522
523 $self->SUPER::event_update_game;
524
525 return unless $self->{joined};
526
527 $self->{colour} = $self->player_colour ($self->{conn}{name});
528
529 my $title = defined $self->{channel}
530 ? $self->owner->as_string . " " . $self->opponent_string
531 : "Game Window";
532 $self->set_title ("KGS Game $title");
533 $self->{title}->set_text ($title);
534
535 $self->{user}[COLOUR_BLACK] = $self->{black};
536 $self->{user}[COLOUR_WHITE] = $self->{white};
537
538 # show board
539 if ($self->is_inprogress) {
540 if (!$self->{boardbox}->parent) {
541 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
542 $self->{left}->add ($self->{boardbox});
543 $self->{board}->signal_connect (button_release => sub {
544 if ($_[1] == 1) {
545 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
546 }
547 });
548 }
549 if (my $ch = delete $self->{challenge}) {
550 $_->{inlay}->destroy for values %$ch;
551 }
552 $self->update_cursor;
553 }
554
555 $self->{left}->show_all;
556
557 $self->{rules_inlay}->refresh;
558
559}
560
561sub draw_setup {
562 my ($self, $inlay) = @_;
563
564 return unless $self->{joined};
565
566 my $rules = $self->{rules};
567
568 my $text = "";
569
570 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
571 if $self->{teacher};
572
573 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
574 if $self->{owner}->is_valid;
575
576 if ($self->is_inprogress) {
577 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
578 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
579 }
580 $text .= "\nType: " . util::toxml $gametype{$self->type};
581
582 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
583
584 $text .= "\nTime: ";
585
586 if ($rules->{timesys} == TIMESYS_NONE) {
587 $text .= "UNLIMITED";
588 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
589 $text .= util::format_time $rules->{time};
590 $text .= " ABS";
591 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
592 $text .= util::format_time $rules->{time};
593 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
594 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
595 $text .= util::format_time $rules->{time};
596 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
597 }
598
599 $text .= "\nFlags:";
600 $text .= " private" if $self->is_private;
601 $text .= " started" if $self->is_inprogress;
602 $text .= " adjourned" if $self->is_adjourned;
603 $text .= " scored" if $self->is_scored;
604 $text .= " saved" if $self->is_saved;
605
606 if ($self->is_inprogress) {
607 $text .= "\nHandicap: " . $self->{handicap};
608 $text .= "\nKomi: " . $self->{komi};
609 $text .= "\nSize: " . $self->size_string;
610 }
611
612 if ($self->is_scored) {
613 $text .= "\nResult: " . $self->score_string;
614 }
615
616 $inlay->append_text ("<infoblock>$text</infoblock>");
617
618}
619
620sub event_update_rules {
621 my ($self, $rules) = @_;
622
623 $self->{rules} = $rules;
624
625 if ($self->{user}) {
626 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
627 for COLOUR_BLACK, COLOUR_WHITE;
628 }
629
630 sound::play 3, "gamestart";
631
632 $self->{rules_inlay}->refresh;
633}
634 756
635sub event_resign_game { 757sub event_resign_game {
636 my ($self, $player) = @_; 758 my ($self, $player) = @_;
637 759
638 sound::play 3, "resign"; 760 sound::play 3, "resign";
639 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" 761 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
640 . "\n<user>" 762 . "\n<user>"
641 . (util::toxml $self->{user}[$msg->{player}]->as_string) 763 . (util::toxml $self->{user}[$msg->{player}]->as_string)
642 . "</user> resigned.</infoblock>"); 764 . "</user> resigned."
765 . "\n<user>"
766 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
767 . "</user> wins the game."
768 . "</infoblock>");
643} 769}
644 770
645sub event_out_of_time { 771sub event_time_win {
646 my ($self, $player) = @_; 772 my ($self, $player) = @_;
647 773
648 sound::play 3, "timewin"; 774 sound::play 3, "timewin";
649 $self->{chat}->append_text ("\n<infoblock><header>Time Over</header>" 775 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>"
776 . "\n<user>"
777 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
778 . "</user> ran out of time and lost."
650 . "\n<user>" 779 . "\n<user>"
651 . (util::toxml $self->{user}[$msg->{player}]->as_string) 780 . (util::toxml $self->{user}[$msg->{player}]->as_string)
781 . "</user> wins the game."
652 . "</user> ran out of time.</infoblock>"); 782 . "</infoblock>");
783}
784
785sub event_owner_left {
786 my ($self) = @_;
787
788 sound::play 2, "info";
789 $self->{chat}->append_text ("\n<infoblock><header>Owner left</header>"
790 . "\nThe owner of this game left.</infoblock>");
791}
792
793sub event_teacher_left {
794 my ($self) = @_;
795
796 sound::play 2, "info";
797 $self->{chat}->append_text ("\n<infoblock><header>Teacher left</header>"
798 . "\nThe teacher left the game.</infoblock>");
653} 799}
654 800
655sub event_done { 801sub event_done {
656 my ($self) = @_; 802 my ($self) = @_;
657 803
658 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) { 804 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) {
805 sound::play 2, "info" unless $inlay->{count};
659 $self->{chat}->append_text ("\n<infoblock><header>Done</header>" 806 $self->{chat}->append_text ("\n<infoblock><header>Press Done</header>"
660 . "\nYour opponent pressed done."); 807 . "\nYour opponent pressed done. Now it's up to you.");
661 } 808 }
809 if ($self->{doneid} & 0x80000000) {
810 sound::play 2, "info" unless $inlay->{count};
811 $self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>"
812 . "\nThe board has changed.");
813 }
814
815 $self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]);
816
817 $self->{chat}->set_end;
662} 818}
663 819
664sub inject_final_result { 820sub inject_final_result {
665 my ($self, $msg) = @_; 821 my ($self, $msg) = @_;
666 822
669 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) 825 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
670 . "</infoblock>" 826 . "</infoblock>"
671 ); 827 );
672} 828}
673 829
674sub inject_set_gametime {
675 my ($self, $msg) = @_;
676
677 my $move = int $self->{moveadj}->get_value;
678
679 my $running = $move == @{$self->{path}} && !$self->{teacher};
680
681 $self->{userpanel}[COLOUR_BLACK]->set_timer (
682 $running && $self->{lastmove_colour} == COLOUR_WHITE && $msg->{NOW},
683 $msg->{black_time}, $msg->{black_moves});
684 $self->{userpanel}[COLOUR_WHITE]->set_timer (
685 $running && $self->{lastmove_colour} == COLOUR_BLACK && $msg->{NOW},
686 $msg->{white_time}, $msg->{white_moves});
687}
688
689sub inject_req_undo { 830sub inject_req_undo {
690 my ($self, $msg) = @_; 831 my ($self, $msg) = @_;
691 832
692 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay; 833 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay;
693 return if $inlay->{ignore}; 834 return if $inlay->{ignore};
694 835
695 sound::play 3, "warning" unless $inlay->{count}; 836 sound::play 2, "warning" unless $inlay->{count};
696 $inlay->{count}++; 837 $inlay->{count}++;
697 838
698 $inlay->clear; 839 $inlay->clear;
699 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n"); 840 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n");
700 $inlay->append_button ("Grant", sub { 841 $inlay->append_button ("Grant", sub {
701 $inlay->clear; 842 (delete $self->{undo_inlay})->clear;
702 $self->send (grant_undo => channel => $self->{channel}); 843 $self->send (grant_undo => channel => $self->{channel});
703 }); 844 });
704 $inlay->append_button ("Ignore", sub { 845 $inlay->append_button ("Ignore", sub {
705 $inlay->clear; 846 $inlay->clear;
706 $inlay->{ignore} = 1; 847 $inlay->{ignore} = 1;
711} 852}
712 853
713sub inject_new_game { 854sub inject_new_game {
714 my ($self, $msg) = @_; 855 my ($self, $msg) = @_;
715 856
716 $self->{chat}->append_text ("\n<header>ACK from server ($msg->{cid} == $self->{cid})</header>"); 857 if ($msg->{cid} != $self->{cid}) {
858 $self->part;
859 warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d#
860 }
861
862 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>");
717 delete $self->{cid}; 863 delete $self->{cid};
718} 864}
865
866### CHALLENGE HANDLING ######################################################
719 867
720sub draw_challenge { 868sub draw_challenge {
721 my ($self, $id) = @_; 869 my ($self, $id) = @_;
722 870
723 my $info = $self->{challenge}{$id}; 871 my $info = $self->{challenge}{$id};
725 my $rules = $info->{rules}; 873 my $rules = $info->{rules};
726 874
727 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;; 875 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;;
728 my $opponent = $as_black ? $info->{white} : $info->{black}; 876 my $opponent = $as_black ? $info->{white} : $info->{black};
729 877
730 my ($size, $time, $interval, $count); 878 my ($size, $time, $interval, $count, $type);
731 879
732 if (!$self->{channel}) { 880 if (!$self->{channel}) {
733 $inlay->append_text ("\nNotes: "); 881 $inlay->append_text ("\nNotes: ");
734 $inlay->append_entry (\$info->{notes}, 20, ""); 882 $inlay->append_entry (\$info->{notes}, 20, "");
735 $inlay->append_text ("\nGlobal Offer: "); 883 $inlay->append_text ("\nGlobal Offer: ");
740 } else { 888 } else {
741 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes}); 889 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes});
742 } 890 }
743 891
744 $inlay->append_text ("\nType: "); 892 $inlay->append_text ("\nType: ");
745 $inlay->append_optionmenu ( 893 $type = $inlay->append_optionmenu (
746 \$info->{gametype}, 894 \$info->{gametype},
747 GAMETYPE_DEMONSTRATION , "Demonstration", 895 GAMETYPE_DEMONSTRATION , "Demonstration (not yet)",
748 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P)", 896 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)",
749 GAMETYPE_TEACHING , "Teaching", 897 GAMETYPE_TEACHING , "Teaching (not yet)",
750 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P)", 898 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)",
751 GAMETYPE_SIMUL , "Simul (not yet!)", 899 GAMETYPE_SIMUL , "Simul (not yet!)",
752 GAMETYPE_FREE , "Free", 900 GAMETYPE_FREE , "Free",
753 GAMETYPE_RATED , "Rated", 901 GAMETYPE_RATED , "Rated",
754 sub { 902 sub {
755 $size->set_history (2) if $_[0] eq GAMETYPE_RATED; 903 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
779 RULESET_NEW_ZEALAND, "New Zealand", 927 RULESET_NEW_ZEALAND, "New Zealand",
780 ); 928 );
781 929
782 $inlay->append_text ("\nSize: "); 930 $inlay->append_text ("\nSize: ");
783 $size = $inlay->append_optionmenu ( 931 $size = $inlay->append_optionmenu (
932 \$info->{rules}{size},
784 \$info->{rules}{size}, 9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38 933 (9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38),
934 sub {
935 $type->set_history (5) # reset to free
936 if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED;
937 },
785 ); 938 );
786 939
787 if ($self->{channel}) { 940 if ($self->{channel}) {
788 $inlay->append_text ("\nHandicap: "); 941 $inlay->append_text ("\nHandicap: ");
789 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); 942 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
874 }); 1027 });
875 } 1028 }
876 } 1029 }
877} 1030}
878 1031
879sub draw_users { 1032sub new_game_challenge {
880 my ($self, $inlay) = @_; 1033 my ($self) = @_;
881 1034
882 for (sort keys %{$self->{users}}) { 1035 my $d = $self->{app}{defaults};
883 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); 1036
1037 $self->{challenge}{""} = {
1038 gametype => $d->{gametype},
1039 flags => 0,
1040 notes => $d->{stones},
1041 rules => {
1042 ruleset => $d->{ruleset},
1043 size => $d->{size},
1044 timesys => $d->{timesys},
1045 time => $d->{time},
1046 interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time},
1047 count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones},
1048 },
1049
1050 inlay => $self->{chat}->new_inlay,
884 } 1051 };
1052 $self->draw_challenge ("");
885} 1053}
886 1054
887sub event_challenge { 1055sub event_challenge {
888 my ($self, $info) = @_; 1056 my ($self, $info) = @_;
889 1057
890 my $as_black = $info->{black}->{name} eq $self->{conn}{name}; 1058 my $as_black = $info->{black}->{name} eq $self->{conn}{name};
891 my $opponent = $as_black ? $info->{white} : $info->{black}; 1059 my $opponent = $as_black ? $info->{white} : $info->{black};
892 1060
893 my $id = $opponent->{name}; 1061 my $id = $opponent->{name};
1062
1063 sound::play 2, "info";
894 1064
895 $self->{challenge}{$id} = $info; 1065 $self->{challenge}{$id} = $info;
896 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay ( 1066 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay (
897 exists $self->{challenge}{""} 1067 exists $self->{challenge}{""}
898 ? "Challenge from $opponent->{name}" 1068 ? "Challenge from " . $opponent->as_string
899 : "Challenge to $opponent->{name}", 1069 : "Challenge to " . $opponent->as_string,
900 sub { 1070 sub {
901 $self->{challenge}{$id}{inlay} = $_[0]; 1071 $self->{challenge}{$id}{inlay} = $_[0];
902 $self->draw_challenge ($id); 1072 $self->draw_challenge ($id);
903 }, 1073 },
904 !exists $self->{challenge}{""} # only open when not offerer 1074 !exists $self->{challenge}{""} # only open when not offerer

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines