ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.85
Committed: Thu May 20 23:17:25 2004 UTC (20 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.84: +0 -1 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 { "ERROR" };
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 { %{$_[0]} = () });
210
211 $self->add (my $hpane = new Gtk2::HPaned);
212 gtk::state $hpane, "game::hpane", undef, position => 500;
213
214 # LEFT PANE
215
216 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
217
218 $self->{boardbox} = new Gtk2::VBox;
219
220 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
221
222 # challenge
223
224 $self->{challenge} = new challenge channel => $self->{channel};
225
226 # board box (aspect/canvas)
227
228 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
229
230 {
231 $frame->add (my $vbox = new Gtk2::VBox);
232 $vbox->add ($self->{title} = new Gtk2::Label $title);
233
234 $vbox->add (my $hbox = new Gtk2::HBox);
235
236 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0);
237
238 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
239
240 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
241 $scale->set_draw_value (0);
242 $scale->set_digits (0);
243
244 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
245 }
246
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
258 $sw->add ($self->{userlist} = new userlist);
259
260 $self->{vpane}->add (my $vbox = new Gtk2::VBox);
261
262 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
263
264 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
265 for COLOUR_WHITE, COLOUR_BLACK;
266
267 $vbox->pack_start (($self->{chat} = new chat), 1, 1, 0);
268 weaken $self->{chat};
269
270 $self->{chat}->signal_connect (command => sub {
271 my ($chat, $cmd, $arg) = @_;
272 if ($cmd eq "rsave") {
273 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
274 } else {
275 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
276 }
277 });
278
279 $self;
280 }
281
282 sub event_update_users {
283 my ($self, $add, $update, $remove) = @_;
284
285 return unless $self->{userlist};
286
287 $self->{userlist}->update ($add, $update, $remove);
288
289 my %important;
290 $important{$self->{user1}{name}}++;
291 $important{$self->{user2}{name}}++;
292 $important{$self->{user3}{name}}++;
293
294 if (my @users = grep $important{$_->{name}}, @$add) {
295 $self->{chat}->append_text ("\n<header>Joins:</header>");
296 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
297 }
298 if (my @users = grep $important{$_->{name}}, @$remove) {
299 $self->{chat}->append_text ("\n<header>Parts:</header>");
300 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
301 }
302
303 }
304
305 sub join {
306 my ($self) = @_;
307 return if $self->{joined};
308
309 $self->SUPER::join;
310
311 $self->show_all;
312 }
313
314 sub update_board {
315 my ($self) = @_;
316 return unless $self->{path};
317
318 my $move = int $self->{moveadj}->get_value;
319
320 my $running = $move == @{$self->{path}};
321
322 $self->{board_label}->set_text ("Move " . ($move - 1));
323
324 $self->{cur_board} = new KGS::Game::Board $self->{size};
325 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
326
327 for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
328 $self->{userpanel}[$colour]->set_state (
329 $self->{cur_board}{captures}[$colour],
330 $self->{cur_board}{timer}[$colour],
331 ($running && $self->{lastmove_colour} == !$colour)
332 ? $self->{lastmove_time} : 0
333 );
334 }
335
336 $self->{board}->set_board ($self->{cur_board});
337 }
338
339 sub event_update_tree {
340 my ($self) = @_;
341
342 $self->{path} = $self->get_path;
343
344 if ($self->{moveadj}) {
345 my $upper = $self->{moveadj}->upper;
346 my $pos = $self->{moveadj}->get_value;
347 my $move = scalar @{$self->{path}};
348
349 $self->{moveadj}->upper ($move);
350
351 $self->{moveadj}->changed;
352 if ($pos == $upper) {
353 $self->{moveadj}->value ($move);
354 $self->{moveadj}->value_changed;
355 }
356 }
357 }
358
359 sub event_update_comments {
360 my ($self, $node, $comment, $newnode) = @_;
361 $self->SUPER::event_update_comments($node, $comment, $newnode);
362
363 my $text;
364
365 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
366 if $newnode;
367
368 for (split /\n/, $comment) {
369 $text .= "\n";
370 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
371 $text .= "<user>" . (util::toxml $1) . "</user>:";
372 }
373
374 # coords only for 19x19 so far
375 $_ = util::toxml $_;
376 s{
377 (
378 \b
379 (?:[bw])?
380 [, ]{0,2}
381 [a-hj-t] # valid for upto 19x19
382 \s?
383 [1-9]?[0-9]
384 \b
385 )
386 }{
387 "<coord>$1</coord>";
388 }sgexi;
389
390 $text .= $_;
391 }
392
393 $self->{chat}->append_text ($text);
394 }
395
396 sub event_join {
397 my ($self) = @_;
398
399 $self->SUPER::event_join (@_);
400 $self->event_update_game;
401 }
402
403 sub event_part {
404 my ($self) = @_;
405
406 $self->SUPER::event_part;
407 $self->destroy;
408 }
409
410 sub event_move {
411 my ($self, $pass) = @_;
412 sound::play 1, $pass ? "pass" : "move";
413 }
414
415 sub event_update_game {
416 my ($self) = @_;
417 $self->SUPER::event_update_game;
418
419 return unless $self->{joined};
420
421 my $title = defined $self->{channel}
422 ? $self->owner->as_string . " " . $self->opponent_string
423 : "Game Window";
424 $self->set_title("KGS Game $title");
425 $self->{title}->set_text ($title);
426
427 $self->{user}[COLOUR_BLACK] = $self->{user1};
428 $self->{user}[COLOUR_WHITE] = $self->{user2};
429
430 # show board
431
432 if ($self->is_inprogress) {
433 $self->{left}->remove ($self->{challenge}->widget) if $self->{challenge} && $self->{boardbox}->parent;
434 $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent;
435 } else {
436 $self->{left}->remove ($self->{boardbox}) if $self->{boardbox}->parent;
437 $self->{left}->add ($self->{challenge}->widget) unless $self->{challenge}->widget->parent;
438 }
439 $self->{left}->show_all;
440
441 # view text
442
443 eval { #d#
444 my @ga;
445 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
446 . " (" . (util::toxml $gameopt{$self->option}) . ")";
447 $ga[1] = "\nFlags:";
448 $ga[1] .= " started" if $self->is_inprogress;
449 $ga[1] .= " adjourned" if $self->is_adjourned;
450 $ga[1] .= " scored" if $self->is_scored;
451 $ga[1] .= " saved" if $self->is_saved;
452
453 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>"
454 if $self->{user3}->is_inprogress;
455
456 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
457 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>"
458 if $self->is_inprogress;
459
460 if ($self->is_inprogress) {
461 $ga[4] = "\nHandicap: " . $self->{handicap};
462 $ga[5] = "\nKomi: " . $self->{komi};
463 $ga[6] = "\nSize: " . $self->size_string;
464 }
465
466 if ($self->is_scored) {
467 $ga[7] = "\nResult: " . $self->score_string;
468 }
469
470 $text = "\n<infoblock><header>Game Update</header>";
471 for (0..7) {
472 if ($self->{gatext}[$_] ne $ga[$_]) {
473 $text .= $ga[$_];
474 }
475 }
476 $text .= "</infoblock>";
477
478 $self->{gatext} = \@ga;
479 };
480
481 $self->{chat}->append_text ($text);
482 }
483
484 sub event_update_rules {
485 my ($self, $rules) = @_;
486
487 if ($self->{user}) {
488 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
489 for COLOUR_BLACK, COLOUR_WHITE;
490 }
491
492 sound::play 3, "gamestart";
493
494 my $text = "\n<header>Game Rules</header>";
495
496 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
497
498 $text .= "\nTime: ";
499
500 if ($rules->{timesys} == TIMESYS_NONE) {
501 $text .= "UNLIMITED";
502 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
503 $text .= util::format_time $rules->{time};
504 $text .= " ABS";
505 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
506 $text .= util::format_time $rules->{time};
507 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
508 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
509 $text .= util::format_time $rules->{time};
510 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
511 }
512
513 $self->{chat}->append_text ("<infoblock>$text</infoblock>");
514 }
515
516 sub inject_resign_game {
517 my ($self, $msg) = @_;
518
519 sound::play 3, "resign";
520
521 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
522 . "\n<user>"
523 . (util::toxml $self->{user}[$msg->{player}]->as_string)
524 . "</user> resigned.</infoblock>");
525 }
526
527 sub inject_final_result {
528 my ($self, $msg) = @_;
529
530 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
531 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
532 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
533 . "</infoblock>"
534 );
535 }
536
537 sub event_challenge {
538 my ($self, $challenge) = @_;
539
540 use KGS::Listener::Debug;
541 $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge));
542 }
543
544 1;
545