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