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.69 by pcg, Tue Jul 22 21:24:50 2003 UTC vs.
Revision 1.97 by pcg, Sat May 29 06:38:27 2004 UTC

1use utf8; 1use utf8;
2
3use Scalar::Util ();
2 4
3package game::goclock; 5package game::goclock;
4 6
5# Lo and Behold! I admit it! The rounding stuff etc.. in goclock 7# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
6# is completely borked. 8# is completely borked.
7 9
8use Time::HiRes (); 10use Time::HiRes ();
9 11
10use KGS::Constants; 12use KGS::Constants;
11 13
12use base gtk::widget; 14use Glib::Object::Subclass
15 Gtk2::Label;
13 16
14sub new { 17sub INIT_INSTANCE {
15 my $class = shift; 18 my $self = shift;
16 my $self = $class->SUPER::new(@_);
17 19
18 $self->{widget} = new Gtk2::Label; 20 $self->signal_connect (destroy => sub { $_[0]->stop });
19 21
20 $self->{set} = sub { }; 22 $self->{set} = sub { };
21 $self->{format} = sub { "ERROR" }; 23 $self->{format} = sub { "???" };
22
23 $self;
24} 24}
25 25
26sub configure { 26sub configure {
27 my ($self, $timesys, $main, $interval, $count) = @_; 27 my ($self, $timesys, $main, $interval, $count) = @_;
28 28
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
74 my $timer = $self->{time} + $self->{start} - $timestamp; 75 my $timer = $self->{time} + $self->{start} - $timestamp;
75 76
76 # 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,
77 # and this gives smoother timers ;) 78 # and this gives smoother timers ;)
78 my @format = $self->{format}->(int ($timer + 0.4)); 79 my @format = $self->{format}->(int ($timer + 0.4));
79 $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4))); 80 $self->set_text ($self->{format}->(int ($timer + 0.4)));
80 81
81 $timer - int $timer; 82 $timer - int $timer;
82} 83}
83 84
84sub set_time { 85sub set_time {
117 my ($self) = @_; 118 my ($self) = @_;
118 119
119 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 120 remove Glib::Source delete $self->{timeout} if $self->{timeout};
120} 121}
121 122
122sub destroy {
123 my ($self) = @_;
124 $self->stop;
125 $self->SUPER::destroy;
126}
127
128package game::userpanel; 123package game::userpanel;
129 124
130use base gtk::widget; 125use Glib::Object::Subclass
126 Gtk2::HBox,
127 properties => [
128 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
129 ];
131 130
132sub new { 131sub INIT_INSTANCE {
133 my $class = shift; 132 my ($self) = @_;
134 my $self = $class->SUPER::new(@_);
135 133
136 $self->{widget} = new Gtk2::HBox;
137
138 $self->{widget}->add (my $vbox = new Gtk2::VBox); 134 $self->add (my $vbox = new Gtk2::VBox);
139 135
140 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 136 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
141 $vbox->add ($self->{info} = new Gtk2::Label ""); 137 $vbox->add ($self->{info} = new Gtk2::Label "");
142 $vbox->add (($self->{clock} = new game::goclock)->widget); 138 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
143 139
144 $vbox->add ($self->{imagebox} = new Gtk2::VBox); 140 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
145 141
146 $self; 142 $self;
147} 143}
183 $self->{info}->set_text ("$captures pris."); 179 $self->{info}->set_text ("$captures pris.");
184} 180}
185 181
186package game; 182package game;
187 183
184use Scalar::Util qw(weaken);
185
188use KGS::Constants; 186use KGS::Constants;
189use KGS::Game::Board; 187use KGS::Game::Board;
190 188
189use Gtk2::GoBoard;
190use Gtk2::GoBoard::Constants;
191
192use Glib::Object::Subclass
193 Gtk2::Window;
194
191use base KGS::Listener::Game; 195use base KGS::Listener::Game;
192use base KGS::Game; 196use base KGS::Game;
193 197
194use base gtk::widget;
195
196use POSIX qw(ceil); 198use POSIX qw(ceil);
197 199
198sub new { 200sub new {
199 my $self = shift; 201 my ($self, %arg) = @_;
200 $self = $self->SUPER::new(@_); 202 $self = $self->Glib::Object::new;
203 $self->{$_} = delete $arg{$_} for keys %arg;
201 204
202 $self->listen($self->{conn});
203
204 $self->{window} = new Gtk2::Window 'toplevel';
205 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500]; 205 gtk::state $self, "game::window", undef, window_size => [600, 500];
206 206
207 $self->{window}->signal_connect(delete_event => sub { 207 $self->signal_connect (destroy => sub {
208 $self->part; 208 $self->unlisten;
209 $self->destroy; 209 delete $self->{app}{game}{$self->{channel}};
210 1; 210 %{$_[0]} = ();
211 }); 211 });#d#
212 212
213 $self->{window}->add($self->{hpane} = new Gtk2::HPaned); 213 $self->add (my $hpane = new Gtk2::HPaned);
214 gtk::state $self->{hpane}, "game::hpane", undef, position => 500; 214 gtk::state $hpane, "game::hpane", undef, position => 500;
215 215
216 # LEFT PANE 216 # LEFT PANE
217 217
218 $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 0); 218 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
219 219
220 $self->{boardbox} = new Gtk2::VBox; 220 $self->{boardbox} = new Gtk2::VBox;
221 221
222 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); 222 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
223 223
224 # challenge
225
226 $self->{challenge} = new challenge channel => $self->{channel};
227
228 # board box (aspect/canvas) 224 # board box (aspect/canvas)
229 225
230 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 226 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
227
228 # RIGHT PANE
229
230 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
231 $hpane->set (position_set => 1);
232
233 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
231 234
232 { 235 {
233 $frame->add (my $vbox = new Gtk2::VBox); 236 $frame->add (my $vbox = new Gtk2::VBox);
234 $vbox->add ($self->{title} = new Gtk2::Label $title); 237 $vbox->add ($self->{title} = new Gtk2::Label $title);
235 238
244 $scale->set_digits (0); 247 $scale->set_digits (0);
245 248
246 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 249 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
247 } 250 }
248 251
249 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
250
251 # RIGHT PANE
252
253 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 1, 1);
254 $self->{hpane}->set(position_set => 1);
255 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
256
257 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
258 $sw->set_policy("automatic", "always");
259
260 $sw->add(($self->{userlist} = new userlist)->widget);
261
262 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
263
264 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); 252 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
253
265 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget); 254 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
266 $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget); 255 for COLOUR_WHITE, COLOUR_BLACK;
267 256
268 $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0); 257 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
269 258
270 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); 259 $self->set_channel ($self->{channel});
271 $self->{entry}->signal_connect(activate => sub {
272 my $text = $self->{entry}->get_text;
273 $self->say($text) if $text =~ /\S/;
274 $self->{entry}->set_text("");
275 });
276 260
277 $self->event_update_game; 261 $self->show_all;
262
278 $self; 263 $self;
264}
265
266sub set_channel {
267 my ($self, $channel) = @_;
268
269 $self->{channel} = $channel;
270
271 if ($self->{channel} > 0) {
272 $self->listen ($self->{conn});
273
274 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1);
275 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1);
276
277 $self->signal_connect (delete_event => sub { $self->part; 1 });
278 $self->{chat}->signal_connect (command => sub {
279 my ($chat, $cmd, $arg) = @_;
280 if ($cmd eq "rsave") {
281 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
282 } else {
283 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
284 }
285 });
286 }
279} 287}
280 288
281sub event_update_users { 289sub event_update_users {
282 my ($self, $add, $update, $remove) = @_; 290 my ($self, $add, $update, $remove) = @_;
283 291
284 $self->{userlist}->update ($add, $update, $remove); 292# $self->{userlist}->update ($add, $update, $remove);
293
294 $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy
295 for @$remove;
296
297 $self->{users_inlay}->refresh;
285 298
286 my %important; 299 my %important;
287 $important{$self->{user1}{name}}++; 300 $important{$self->{user1}{name}}++;
288 $important{$self->{user2}{name}}++; 301 $important{$self->{user2}{name}}++;
289 $important{$self->{user3}{name}}++; 302 $important{$self->{user3}{name}}++;
290 303
291 if (my @users = grep $important{$_->{name}}, @$add) { 304 if (my @users = grep $important{$_->{name}}, @$add) {
292 $self->{text}->append_text ("\n<header>Joins:</header>"); 305 $self->{chat}->append_text ("\n<header>Joins:</header>");
293 $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; 306 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
294 } 307 }
295 if (my @users = grep $important{$_->{name}}, @$remove) { 308 if (my @users = grep $important{$_->{name}}, @$remove) {
296 $self->{text}->append_text ("\n<header>Parts:</header>"); 309 $self->{chat}->append_text ("\n<header>Parts:</header>");
297 $self->{text}->append_text (" <user>" . $_->as_string . "</user>") for @users; 310 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
298 } 311 }
299
300} 312}
301 313
302sub join { 314sub join {
303 my ($self) = @_; 315 my ($self) = @_;
304 return if $self->{joined}; 316 return if $self->{joined};
305 317
306 $self->SUPER::join; 318 $self->SUPER::join;
307
308 $self->{window}->show_all;
309} 319}
310 320
311sub part { 321sub update_cursor {
312 my ($self) = @_; 322 my ($self) = @_;
313 323
314 $self->SUPER::part; 324 my $move = int $self->{moveadj}->get_value;
315 $self->destroy; 325 my $cb;
326
327 my $running = $move == @{$self->{path}}
328 && $self->is_active;
329
330 warn "plaiyng $self->{playing} running $running teach $self->{teacher}\n";
331 warn !($self->{playing} && $running);
332 warn $self->{teacher} ne $self->{app}{conn};
333 if (!($self->{playing} && $running) && $self->{teacher} ne $self->{app}{conn}) {
334 warn "A\n";
335 $self->{board}->set (cursor_mask => 0, cursor_value => 0);
336 } elsif ($self->{user1}{name} eq $self->{conn}{name}) {
337 warn "B\n";
338 $self->{board}->set (cursor_mask => MARK_B | MARK_W, cursor_value => MARK_B | MARK_GRAYED);
339 $cb = sub {
340 $self->send (upd_tree =>
341 channel => $self->{channel},
342 tree => $self->gen_move_tree (COLOUR_BLACK, $_[0], $_[1]),
343 );
344 };
345 } elsif ($self->{user2}{name} eq $self->{conn}{name}) {
346 warn "C\n";
347 $self->{board}->set (cursor_mask => MARK_B | MARK_W, cursor_value => MARK_W | MARK_GRAYED);
348 $cb = sub {
349 $self->send (upd_tree =>
350 channel => $self->{channel},
351 tree => $self->gen_move_tree (COLOUR_WHITE, $_[0], $_[1]),
352 );
353 };
354 }
355
356 $self->{board_click} = $cb;
316} 357}
317 358
318sub update_board { 359sub update_board {
319 my ($self) = @_; 360 my ($self) = @_;
320 return unless $self->{path}; 361 return unless $self->{path};
321 362
322 my $move = int $self->{moveadj}->get_value; 363 my $move = int $self->{moveadj}->get_value;
323 364
324 my $running = $move == @{$self->{path}}; 365 my $running = $move == @{$self->{path}}
366 && !$self->{teacher}
367 && $self->is_active;
325 368
326 $self->{board_label}->set_text ("Move " . ($move - 1)); 369 $self->{board_label}->set_text ("Move " . ($move - 1));
327 370
328 $self->{cur_board} = new KGS::Game::Board $self->{size}; 371 $self->{cur_board} = new KGS::Game::Board $self->{size};
329 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); 372 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
330 373
331 for my $colour (WHITE, BLACK) { 374 for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
332 $self->{userpanel}[$colour]->set_state ( 375 $self->{userpanel}[$colour]->set_state (
333 $self->{cur_board}{captures}[$colour], 376 $self->{cur_board}{captures}[$colour],
334 $self->{cur_board}{timer}[$colour], 377 $self->{cur_board}{timer}[$colour],
335 ($running && $self->{lastmove_colour} == !$colour) 378 ($running && $self->{lastmove_colour} == !$colour)
336 ? $self->{lastmove_time} : 0 379 ? $self->{lastmove_time} : 0
337 ); 380 );
338 } 381 }
339 382
340 $self->{board}->set_board ($self->{cur_board}); 383 $self->{board}->set_board ($self->{cur_board});
384
385 $self->update_cursor;
341} 386}
342 387
343sub event_update_tree { 388sub event_update_tree {
344 my ($self) = @_; 389 my ($self) = @_;
345 390
392 }sgexi; 437 }sgexi;
393 438
394 $text .= $_; 439 $text .= $_;
395 } 440 }
396 441
397 $self->{text}->append_text ($text); 442 $self->{chat}->append_text ($text);
398} 443}
399 444
400sub event_join { 445sub event_join {
401 my ($self) = @_; 446 my ($self) = @_;
447
402 $self->SUPER::event_join; 448 $self->SUPER::event_join (@_);
449 $self->init_tree;
450 $self->event_update_game;
403} 451}
404 452
405sub event_part { 453sub event_part {
406 my ($self) = @_; 454 my ($self) = @_;
455
407 $self->SUPER::event_part; 456 $self->SUPER::event_part;
408 $self->destroy; 457 $self->destroy;
409} 458}
410 459
411sub event_move { 460sub event_move {
413 sound::play 1, $pass ? "pass" : "move"; 462 sound::play 1, $pass ? "pass" : "move";
414} 463}
415 464
416sub event_update_game { 465sub event_update_game {
417 my ($self) = @_; 466 my ($self) = @_;
467
418 $self->SUPER::event_update_game; 468 $self->SUPER::event_update_game;
419 469
470 return unless $self->{joined};
471
472 $self->{playing} = $self->{teacher} eq $self->{conn}{name}
473 || $self->is_playing ($self->{conn}{name});
474
420 my $title = defined $self->{channel} 475 my $title = defined $self->{channel}
421 ? $self->owner->as_string . " " . $self->opponent_string 476 ? $self->owner->as_string . " " . $self->opponent_string
422 : "Game Window"; 477 : "Game Window";
423 $self->{window}->set_title("KGS Game $title"); 478 $self->set_title("KGS Game $title");
424 $self->{title}->set_text ($title); 479 $self->{title}->set_text ($title);
425 480
426 $self->{user}[BLACK] = $self->{user1}; 481 $self->{user}[COLOUR_BLACK] = $self->{user1};
427 $self->{user}[WHITE] = $self->{user2}; 482 $self->{user}[COLOUR_WHITE] = $self->{user2};
428 483
429 # show board 484 # show board
485 if ($self->is_inprogress) {
486 if (!$self->{boardbox}->parent) {
487 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
488 $self->{left}->add ($self->{boardbox});
489 $self->{board}->signal_connect (button_release => sub {
490 warn "XXX @_\n";#d#
491 if ($_[1] == 1) {
492 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
493 }
494 });
495 }
496 if (my $ch = delete $self->{challenge}) {
497 (delete $_->{inlay})->destroy for values %$ch;
498 }
499 $self->update_cursor;
500 }
501
502 $self->{left}->show_all;
503
504 $self->{rules_inlay}->refresh;
430 505
431 $self->{left}->remove ($_) for $self->{left}->get_children; 506}
432 if ($self->is_valid) {
433 $self->{left}->add ($self->{boardbox});
434 (delete $self->{challenge})->destroy if $self->{challenge};
435 } else {
436 $self->{left}->add ($self->{challenge}->widget);
437 }
438 $self->{left}->show_all;
439 507
440 # view text 508sub draw_setup {
441 509 my ($self, $inlay) = @_;
442 eval { #d#
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] .= " valid" if $self->is_valid;
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 510
511 return unless $self->{joined};
512
513 my $rules = $self->{rules};
514
515 my $text = "";
516
517 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
518 if $self->{teacher};
519
452 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid; 520 $text .= "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>"
521 if $self->{user3}->is_valid;
453 522
523 if ($self->is_inprogress) {
454 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>" 524 $text .= "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
455 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>" 525 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>";
456 if $self->is_valid;
457
458 if ($self->is_valid) {
459 $ga[4] = "\nHandicap: " . $self->{handicap};
460 $ga[5] = "\nKomi: " . $self->{komi};
461 $ga[6] = "\nSize: " . $self->size_string;
462 }
463
464 if ($self->is_scored) {
465 $ga[7] = "\nResult: " . $self->score_string;
466 }
467
468 $text = "\n<infoblock><header>Game Update</header>";
469 for (0..7) {
470 if ($self->{gatext}[$_] ne $ga[$_]) {
471 $text .= $ga[$_];
472 }
473 }
474 $text .= "</infoblock>";
475
476 $self->{gatext} = \@ga;
477 }; 526 }
478 527 $text .= "\nType: " . util::toxml $gametype{$self->type};
479 $self->{text}->append_text ($text);
480}
481
482sub event_update_rules {
483 my ($self, $rules) = @_;
484
485 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
486 for BLACK, WHITE;
487
488 sound::play 3, "gamestart";
489
490 my $text = "\n<header>Game Rules</header>";
491 528
492 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; 529 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
493 530
494 $text .= "\nTime: "; 531 $text .= "\nTime: ";
495 532
497 $text .= "UNLIMITED"; 534 $text .= "UNLIMITED";
498 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { 535 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
499 $text .= util::format_time $rules->{time}; 536 $text .= util::format_time $rules->{time};
500 $text .= " ABS"; 537 $text .= " ABS";
501 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { 538 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
502 $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count}; 539 $text .= util::format_time $rules->{time};
503 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; 540 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
504 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { 541 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
505 $text .= util::format_time $rules->{time}; 542 $text .= util::format_time $rules->{time};
506 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; 543 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
507 } 544 }
508 545
546 $text .= "\nFlags:";
547 $text .= " private" if $self->is_private;
548 $text .= " started" if $self->is_inprogress;
549 $text .= " adjourned" if $self->is_adjourned;
550 $text .= " scored" if $self->is_scored;
551 $text .= " saved" if $self->is_saved;
552
553 if ($self->is_inprogress) {
554 $text .= "\nHandicap: " . $self->{handicap};
555 $text .= "\nKomi: " . $self->{komi};
556 $text .= "\nSize: " . $self->size_string;
557 }
558
559 if ($self->is_scored) {
560 $text .= "\nResult: " . $self->score_string;
561 }
562
509 $self->{text}->append_text ("<infoblock>$text</infoblock>"); 563 $inlay->append_text ("<infoblock>$text</infoblock>");
564
565}
566
567sub event_update_rules {
568 my ($self, $rules) = @_;
569
570 $self->{rules} = $rules;
571
572 if ($self->{user}) {
573 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
574 for COLOUR_BLACK, COLOUR_WHITE;
575 }
576
577 sound::play 3, "gamestart";
578
579 $self->{rules_inlay}->refresh;
510} 580}
511 581
512sub inject_resign_game { 582sub inject_resign_game {
513 my ($self, $msg) = @_; 583 my ($self, $msg) = @_;
514 584
515 sound::play 3, "resign"; 585 sound::play 3, "resign";
516 586
517 $self->{text}->append_text ("\n<infoblock><header>Resign</header>" 587 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
518 . "\n<user>" 588 . "\n<user>"
519 . (util::toxml $self->{user}[$msg->{player}]->as_string) 589 . (util::toxml $self->{user}[$msg->{player}]->as_string)
520 . "</user> resigned.</infoblock>"); 590 . "</user> resigned.</infoblock>");
521} 591}
522 592
523sub inject_final_result { 593sub inject_final_result {
524 my ($self, $msg) = @_; 594 my ($self, $msg) = @_;
525 595
526 $self->{text}->append_text ("<infoblock>\n<header>Game Over</header>" 596 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
527 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) 597 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
528 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) 598 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
529 . "</infoblock>" 599 . "</infoblock>"
530 ); 600 );
531} 601}
532 602
603sub inject_new_game {
604 my ($self, $msg) = @_;
605
606 $self->{chat}->append_text ("\n<header>ACK from server ($msg->{cid} == $self->{cid})</header>");
607 delete $self->{cid};
608}
609
610sub draw_challenge {
611 my ($self, $id) = @_;
612
613 my $challenge = $self->{challenge}{$id};
614
615 my $inlay = $challenge->{inlay};
616 my $rules = $challenge->{rules};
617
618 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name} ? 1 : 0;;
619 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
620
621 my ($size, $time, $interval, $count);
622
623 if (!$self->{channel}) {
624 $inlay->append_text ("\nNotes: ");
625 $inlay->append_entry (\$channel->{notes}, 20, "");
626 $inlay->append_text ("\nGlobal Offer: ");
627 $inlay->append_optionmenu (\$challenge->{flags},
628 0 => "No",
629 2 => "Yes",
630 );
631 } else {
632 $inlay->append_text ("\nNotes: " . util::toxml $channel->{notes});
633 }
634
635 $inlay->append_text ("\nType: ");
636 $inlay->append_optionmenu (
637 \$challenge->{type},
638 GAMETYPE_DEMONSTRATION , "Demonstration",
639 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P)",
640 GAMETYPE_TEACHING , "Teaching",
641 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P)",
642 GAMETYPE_SIMUL , "Simul (not yet!)",
643 GAMETYPE_FREE , "Free",
644 GAMETYPE_RATED , "Rated",
645 sub {
646 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
647 },
648 );
649
650 if ($self->{channel}) {
651 $inlay->append_text ("\nMy Colour: ");
652 $inlay->append_optionmenu (
653 \$as_black,
654 0 => "White",
655 1 => "Black",
656 sub {
657 if ($challenge->{$_[0] ? "user1" : "user2"}{name} ne $self->{conn}{name}) {
658 ($challenge->{user1}, $challenge->{user2}) = ($challenge->{user2}, $challenge->{user1});
659 }
660 }
661 );
662 }
663
664 $inlay->append_text ("\nRuleset: ");
665 $inlay->append_optionmenu (
666 \$challenge->{rules}{ruleset},
667 RULESET_JAPANESE , "Japanese",
668 RULESET_CHINESE , "Chinese",
669 RULESET_AGA , "AGA",
670 RULESET_NEW_ZEALAND, "New Zealand",
671 );
672
673 $inlay->append_text ("\nSize: ");
674 $size = $inlay->append_optionmenu (
675 \$challenge->{rules}{size}, 9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38
676 );
677
678 if ($self->{channel}) {
679 $inlay->append_text ("\nHandicap: ");
680 $inlay->append_optionmenu (\$challenge->{rules}{handicap}, map +($_, $_), 0..9);
681
682 $inlay->append_text ("\nKomi: ");
683 $inlay->append_entry (\$challenge->{rules}{komi}, 5);
684 }
685
686 $inlay->append_text ("\nTimesys: ");
687 $inlay->append_optionmenu (
688 \$challenge->{rules}{timesys},
689 &TIMESYS_NONE => "None",
690 &TIMESYS_ABSOLUTE => "Absolute",
691 &TIMESYS_BYO_YOMI => "Byo Yomi",
692 &TIMESYS_CANADIAN => "Canadian",
693 sub {
694 my ($new) = @_;
695
696 if ($new eq TIMESYS_NONE) {
697 $time->hide;
698 $interval->hide;
699 $count->hide;
700 } else {
701 $time->show;
702 $time->set_text ($self->{app}{defaults}{time});
703 if ($new eq TIMESYS_ABSOLUTE) {
704 $interval->hide;
705 $count->hide;
706 } else {
707 $interval->show;
708 $count->show;
709 if ($new eq TIMESYS_BYO_YOMI) {
710 $interval->set_text ($self->{app}{defaults}{byo_time});
711 $count->set_text ($self->{app}{defaults}{byo_period});
712 } elsif ($new eq TIMESYS_CANADIAN) {
713 $interval->set_text ($self->{app}{defaults}{can_time});
714 $count->set_text ($self->{app}{defaults}{can_period});
715 }
716 }
717 }
718 }
719 );
720
721 $inlay->append_text ("\nMain Time: ");
722 $time = $inlay->append_entry (\$challenge->{rules}{time}, 5);
723 $inlay->append_text ("\nInterval: ");
724 $interval = $inlay->append_entry (\$challenge->{rules}{interval}, 3);
725 $inlay->append_text ("\nPeriods/Stones: ");
726 $count = $inlay->append_entry (\$challenge->{rules}{count}, 2);
727
728 $inlay->append_text ("\n");
729
730 if (!$self->{channel}) {
731 $inlay->append_button ("Create Challenge", sub {
732 $inlay->clear;
733 $self->{cid} = $self->{conn}->alloc_clientid;
734 $self->send (new_game =>
735 channel => delete $self->{roomid},
736 cid => $self->{cid},
737 type => $challenge->{type},
738 flags => $challenge->{flags},
739 rules => $challenge->{rules},
740 notes => $challenge->{notes},
741 );
742 });
743 } else {
744 $inlay->append_button ("OK", sub {
745 $inlay->clear;
746 $self->send (challenge =>
747 channel => $self->{channel},
748 challenge => $challenge,
749 );
750 });
751 if (exists $self->{challenge}{""}) {
752 $inlay->append_button ("Reject", sub {
753 $inlay->clear;
754 $self->send (reject_challenge =>
755 channel => $self->{channel},
756 name => $opponent->{name},
757 );
758 });
759 }
760 }
761}
762
763sub draw_users {
764 my ($self, $inlay) = @_;
765
766 for (sort keys %{$self->{users}}) {
767 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
768 }
769}
770
533sub event_challenge { 771sub event_challenge {
534 my ($self, $challenge) = @_; 772 my ($self, $challenge) = @_;
535 773
536 use KGS::Listener::Debug; 774 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
537 $self->{text}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge)); 775 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
538}
539 776
540sub destroy { 777 my $id = $opponent->{name};
541 my ($self) = @_; 778
542 $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy 779 $self->{challenge}{$id} = $challenge;
543 for BLACK, WHITE; 780 $self->{chat}->new_switchable_inlay (
544 $self->SUPER::destroy; 781 exists $self->{challenge}{""} ? "Challenge from $opponent->{name}" : "Challenge to $opponent->{name}",
545 delete $self->{app}{gamelist}{game}{$self->{channel}}; 782 sub {
783 $self->{challenge}{$id}{inlay} = $_[0];
784 $self->draw_challenge ($id);
785 },
786 !exists $self->{challenge}{""} # only open when not offerer
787 );
546} 788}
547 789
5481; 7901;
549 791

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines