… | |
… | |
55 | my $time = int (($_[0] - 1) % $interval + 1); |
55 | my $time = int (($_[0] - 1) % $interval + 1); |
56 | |
56 | |
57 | sprintf "%s/%d =%d", |
57 | sprintf "%s/%d =%d", |
58 | util::format_time $time, |
58 | util::format_time $time, |
59 | $self->{moves}, |
59 | $self->{moves}, |
|
|
60 | $self->{moves} > 1 |
60 | $time / ($self->{moves} || 1); |
61 | ? $time / $self->{moves} |
61 | |
62 | : $interval; |
62 | } |
63 | } |
63 | }; |
64 | }; |
64 | |
65 | |
65 | } else { |
66 | } else { |
66 | # none, or unknown |
67 | # none, or unknown |
… | |
… | |
145 | |
146 | |
146 | $self; |
147 | $self; |
147 | } |
148 | } |
148 | |
149 | |
149 | sub configure { |
150 | sub configure { |
150 | my ($self, $user, $rules) = @_; |
151 | my ($self, $app, $user, $rules) = @_; |
151 | |
152 | |
152 | if ($self->{name}->get_text ne $user->as_string) { |
153 | if ($self->{name}->get_text ne $user->as_string) { |
153 | $self->{name}->set_text ($user->as_string); |
154 | $self->{name}->set_text ($user->as_string); |
154 | |
155 | |
155 | $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
156 | $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
156 | $self->{imagebox}->add (gtk::image_from_data undef); |
157 | $self->{imagebox}->add (gtk::image_from_data undef); |
157 | $self->{imagebox}->show_all; |
158 | $self->{imagebox}->show_all; |
158 | |
159 | |
159 | if ($user->has_pic) { |
160 | if ($user->has_pic) { |
160 | # the big picture... |
161 | # the big picture... |
161 | appwin::userpic ($user->{name}, sub { |
162 | $app->userpic ($user->{name}, sub { |
162 | return unless $self->{imagebox}; |
163 | return unless $self->{imagebox}; |
|
|
164 | |
163 | if ($_[0]) { |
165 | if ($_[0]) { |
164 | $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
166 | $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
165 | $self->{imagebox}->add (gtk::image_from_data $_[0]); |
167 | $self->{imagebox}->add (gtk::image_from_data $_[0]); |
166 | $self->{imagebox}->show_all; |
168 | $self->{imagebox}->show_all; |
167 | } |
169 | } |
… | |
… | |
185 | package game; |
187 | package game; |
186 | |
188 | |
187 | use KGS::Constants; |
189 | use KGS::Constants; |
188 | use KGS::Game::Board; |
190 | use KGS::Game::Board; |
189 | |
191 | |
|
|
192 | use Gtk2::GoBoard; |
|
|
193 | |
190 | use base KGS::Listener::Game; |
194 | use base KGS::Listener::Game; |
191 | use base KGS::Game; |
195 | use base KGS::Game; |
192 | |
196 | |
193 | use base gtk::widget; |
197 | use base gtk::widget; |
194 | |
198 | |
… | |
… | |
259 | $sw->add(($self->{userlist} = new userlist)->widget); |
263 | $sw->add(($self->{userlist} = new userlist)->widget); |
260 | |
264 | |
261 | $self->{vpane}->add(my $vbox = new Gtk2::VBox); |
265 | $self->{vpane}->add(my $vbox = new Gtk2::VBox); |
262 | |
266 | |
263 | $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
267 | $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
264 | $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget); |
268 | $hbox->add (($self->{userpanel}[COLOUR_WHITE] = new game::userpanel colour => COLOUR_WHITE)->widget); |
265 | $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget); |
269 | $hbox->add (($self->{userpanel}[COLOUR_BLACK] = new game::userpanel colour => COLOUR_BLACK)->widget); |
266 | |
270 | |
267 | $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0); |
271 | $vbox->pack_start(($self->{chat} = new chat), 1, 1, 0); |
268 | |
272 | |
269 | $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); |
|
|
270 | $self->{entry}->signal_connect(activate => sub { |
273 | $self->{chat}->signal_connect(command => sub { |
271 | my $text = $self->{entry}->get_text; |
274 | my ($chat, $cmd, $arg) = @_; |
272 | $self->say($text) if $text =~ /\S/; |
275 | if ($cmd eq "rsave") { |
273 | $self->{entry}->set_text(""); |
276 | Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d# |
|
|
277 | } else { |
|
|
278 | $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self); |
|
|
279 | } |
274 | }); |
280 | }); |
275 | |
281 | |
276 | $self->event_update_game; |
282 | $self->event_update_game; |
277 | $self; |
283 | $self; |
278 | } |
284 | } |
279 | |
285 | |
280 | sub event_update_users { |
286 | sub event_update_users { |
281 | my ($self, $add, $update, $remove) = @_; |
287 | my ($self, $add, $update, $remove) = @_; |
|
|
288 | |
|
|
289 | return unless $self->{userlist}; |
282 | |
290 | |
283 | $self->{userlist}->update ($add, $update, $remove); |
291 | $self->{userlist}->update ($add, $update, $remove); |
284 | |
292 | |
285 | my %important; |
293 | my %important; |
286 | $important{$self->{user1}{name}}++; |
294 | $important{$self->{user1}{name}}++; |
287 | $important{$self->{user2}{name}}++; |
295 | $important{$self->{user2}{name}}++; |
288 | $important{$self->{user3}{name}}++; |
296 | $important{$self->{user3}{name}}++; |
289 | |
297 | |
290 | if (my @users = grep $important{$_->{name}}, @$add) { |
298 | if (my @users = grep $important{$_->{name}}, @$add) { |
291 | $self->{text}->append_text ("\n<header>Joins:</header>"); |
299 | $self->{chat}->append_text ("\n<header>Joins:</header>"); |
292 | $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
300 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
293 | } |
301 | } |
294 | if (my @users = grep $important{$_->{name}}, @$remove) { |
302 | if (my @users = grep $important{$_->{name}}, @$remove) { |
295 | $self->{text}->append_text ("\n<header>Parts:</header>"); |
303 | $self->{chat}->append_text ("\n<header>Parts:</header>"); |
296 | $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
304 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
297 | } |
305 | } |
298 | |
306 | |
299 | } |
307 | } |
300 | |
308 | |
301 | sub join { |
309 | sub join { |
… | |
… | |
325 | $self->{board_label}->set_text ("Move " . ($move - 1)); |
333 | $self->{board_label}->set_text ("Move " . ($move - 1)); |
326 | |
334 | |
327 | $self->{cur_board} = new KGS::Game::Board $self->{size}; |
335 | $self->{cur_board} = new KGS::Game::Board $self->{size}; |
328 | $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); |
336 | $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); |
329 | |
337 | |
330 | for my $colour (WHITE, BLACK) { |
338 | for my $colour (COLOUR_WHITE, COLOUR_BLACK) { |
331 | $self->{userpanel}[$colour]->set_state ( |
339 | $self->{userpanel}[$colour]->set_state ( |
332 | $self->{cur_board}{captures}[$colour], |
340 | $self->{cur_board}{captures}[$colour], |
333 | $self->{cur_board}{timer}[$colour], |
341 | $self->{cur_board}{timer}[$colour], |
334 | ($running && $self->{lastmove_colour} == !$colour) |
342 | ($running && $self->{lastmove_colour} == !$colour) |
335 | ? $self->{lastmove_time} : 0 |
343 | ? $self->{lastmove_time} : 0 |
… | |
… | |
391 | }sgexi; |
399 | }sgexi; |
392 | |
400 | |
393 | $text .= $_; |
401 | $text .= $_; |
394 | } |
402 | } |
395 | |
403 | |
396 | $self->{text}->append_text ($text); |
404 | $self->{chat}->append_text ($text); |
397 | } |
405 | } |
398 | |
406 | |
399 | sub event_join { |
407 | sub event_join { |
400 | my ($self) = @_; |
408 | my ($self) = @_; |
401 | $self->SUPER::event_join; |
409 | $self->SUPER::event_join; |
… | |
… | |
414 | |
422 | |
415 | sub event_update_game { |
423 | sub event_update_game { |
416 | my ($self) = @_; |
424 | my ($self) = @_; |
417 | $self->SUPER::event_update_game; |
425 | $self->SUPER::event_update_game; |
418 | |
426 | |
419 | my $title = $self->{channel} ? $self->owner->as_string . " " . $self->opponent_string : "Game Window"; |
427 | return unless $self->{window}; |
|
|
428 | |
|
|
429 | my $title = defined $self->{channel} |
|
|
430 | ? $self->owner->as_string . " " . $self->opponent_string |
|
|
431 | : "Game Window"; |
420 | $self->{window}->set_title("KGS Game $title"); |
432 | $self->{window}->set_title("KGS Game $title"); |
421 | $self->{title}->set_text ($title); |
433 | $self->{title}->set_text ($title); |
422 | |
434 | |
423 | $self->{user}[BLACK] = $self->{user1}; |
435 | $self->{user}[COLOUR_BLACK] = $self->{user1}; |
424 | $self->{user}[WHITE] = $self->{user2}; |
436 | $self->{user}[COLOUR_WHITE] = $self->{user2}; |
425 | |
437 | |
426 | # show board |
438 | # show board |
427 | |
439 | |
428 | $self->{left}->remove ($_) for $self->{left}->get_children; |
|
|
429 | if ($self->is_valid) { |
440 | if ($self->is_inprogress) { |
430 | $self->{left}->add ($self->{boardbox}); |
441 | $self->{left}->remove ($self->{challenge}->widget) if $self->{challenge} && $self->{boardbox}->parent; |
431 | (delete $self->{challenge})->destroy if $self->{challenge}; |
442 | $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent; |
432 | } else { |
443 | } else { |
433 | $self->{left}->add ($self->{challenge}->widget); |
444 | $self->{left}->remove ($self->{boardbox}) if $self->{boardbox}->parent; |
|
|
445 | $self->{left}->add ($self->{challenge}->widget) unless $self->{challenge}->widget->parent; |
434 | } |
446 | } |
435 | $self->{left}->show_all; |
447 | $self->{left}->show_all; |
436 | |
448 | |
437 | # view text |
449 | # view text |
438 | |
450 | |
439 | eval { #d# |
451 | eval { #d# |
440 | my @ga; |
452 | my @ga; |
441 | $ga[0] = "\nType: " . (util::toxml $gametype{$self->type}) |
453 | $ga[0] = "\nType: " . (util::toxml $gametype{$self->type}) |
442 | . " (" . (util::toxml $gameopt{$self->option}) . ")"; |
454 | . " (" . (util::toxml $gameopt{$self->option}) . ")"; |
443 | $ga[1] = "\nFlags:"; |
455 | $ga[1] = "\nFlags:"; |
444 | $ga[1] .= " valid" if $self->is_valid; |
456 | $ga[1] .= " started" if $self->is_inprogress; |
445 | $ga[1] .= " adjourned" if $self->is_adjourned; |
457 | $ga[1] .= " adjourned" if $self->is_adjourned; |
446 | $ga[1] .= " scored" if $self->is_scored; |
458 | $ga[1] .= " scored" if $self->is_scored; |
447 | $ga[1] .= " saved" if $self->is_saved; |
459 | $ga[1] .= " saved" if $self->is_saved; |
448 | |
460 | |
449 | $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid; |
461 | $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" |
|
|
462 | if $self->{user3}->is_inprogress; |
450 | |
463 | |
451 | $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>" |
464 | $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>" |
452 | . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>" |
465 | . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>" |
453 | if $self->is_valid; |
466 | if $self->is_inprogress; |
454 | |
467 | |
455 | if ($self->is_valid) { |
468 | if ($self->is_inprogress) { |
456 | $ga[4] = "\nHandicap: " . $self->{handicap}; |
469 | $ga[4] = "\nHandicap: " . $self->{handicap}; |
457 | $ga[5] = "\nKomi: " . $self->{komi}; |
470 | $ga[5] = "\nKomi: " . $self->{komi}; |
458 | $ga[6] = "\nSize: " . $self->size_string; |
471 | $ga[6] = "\nSize: " . $self->size_string; |
459 | } |
472 | } |
460 | |
473 | |
… | |
… | |
471 | $text .= "</infoblock>"; |
484 | $text .= "</infoblock>"; |
472 | |
485 | |
473 | $self->{gatext} = \@ga; |
486 | $self->{gatext} = \@ga; |
474 | }; |
487 | }; |
475 | |
488 | |
476 | $self->{text}->append_text ($text); |
489 | $self->{chat}->append_text ($text); |
477 | } |
490 | } |
478 | |
491 | |
479 | sub event_update_rules { |
492 | sub event_update_rules { |
480 | my ($self, $rules) = @_; |
493 | my ($self, $rules) = @_; |
481 | |
494 | |
482 | $self->{userpanel}[$_]->configure ($self->{user}[$_], $rules) |
495 | $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules) |
483 | for BLACK, WHITE; |
496 | for COLOUR_BLACK, COLOUR_WHITE; |
484 | |
497 | |
485 | sound::play 3, "gamestart"; |
498 | sound::play 3, "gamestart"; |
486 | |
499 | |
487 | my $text = "\n<header>Game Rules</header>"; |
500 | my $text = "\n<header>Game Rules</header>"; |
488 | |
501 | |
… | |
… | |
494 | $text .= "UNLIMITED"; |
507 | $text .= "UNLIMITED"; |
495 | } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { |
508 | } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { |
496 | $text .= util::format_time $rules->{time}; |
509 | $text .= util::format_time $rules->{time}; |
497 | $text .= " ABS"; |
510 | $text .= " ABS"; |
498 | } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { |
511 | } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { |
499 | $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count}; |
512 | $text .= util::format_time $rules->{time}; |
500 | $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; |
513 | $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; |
501 | } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
514 | } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
502 | $text .= util::format_time $rules->{time}; |
515 | $text .= util::format_time $rules->{time}; |
503 | $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
516 | $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
504 | } |
517 | } |
505 | |
518 | |
506 | $self->{text}->append_text ("<infoblock>$text</infoblock>"); |
519 | $self->{chat}->append_text ("<infoblock>$text</infoblock>"); |
507 | } |
520 | } |
508 | |
521 | |
509 | sub inject_resign_game { |
522 | sub inject_resign_game { |
510 | my ($self, $msg) = @_; |
523 | my ($self, $msg) = @_; |
511 | |
524 | |
512 | sound::play 3, "resign"; |
525 | sound::play 3, "resign"; |
513 | |
526 | |
514 | $self->{text}->append_text ("\n<infoblock><header>Resign</header>" |
527 | $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" |
515 | . "\n<user>" |
528 | . "\n<user>" |
516 | . (util::toxml $self->{user}[$msg->{player}]->as_string) |
529 | . (util::toxml $self->{user}[$msg->{player}]->as_string) |
517 | . "</user> resigned.</infoblock>"); |
530 | . "</user> resigned.</infoblock>"); |
518 | } |
531 | } |
519 | |
532 | |
520 | sub inject_final_result { |
533 | sub inject_final_result { |
521 | my ($self, $msg) = @_; |
534 | my ($self, $msg) = @_; |
522 | |
535 | |
523 | $self->{text}->append_text ("<infoblock>\n<header>Game Over</header>" |
536 | $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>" |
524 | . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) |
537 | . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) |
525 | . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
538 | . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
526 | . "</infoblock>" |
539 | . "</infoblock>" |
527 | ); |
540 | ); |
528 | } |
541 | } |
529 | |
542 | |
|
|
543 | sub event_challenge { |
|
|
544 | my ($self, $challenge) = @_; |
|
|
545 | |
|
|
546 | use KGS::Listener::Debug; |
|
|
547 | $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge)); |
|
|
548 | } |
|
|
549 | |
530 | sub destroy { |
550 | sub destroy { |
531 | my ($self) = @_; |
551 | my ($self) = @_; |
|
|
552 | |
|
|
553 | delete $self->{app}{gamelist}{game}{$self->{channel}}; |
532 | $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy |
554 | $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy |
533 | for BLACK, WHITE; |
555 | for COLOUR_BLACK, COLOUR_WHITE; |
534 | $self->SUPER::destroy; |
556 | $self->SUPER::destroy; |
535 | delete $appwin::gamelist->{game}{$self->{channel}}; |
|
|
536 | } |
557 | } |
537 | |
558 | |
538 | 1; |
559 | 1; |
539 | |
560 | |