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.110 by root, Mon May 31 17:06:19 2004 UTC vs.
Revision 1.130 by root, Thu Jun 3 15:30:09 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.
32sub configure { 34sub configure {
33 my ($self, $timesys, $main, $interval, $count) = @_; 35 my ($self, $timesys, $main, $interval, $count) = @_;
34 36
35 if ($timesys == TIMESYS_ABSOLUTE) { 37 if ($timesys == TIMESYS_ABSOLUTE) {
36 $self->{format} = sub { 38 $self->{format} = sub {
37 if ($_[0] <= 0) { 39 if ($_[0] < 0) {
38 "TIMEOUT"; 40 "TIMEOUT";
39 } else { 41 } else {
40 util::format_time $_[0]; 42 util::format_time $_[0];
41 } 43 }
42 }; 44 };
43 45
44 } elsif ($timesys == TIMESYS_BYO_YOMI) { 46 } elsif ($timesys == TIMESYS_BYO_YOMI) {
45 my $low = $interval * $count; 47 my $low = $interval * $count;
46 48
47 $self->{format} = sub { 49 $self->{format} = sub {
48 if ($_[0] <= 0) { 50 if ($_[0] < 0) {
49 "TIMEOUT"; 51 "TIMEOUT";
50 } elsif ($_[0] > $low) { 52 } elsif ($_[0] > $low) {
51 util::format_time $_[0] - $low; 53 util::format_time $_[0] - $low;
52 } else { 54 } else {
53 sprintf "%s (%d)", 55 sprintf "%s (%d)",
56 } 58 }
57 }; 59 };
58 60
59 } elsif ($timesys == TIMESYS_CANADIAN) { 61 } elsif ($timesys == TIMESYS_CANADIAN) {
60 $self->{format} = sub { 62 $self->{format} = sub {
61 if ($_[0] <= 0) { 63 if ($_[0] < 0) {
62 "TIMEOUT"; 64 "TIMEOUT";
63 } elsif (!$self->{moves}) { 65 } elsif (!$self->{moves}) {
64 util::format_time $_[0] - $low; 66 util::format_time $_[0] - $low;
65 } else { 67 } else {
66 my $time = int (($_[0] - 1) % $interval + 1); 68 my $time = int (($_[0] - 1) % $interval + 1);
127 my ($self) = @_; 129 my ($self) = @_;
128 130
129 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 131 remove Glib::Source delete $self->{timeout} if $self->{timeout};
130} 132}
131 133
134### USER PANEL ##############################################################
135
132package game::userpanel; 136package game::userpanel;
133 137
138use KGS::Constants;
139
134use Glib::Object::Subclass 140use Glib::Object::Subclass
135 Gtk2::HBox, 141 Gtk2::Frame,
136 properties => [ 142 properties => [
137 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)]),
138 ]; 146 ];
139 147
140sub INIT_INSTANCE { 148sub INIT_INSTANCE {
141 my ($self) = @_; 149 my ($self) = @_;
142 150
151 $self->add ($self->{window} = my $window = new Gtk2::EventBox); # for bg
152
143 $self->add (my $vbox = new Gtk2::VBox); 153 $window->add (my $vbox = new Gtk2::VBox);
144 154
145 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 155 $vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0);
146 $vbox->add ($self->{info} = new Gtk2::Label ""); 156 $vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0);
147 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock}; 157 $vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0);
148 158
149 $vbox->add ($self->{imagebox} = new Gtk2::VBox); 159 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
150 160
151 $self; 161 $self;
162}
163
164sub SET_PROPERTY {
165 my ($self, $pspec, $value) = @_;
166
167 $self->{$pspec->get_name} = $value;
168
169 $self->set_name ("userpanel-$self->{colour}");
152} 170}
153 171
154sub configure { 172sub configure {
155 my ($self, $app, $user, $rules) = @_; 173 my ($self, $app, $user, $rules) = @_;
156 174
188 my ($self, $start, $time, $moves) = @_; 206 my ($self, $start, $time, $moves) = @_;
189 207
190 $self->{clock}->set_time ($start, $time, $moves); 208 $self->{clock}->set_time ($start, $time, $moves);
191} 209}
192 210
211### GAME WINDOW #############################################################
212
193package game; 213package game;
194 214
195use Scalar::Util qw(weaken); 215use Scalar::Util qw(weaken);
196 216
197use KGS::Constants; 217use KGS::Constants;
208 228
209use POSIX qw(ceil); 229use POSIX qw(ceil);
210 230
211sub new { 231sub new {
212 my ($self, %arg) = @_; 232 my ($self, %arg) = @_;
233
213 $self = $self->Glib::Object::new; 234 $self = $self->Glib::Object::new;
214 $self->{$_} = delete $arg{$_} for keys %arg; 235 $self->{$_} = delete $arg{$_} for keys %arg;
215 236
216 gtk::state $self, "game::window", undef, window_size => [600, 500]; 237 gtk::state $self, "game::window", undef, window_size => [620, 460];
238 $self->set (allow_shrink => 1);
217 239
218 $self->signal_connect (destroy => sub { 240 $self->signal_connect (destroy => sub {
219 $self->unlisten; 241 $self->unlisten;
220 delete $self->{app}{game}{$self->{channel}}; 242 delete $self->{app}{game}{$self->{channel}};
221 %{$_[0]} = (); 243 %{$_[0]} = ();
222 });#d# 244 });#d#
223 245
224 $self->add (my $hpane = new Gtk2::HPaned); 246 $self->add (my $hpane = new Gtk2::HPaned);
225 gtk::state $hpane, "game::hpane", undef, position => 500; 247 gtk::state $hpane, "game::hpane", undef, position => 420;
226 248
227 # LEFT PANE 249 # LEFT PANE
228 250
229 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0); 251 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
230 252
231 $self->{boardbox} = new Gtk2::VBox;
232
233 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); 253 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
234 254
235 # board box (aspect/canvas) 255 # board box (aspect/canvas)
236 256
237 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
238
239 # RIGHT PANE 257 # RIGHT PANE
240 258
241 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1); 259 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
242 $hpane->set (position_set => 1); 260 $hpane->set (position_set => 1);
243 261
244 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0); 262 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
245 263
246 { 264 {
247 $frame->add (my $vbox = new Gtk2::VBox); 265 $frame->add (my $vbox = new Gtk2::VBox);
248 $vbox->add ($self->{title} = new Gtk2::Label $title); 266 $vbox->add ($self->{title} = new Gtk2::Label "-");
249 267
250 $vbox->add (my $hbox = new Gtk2::HBox); 268 $vbox->add (my $hbox = new Gtk2::HBox);
251 269
252 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0); 270 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0);
253 271
254 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0; 272 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
255 273
256 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); 274 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
257 $scale->set_draw_value (0); 275 $scale->set_draw_value (0);
269 for COLOUR_WHITE, COLOUR_BLACK; 287 for COLOUR_WHITE, COLOUR_BLACK;
270 288
271 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0); 289 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
272 290
273 $buttonbox->add ($self->{button_pass} = 291 $buttonbox->add ($self->{button_pass} =
274 Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0)); 292 Gtk2::Button->Glib::Object::new (label => "Pass", visible => 0));
275 $self->{button_pass}->signal_connect (clicked => sub { 293 $self->{button_pass}->signal_connect (clicked => sub {
276 $self->{board_click}->(255, 255) if $self->{board_click}; 294 $self->{board_click}->(255, 255) if $self->{board_click};
277 }); 295 });
296 eval { $self->{button_pass}->set (no_show_all => 1) }; # workaround for gtk+-2.2
278 $buttonbox->add ($self->{button_undo} = 297 $buttonbox->add ($self->{button_undo} =
279 Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0)); 298 Gtk2::Button->Glib::Object::new (label => "Undo", visible => 0));
280 $self->{button_undo}->signal_connect (clicked => sub { 299 $self->{button_undo}->signal_connect (clicked => sub {
281 $self->send (req_undo => channel => $self->{channel}); 300 $self->send (req_undo => channel => $self->{channel});
282 }); 301 });
302 eval { $self->{button_undo}->set (no_show_all => 1) }; # workaround for gtk+-2.2
283 $buttonbox->add ($self->{button_resign} = 303 $buttonbox->add ($self->{button_resign} =
284 Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0)); 304 Gtk2::Button->Glib::Object::new (label => "Resign", visible => 0));
285 $self->{button_resign}->signal_connect (clicked => sub { 305 $self->{button_resign}->signal_connect (clicked => sub {
286 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour}); 306 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour});
287 }); 307 });
308 eval { $self->{button_resign}->set (no_show_all => 1) }; # workaround for gtk+-2.2
288 309
289 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); 310 $vbox->pack_start (($self->{chat} = new chat), 1, 1, 0);
290 311
291 $self->set_channel ($self->{channel}); 312 $self->set_channel ($self->{channel});
292 313
293 $self->show_all; 314 $self->show_all;
294 315
316 } 337 }
317 }); 338 });
318 } 339 }
319} 340}
320 341
342### JOIN/LEAVE ##############################################################
343
344sub join {
345 my ($self) = @_;
346 return if $self->{joined};
347
348 $self->SUPER::join;
349}
350
351sub part {
352 my ($self) = @_;
353
354 $self->hide;
355 $self->SUPER::part;
356}
357
358sub event_join {
359 my ($self) = @_;
360
361 $self->SUPER::event_join (@_);
362 $self->init_tree;
363 $self->event_update_game;
364}
365
366sub event_part {
367 my ($self) = @_;
368
369 $self->SUPER::event_part;
370 $self->destroy;
371}
372
373sub event_quit {
374 my ($self) = @_;
375
376 $self->SUPER::event_quit;
377 $self->destroy;
378}
379
380### USERS ###################################################################
381
382sub draw_users {
383 my ($self, $inlay) = @_;
384
385 for (sort keys %{$self->{users}}) {
386 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
387 }
388}
389
321sub event_update_users { 390sub event_update_users {
322 my ($self, $add, $update, $remove) = @_; 391 my ($self, $add, $update, $remove) = @_;
323 392
324# $self->{userlist}->update ($add, $update, $remove); 393# $self->{userlist}->update ($add, $update, $remove);
325 394
332 $important{$self->{black}{name}}++; 401 $important{$self->{black}{name}}++;
333 $important{$self->{white}{name}}++; 402 $important{$self->{white}{name}}++;
334 $important{$self->{owner}{name}}++; 403 $important{$self->{owner}{name}}++;
335 404
336 if (my @users = grep $important{$_->{name}}, @$add) { 405 if (my @users = grep $important{$_->{name}}, @$add) {
337 $self->{chat}->append_text ("\n<header>Joins:</header>"); 406 $self->{chat}->append_text ("\n<leader>Joins:</leader>");
338 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; 407 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
339 } 408 }
340 if (my @users = grep $important{$_->{name}}, @$remove) { 409 if (my @users = grep $important{$_->{name}}, @$remove) {
341 $self->{chat}->append_text ("\n<header>Parts:</header>"); 410 $self->{chat}->append_text ("\n<leader>Parts:</leader>");
342 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; 411 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
343 } 412 }
344} 413}
345 414
346sub join { 415### GAME INFO ###############################################################
416
417sub draw_setup {
347 my ($self) = @_; 418 my ($self, $inlay) = @_;
419
348 return if $self->{joined}; 420 return unless $self->{joined};
349 421
350 $self->SUPER::join; 422 my $rules = $self->{rules};
423
424 my $text = "";
425
426 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
427 if $self->{teacher};
428
429 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
430 if $self->{owner}->is_valid;
431
432 if ($self->is_inprogress) {
433 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
434 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
435 }
436 $text .= "\nType: " . util::toxml $gametype{$self->type};
437
438 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
439
440 $text .= "\nTime: ";
441
442 if ($rules->{timesys} == TIMESYS_NONE) {
443 $text .= "UNLIMITED";
444 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
445 $text .= util::format_time $rules->{time};
446 $text .= " ABS";
447 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
448 $text .= util::format_time $rules->{time};
449 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
450 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
451 $text .= util::format_time $rules->{time};
452 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
453 }
454
455 $text .= "\nFlags:";
456 $text .= " private" if $self->is_private;
457 $text .= " started" if $self->is_inprogress;
458 $text .= " adjourned" if $self->is_adjourned;
459 $text .= " scored" if $self->is_scored;
460 $text .= " saved" if $self->is_saved;
461
462 if ($self->is_inprogress) {
463 $text .= "\nHandicap: " . $self->{handicap};
464 $text .= "\nKomi: " . $self->{komi};
465 $text .= "\nSize: " . $self->size_string;
466 }
467
468 if ($self->is_scored) {
469 $text .= "\nResult: " . $self->score_string;
470 }
471
472 $inlay->append_text ("<infoblock>$text</infoblock>");
473
474}
475
476sub event_update_game {
477 my ($self) = @_;
478
479 $self->SUPER::event_update_game;
480
481 return unless $self->{joined};
482
483 $self->{colour} = $self->player_colour ($self->{conn}{name});
484
485 $self->{user}[COLOUR_BLACK] = $self->{black};
486 $self->{user}[COLOUR_WHITE] = $self->{white};
487
488 # show board
489 if ($self->is_inprogress) {
490 if (!$self->{board}) {
491 $self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
492 $self->{board}->signal_connect (button_release => sub {
493 return unless $self->{cur_board};
494 if ($_[1] == 1) {
495 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
496 }
497 });
498 $self->{board}->show_all;
499 }
500 if (my $ch = delete $self->{challenge}) {
501 $_->{inlay}->destroy for values %$ch;
502 }
503 $self->update_cursor;
504 }
505
506 my $title = defined $self->{channel}
507 ? $self->owner->as_string . " " . $self->opponent_string
508 : "Game Window";
509 $self->set_title ("KGS Game $title");
510 $self->{title}->set_text ($title); # title gets redrawn wrongly
511
512 $self->{rules_inlay}->refresh;
513
514 if (exists $self->{teacher}) {
515 $self->{teacher_inlay} ||= $self->{chat}->new_inlay;
516 $self->{teacher_inlay}->clear;
517 $self->{teacher_inlay}->append_text ("\n<header>Teacher:</header> <user>"
518 . (util::toxml $self->{teacher}) . "</user>");
519 } elsif ($self->{teacher_inlay}) {
520 (delete $self->{teacher_inlay})->clear;
521 }
522
523 $self->update_cursor;
524}
525
526sub event_update_rules {
527 my ($self, $rules) = @_;
528
529 $self->{rules} = $rules;
530
531 if ($self->{user}) {
532 # todo. gets drawn wrongly
533
534 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
535 for COLOUR_BLACK, COLOUR_WHITE;
536 }
537
538 sound::play 3, "gamestart";
539 $self->{rules_inlay}->refresh;
540}
541
542### BOARD DISPLAY ###########################################################
543
544sub update_timers {
545 my ($self, $timers) = @_;
546
547 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
548
549 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
550 my $t = $timers->[$colour];
551 $self->{userpanel}[$colour]->set_timer (
552 $running && $colour == $self->{whosemove} && $t->[0],
553 $t->[1] || $self->{rules}{time}
554 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
555 && $self->{rules}{interval} * $self->{rules}{count}),
556 $t->[2]);
557 }
558}
559
560sub inject_set_gametime {
561 my ($self, $msg) = @_;
562
563 $self->{timers} = [
564 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
565 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
566 ];
567
568 $self->update_timers ($self->{timers})
569 if $self->{showmove} == @{$self->{path}};
351} 570}
352 571
353sub update_cursor { 572sub update_cursor {
354 my ($self) = @_; 573 my ($self) = @_;
574
575 return unless $self->{cur_board};
576
577 if ($self->{rules}{ruleset} == RULESET_JAPANESE) {
578 if ($self->{curnode}{move} == 0) {
579 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
580 } else {
581 $self->{whosemove} = 1 - $self->{cur_board}{last};
582 }
583 } else {
584 # Chinese, Aga, NZ all have manual placement
585 if ($self->{curnode}{move} < $self->{handicap}) {
586 $self->{whosemove} = COLOUR_BLACK;
587 } elsif ($self->{curnode}{move} == $self->{handicap}) {
588 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
589 } else {
590 $self->{whosemove} = 1 - $self->{cur_board}{last};
591 }
592 }
355 593
356 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active; 594 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active;
357 595
358 delete $self->{board_click}; 596 delete $self->{board_click};
359 597
360 if ($self->{teacher} eq $self->{app}{conn}) { 598 if ($self->{teacher} eq $self->{app}{conn}) {
361 #TODO# # teaching mode not implemented 599 #TODO# # teaching mode not implemented
362 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); 600 $self->{button_pass}->set (label => "Pass", sensitive => 1);
601 $self->{button_pass}->show;
363 $self->{button_undo}->hide; 602 $self->{button_undo}->hide;
364 $self->{button_resign}->hide; 603 $self->{button_resign}->hide;
365 $self->{board}->set (cursor => undef); 604 $self->{board}->set (cursor => undef);
366 605
367 } elsif ($running && $self->{colour} != COLOUR_NONE) { 606 } elsif ($running && $self->{colour} != COLOUR_NONE) {
369 $self->{button_undo}->show; 608 $self->{button_undo}->show;
370 $self->{button_resign}->show; 609 $self->{button_resign}->show;
371 610
372 if ($self->{cur_board}{score}) { 611 if ($self->{cur_board}{score}) {
373 # during scoring 612 # during scoring
374 $self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1); 613 $self->{button_pass}->set (label => "Done", sensitive => 1);
614 $self->{button_pass}->show;
375 $self->{board}->set (cursor => sub { 615 $self->{board}->set (cursor => sub {
376 $_[0] & (MARK_B | MARK_W) 616 $_[0] & (MARK_B | MARK_W)
377 ? $_[0] ^ MARK_GRAYED 617 ? $_[0] ^ MARK_GRAYED
378 : $_[0]; 618 : $_[0];
379 }); 619 });
391 } 631 }
392 }; 632 };
393 633
394 } elsif ($self->{colour} == $self->{whosemove}) { 634 } elsif ($self->{colour} == $self->{whosemove}) {
395 # normal move 635 # normal move
396 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); 636 $self->{button_pass}->set (label => "Pass", sensitive => 1);
637 $self->{button_pass}->show;
397 $self->{board}->set (cursor => sub { 638 $self->{board}->set (cursor => sub {
398 # if is_valid_move oder so#TODO# 639 $self->{cur_board}
399 $_[0] & (MARK_B | MARK_W) 640 && $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
400 ? $_[0] 641 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND)
401 : $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B); 642 ? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B)
643 : $_[0];
402 }); 644 });
403 $self->{board_click} = sub { 645 $self->{board_click} = sub {
646 return unless
647 $self->{cur_board}->is_valid_move ($self->{colour}, $_[0], $_[1],
648 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND);
404 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); 649 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
405 $self->{board}->set (cursor => undef); 650 $self->{board}->set (cursor => undef);
406 delete $self->{board_click}; 651 delete $self->{board_click};
407 $self->{button_pass}->sensitive (0); 652 $self->{button_pass}->sensitive (0);
408 }; 653 };
409 } else { 654 } else {
410 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1); 655 $self->{button_pass}->set (label => "Pass", sensitive => 0);
656 $self->{button_pass}->show;
411 $self->{board}->set (cursor => undef); 657 $self->{board}->set (cursor => undef);
412 } 658 }
413 } else { 659 } else {
414 $self->{button_undo}->hide; 660 $self->{button_undo}->hide;
415 $self->{button_resign}->hide; 661 $self->{button_resign}->hide;
417 $self->{board}->set (cursor => undef); 663 $self->{board}->set (cursor => undef);
418 #TODO# # implement coordinate-grabbing 664 #TODO# # implement coordinate-grabbing
419 } 665 }
420} 666}
421 667
422sub update_timers {
423 my ($self, $timers) = @_;
424
425 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
426
427 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
428 my $t = $timers->[$colour];
429 $self->{userpanel}[$colour]->set_timer (
430 $running && $colour == $self->{whosemove} && $t->[0],
431 $t->[1] || $self->{rules}{time}
432 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
433 && $self->{rules}{interval} * $self->{rules}{count}),
434 $t->[2] || $self->{rules}{count});
435 }
436}
437
438sub update_board { 668sub update_board {
439 my ($self) = @_; 669 my ($self) = @_;
670
440 return unless $self->{path}; 671 return unless $self->{path};
441 672
442 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1)); 673 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1));
443 674
444 $self->{cur_board} = new KGS::Game::Board $self->{size}; 675 $self->{cur_board} = new KGS::Game::Board $self->{size};
445 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]); 676 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]);
446 677
447 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) 678 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
448 for COLOUR_WHITE, COLOUR_BLACK; 679 for COLOUR_WHITE, COLOUR_BLACK;
449 680
450 if ($self->{rules}{ruleset} == RULESET_JAPANESE) { 681 $self->{board}->set_board ($self->{cur_board});
451 if ($self->{curnode}{move} == 0) {
452 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
453 } else {
454 $self->{whosemove} = 1 - $self->{cur_board}{last};
455 }
456 } else {
457 # Chinese, Aga, NZ all have manual placement
458 if ($self->{curnode}{move} < $self->{handicap}) {
459 $self->{whosemove} = COLOUR_BLACK;
460 } elsif ($self->{curnode}{move} == $self->{handicap}) {
461 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
462 } else {
463 $self->{whosemove} = 1 - $self->{cur_board}{last};
464 }
465 }
466 682
467 my $start_time = $self->{rules}{time}; 683 if ($self->{cur_board}{score}) {
684 $self->{score_inlay} ||= $self->{chat}->new_inlay;
685 $self->{score_inlay}->clear;
686 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
687 . "\n<score>"
688 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
689 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
690 . "</score>");
691 } elsif ($self->{score_inlay}) {
692 (delete $self->{score_inlay})->clear;
693 }
694
695 $self->update_cursor;
468 696
469 if ($self->{showmove} == @{$self->{path}}) { 697 if ($self->{showmove} == @{$self->{path}}) {
470 $self->{timers} = [ 698 $self->{timers} = [
471 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}], 699 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}],
472 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}], 700 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}],
477 [0, @{$self->{cur_board}{timer}[0]}], 705 [0, @{$self->{cur_board}{timer}[0]}],
478 [0, @{$self->{cur_board}{timer}[1]}], 706 [0, @{$self->{cur_board}{timer}[1]}],
479 ]); 707 ]);
480 } 708 }
481 709
482 $self->{board}->set_board ($self->{cur_board});
483
484 if ($self->{cur_board}{score}) {
485 $self->{score_inlay} ||= $self->{chat}->new_inlay;
486 $self->{score_inlay}->clear;
487 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
488 . "\n<score>"
489 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
490 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
491 . "</score>");
492 } elsif ($self->{score_inlay}) {
493 (delete $self->{score_inlay})->clear;
494 }
495
496 $self->update_cursor;
497} 710}
498 711
499sub event_update_tree { 712sub event_update_tree {
500 my ($self) = @_; 713 my ($self) = @_;
501 714
519 } 732 }
520} 733}
521 734
522sub event_update_comments { 735sub event_update_comments {
523 my ($self, $node, $comment, $newnode) = @_; 736 my ($self, $node, $comment, $newnode) = @_;
524 $self->SUPER::event_update_comments($node, $comment, $newnode); 737 $self->SUPER::event_update_comments ($node, $comment, $newnode);
525 738
526 my $text; 739 my $text;
527 740
528 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>" 741 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
529 if $newnode; 742 if $newnode;
554 } 767 }
555 768
556 $self->{chat}->append_text ($text); 769 $self->{chat}->append_text ($text);
557} 770}
558 771
559sub event_join {
560 my ($self) = @_;
561
562 $self->SUPER::event_join (@_);
563 $self->init_tree;
564 $self->event_update_game;
565}
566
567sub event_part {
568 my ($self) = @_;
569
570 $self->SUPER::event_part;
571 $self->destroy;
572}
573
574sub event_move { 772sub event_move {
575 my ($self, $pass) = @_; 773 my ($self, $pass) = @_;
576 774
577 sound::play 1, $pass ? "pass" : "move"; 775 sound::play 1, $pass ? "pass" : "move";
578} 776}
579 777
580sub event_update_game { 778### GAMEPLAY EVENTS #########################################################
581 my ($self) = @_;
582
583 $self->SUPER::event_update_game;
584
585 return unless $self->{joined};
586
587 $self->{colour} = $self->player_colour ($self->{conn}{name});
588
589 my $title = defined $self->{channel}
590 ? $self->owner->as_string . " " . $self->opponent_string
591 : "Game Window";
592 $self->set_title ("KGS Game $title");
593 $self->{title}->set_text ($title);
594
595 $self->{user}[COLOUR_BLACK] = $self->{black};
596 $self->{user}[COLOUR_WHITE] = $self->{white};
597
598 # show board
599 if ($self->is_inprogress) {
600 if (!$self->{boardbox}->parent) {
601 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
602 $self->{left}->add ($self->{boardbox});
603 $self->{board}->signal_connect (button_release => sub {
604 if ($_[1] == 1) {
605 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
606 }
607 });
608 }
609 if (my $ch = delete $self->{challenge}) {
610 $_->{inlay}->destroy for values %$ch;
611 }
612 $self->update_cursor;
613 }
614
615 $self->{left}->show_all;
616
617 $self->{rules_inlay}->refresh;
618
619}
620
621sub draw_setup {
622 my ($self, $inlay) = @_;
623
624 return unless $self->{joined};
625
626 my $rules = $self->{rules};
627
628 my $text = "";
629
630 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
631 if $self->{teacher};
632
633 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
634 if $self->{owner}->is_valid;
635
636 if ($self->is_inprogress) {
637 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
638 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
639 }
640 $text .= "\nType: " . util::toxml $gametype{$self->type};
641
642 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
643
644 $text .= "\nTime: ";
645
646 if ($rules->{timesys} == TIMESYS_NONE) {
647 $text .= "UNLIMITED";
648 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
649 $text .= util::format_time $rules->{time};
650 $text .= " ABS";
651 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
652 $text .= util::format_time $rules->{time};
653 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
654 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
655 $text .= util::format_time $rules->{time};
656 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
657 }
658
659 $text .= "\nFlags:";
660 $text .= " private" if $self->is_private;
661 $text .= " started" if $self->is_inprogress;
662 $text .= " adjourned" if $self->is_adjourned;
663 $text .= " scored" if $self->is_scored;
664 $text .= " saved" if $self->is_saved;
665
666 if ($self->is_inprogress) {
667 $text .= "\nHandicap: " . $self->{handicap};
668 $text .= "\nKomi: " . $self->{komi};
669 $text .= "\nSize: " . $self->size_string;
670 }
671
672 if ($self->is_scored) {
673 $text .= "\nResult: " . $self->score_string;
674 }
675
676 $inlay->append_text ("<infoblock>$text</infoblock>");
677
678}
679
680sub event_update_rules {
681 my ($self, $rules) = @_;
682
683 $self->{rules} = $rules;
684
685 if ($self->{user}) {
686 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
687 for COLOUR_BLACK, COLOUR_WHITE;
688 }
689
690 sound::play 3, "gamestart";
691 $self->{rules_inlay}->refresh;
692}
693 779
694sub event_resign_game { 780sub event_resign_game {
695 my ($self, $player) = @_; 781 my ($self, $player) = @_;
696 782
697 sound::play 3, "resign"; 783 sound::play 3, "resign";
698 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" 784 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
699 . "\n<user>" 785 . "\n<user>"
700 . (util::toxml $self->{user}[$msg->{player}]->as_string) 786 . (util::toxml $self->{user}[$msg->{player}]->as_string)
701 . "</user> resigned.</infoblock>"); 787 . "</user> resigned."
788 . "\n<user>"
789 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
790 . "</user> wins the game."
791 . "</infoblock>");
702} 792}
703 793
704sub event_out_of_time { 794sub event_time_win {
705 my ($self, $player) = @_; 795 my ($self, $player) = @_;
706 796
707 sound::play 3, "timewin"; 797 sound::play 3, "timewin";
708 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>" 798 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>"
709 . "\n<user>" 799 . "\n<user>"
800 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
801 . "</user> ran out of time and lost."
802 . "\n<user>"
710 . (util::toxml $self->{user}[$msg->{player}]->as_string) 803 . (util::toxml $self->{user}[$msg->{player}]->as_string)
804 . "</user> wins the game."
805 . "</infoblock>");
806}
807
808sub event_owner_left {
809 my ($self) = @_;
810
811 $self->{chat}->append_text ("\n<infoblock><header>Owner left</header>"
711 . "</user> ran out of time and lost.</infoblock>"); 812 . "\nThe owner of this game left.</infoblock>");
813}
814
815sub event_teacher_left {
816 my ($self) = @_;
817
818 $self->{chat}->append_text ("\n<infoblock><header>Teacher left</header>"
819 . "\nThe teacher left the game.</infoblock>");
712} 820}
713 821
714sub event_done { 822sub event_done {
715 my ($self) = @_; 823 my ($self) = @_;
716 824
736 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>" 844 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
737 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) 845 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
738 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) 846 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
739 . "</infoblock>" 847 . "</infoblock>"
740 ); 848 );
741}
742
743sub inject_set_gametime {
744 my ($self, $msg) = @_;
745
746 $self->{timers} = [
747 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
748 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
749 ];
750
751 $self->update_timers ($self->{timers})
752 if $self->{showmove} == @{$self->{path}};
753} 849}
754 850
755sub inject_req_undo { 851sub inject_req_undo {
756 my ($self, $msg) = @_; 852 my ($self, $msg) = @_;
757 853
786 882
787 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>"); 883 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>");
788 delete $self->{cid}; 884 delete $self->{cid};
789} 885}
790 886
887### CHALLENGE HANDLING ######################################################
888
791sub draw_challenge { 889sub draw_challenge {
792 my ($self, $id) = @_; 890 my ($self, $id) = @_;
793 891
794 my $info = $self->{challenge}{$id}; 892 my $info = $self->{challenge}{$id};
795 my $inlay = $info->{inlay}; 893 my $inlay = $info->{inlay};
800 898
801 my ($size, $time, $interval, $count, $type); 899 my ($size, $time, $interval, $count, $type);
802 900
803 if (!$self->{channel}) { 901 if (!$self->{channel}) {
804 $inlay->append_text ("\nNotes: "); 902 $inlay->append_text ("\nNotes: ");
805 $inlay->append_entry (\$info->{notes}, 20, ""); 903 $inlay->append_widget (gtk::textentry \$info->{notes}, 20, "");
806 $inlay->append_text ("\nGlobal Offer: "); 904 $inlay->append_text ("\nGlobal Offer: ");
807 $inlay->append_optionmenu (\$info->{flags}, 905 $inlay->append_optionmenu (\$info->{flags},
808 0 => "No", 906 0 => "No",
809 2 => "Yes", 907 2 => "Yes",
810 ); 908 );
863 if ($self->{channel}) { 961 if ($self->{channel}) {
864 $inlay->append_text ("\nHandicap: "); 962 $inlay->append_text ("\nHandicap: ");
865 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); 963 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
866 964
867 $inlay->append_text ("\nKomi: "); 965 $inlay->append_text ("\nKomi: ");
868 $inlay->append_entry (\$info->{rules}{komi}, 5); 966 $inlay->append_widget (gtk::numentry \$info->{rules}{komi}, 5);
869 } 967 }
870 968
871 $inlay->append_text ("\nTimesys: "); 969 $inlay->append_text ("\nTimesys: ");
872 $inlay->append_optionmenu ( 970 $inlay->append_optionmenu (
873 \$info->{rules}{timesys}, 971 \$info->{rules}{timesys},
902 } 1000 }
903 } 1001 }
904 ); 1002 );
905 1003
906 $inlay->append_text ("\nMain Time: "); 1004 $inlay->append_text ("\nMain Time: ");
907 $time = $inlay->append_entry (\$info->{rules}{time}, 5); 1005 $time = $inlay->append_widget (gtk::timeentry \$info->{rules}{time}, 5);
908 $inlay->append_text ("\nInterval: "); 1006 $inlay->append_text ("\nInterval: ");
909 $interval = $inlay->append_entry (\$info->{rules}{interval}, 3); 1007 $interval = $inlay->append_widget (gtk::timeentry \$info->{rules}{interval}, 5);
910 $inlay->append_text ("\nPeriods/Stones: "); 1008 $inlay->append_text ("\nPeriods/Stones: ");
911 $count = $inlay->append_entry (\$info->{rules}{count}, 2); 1009 $count = $inlay->append_widget (gtk::numentry \$info->{rules}{count}, 5);
912 1010
913 $inlay->append_text ("\n"); 1011 $inlay->append_text ("\n");
914 1012
915 if (!$self->{channel}) { 1013 if (!$self->{channel}) {
916 $inlay->append_button ("Create Challenge", sub { 1014 $inlay->append_button ("Create Challenge", sub {
950 }); 1048 });
951 } 1049 }
952 } 1050 }
953} 1051}
954 1052
955sub draw_users { 1053sub new_game_challenge {
956 my ($self, $inlay) = @_; 1054 my ($self) = @_;
957 1055
958 for (sort keys %{$self->{users}}) { 1056 my $d = $self->{app}{defaults};
959 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); 1057
1058 $self->{challenge}{""} = {
1059 gametype => $d->{gametype},
1060 flags => 0,
1061 notes => $d->{stones},
1062 rules => {
1063 ruleset => $d->{ruleset},
1064 size => $d->{size},
1065 timesys => $d->{timesys},
1066 time => $d->{time},
1067 interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time},
1068 count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones},
1069 },
1070
1071 inlay => $self->{chat}->new_inlay,
960 } 1072 };
1073 $self->draw_challenge ("");
961} 1074}
962 1075
963sub event_challenge { 1076sub event_challenge {
964 my ($self, $info) = @_; 1077 my ($self, $info) = @_;
965 1078

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines