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.88 by pcg, Fri May 21 16:13:41 2004 UTC vs.
Revision 1.102 by pcg, Sun May 30 06:40:21 2004 UTC

71} 71}
72 72
73sub refresh { 73sub refresh {
74 my ($self, $timestamp) = @_; 74 my ($self, $timestamp) = @_;
75 my $timer = $self->{time} + $self->{start} - $timestamp; 75 my $timer = $self->{time} + $self->{start} - $timestamp;
76 76
77 # we round the timer value slightly... the protocol isn't exact anyways, 77 # we round the timer value slightly... the protocol isn't exact anyways,
78 # and this gives smoother timers ;) 78 # and this gives smoother timers ;)
79 my @format = $self->{format}->(int ($timer + 0.4)); 79 my $timer2 = int $timer + 0.4;
80
81 if ($timer2 <= 0) {
82 $timer2 = 0 if $timer2 < 0;
83 $self->set_text ("TIME OVER");
84 } else {
80 $self->set_text ($self->{format}->(int ($timer + 0.4))); 85 $self->set_text ($self->{format}->($timer2));
86 }
81 87
82 $timer - int $timer; 88 $timer - int $timer;
83} 89}
84 90
85sub set_time { 91sub set_time {
86 my ($self, $time) = @_; 92 my ($self, $time, $moves) = @_;
87 93
88 # we ignore requests to re-set the time of a running clock. 94 # we ignore requests to re-set the time of a running clock.
89 # this is the easiest way to ensure that commentary etc. 95 # this is the easiest way to ensure that commentary etc.
90 # doesn't re-set the clock. yes, this is frickle design, 96 # doesn't re-set the clock. yes, this is frickle design,
91 # but I think the protocol is to blame here, which gives 97 # but I think the protocol is to blame here, which gives
92 # very little time information. (cgoban2 also has had quite 98 # very little time information. (cgoban2 also has had quite
93 # a lot of small time update problems...) 99 # a lot of small time update problems...)
94 unless ($self->{timeout}) { 100 unless ($self->{timeout}) {
95 $self->{set}->($time->[0], $time->[1]); 101 $self->{set}->($time, $moves);
96 $self->refresh ($self->{start}); 102 $self->refresh ($self->{start});
97 } 103 }
98} 104}
99 105
100sub start { 106sub start {
167 } 173 }
168 174
169 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); 175 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
170} 176}
171 177
172sub set_state { 178sub set_captures {
173 my ($self, $captures, $timer, $when) = @_; 179 my ($self, $captures) = @_;
180
181 $self->{info}->set_text ("$captures pris.");
182}
183
184sub set_timer {
185 my ($self, $when, $time, $moves) = @_;
174 186
175 $self->{clock}->stop unless $when; 187 $self->{clock}->stop unless $when;
176 $self->{clock}->set_time ($timer); 188 $self->{clock}->set_time ($time, $moves);
177 $self->{clock}->start ($when) if $when; 189 $self->{clock}->start ($when) if $when;
178
179 $self->{info}->set_text ("$captures pris.");
180} 190}
181 191
182package game; 192package game;
183 193
184use Scalar::Util qw(weaken); 194use Scalar::Util qw(weaken);
185 195
186use KGS::Constants; 196use KGS::Constants;
187use KGS::Game::Board; 197use KGS::Game::Board;
188 198
189use Gtk2::GoBoard; 199use Gtk2::GoBoard;
200use Gtk2::GoBoard::Constants;
201
202use base KGS::Game;
203use base KGS::Listener::Game;
190 204
191use Glib::Object::Subclass 205use Glib::Object::Subclass
192 Gtk2::Window; 206 Gtk2::Window;
193
194use base KGS::Listener::Game;
195use base KGS::Game;
196 207
197use POSIX qw(ceil); 208use POSIX qw(ceil);
198 209
199sub new { 210sub new {
200 my ($self, %arg) = @_; 211 my ($self, %arg) = @_;
201 $self = $self->Glib::Object::new; 212 $self = $self->Glib::Object::new;
202 $self->{$_} = delete $arg{$_} for keys %arg; 213 $self->{$_} = delete $arg{$_} for keys %arg;
203 214
204 $self->listen ($self->{conn});
205
206 gtk::state $self, "game::window", undef, window_size => [600, 500]; 215 gtk::state $self, "game::window", undef, window_size => [600, 500];
207 216
208 $self->signal_connect (delete_event => sub { $self->part; 1 });
209 $self->signal_connect (destroy => sub { 217 $self->signal_connect (destroy => sub {
210 $self->unlisten; 218 $self->unlisten;
211 delete $self->{app}{game}{$self->{channel}}; 219 delete $self->{app}{game}{$self->{channel}};
212 %{$_[0]} = (); 220 %{$_[0]} = ();
213 });#d# 221 });#d#
223 231
224 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); 232 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
225 233
226 # board box (aspect/canvas) 234 # board box (aspect/canvas)
227 235
228 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 236 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
237
238 # RIGHT PANE
239
240 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
241 $hpane->set (position_set => 1);
242
243 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
229 244
230 { 245 {
231 $frame->add (my $vbox = new Gtk2::VBox); 246 $frame->add (my $vbox = new Gtk2::VBox);
232 $vbox->add ($self->{title} = new Gtk2::Label $title); 247 $vbox->add ($self->{title} = new Gtk2::Label $title);
233 248
242 $scale->set_digits (0); 257 $scale->set_digits (0);
243 258
244 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 259 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
245 } 260 }
246 261
247 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
248
249 # RIGHT PANE
250
251 $hpane->pack2 (($self->{vpane} = new Gtk2::VPaned), 1, 1);
252 $hpane->set (position_set => 1);
253 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
254
255# $self->{vpane}->add (my $sw = new Gtk2::ScrolledWindow);
256# $sw->set_policy ("automatic", "always");
257# $sw->add ($self->{userlist} = new userlist);
258
259 $self->{vpane}->add (my $vbox = new Gtk2::VBox);
260
261 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); 262 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
262 263
263 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) 264 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
264 for COLOUR_WHITE, COLOUR_BLACK; 265 for COLOUR_WHITE, COLOUR_BLACK;
266
267 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
268
269 $buttonbox->add ($self->{button_pass} =
270 Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0));
271 $self->{button_pass}->signal_connect (clicked => sub {
272 $self->{board_click}->(255, 255) if $self->{board_click};
273 });
274 $buttonbox->add ($self->{button_undo} =
275 Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0));
276 $self->{button_undo}->signal_connect (clicked => sub {
277 $self->send (req_undo => channel => $self->{channel});
278 });
279 $buttonbox->add ($self->{button_resign} =
280 Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0));
281 $self->{button_resign}->signal_connect (clicked => sub {
282 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour});
283 });
265 284
266 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); 285 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
267 286
268 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Rules", sub { $self->draw_rules (@_) }, 1); 287 $self->set_channel ($self->{channel});
269 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 0);
270 288
271 $self->{chat}->signal_connect (command => sub { 289 $self->show_all;
272 my ($chat, $cmd, $arg) = @_;
273 if ($cmd eq "rsave") {
274 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
275 } else {
276 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
277 }
278 });
279 290
280 $self; 291 $self;
292}
293
294sub set_channel {
295 my ($self, $channel) = @_;
296
297 $self->{channel} = $channel;
298
299 if ($self->{channel} > 0) {
300 $self->listen ($self->{conn});
301
302 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1);
303 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1);
304
305 $self->signal_connect (delete_event => sub { $self->part; 1 });
306 $self->{chat}->signal_connect (command => sub {
307 my ($chat, $cmd, $arg) = @_;
308 if ($cmd eq "rsave") {
309 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
310 } else {
311 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
312 }
313 });
314 }
281} 315}
282 316
283sub event_update_users { 317sub event_update_users {
284 my ($self, $add, $update, $remove) = @_; 318 my ($self, $add, $update, $remove) = @_;
285 319
286# $self->{userlist}->update ($add, $update, $remove); 320# $self->{userlist}->update ($add, $update, $remove);
287 321
322 $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy
323 for @$remove;
324
288 $self->{users_inlay}->refresh; 325 $self->{users_inlay}->refresh;
289 326
290 my %important; 327 my %important;
328 $important{$self->{black}{name}}++;
329 $important{$self->{white}{name}}++;
291 $important{$self->{user1}{name}}++; 330 $important{$self->{owner}{name}}++;
292 $important{$self->{user2}{name}}++;
293 $important{$self->{user3}{name}}++;
294 331
295 if (my @users = grep $important{$_->{name}}, @$add) { 332 if (my @users = grep $important{$_->{name}}, @$add) {
296 $self->{chat}->append_text ("\n<header>Joins:</header>"); 333 $self->{chat}->append_text ("\n<header>Joins:</header>");
297 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; 334 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
298 } 335 }
299 if (my @users = grep $important{$_->{name}}, @$remove) { 336 if (my @users = grep $important{$_->{name}}, @$remove) {
300 $self->{chat}->append_text ("\n<header>Parts:</header>"); 337 $self->{chat}->append_text ("\n<header>Parts:</header>");
301 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; 338 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
302 } 339 }
303
304} 340}
305 341
306sub join { 342sub join {
307 my ($self) = @_; 343 my ($self) = @_;
308 return if $self->{joined}; 344 return if $self->{joined};
309 345
310 $self->SUPER::join; 346 $self->SUPER::join;
311} 347}
312 348
349sub update_cursor {
350 my ($self) = @_;
351
352 my $move = int $self->{moveadj}->get_value;
353 my $cb;
354
355 my $running = $move == @{$self->{path}} && $self->is_active;
356
357 delete $self->{board_click};
358
359 if ($self->{teacher} eq $self->{app}{conn}) {
360 #TODO# # teaching mode not implemented
361 $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1);
362 $self->{button_undo}->hide;
363 $self->{button_resign}->hide;
364 $self->{board}->set (cursor => undef);
365
366 } elsif ($running && $self->{colour} != COLOUR_NONE) {
367 # during game
368 $self->{button_undo}->show;
369 $self->{button_resign}->show;
370
371 if ($self->{cur_board}{score}) {
372 # during scoring
373 $self->{button_pass}->set (label => "Done", sensitive => 1, visible => 1);
374 $self->{board}->set (cursor => sub {
375 $_[0] & (MARK_B | MARK_W)
376 ? $_[0] ^ MARK_GRAYED
377 : $_[0];
378 });
379 $self->{board_click} = sub {
380 if ($_[0] == 255) {
381 $self->{button_pass}->sensitive (0);
382 $self->done;
383 } else {
384 $self->send (mark_dead =>
385 channel => $self->{channel},
386 x => $_[0],
387 y => $_[1],
388 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED),
389 );
390 }
391 };
392
393 } elsif (1 - $self->{colour} == $self->{cur_board}{last}) {
394 # normal move
395 $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1);
396 $self->{board}->set (cursor => sub {
397 # if is_valid_move oder so#TODO#
398 $_[0] & (MARK_B | MARK_W)
399 ? $_[0]
400 : $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B);
401 });
402 $self->{board_click} = sub {
403 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
404 $self->{board}->set (cursor => undef);
405 delete $self->{board_click};
406 $self->{button_pass}->sensitive (0);
407 };
408 } else {
409 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1);
410 }
411 } else {
412 $self->{button_undo}->hide;
413 $self->{button_resign}->hide;
414 $self->{button_pass}->hide;
415 $self->{board}->set (cursor => undef);
416 #TODO# # implement coordinate-grabbing
417 }
418}
419
313sub update_board { 420sub update_board {
314 my ($self) = @_; 421 my ($self) = @_;
315 return unless $self->{path}; 422 return unless $self->{path};
316 423
317 my $move = int $self->{moveadj}->get_value; 424 my $move = int $self->{moveadj}->get_value;
318 425
319 my $running = $move == @{$self->{path}};
320
321 $self->{board_label}->set_text ("Move " . ($move - 1)); 426 $self->{board_label}->set_text ("Move " . ($move - 1));
322 427
323 $self->{cur_board} = new KGS::Game::Board $self->{size}; 428 $self->{cur_board} = new KGS::Game::Board $self->{size};
324 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); 429 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
325 430
431 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
326 for my $colour (COLOUR_WHITE, COLOUR_BLACK) { 432 for COLOUR_WHITE, COLOUR_BLACK;
327 $self->{userpanel}[$colour]->set_state (
328 $self->{cur_board}{captures}[$colour],
329 $self->{cur_board}{timer}[$colour],
330 ($running && $self->{lastmove_colour} == !$colour)
331 ? $self->{lastmove_time} : 0
332 );
333 }
334 433
335 $self->{board}->set_board ($self->{cur_board}); 434 $self->{board}->set_board ($self->{cur_board});
435
436 $self->update_cursor;
336} 437}
337 438
338sub event_update_tree { 439sub event_update_tree {
339 my ($self) = @_; 440 my ($self) = @_;
340 441
394 495
395sub event_join { 496sub event_join {
396 my ($self) = @_; 497 my ($self) = @_;
397 498
398 $self->SUPER::event_join (@_); 499 $self->SUPER::event_join (@_);
500 $self->init_tree;
399 $self->event_update_game; 501 $self->event_update_game;
400 $self->show_all;
401} 502}
402 503
403sub event_part { 504sub event_part {
404 my ($self) = @_; 505 my ($self) = @_;
405 506
408} 509}
409 510
410sub event_move { 511sub event_move {
411 my ($self, $pass) = @_; 512 my ($self, $pass) = @_;
412 sound::play 1, $pass ? "pass" : "move"; 513 sound::play 1, $pass ? "pass" : "move";
514
515 if ($self->{undo_inlay}) {
516 (delete $self->{undo_inlay})->clear;
517 }
413} 518}
414 519
415sub event_update_game { 520sub event_update_game {
416 my ($self) = @_; 521 my ($self) = @_;
522
417 $self->SUPER::event_update_game; 523 $self->SUPER::event_update_game;
418 524
419 return unless $self->{joined}; 525 return unless $self->{joined};
420 526
527 $self->{colour} = $self->player_colour ($self->{conn}{name});
528
421 my $title = defined $self->{channel} 529 my $title = defined $self->{channel}
422 ? $self->owner->as_string . " " . $self->opponent_string 530 ? $self->owner->as_string . " " . $self->opponent_string
423 : "Game Window"; 531 : "Game Window";
424 $self->set_title("KGS Game $title"); 532 $self->set_title ("KGS Game $title");
425 $self->{title}->set_text ($title); 533 $self->{title}->set_text ($title);
426 534
427 $self->{user}[COLOUR_BLACK] = $self->{user1}; 535 $self->{user}[COLOUR_BLACK] = $self->{black};
428 $self->{user}[COLOUR_WHITE] = $self->{user2}; 536 $self->{user}[COLOUR_WHITE] = $self->{white};
429 537
430 # show board 538 # show board
431 if ($self->is_inprogress) { 539 if ($self->is_inprogress) {
432 $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent; 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 }
433 if (my $ch = delete $self->{challenge}) { 549 if (my $ch = delete $self->{challenge}) {
434 (delete $_->{inlay})->clear for values %$ch; 550 $_->{inlay}->destroy for values %$ch;
435 } 551 }
552 $self->update_cursor;
436 } 553 }
437 554
438 $self->{left}->show_all; 555 $self->{left}->show_all;
439 556
440 # view text 557 $self->{rules_inlay}->refresh;
441 558
442 eval { #d# 559}
443 my @ga;
444 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
445 . " (" . (util::toxml $gameopt{$self->option}) . ")";
446 $ga[1] = "\nFlags:";
447 $ga[1] .= " started" if $self->is_inprogress;
448 $ga[1] .= " adjourned" if $self->is_adjourned;
449 $ga[1] .= " scored" if $self->is_scored;
450 $ga[1] .= " saved" if $self->is_saved;
451 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
452 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" 573 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
453 if $self->{user3}->is_inprogress; 574 if $self->{owner}->is_valid;
454
455 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
456 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>"
457 if $self->is_inprogress;
458 575
459 if ($self->is_inprogress) { 576 if ($self->is_inprogress) {
460 $ga[4] = "\nHandicap: " . $self->{handicap}; 577 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
461 $ga[5] = "\nKomi: " . $self->{komi}; 578 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
462 $ga[6] = "\nSize: " . $self->size_string;
463 }
464
465 if ($self->is_scored) {
466 $ga[7] = "\nResult: " . $self->score_string;
467 }
468
469 $text = "\n<infoblock><header>Game Update</header>";
470 for (0..7) {
471 if ($self->{gatext}[$_] ne $ga[$_]) {
472 $text .= $ga[$_];
473 }
474 }
475 $text .= "</infoblock>";
476
477 $self->{gatext} = \@ga;
478 }; 579 }
479 580 $text .= "\nType: " . util::toxml $gametype{$self->type};
480 $self->{chat}->append_text ($text);
481}
482
483sub draw_rules {
484 my ($self, $inlay) = @_;
485
486 my $rules = $self->{rules};
487
488 my $text = "";
489 581
490 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; 582 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
491 583
492 $text .= "\nTime: "; 584 $text .= "\nTime: ";
493 585
502 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { 594 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
503 $text .= util::format_time $rules->{time}; 595 $text .= util::format_time $rules->{time};
504 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; 596 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
505 } 597 }
506 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
507 $inlay->append_text ("<infoblock>$text</infoblock>"); 616 $inlay->append_text ("<infoblock>$text</infoblock>");
617
508} 618}
509 619
510sub event_update_rules { 620sub event_update_rules {
511 my ($self, $rules) = @_; 621 my ($self, $rules) = @_;
512 622
517 for COLOUR_BLACK, COLOUR_WHITE; 627 for COLOUR_BLACK, COLOUR_WHITE;
518 } 628 }
519 629
520 sound::play 3, "gamestart"; 630 sound::play 3, "gamestart";
521 631
522 $self->draw_rules ($self->{rules_inlay}); 632 $self->{rules_inlay}->refresh;
523} 633}
524 634
525sub inject_resign_game { 635sub event_resign_game {
526 my ($self, $msg) = @_; 636 my ($self, $player) = @_;
527 637
528 sound::play 3, "resign"; 638 sound::play 3, "resign";
529
530 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" 639 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
531 . "\n<user>" 640 . "\n<user>"
532 . (util::toxml $self->{user}[$msg->{player}]->as_string) 641 . (util::toxml $self->{user}[$msg->{player}]->as_string)
533 . "</user> resigned.</infoblock>"); 642 . "</user> resigned.</infoblock>");
643}
644
645sub event_out_of_time {
646 my ($self, $player) = @_;
647
648 sound::play 3, "timewin";
649 $self->{chat}->append_text ("\n<infoblock><header>Time Over</header>"
650 . "\n<user>"
651 . (util::toxml $self->{user}[$msg->{player}]->as_string)
652 . "</user> ran out of time.</infoblock>");
653}
654
655sub event_done {
656 my ($self) = @_;
657
658 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) {
659 $self->{chat}->append_text ("\n<infoblock><header>Done</header>"
660 . "\nYour opponent pressed done.");
661 }
534} 662}
535 663
536sub inject_final_result { 664sub inject_final_result {
537 my ($self, $msg) = @_; 665 my ($self, $msg) = @_;
538 666
541 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) 669 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
542 . "</infoblock>" 670 . "</infoblock>"
543 ); 671 );
544} 672}
545 673
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 {
690 my ($self, $msg) = @_;
691
692 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay;
693 return if $inlay->{ignore};
694
695 sound::play 3, "warning" unless $inlay->{count};
696 $inlay->{count}++;
697
698 $inlay->clear;
699 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n");
700 $inlay->append_button ("Grant", sub {
701 $inlay->clear;
702 $self->send (grant_undo => channel => $self->{channel});
703 });
704 $inlay->append_button ("Ignore", sub {
705 $inlay->clear;
706 $inlay->{ignore} = 1;
707 # but leave inlay, so further undo requests get counted
708 });
709
710 $self->{chat}->set_end;
711}
712
713sub inject_new_game {
714 my ($self, $msg) = @_;
715
716 $self->{chat}->append_text ("\n<header>ACK from server ($msg->{cid} == $self->{cid})</header>");
717 delete $self->{cid};
718}
719
546sub draw_challenge { 720sub draw_challenge {
547 my ($self, $c) = @_; 721 my ($self, $id) = @_;
548 722
723 my $info = $self->{challenge}{$id};
549 my $inlay = $c->{inlay}; 724 my $inlay = $info->{inlay};
550 my $challenge = $c->{challenge};
551 my $rules = $challenge->{rules}; 725 my $rules = $info->{rules};
552 726
553 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name}; 727 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;;
554 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1}; 728 my $opponent = $as_black ? $info->{white} : $info->{black};
555 729
556 $inlay->append_text ("\n<challenge>Challenge to <user>" . $opponent->as_string . "</user></challenge>"); 730 my ($size, $time, $interval, $count);
731
732 if (!$self->{channel}) {
733 $inlay->append_text ("\nNotes: ");
734 $inlay->append_entry (\$info->{notes}, 20, "");
735 $inlay->append_text ("\nGlobal Offer: ");
736 $inlay->append_optionmenu (\$info->{flags},
737 0 => "No",
738 2 => "Yes",
739 );
740 } else {
741 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes});
742 }
743
744 $inlay->append_text ("\nType: ");
745 $inlay->append_optionmenu (
746 \$info->{gametype},
747 GAMETYPE_DEMONSTRATION , "Demonstration",
748 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P)",
749 GAMETYPE_TEACHING , "Teaching",
750 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P)",
751 GAMETYPE_SIMUL , "Simul (not yet!)",
752 GAMETYPE_FREE , "Free",
753 GAMETYPE_RATED , "Rated",
754 sub {
755 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
756 },
757 );
758
759 if ($self->{channel}) {
760 $inlay->append_text ("\nMy Colour: ");
761 $inlay->append_optionmenu (
762 \$as_black,
763 0 => "White",
764 1 => "Black",
765 sub {
766 if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) {
767 ($info->{black}, $info->{white}) = ($info->{white}, $info->{black});
768 }
769 }
770 );
771 }
772
773 $inlay->append_text ("\nRuleset: ");
774 $inlay->append_optionmenu (
775 \$info->{rules}{ruleset},
776 RULESET_JAPANESE , "Japanese",
777 RULESET_CHINESE , "Chinese",
778 RULESET_AGA , "AGA",
779 RULESET_NEW_ZEALAND, "New Zealand",
780 );
781
782 $inlay->append_text ("\nSize: ");
783 $size = $inlay->append_optionmenu (
784 \$info->{rules}{size}, 9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38
785 );
786
787 if ($self->{channel}) {
557 $inlay->append_text ("\nHandicap: $rules->{handicap}"); 788 $inlay->append_text ("\nHandicap: ");
789 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
558 790
559#bless( ( 791 $inlay->append_text ("\nKomi: ");
560# gametype => 3, 792 $inlay->append_entry (\$info->{rules}{komi}, 5);
561# user1 => bless( { 793 }
562# flags => 2633, 794
563# name => 'dorkusx' 795 $inlay->append_text ("\nTimesys: ");
564# }, 'KGS::User' ), 796 $inlay->append_optionmenu (
565# rules => bless( { 797 \$info->{rules}{timesys},
566# count => 5, 798 &TIMESYS_NONE => "None",
567# time => 900, 799 &TIMESYS_ABSOLUTE => "Absolute",
568# timesys => 2, 800 &TIMESYS_BYO_YOMI => "Byo Yomi",
569# interval => 30, 801 &TIMESYS_CANADIAN => "Canadian",
570# komi => '6.5', 802 sub {
571# size => 19, 803 my ($new) = @_;
572# ruleset => 0, 804
573# handicap => 0 805 if ($new eq TIMESYS_NONE) {
574# }, 'KGS::Rules' ), 806 $time->hide;
575# user2 => bless( { 807 $interval->hide;
576# flags => 436220808, 808 $count->hide;
577# name => 'Nerdamus' 809 } else {
578# }, 'KGS::User' ) 810 $time->show;
579# ), 'KGS::Challenge' ) 811 $time->set_text ($self->{app}{defaults}{time});
812 if ($new eq TIMESYS_ABSOLUTE) {
813 $interval->hide;
814 $count->hide;
815 } else {
816 $interval->show;
817 $count->show;
818 if ($new eq TIMESYS_BYO_YOMI) {
819 $interval->set_text ($self->{app}{defaults}{byo_time});
820 $count->set_text ($self->{app}{defaults}{byo_period});
821 } elsif ($new eq TIMESYS_CANADIAN) {
822 $interval->set_text ($self->{app}{defaults}{can_time});
823 $count->set_text ($self->{app}{defaults}{can_period});
824 }
825 }
826 }
827 }
828 );
829
830 $inlay->append_text ("\nMain Time: ");
831 $time = $inlay->append_entry (\$info->{rules}{time}, 5);
832 $inlay->append_text ("\nInterval: ");
833 $interval = $inlay->append_entry (\$info->{rules}{interval}, 3);
834 $inlay->append_text ("\nPeriods/Stones: ");
835 $count = $inlay->append_entry (\$info->{rules}{count}, 2);
836
837 $inlay->append_text ("\n");
838
839 if (!$self->{channel}) {
840 $inlay->append_button ("Create Challenge", sub {
841 $inlay->clear;
842 $self->{cid} = $self->{conn}->alloc_clientid;
843 $self->send (new_game =>
844 channel => delete $self->{roomid},
845 gametype => $info->{gametype},
846 cid => $self->{cid},
847 flags => $info->{flags},
848 rules => $info->{rules},
849 notes => $info->{notes},
850 );
851 });
852 } else {
853 $inlay->append_button ("OK", sub {
854 $inlay->clear;
855 $self->send (challenge =>
856 channel => $self->{channel},
857 black => $info->{black},
858 white => $info->{white},
859 gametype => $info->{gametype},
860 cid => $info->{cid},
861 rules => $info->{rules},
862 );
863 });
864 if (exists $self->{challenge}{""}) {
865 $inlay->append_button ("Reject", sub {
866 $inlay->clear;
867 $self->send (reject_challenge =>
868 channel => $self->{channel},
869 name => $opponent->{name},
870 gametype => $info->{gametype},
871 cid => $info->{cid},
872 rules => $info->{rules},
873 );
874 });
875 }
876 }
580} 877}
581 878
582sub draw_users { 879sub draw_users {
583 my ($self, $inlay) = @_; 880 my ($self, $inlay) = @_;
584 881
585 for (sort keys %{$self->{users}}) { 882 for (sort keys %{$self->{users}}) {
586 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); 883 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
587 } 884 }
588} 885}
589 886
590sub event_challenge { 887sub event_challenge {
591 my ($self, $challenge) = @_; 888 my ($self, $info) = @_;
592 889
593 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name}; 890 my $as_black = $info->{black}->{name} eq $self->{conn}{name};
594 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1}; 891 my $opponent = $as_black ? $info->{white} : $info->{black};
595 892
596 my $c = $self->{challenge}{$opponent->{name}} ||= {}; 893 my $id = $opponent->{name};
597 894
598 $c->{inlay} ||= $self->{chat}->new_inlay; 895 $self->{challenge}{$id} = $info;
599 $c->{challenge} = $challenge; 896 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay (
600 897 exists $self->{challenge}{""}
898 ? "Challenge from $opponent->{name}"
899 : "Challenge to $opponent->{name}",
900 sub {
901 $self->{challenge}{$id}{inlay} = $_[0];
601 $self->draw_challenge ($c); 902 $self->draw_challenge ($id);
602 903 },
603# require KGS::Listener::Debug; 904 !exists $self->{challenge}{""} # only open when not offerer
604# $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge)); 905 );
605} 906}
606 907
6071; 9081;
608 909

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines