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