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