ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.117
Committed: Tue Jun 1 21:24:11 2004 UTC (20 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.116: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 use utf8;
2
3 use Scalar::Util ();
4
5 ### GO CLOCK WIDGET #########################################################
6
7 package game::goclock;
8
9 # Lo and Behold! I admit it! The rounding stuff etc.. in goclock
10 # is completely borked.
11
12 use Time::HiRes ();
13
14 use KGS::Constants;
15
16 use Glib::Object::Subclass
17 Gtk2::Label;
18
19 sub INIT_INSTANCE {
20 my $self = shift;
21
22 $self->signal_connect (destroy => sub { $_[0]->stop });
23
24 $self->{set} = sub { };
25 $self->{format} = sub { "???" };
26 }
27
28 sub FINALIZE_INSTANCE {
29 my $self = shift;
30
31 $self->stop;
32 }
33
34 sub configure {
35 my ($self, $timesys, $main, $interval, $count) = @_;
36
37 if ($timesys == TIMESYS_ABSOLUTE) {
38 $self->{format} = sub {
39 if ($_[0] < 0) {
40 "TIMEOUT";
41 } else {
42 util::format_time $_[0];
43 }
44 };
45
46 } elsif ($timesys == TIMESYS_BYO_YOMI) {
47 my $low = $interval * $count;
48
49 $self->{format} = sub {
50 if ($_[0] < 0) {
51 "TIMEOUT";
52 } elsif ($_[0] > $low) {
53 util::format_time $_[0] - $low;
54 } else {
55 sprintf "%s (%d)",
56 util::format_time int (($_[0] - 1) % $interval + 1),
57 ($_[0] - 1) / $interval;
58 }
59 };
60
61 } elsif ($timesys == TIMESYS_CANADIAN) {
62 $self->{format} = sub {
63 if ($_[0] < 0) {
64 "TIMEOUT";
65 } elsif (!$self->{moves}) {
66 util::format_time $_[0] - $low;
67 } else {
68 my $time = int (($_[0] - 1) % $interval + 1);
69
70 sprintf "%s/%d =%d",
71 util::format_time $time,
72 $self->{moves},
73 $self->{moves} > 1
74 ? $time / $self->{moves}
75 : $interval;
76 }
77 };
78
79 } else {
80 # none, or unknown
81 $self->{format} = sub { "-" }
82 }
83 }
84
85 sub refresh {
86 my ($self, $timestamp) = @_;
87 my $timer = $self->{time} + $self->{start} - $timestamp;
88
89 # we round the timer value slightly... the protocol isn't exact anyways,
90 # and this gives smoother timers ;)
91 my $timer2 = int $timer + 0.4;
92
93 $self->set_text ($self->{format}->($timer2));
94
95 $timer - int $timer;
96 }
97
98 sub set_time {
99 my ($self, $start, $time, $moves) = @_;
100
101 $self->{time} = $time;
102 $self->{moves} = $moves;
103
104 if ($start) {
105 $self->{start} = $start;
106 $self->start;
107 } else {
108 $self->stop;
109 $self->refresh ($self->{start});
110 }
111 }
112
113 sub start {
114 my ($self) = @_;
115
116 $self->stop;
117
118 my $timeout; $timeout = sub {
119 my $next = $self->refresh (Time::HiRes::time) * 1000;
120 $next += 1000 if $next < 0;
121 $self->{timeout} = add Glib::Timeout $next, $timeout;
122 0;
123 };
124
125 $timeout->();
126 }
127
128 sub stop {
129 my ($self) = @_;
130
131 remove Glib::Source delete $self->{timeout} if $self->{timeout};
132 }
133
134 ### USER PANEL ##############################################################
135
136 package game::userpanel;
137
138 use KGS::Constants;
139
140 use Glib::Object::Subclass
141 Gtk2::Frame,
142 properties => [
143 Glib::ParamSpec->IV ("colour", "colour", "User Colour",
144 COLOUR_BLACK, COLOUR_WHITE, COLOUR_BLACK,
145 [qw(construct-only readable writable)]),
146 ];
147
148 sub INIT_INSTANCE {
149 my ($self) = @_;
150
151 $self->add (my $vbox = new Gtk2::VBox);
152
153 $vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0);
154 $vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0);
155 $vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0);
156
157 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
158
159 $self;
160 }
161
162 sub configure {
163 my ($self, $app, $user, $rules) = @_;
164
165 if ($self->{name}->get_text ne $user->as_string) {
166 $self->{name}->set_text ($user->as_string);
167
168 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
169 $self->{imagebox}->add (gtk::image_from_data undef);
170 $self->{imagebox}->show_all;
171
172 if ($user->has_pic) {
173 # the big picture...
174 $app->userpic ($user->{name}, sub {
175 return unless $self->{imagebox};
176
177 if ($_[0]) {
178 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
179 $self->{imagebox}->add (gtk::image_from_data $_[0]);
180 $self->{imagebox}->show_all;
181 }
182 });
183 }
184 }
185
186 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
187 }
188
189 sub set_captures {
190 my ($self, $captures) = @_;
191
192 $self->{info}->set_text ("$captures pris.");
193 }
194
195 sub set_timer {
196 my ($self, $start, $time, $moves) = @_;
197
198 $self->{clock}->set_time ($start, $time, $moves);
199 }
200
201 ### GAME WINDOW #############################################################
202
203 package game;
204
205 use Scalar::Util qw(weaken);
206
207 use KGS::Constants;
208 use KGS::Game::Board;
209
210 use Gtk2::GoBoard;
211 use Gtk2::GoBoard::Constants;
212
213 use base KGS::Game;
214 use base KGS::Listener::Game;
215
216 use Glib::Object::Subclass
217 Gtk2::Window;
218
219 use POSIX qw(ceil);
220
221 sub new {
222 my ($self, %arg) = @_;
223 $self = $self->Glib::Object::new;
224 $self->{$_} = delete $arg{$_} for keys %arg;
225
226 gtk::state $self, "game::window", undef, window_size => [600, 500];
227
228 $self->signal_connect (destroy => sub {
229 $self->unlisten;
230 delete $self->{app}{game}{$self->{channel}};
231 %{$_[0]} = ();
232 });#d#
233
234 $self->add (my $hpane = new Gtk2::HPaned);
235 gtk::state $hpane, "game::hpane", undef, position => 500;
236
237 # LEFT PANE
238
239 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
240
241 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
242
243 # board box (aspect/canvas)
244
245 # RIGHT PANE
246
247 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
248 $hpane->set (position_set => 1);
249
250 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
251
252 {
253 $frame->add (my $vbox = new Gtk2::VBox);
254 $vbox->add ($self->{title} = new Gtk2::Label "-");
255
256 $vbox->add (my $hbox = new Gtk2::HBox);
257
258 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0);
259
260 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
261
262 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
263 $scale->set_draw_value (0);
264 $scale->set_digits (0);
265
266 $self->{moveadj}->signal_connect (value_changed => sub {
267 $self->{showmove} = int $self->{moveadj}->get_value;
268 $self->update_board;
269 });
270 }
271
272 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
273
274 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
275 for COLOUR_WHITE, COLOUR_BLACK;
276
277 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
278
279 $buttonbox->add ($self->{button_pass} =
280 Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0));
281 $self->{button_pass}->signal_connect (clicked => sub {
282 $self->{board_click}->(255, 255) if $self->{board_click};
283 });
284 $buttonbox->add ($self->{button_undo} =
285 Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0));
286 $self->{button_undo}->signal_connect (clicked => sub {
287 $self->send (req_undo => channel => $self->{channel});
288 });
289 $buttonbox->add ($self->{button_resign} =
290 Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0));
291 $self->{button_resign}->signal_connect (clicked => sub {
292 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour});
293 });
294
295 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
296
297 $self->set_channel ($self->{channel});
298
299 $self->show_all;
300
301 $self;
302 }
303
304 sub set_channel {
305 my ($self, $channel) = @_;
306
307 $self->{channel} = $channel;
308
309 if ($self->{channel} > 0) {
310 $self->listen ($self->{conn});
311
312 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1);
313 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1);
314
315 $self->signal_connect (delete_event => sub { $self->part; 1 });
316 $self->{chat}->signal_connect (command => sub {
317 my ($chat, $cmd, $arg) = @_;
318 if ($cmd eq "rsave") {
319 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
320 } else {
321 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
322 }
323 });
324 }
325 }
326
327 ### JOIN/LEAVE ##############################################################
328
329 sub join {
330 my ($self) = @_;
331 return if $self->{joined};
332
333 $self->SUPER::join;
334 }
335
336 sub event_join {
337 my ($self) = @_;
338
339 $self->SUPER::event_join (@_);
340 $self->init_tree;
341 $self->event_update_game;
342 }
343
344 sub event_part {
345 my ($self) = @_;
346
347 $self->SUPER::event_part;
348 $self->destroy;
349 }
350
351 sub event_quit {
352 my ($self) = @_;
353
354 $self->SUPER::event_quit;
355 $self->destroy;
356 }
357
358 ### USERS ###################################################################
359
360 sub draw_users {
361 my ($self, $inlay) = @_;
362
363 for (sort keys %{$self->{users}}) {
364 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
365 }
366 }
367
368 sub event_update_users {
369 my ($self, $add, $update, $remove) = @_;
370
371 # $self->{userlist}->update ($add, $update, $remove);
372
373 $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy
374 for @$remove;
375
376 $self->{users_inlay}->refresh;
377
378 my %important;
379 $important{$self->{black}{name}}++;
380 $important{$self->{white}{name}}++;
381 $important{$self->{owner}{name}}++;
382
383 if (my @users = grep $important{$_->{name}}, @$add) {
384 $self->{chat}->append_text ("\n<header>Joins:</header>");
385 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
386 }
387 if (my @users = grep $important{$_->{name}}, @$remove) {
388 $self->{chat}->append_text ("\n<header>Parts:</header>");
389 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
390 }
391 }
392
393 ### GAME INFO ###############################################################
394
395 sub draw_setup {
396 my ($self, $inlay) = @_;
397
398 return unless $self->{joined};
399
400 my $rules = $self->{rules};
401
402 my $text = "";
403
404 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
405 if $self->{teacher};
406
407 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
408 if $self->{owner}->is_valid;
409
410 if ($self->is_inprogress) {
411 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
412 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
413 }
414 $text .= "\nType: " . util::toxml $gametype{$self->type};
415
416 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
417
418 $text .= "\nTime: ";
419
420 if ($rules->{timesys} == TIMESYS_NONE) {
421 $text .= "UNLIMITED";
422 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
423 $text .= util::format_time $rules->{time};
424 $text .= " ABS";
425 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
426 $text .= util::format_time $rules->{time};
427 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
428 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
429 $text .= util::format_time $rules->{time};
430 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
431 }
432
433 $text .= "\nFlags:";
434 $text .= " private" if $self->is_private;
435 $text .= " started" if $self->is_inprogress;
436 $text .= " adjourned" if $self->is_adjourned;
437 $text .= " scored" if $self->is_scored;
438 $text .= " saved" if $self->is_saved;
439
440 if ($self->is_inprogress) {
441 $text .= "\nHandicap: " . $self->{handicap};
442 $text .= "\nKomi: " . $self->{komi};
443 $text .= "\nSize: " . $self->size_string;
444 }
445
446 if ($self->is_scored) {
447 $text .= "\nResult: " . $self->score_string;
448 }
449
450 $inlay->append_text ("<infoblock>$text</infoblock>");
451
452 }
453
454 sub event_update_game {
455 my ($self) = @_;
456
457 $self->SUPER::event_update_game;
458
459 return unless $self->{joined};
460
461 $self->{colour} = $self->player_colour ($self->{conn}{name});
462
463 $self->{user}[COLOUR_BLACK] = $self->{black};
464 $self->{user}[COLOUR_WHITE] = $self->{white};
465
466 # show board
467 if ($self->is_inprogress) {
468 if (!$self->{board}) {
469 $self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
470 $self->{board}->signal_connect (button_release => sub {
471 return unless $self->{cur_board};
472 if ($_[1] == 1) {
473 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
474 }
475 });
476 $self->{board}->show_all;
477 }
478 if (my $ch = delete $self->{challenge}) {
479 $_->{inlay}->destroy for values %$ch;
480 }
481 $self->update_cursor;
482 }
483
484 my $title = defined $self->{channel}
485 ? $self->owner->as_string . " " . $self->opponent_string
486 : "Game Window";
487 $self->set_title ("KGS Game $title");
488 $self->{title}->set_text ($title); # title gets redrawn wrongly
489
490 $self->{rules_inlay}->refresh;
491
492 if (exists $self->{teacher}) {
493 $self->{teacher_inlay} ||= $self->{chat}->new_inlay;
494 $self->{teacher_inlay}->clear;
495 $self->{teacher_inlay}->append_text ("\n<header>Teacher:</header> <user>"
496 . (util::toxml $self->{teacher}) . "</user>");
497 } elsif ($self->{teacher_inlay}) {
498 (delete $self->{teacher_inlay})->clear;
499 }
500
501 $self->update_cursor;
502 }
503
504 sub event_update_rules {
505 my ($self, $rules) = @_;
506
507 $self->{rules} = $rules;
508
509 if ($self->{user}) {
510 # todo. gets drawn wrongly
511
512 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
513 for COLOUR_BLACK, COLOUR_WHITE;
514 }
515
516 sound::play 3, "gamestart";
517 $self->{rules_inlay}->refresh;
518 }
519
520 ### BOARD DISPLAY ###########################################################
521
522 sub update_timers {
523 my ($self, $timers) = @_;
524
525 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
526
527 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
528 my $t = $timers->[$colour];
529 $self->{userpanel}[$colour]->set_timer (
530 $running && $colour == $self->{whosemove} && $t->[0],
531 $t->[1] || $self->{rules}{time}
532 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
533 && $self->{rules}{interval} * $self->{rules}{count}),
534 $t->[2]);
535 }
536 }
537
538 sub inject_set_gametime {
539 my ($self, $msg) = @_;
540
541 $self->{timers} = [
542 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
543 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
544 ];
545
546 $self->update_timers ($self->{timers})
547 if $self->{showmove} == @{$self->{path}};
548 }
549
550 sub update_cursor {
551 my ($self) = @_;
552
553 return unless $self->{cur_board};
554
555 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active;
556
557 delete $self->{board_click};
558
559 if ($self->{teacher} eq $self->{app}{conn}) {
560 #TODO# # teaching mode not implemented
561 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
562 $self->{button_undo}->hide;
563 $self->{button_resign}->hide;
564 $self->{board}->set (cursor => undef);
565
566 } elsif ($running && $self->{colour} != COLOUR_NONE) {
567 # during game
568 $self->{button_undo}->show;
569 $self->{button_resign}->show;
570
571 if ($self->{cur_board}{score}) {
572 # during scoring
573 $self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1);
574 $self->{board}->set (cursor => sub {
575 $_[0] & (MARK_B | MARK_W)
576 ? $_[0] ^ MARK_GRAYED
577 : $_[0];
578 });
579 $self->{board_click} = sub {
580 if ($_[0] == 255) {
581 $self->{button_pass}->sensitive (0);
582 $self->done;
583 } else {
584 $self->send (mark_dead =>
585 channel => $self->{channel},
586 x => $_[0],
587 y => $_[1],
588 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED),
589 );
590 }
591 };
592
593 } elsif ($self->{colour} == $self->{whosemove}) {
594 # normal move
595 $self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1);
596 $self->{board}->set (cursor => sub {
597 $self->{cur_board}
598 && $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
599 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND)
600 ? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B)
601 : $_[0];
602 });
603 $self->{board_click} = sub {
604 return unless
605 $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
606 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND);
607 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
608 $self->{board}->set (cursor => undef);
609 delete $self->{board_click};
610 $self->{button_pass}->sensitive (0);
611 };
612 } else {
613 $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1);
614 $self->{board}->set (cursor => undef);
615 }
616 } else {
617 $self->{button_undo}->hide;
618 $self->{button_resign}->hide;
619 $self->{button_pass}->hide;
620 $self->{board}->set (cursor => undef);
621 #TODO# # implement coordinate-grabbing
622 }
623 }
624
625 sub update_board {
626 my ($self) = @_;
627
628 return unless $self->{path};
629
630 if ($self->{rules}{ruleset} == RULESET_JAPANESE) {
631 if ($self->{curnode}{move} == 0) {
632 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
633 } else {
634 $self->{whosemove} = 1 - $self->{cur_board}{last};
635 }
636 } else {
637 # Chinese, Aga, NZ all have manual placement
638 if ($self->{curnode}{move} < $self->{handicap}) {
639 $self->{whosemove} = COLOUR_BLACK;
640 } elsif ($self->{curnode}{move} == $self->{handicap}) {
641 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
642 } else {
643 $self->{whosemove} = 1 - $self->{cur_board}{last};
644 }
645 }
646
647 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1));
648
649 $self->{cur_board} = new KGS::Game::Board $self->{size};
650 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]);
651
652 $self->update_cursor;
653
654 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
655 for COLOUR_WHITE, COLOUR_BLACK;
656
657 my $start_time = $self->{rules}{time};
658
659 if ($self->{showmove} == @{$self->{path}}) {
660 $self->{timers} = [
661 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}],
662 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}],
663 ];
664 $self->update_timers ($self->{timers});
665 } else {
666 $self->update_timers ([
667 [0, @{$self->{cur_board}{timer}[0]}],
668 [0, @{$self->{cur_board}{timer}[1]}],
669 ]);
670 }
671
672 $self->{board}->set_board ($self->{cur_board});
673
674 if ($self->{cur_board}{score}) {
675 $self->{score_inlay} ||= $self->{chat}->new_inlay;
676 $self->{score_inlay}->clear;
677 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
678 . "\n<score>"
679 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
680 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
681 . "</score>");
682 } elsif ($self->{score_inlay}) {
683 (delete $self->{score_inlay})->clear;
684 }
685 }
686
687 sub event_update_tree {
688 my ($self) = @_;
689
690 (delete $self->{undo_inlay})->clear
691 if $self->{undo_inlay};
692
693 $self->{path} = $self->get_path;
694
695 if ($self->{moveadj}) {
696 my $upper = $self->{moveadj}->upper;
697 my $pos = $self->{moveadj}->get_value;
698 my $move = scalar @{$self->{path}};
699
700 $self->{moveadj}->upper ($move);
701
702 $self->{moveadj}->changed;
703 if ($pos == $upper) {
704 $self->{moveadj}->value ($move);
705 $self->{moveadj}->value_changed;
706 }
707 }
708 }
709
710 sub event_update_comments {
711 my ($self, $node, $comment, $newnode) = @_;
712 $self->SUPER::event_update_comments($node, $comment, $newnode);
713
714 my $text;
715
716 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
717 if $newnode;
718
719 for (split /\n/, $comment) {
720 $text .= "\n";
721 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
722 $text .= "<user>" . (util::toxml $1) . "</user>:";
723 }
724
725 # coords only for 19x19 so far
726 $_ = util::toxml $_;
727 s{
728 (
729 \b
730 (?:[bw])?
731 [, ]{0,2}
732 [a-hj-t] # valid for upto 19x19
733 \s?
734 [1-9]?[0-9]
735 \b
736 )
737 }{
738 "<coord>$1</coord>";
739 }sgexi;
740
741 $text .= $_;
742 }
743
744 $self->{chat}->append_text ($text);
745 }
746
747 sub event_move {
748 my ($self, $pass) = @_;
749
750 sound::play 1, $pass ? "pass" : "move";
751 }
752
753 ### GAMEPLAY EVENTS #########################################################
754
755 sub event_resign_game {
756 my ($self, $player) = @_;
757
758 sound::play 3, "resign";
759 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
760 . "\n<user>"
761 . (util::toxml $self->{user}[$msg->{player}]->as_string)
762 . "</user> resigned."
763 . "\n<user>"
764 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
765 . "</user> wins the game."
766 . "</infoblock>");
767 }
768
769 sub event_time_win {
770 my ($self, $player) = @_;
771
772 sound::play 3, "timewin";
773 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>"
774 . "\n<user>"
775 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
776 . "</user> ran out of time and lost."
777 . "\n<user>"
778 . (util::toxml $self->{user}[$msg->{player}]->as_string)
779 . "</user> wins the game."
780 . "</infoblock>");
781 }
782
783 sub event_owner_left {
784 my ($self) = @_;
785
786 $self->{chat}->append_text ("\n<infoblock><header>Owner left</header>"
787 . "\nThe owner of this game left.</infoblock>");
788 }
789
790 sub event_teacher_left {
791 my ($self) = @_;
792
793 $self->{chat}->append_text ("\n<infoblock><header>Teacher left</header>"
794 . "\nThe teacher left the game.</infoblock>");
795 }
796
797 sub event_done {
798 my ($self) = @_;
799
800 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) {
801 sound::play 2, "info" unless $inlay->{count};
802 $self->{chat}->append_text ("\n<infoblock><header>Press Done</header>"
803 . "\nYour opponent pressed done. Now it's up to you.");
804 }
805 if ($self->{doneid} & 0x80000000) {
806 sound::play 2, "info" unless $inlay->{count};
807 $self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>"
808 . "\nThe board has changed.");
809 }
810
811 $self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]);
812
813 $self->{chat}->set_end;
814 }
815
816 sub inject_final_result {
817 my ($self, $msg) = @_;
818
819 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
820 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
821 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
822 . "</infoblock>"
823 );
824 }
825
826 sub inject_req_undo {
827 my ($self, $msg) = @_;
828
829 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay;
830 return if $inlay->{ignore};
831
832 sound::play 2, "warning" unless $inlay->{count};
833 $inlay->{count}++;
834
835 $inlay->clear;
836 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n");
837 $inlay->append_button ("Grant", sub {
838 (delete $self->{undo_inlay})->clear;
839 $self->send (grant_undo => channel => $self->{channel});
840 });
841 $inlay->append_button ("Ignore", sub {
842 $inlay->clear;
843 $inlay->{ignore} = 1;
844 # but leave inlay, so further undo requests get counted
845 });
846
847 $self->{chat}->set_end;
848 }
849
850 sub inject_new_game {
851 my ($self, $msg) = @_;
852
853 if ($msg->{cid} != $self->{cid}) {
854 $self->part;
855 warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d#
856 }
857
858 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>");
859 delete $self->{cid};
860 }
861
862 ### CHALLENGE HANDLING ######################################################
863
864 sub draw_challenge {
865 my ($self, $id) = @_;
866
867 my $info = $self->{challenge}{$id};
868 my $inlay = $info->{inlay};
869 my $rules = $info->{rules};
870
871 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;;
872 my $opponent = $as_black ? $info->{white} : $info->{black};
873
874 my ($size, $time, $interval, $count, $type);
875
876 if (!$self->{channel}) {
877 $inlay->append_text ("\nNotes: ");
878 $inlay->append_entry (\$info->{notes}, 20, "");
879 $inlay->append_text ("\nGlobal Offer: ");
880 $inlay->append_optionmenu (\$info->{flags},
881 0 => "No",
882 2 => "Yes",
883 );
884 } else {
885 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes});
886 }
887
888 $inlay->append_text ("\nType: ");
889 $type = $inlay->append_optionmenu (
890 \$info->{gametype},
891 GAMETYPE_DEMONSTRATION , "Demonstration (not yet)",
892 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)",
893 GAMETYPE_TEACHING , "Teaching (not yet)",
894 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)",
895 GAMETYPE_SIMUL , "Simul (not yet!)",
896 GAMETYPE_FREE , "Free",
897 GAMETYPE_RATED , "Rated",
898 sub {
899 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
900 },
901 );
902
903 if ($self->{channel}) {
904 $inlay->append_text ("\nMy Colour: ");
905 $inlay->append_optionmenu (
906 \$as_black,
907 0 => "White",
908 1 => "Black",
909 sub {
910 if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) {
911 ($info->{black}, $info->{white}) = ($info->{white}, $info->{black});
912 }
913 }
914 );
915 }
916
917 $inlay->append_text ("\nRuleset: ");
918 $inlay->append_optionmenu (
919 \$info->{rules}{ruleset},
920 RULESET_JAPANESE , "Japanese",
921 RULESET_CHINESE , "Chinese",
922 RULESET_AGA , "AGA",
923 RULESET_NEW_ZEALAND, "New Zealand",
924 );
925
926 $inlay->append_text ("\nSize: ");
927 $size = $inlay->append_optionmenu (
928 \$info->{rules}{size},
929 (9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38),
930 sub {
931 $type->set_history (5) # reset to free
932 if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED;
933 },
934 );
935
936 if ($self->{channel}) {
937 $inlay->append_text ("\nHandicap: ");
938 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
939
940 $inlay->append_text ("\nKomi: ");
941 $inlay->append_entry (\$info->{rules}{komi}, 5);
942 }
943
944 $inlay->append_text ("\nTimesys: ");
945 $inlay->append_optionmenu (
946 \$info->{rules}{timesys},
947 &TIMESYS_NONE => "None",
948 &TIMESYS_ABSOLUTE => "Absolute",
949 &TIMESYS_BYO_YOMI => "Byo Yomi",
950 &TIMESYS_CANADIAN => "Canadian",
951 sub {
952 my ($new) = @_;
953
954 if ($new eq TIMESYS_NONE) {
955 $time->hide;
956 $interval->hide;
957 $count->hide;
958 } else {
959 $time->show;
960 $time->set_text ($self->{app}{defaults}{time});
961 if ($new eq TIMESYS_ABSOLUTE) {
962 $interval->hide;
963 $count->hide;
964 } else {
965 $interval->show;
966 $count->show;
967 if ($new eq TIMESYS_BYO_YOMI) {
968 $interval->set_text ($self->{app}{defaults}{byo_time});
969 $count->set_text ($self->{app}{defaults}{byo_period});
970 } elsif ($new eq TIMESYS_CANADIAN) {
971 $interval->set_text ($self->{app}{defaults}{can_time});
972 $count->set_text ($self->{app}{defaults}{can_period});
973 }
974 }
975 }
976 }
977 );
978
979 $inlay->append_text ("\nMain Time: ");
980 $time = $inlay->append_entry (\$info->{rules}{time}, 5);
981 $inlay->append_text ("\nInterval: ");
982 $interval = $inlay->append_entry (\$info->{rules}{interval}, 3);
983 $inlay->append_text ("\nPeriods/Stones: ");
984 $count = $inlay->append_entry (\$info->{rules}{count}, 2);
985
986 $inlay->append_text ("\n");
987
988 if (!$self->{channel}) {
989 $inlay->append_button ("Create Challenge", sub {
990 $inlay->clear;
991 $self->{cid} = $self->{conn}->alloc_clientid;
992 $self->send (new_game =>
993 channel => delete $self->{roomid},
994 gametype => $info->{gametype},
995 cid => $self->{cid},
996 flags => $info->{flags},
997 rules => $info->{rules},
998 notes => $info->{notes},
999 );
1000 });
1001 } else {
1002 $inlay->append_button ("OK", sub {
1003 $inlay->clear;
1004 $self->send (challenge =>
1005 channel => $self->{channel},
1006 black => $info->{black},
1007 white => $info->{white},
1008 gametype => $info->{gametype},
1009 cid => $info->{cid},
1010 rules => $info->{rules},
1011 );
1012 });
1013 if (exists $self->{challenge}{""}) {
1014 $inlay->append_button ("Reject", sub {
1015 $inlay->clear;
1016 $self->send (reject_challenge =>
1017 channel => $self->{channel},
1018 name => $opponent->{name},
1019 gametype => $info->{gametype},
1020 cid => $info->{cid},
1021 rules => $info->{rules},
1022 );
1023 });
1024 }
1025 }
1026 }
1027
1028 sub new_game_challenge {
1029 my ($self) = @_;
1030
1031 my $d = $self->{app}{defaults};
1032
1033 $self->{challenge}{""} = {
1034 gametype => $d->{gametype},
1035 flags => 0,
1036 notes => $d->{stones},
1037 rules => {
1038 ruleset => $d->{ruleset},
1039 size => $d->{size},
1040 timesys => $d->{timesys},
1041 time => $d->{time},
1042 interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time},
1043 count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones},
1044 },
1045
1046 inlay => $self->{chat}->new_inlay,
1047 };
1048 $self->draw_challenge ("");
1049 }
1050
1051 sub event_challenge {
1052 my ($self, $info) = @_;
1053
1054 my $as_black = $info->{black}->{name} eq $self->{conn}{name};
1055 my $opponent = $as_black ? $info->{white} : $info->{black};
1056
1057 my $id = $opponent->{name};
1058
1059 sound::play 2, "info";
1060
1061 $self->{challenge}{$id} = $info;
1062 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay (
1063 exists $self->{challenge}{""}
1064 ? "Challenge from " . $opponent->as_string
1065 : "Challenge to " . $opponent->as_string,
1066 sub {
1067 $self->{challenge}{$id}{inlay} = $_[0];
1068 $self->draw_challenge ($id);
1069 },
1070 !exists $self->{challenge}{""} # only open when not offerer
1071 );
1072 }
1073
1074 1;
1075