ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.91
Committed: Sun May 23 02:23:54 2004 UTC (20 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.90: +8 -13 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 use utf8;
2
3 use Scalar::Util ();
4
5 package game::goclock;
6
7 # Lo and Behold! I admit it! The rounding stuff etc.. in goclock
8 # is completely borked.
9
10 use Time::HiRes ();
11
12 use KGS::Constants;
13
14 use Glib::Object::Subclass
15 Gtk2::Label;
16
17 sub INIT_INSTANCE {
18 my $self = shift;
19
20 $self->signal_connect (destroy => sub { $_[0]->stop });
21
22 $self->{set} = sub { };
23 $self->{format} = sub { "???" };
24 }
25
26 sub configure {
27 my ($self, $timesys, $main, $interval, $count) = @_;
28
29 if ($timesys == TIMESYS_ABSOLUTE) {
30 $self->{set} = sub { $self->{time} = $_[0] };
31 $self->{format} = sub { util::format_time $_[0] };
32
33 } elsif ($timesys == TIMESYS_BYO_YOMI) {
34 my $low = $interval * $count;
35
36 $self->{set} = sub { $self->{time} = $_[0] };
37
38 $self->{format} = sub {
39 if ($_[0] > $low) {
40 util::format_time $_[0] - $low;
41 } else {
42 sprintf "%s (%d)",
43 util::format_time int (($_[0] - 1) % $interval + 1),
44 ($_[0] - 1) / $interval;
45 }
46 };
47
48 } elsif ($timesys == TIMESYS_CANADIAN) {
49 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
50
51 $self->{format} = sub {
52 if (!$self->{moves}) {
53 util::format_time $_[0] - $low;
54 } else {
55 my $time = int (($_[0] - 1) % $interval + 1);
56
57 sprintf "%s/%d =%d",
58 util::format_time $time,
59 $self->{moves},
60 $self->{moves} > 1
61 ? $time / $self->{moves}
62 : $interval;
63 }
64 };
65
66 } else {
67 # none, or unknown
68 $self->{set} = sub { };
69 $self->{format} = sub { "---" }
70 }
71 }
72
73 sub refresh {
74 my ($self, $timestamp) = @_;
75 my $timer = $self->{time} + $self->{start} - $timestamp;
76
77 # we round the timer value slightly... the protocol isn't exact anyways,
78 # and this gives smoother timers ;)
79 my @format = $self->{format}->(int ($timer + 0.4));
80 $self->set_text ($self->{format}->(int ($timer + 0.4)));
81
82 $timer - int $timer;
83 }
84
85 sub set_time {
86 my ($self, $time) = @_;
87
88 # we ignore requests to re-set the time of a running clock.
89 # this is the easiest way to ensure that commentary etc.
90 # doesn't re-set the clock. yes, this is frickle design,
91 # but I think the protocol is to blame here, which gives
92 # very little time information. (cgoban2 also has had quite
93 # a lot of small time update problems...)
94 unless ($self->{timeout}) {
95 $self->{set}->($time->[0], $time->[1]);
96 $self->refresh ($self->{start});
97 }
98 }
99
100 sub start {
101 my ($self, $when) = @_;
102
103 $self->stop;
104
105 $self->{start} = $when;
106
107 my $timeout; $timeout = sub {
108 my $next = $self->refresh (Time::HiRes::time) * 1000;
109 $next += 1000 if $next < 0;
110 $self->{timeout} = add Glib::Timeout $next, $timeout;
111 0;
112 };
113
114 $timeout->();
115 }
116
117 sub stop {
118 my ($self) = @_;
119
120 remove Glib::Source delete $self->{timeout} if $self->{timeout};
121 }
122
123 package game::userpanel;
124
125 use Glib::Object::Subclass
126 Gtk2::HBox,
127 properties => [
128 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
129 ];
130
131 sub INIT_INSTANCE {
132 my ($self) = @_;
133
134 $self->add (my $vbox = new Gtk2::VBox);
135
136 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
137 $vbox->add ($self->{info} = new Gtk2::Label "");
138 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
139
140 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
141
142 $self;
143 }
144
145 sub configure {
146 my ($self, $app, $user, $rules) = @_;
147
148 if ($self->{name}->get_text ne $user->as_string) {
149 $self->{name}->set_text ($user->as_string);
150
151 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
152 $self->{imagebox}->add (gtk::image_from_data undef);
153 $self->{imagebox}->show_all;
154
155 if ($user->has_pic) {
156 # the big picture...
157 $app->userpic ($user->{name}, sub {
158 return unless $self->{imagebox};
159
160 if ($_[0]) {
161 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
162 $self->{imagebox}->add (gtk::image_from_data $_[0]);
163 $self->{imagebox}->show_all;
164 }
165 });
166 }
167 }
168
169 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
170 }
171
172 sub set_state {
173 my ($self, $captures, $timer, $when) = @_;
174
175 $self->{clock}->stop unless $when;
176 $self->{clock}->set_time ($timer);
177 $self->{clock}->start ($when) if $when;
178
179 $self->{info}->set_text ("$captures pris.");
180 }
181
182 package game;
183
184 use Scalar::Util qw(weaken);
185
186 use KGS::Constants;
187 use KGS::Game::Board;
188
189 use Gtk2::GoBoard;
190
191 use Glib::Object::Subclass
192 Gtk2::Window;
193
194 use base KGS::Listener::Game;
195 use base KGS::Game;
196
197 use POSIX qw(ceil);
198
199 sub new {
200 my ($self, %arg) = @_;
201 $self = $self->Glib::Object::new;
202 $self->{$_} = delete $arg{$_} for keys %arg;
203
204 $self->listen ($self->{conn});
205
206 gtk::state $self, "game::window", undef, window_size => [600, 500];
207
208 $self->signal_connect (delete_event => sub { $self->part; 1 });
209 $self->signal_connect (destroy => sub {
210 $self->unlisten;
211 delete $self->{app}{game}{$self->{channel}};
212 %{$_[0]} = ();
213 });#d#
214
215 $self->add (my $hpane = new Gtk2::HPaned);
216 gtk::state $hpane, "game::hpane", undef, position => 500;
217
218 # LEFT PANE
219
220 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
221
222 $self->{boardbox} = new Gtk2::VBox;
223
224 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
225
226 # board box (aspect/canvas)
227
228 #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
229
230 # RIGHT PANE
231
232 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
233 $hpane->set (position_set => 1);
234
235 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
236
237 {
238 $frame->add (my $vbox = new Gtk2::VBox);
239 $vbox->add ($self->{title} = new Gtk2::Label $title);
240
241 $vbox->add (my $hbox = new Gtk2::HBox);
242
243 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0);
244
245 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
246
247 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
248 $scale->set_draw_value (0);
249 $scale->set_digits (0);
250
251 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
252 }
253
254 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
255
256 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
257
258 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
259 for COLOUR_WHITE, COLOUR_BLACK;
260
261 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
262
263 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Rules", sub { $self->draw_rules (@_) }, 1);
264 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 0);
265
266 $self->{chat}->signal_connect (command => sub {
267 my ($chat, $cmd, $arg) = @_;
268 if ($cmd eq "rsave") {
269 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
270 } else {
271 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
272 }
273 });
274
275 $self;
276 }
277
278 sub event_update_users {
279 my ($self, $add, $update, $remove) = @_;
280
281 # $self->{userlist}->update ($add, $update, $remove);
282
283 $self->{users_inlay}->refresh;
284
285 my %important;
286 $important{$self->{user1}{name}}++;
287 $important{$self->{user2}{name}}++;
288 $important{$self->{user3}{name}}++;
289
290 if (my @users = grep $important{$_->{name}}, @$add) {
291 $self->{chat}->append_text ("\n<header>Joins:</header>");
292 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
293 }
294 if (my @users = grep $important{$_->{name}}, @$remove) {
295 $self->{chat}->append_text ("\n<header>Parts:</header>");
296 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
297 }
298
299 }
300
301 sub join {
302 my ($self) = @_;
303 return if $self->{joined};
304
305 $self->SUPER::join;
306 }
307
308 sub update_board {
309 my ($self) = @_;
310 return unless $self->{path};
311
312 my $move = int $self->{moveadj}->get_value;
313
314 my $running = $move == @{$self->{path}};
315
316 $self->{board_label}->set_text ("Move " . ($move - 1));
317
318 $self->{cur_board} = new KGS::Game::Board $self->{size};
319 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
320
321 for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
322 $self->{userpanel}[$colour]->set_state (
323 $self->{cur_board}{captures}[$colour],
324 $self->{cur_board}{timer}[$colour],
325 ($running && $self->{lastmove_colour} == !$colour)
326 ? $self->{lastmove_time} : 0
327 );
328 }
329
330 $self->{board}->set_board ($self->{cur_board});
331 }
332
333 sub event_update_tree {
334 my ($self) = @_;
335
336 $self->{path} = $self->get_path;
337
338 if ($self->{moveadj}) {
339 my $upper = $self->{moveadj}->upper;
340 my $pos = $self->{moveadj}->get_value;
341 my $move = scalar @{$self->{path}};
342
343 $self->{moveadj}->upper ($move);
344
345 $self->{moveadj}->changed;
346 if ($pos == $upper) {
347 $self->{moveadj}->value ($move);
348 $self->{moveadj}->value_changed;
349 }
350 }
351 }
352
353 sub event_update_comments {
354 my ($self, $node, $comment, $newnode) = @_;
355 $self->SUPER::event_update_comments($node, $comment, $newnode);
356
357 my $text;
358
359 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
360 if $newnode;
361
362 for (split /\n/, $comment) {
363 $text .= "\n";
364 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
365 $text .= "<user>" . (util::toxml $1) . "</user>:";
366 }
367
368 # coords only for 19x19 so far
369 $_ = util::toxml $_;
370 s{
371 (
372 \b
373 (?:[bw])?
374 [, ]{0,2}
375 [a-hj-t] # valid for upto 19x19
376 \s?
377 [1-9]?[0-9]
378 \b
379 )
380 }{
381 "<coord>$1</coord>";
382 }sgexi;
383
384 $text .= $_;
385 }
386
387 $self->{chat}->append_text ($text);
388 }
389
390 sub event_join {
391 my ($self) = @_;
392
393 $self->SUPER::event_join (@_);
394 $self->event_update_game;
395 $self->show_all;
396 }
397
398 sub event_part {
399 my ($self) = @_;
400
401 $self->SUPER::event_part;
402 $self->destroy;
403 }
404
405 sub event_move {
406 my ($self, $pass) = @_;
407 sound::play 1, $pass ? "pass" : "move";
408 }
409
410 sub event_update_game {
411 my ($self) = @_;
412 $self->SUPER::event_update_game;
413
414 return unless $self->{joined};
415
416 my $title = defined $self->{channel}
417 ? $self->owner->as_string . " " . $self->opponent_string
418 : "Game Window";
419 $self->set_title("KGS Game $title");
420 $self->{title}->set_text ($title);
421
422 $self->{user}[COLOUR_BLACK] = $self->{user1};
423 $self->{user}[COLOUR_WHITE] = $self->{user2};
424
425 # show board
426 if ($self->is_inprogress) {
427 $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent;
428 if (my $ch = delete $self->{challenge}) {
429 (delete $_->{inlay})->clear for values %$ch;
430 }
431 }
432
433 $self->{left}->show_all;
434
435 # view text
436
437 eval { #d#
438 my @ga;
439 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
440 . " (" . (util::toxml $gameopt{$self->option}) . ")";
441 $ga[1] = "\nFlags:";
442 $ga[1] .= " started" if $self->is_inprogress;
443 $ga[1] .= " adjourned" if $self->is_adjourned;
444 $ga[1] .= " scored" if $self->is_scored;
445 $ga[1] .= " saved" if $self->is_saved;
446
447 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>"
448 if $self->{user3}->is_inprogress;
449
450 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
451 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>"
452 if $self->is_inprogress;
453
454 if ($self->is_inprogress) {
455 $ga[4] = "\nHandicap: " . $self->{handicap};
456 $ga[5] = "\nKomi: " . $self->{komi};
457 $ga[6] = "\nSize: " . $self->size_string;
458 }
459
460 if ($self->is_scored) {
461 $ga[7] = "\nResult: " . $self->score_string;
462 }
463
464 $text = "\n<infoblock><header>Game Update</header>";
465 for (0..7) {
466 if ($self->{gatext}[$_] ne $ga[$_]) {
467 $text .= $ga[$_];
468 }
469 }
470 $text .= "</infoblock>";
471
472 $self->{gatext} = \@ga;
473 };
474
475 $self->{chat}->append_text ($text);
476 }
477
478 sub draw_rules {
479 my ($self, $inlay) = @_;
480
481 my $rules = $self->{rules};
482
483 my $text = "";
484
485 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
486
487 $text .= "\nTime: ";
488
489 if ($rules->{timesys} == TIMESYS_NONE) {
490 $text .= "UNLIMITED";
491 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
492 $text .= util::format_time $rules->{time};
493 $text .= " ABS";
494 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
495 $text .= util::format_time $rules->{time};
496 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
497 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
498 $text .= util::format_time $rules->{time};
499 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
500 }
501
502 $inlay->append_text ("<infoblock>$text</infoblock>");
503 }
504
505 sub event_update_rules {
506 my ($self, $rules) = @_;
507
508 $self->{rules} = $rules;
509
510 if ($self->{user}) {
511 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
512 for COLOUR_BLACK, COLOUR_WHITE;
513 }
514
515 sound::play 3, "gamestart";
516
517 $self->{rules_inlay}->refresh;
518 }
519
520 sub inject_resign_game {
521 my ($self, $msg) = @_;
522
523 sound::play 3, "resign";
524
525 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
526 . "\n<user>"
527 . (util::toxml $self->{user}[$msg->{player}]->as_string)
528 . "</user> resigned.</infoblock>");
529 }
530
531 sub inject_final_result {
532 my ($self, $msg) = @_;
533
534 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
535 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
536 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
537 . "</infoblock>"
538 );
539 }
540
541 sub draw_challenge {
542 my ($self, $c) = @_;
543
544 my $inlay = $c->{inlay};
545 my $challenge = $c->{challenge};
546 my $rules = $challenge->{rules};
547
548 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
549 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
550
551 $inlay->append_text ("\n<challenge>Challenge to <user>" . $opponent->as_string . "</user></challenge>");
552 $inlay->append_text ("\nHandicap: $rules->{handicap}");
553
554 #bless( (
555 # gametype => 3,
556 # user1 => bless( {
557 # flags => 2633,
558 # name => 'dorkusx'
559 # }, 'KGS::User' ),
560 # rules => bless( {
561 # count => 5,
562 # time => 900,
563 # timesys => 2,
564 # interval => 30,
565 # komi => '6.5',
566 # size => 19,
567 # ruleset => 0,
568 # handicap => 0
569 # }, 'KGS::Rules' ),
570 # user2 => bless( {
571 # flags => 436220808,
572 # name => 'Nerdamus'
573 # }, 'KGS::User' )
574 # ), 'KGS::Challenge' )
575 }
576
577 sub draw_users {
578 my ($self, $inlay) = @_;
579
580 for (sort keys %{$self->{users}}) {
581 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
582 }
583 }
584
585 sub event_challenge {
586 my ($self, $challenge) = @_;
587
588 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
589 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
590
591 my $c = $self->{challenge}{$opponent->{name}} ||= {};
592
593 $c->{inlay} ||= $self->{chat}->new_inlay;
594 $c->{challenge} = $challenge;
595
596 $self->draw_challenge ($c);
597
598 # require KGS::Listener::Debug;
599 # $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge));
600 }
601
602 1;
603