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 FINALIZE_INSTANCE { |
27 |
my $self = shift; |
28 |
|
29 |
$self->stop; |
30 |
} |
31 |
|
32 |
sub configure { |
33 |
my ($self, $timesys, $main, $interval, $count) = @_; |
34 |
|
35 |
if ($timesys == TIMESYS_ABSOLUTE) { |
36 |
$self->{format} = sub { |
37 |
if ($_[0] <= 0) { |
38 |
"TIMEOUT"; |
39 |
} else { |
40 |
util::format_time $_[0]; |
41 |
} |
42 |
}; |
43 |
|
44 |
} elsif ($timesys == TIMESYS_BYO_YOMI) { |
45 |
my $low = $interval * $count; |
46 |
|
47 |
$self->{format} = sub { |
48 |
if ($_[0] <= 0) { |
49 |
"TIMEOUT"; |
50 |
} elsif ($_[0] > $low) { |
51 |
util::format_time $_[0] - $low; |
52 |
} else { |
53 |
sprintf "%s (%d)", |
54 |
util::format_time int (($_[0] - 1) % $interval + 1), |
55 |
($_[0] - 1) / $interval; |
56 |
} |
57 |
}; |
58 |
|
59 |
} elsif ($timesys == TIMESYS_CANADIAN) { |
60 |
$self->{format} = sub { |
61 |
if ($_[0] <= 0) { |
62 |
"TIMEOUT"; |
63 |
} elsif (!$self->{moves}) { |
64 |
util::format_time $_[0] - $low; |
65 |
} else { |
66 |
my $time = int (($_[0] - 1) % $interval + 1); |
67 |
|
68 |
sprintf "%s/%d =%d", |
69 |
util::format_time $time, |
70 |
$self->{moves}, |
71 |
$self->{moves} > 1 |
72 |
? $time / $self->{moves} |
73 |
: $interval; |
74 |
} |
75 |
}; |
76 |
|
77 |
} else { |
78 |
# none, or unknown |
79 |
$self->{format} = sub { "-" } |
80 |
} |
81 |
} |
82 |
|
83 |
sub refresh { |
84 |
my ($self, $timestamp) = @_; |
85 |
my $timer = $self->{time} + $self->{start} - $timestamp; |
86 |
|
87 |
# we round the timer value slightly... the protocol isn't exact anyways, |
88 |
# and this gives smoother timers ;) |
89 |
my $timer2 = int $timer + 0.4; |
90 |
|
91 |
$self->set_text ($self->{format}->($timer2)); |
92 |
|
93 |
$timer - int $timer; |
94 |
} |
95 |
|
96 |
sub set_time { |
97 |
my ($self, $start, $time, $moves) = @_; |
98 |
|
99 |
$self->{time} = $time; |
100 |
$self->{moves} = $moves; |
101 |
|
102 |
if ($start) { |
103 |
$self->{start} = $start; |
104 |
$self->start; |
105 |
} else { |
106 |
$self->stop; |
107 |
$self->refresh ($self->{start}); |
108 |
} |
109 |
} |
110 |
|
111 |
sub start { |
112 |
my ($self) = @_; |
113 |
|
114 |
$self->stop; |
115 |
|
116 |
my $timeout; $timeout = sub { |
117 |
my $next = $self->refresh (Time::HiRes::time) * 1000; |
118 |
$next += 1000 if $next < 0; |
119 |
$self->{timeout} = add Glib::Timeout $next, $timeout; |
120 |
0; |
121 |
}; |
122 |
|
123 |
$timeout->(); |
124 |
} |
125 |
|
126 |
sub stop { |
127 |
my ($self) = @_; |
128 |
|
129 |
remove Glib::Source delete $self->{timeout} if $self->{timeout}; |
130 |
} |
131 |
|
132 |
package game::userpanel; |
133 |
|
134 |
use Glib::Object::Subclass |
135 |
Gtk2::HBox, |
136 |
properties => [ |
137 |
Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]), |
138 |
]; |
139 |
|
140 |
sub INIT_INSTANCE { |
141 |
my ($self) = @_; |
142 |
|
143 |
$self->add (my $vbox = new Gtk2::VBox); |
144 |
|
145 |
$vbox->add ($self->{name} = new Gtk2::Label $self->{name}); |
146 |
$vbox->add ($self->{info} = new Gtk2::Label ""); |
147 |
$vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock}; |
148 |
|
149 |
$vbox->add ($self->{imagebox} = new Gtk2::VBox); |
150 |
|
151 |
$self; |
152 |
} |
153 |
|
154 |
sub configure { |
155 |
my ($self, $app, $user, $rules) = @_; |
156 |
|
157 |
if ($self->{name}->get_text ne $user->as_string) { |
158 |
$self->{name}->set_text ($user->as_string); |
159 |
|
160 |
$self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
161 |
$self->{imagebox}->add (gtk::image_from_data undef); |
162 |
$self->{imagebox}->show_all; |
163 |
|
164 |
if ($user->has_pic) { |
165 |
# the big picture... |
166 |
$app->userpic ($user->{name}, sub { |
167 |
return unless $self->{imagebox}; |
168 |
|
169 |
if ($_[0]) { |
170 |
$self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
171 |
$self->{imagebox}->add (gtk::image_from_data $_[0]); |
172 |
$self->{imagebox}->show_all; |
173 |
} |
174 |
}); |
175 |
} |
176 |
} |
177 |
|
178 |
$self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); |
179 |
} |
180 |
|
181 |
sub set_captures { |
182 |
my ($self, $captures) = @_; |
183 |
|
184 |
$self->{info}->set_text ("$captures pris."); |
185 |
} |
186 |
|
187 |
sub set_timer { |
188 |
my ($self, $start, $time, $moves) = @_; |
189 |
|
190 |
$self->{clock}->set_time ($start, $time, $moves); |
191 |
} |
192 |
|
193 |
package game; |
194 |
|
195 |
use Scalar::Util qw(weaken); |
196 |
|
197 |
use KGS::Constants; |
198 |
use KGS::Game::Board; |
199 |
|
200 |
use Gtk2::GoBoard; |
201 |
use Gtk2::GoBoard::Constants; |
202 |
|
203 |
use base KGS::Game; |
204 |
use base KGS::Listener::Game; |
205 |
|
206 |
use Glib::Object::Subclass |
207 |
Gtk2::Window; |
208 |
|
209 |
use POSIX qw(ceil); |
210 |
|
211 |
sub new { |
212 |
my ($self, %arg) = @_; |
213 |
$self = $self->Glib::Object::new; |
214 |
$self->{$_} = delete $arg{$_} for keys %arg; |
215 |
|
216 |
gtk::state $self, "game::window", undef, window_size => [600, 500]; |
217 |
|
218 |
$self->signal_connect (destroy => sub { |
219 |
$self->unlisten; |
220 |
delete $self->{app}{game}{$self->{channel}}; |
221 |
%{$_[0]} = (); |
222 |
});#d# |
223 |
|
224 |
$self->add (my $hpane = new Gtk2::HPaned); |
225 |
gtk::state $hpane, "game::hpane", undef, position => 500; |
226 |
|
227 |
# LEFT PANE |
228 |
|
229 |
$hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0); |
230 |
|
231 |
$self->{boardbox} = new Gtk2::VBox; |
232 |
|
233 |
$hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
234 |
|
235 |
# board box (aspect/canvas) |
236 |
|
237 |
#$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); |
238 |
|
239 |
# RIGHT PANE |
240 |
|
241 |
$hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1); |
242 |
$hpane->set (position_set => 1); |
243 |
|
244 |
$vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0); |
245 |
|
246 |
{ |
247 |
$frame->add (my $vbox = new Gtk2::VBox); |
248 |
$vbox->add ($self->{title} = new Gtk2::Label $title); |
249 |
|
250 |
$vbox->add (my $hbox = new Gtk2::HBox); |
251 |
|
252 |
$hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0); |
253 |
|
254 |
$self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0; |
255 |
|
256 |
$hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); |
257 |
$scale->set_draw_value (0); |
258 |
$scale->set_digits (0); |
259 |
|
260 |
$self->{moveadj}->signal_connect (value_changed => sub { |
261 |
$self->{showmove} = int $self->{moveadj}->get_value; |
262 |
$self->update_board; |
263 |
}); |
264 |
} |
265 |
|
266 |
$vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
267 |
|
268 |
$hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) |
269 |
for COLOUR_WHITE, COLOUR_BLACK; |
270 |
|
271 |
$vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
272 |
|
273 |
$buttonbox->add ($self->{button_pass} = |
274 |
Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0)); |
275 |
$self->{button_pass}->signal_connect (clicked => sub { |
276 |
$self->{board_click}->(255, 255) if $self->{board_click}; |
277 |
}); |
278 |
$buttonbox->add ($self->{button_undo} = |
279 |
Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0)); |
280 |
$self->{button_undo}->signal_connect (clicked => sub { |
281 |
$self->send (req_undo => channel => $self->{channel}); |
282 |
}); |
283 |
$buttonbox->add ($self->{button_resign} = |
284 |
Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0)); |
285 |
$self->{button_resign}->signal_connect (clicked => sub { |
286 |
$self->send (resign_game => channel => $self->{channel}, player => $self->{colour}); |
287 |
}); |
288 |
|
289 |
$vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); |
290 |
|
291 |
$self->set_channel ($self->{channel}); |
292 |
|
293 |
$self->show_all; |
294 |
|
295 |
$self; |
296 |
} |
297 |
|
298 |
sub set_channel { |
299 |
my ($self, $channel) = @_; |
300 |
|
301 |
$self->{channel} = $channel; |
302 |
|
303 |
if ($self->{channel} > 0) { |
304 |
$self->listen ($self->{conn}); |
305 |
|
306 |
$self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1); |
307 |
$self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1); |
308 |
|
309 |
$self->signal_connect (delete_event => sub { $self->part; 1 }); |
310 |
$self->{chat}->signal_connect (command => sub { |
311 |
my ($chat, $cmd, $arg) = @_; |
312 |
if ($cmd eq "rsave") { |
313 |
Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d# |
314 |
} else { |
315 |
$self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self); |
316 |
} |
317 |
}); |
318 |
} |
319 |
} |
320 |
|
321 |
sub event_update_users { |
322 |
my ($self, $add, $update, $remove) = @_; |
323 |
|
324 |
# $self->{userlist}->update ($add, $update, $remove); |
325 |
|
326 |
$self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy |
327 |
for @$remove; |
328 |
|
329 |
$self->{users_inlay}->refresh; |
330 |
|
331 |
my %important; |
332 |
$important{$self->{black}{name}}++; |
333 |
$important{$self->{white}{name}}++; |
334 |
$important{$self->{owner}{name}}++; |
335 |
|
336 |
if (my @users = grep $important{$_->{name}}, @$add) { |
337 |
$self->{chat}->append_text ("\n<header>Joins:</header>"); |
338 |
$self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
339 |
} |
340 |
if (my @users = grep $important{$_->{name}}, @$remove) { |
341 |
$self->{chat}->append_text ("\n<header>Parts:</header>"); |
342 |
$self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
343 |
} |
344 |
} |
345 |
|
346 |
sub join { |
347 |
my ($self) = @_; |
348 |
return if $self->{joined}; |
349 |
|
350 |
$self->SUPER::join; |
351 |
} |
352 |
|
353 |
sub update_cursor { |
354 |
my ($self) = @_; |
355 |
|
356 |
my $running = $self->{showmove} == @{$self->{path}} && $self->is_active; |
357 |
|
358 |
delete $self->{board_click}; |
359 |
|
360 |
if ($self->{teacher} eq $self->{app}{conn}) { |
361 |
#TODO# # teaching mode not implemented |
362 |
$self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); |
363 |
$self->{button_undo}->hide; |
364 |
$self->{button_resign}->hide; |
365 |
$self->{board}->set (cursor => undef); |
366 |
|
367 |
} elsif ($running && $self->{colour} != COLOUR_NONE) { |
368 |
# during game |
369 |
$self->{button_undo}->show; |
370 |
$self->{button_resign}->show; |
371 |
|
372 |
if ($self->{cur_board}{score}) { |
373 |
# during scoring |
374 |
$self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1); |
375 |
$self->{board}->set (cursor => sub { |
376 |
$_[0] & (MARK_B | MARK_W) |
377 |
? $_[0] ^ MARK_GRAYED |
378 |
: $_[0]; |
379 |
}); |
380 |
$self->{board_click} = sub { |
381 |
if ($_[0] == 255) { |
382 |
$self->{button_pass}->sensitive (0); |
383 |
warn sprintf "SEND DONE @{$self->{done}} %x\n", $self->{doneid};#d# |
384 |
$self->done; |
385 |
} else { |
386 |
$self->send (mark_dead => |
387 |
channel => $self->{channel}, |
388 |
x => $_[0], |
389 |
y => $_[1], |
390 |
dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED), |
391 |
); |
392 |
} |
393 |
}; |
394 |
|
395 |
} elsif ($self->{colour} == $self->{whosemove}) { |
396 |
# normal move |
397 |
$self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); |
398 |
$self->{board}->set (cursor => sub { |
399 |
# if is_valid_move oder so#TODO# |
400 |
$_[0] & (MARK_B | MARK_W) |
401 |
? $_[0] |
402 |
: $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B); |
403 |
}); |
404 |
$self->{board_click} = sub { |
405 |
$self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); |
406 |
$self->{board}->set (cursor => undef); |
407 |
delete $self->{board_click}; |
408 |
$self->{button_pass}->sensitive (0); |
409 |
}; |
410 |
} else { |
411 |
$self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1); |
412 |
$self->{board}->set (cursor => undef); |
413 |
} |
414 |
} else { |
415 |
$self->{button_undo}->hide; |
416 |
$self->{button_resign}->hide; |
417 |
$self->{button_pass}->hide; |
418 |
$self->{board}->set (cursor => undef); |
419 |
#TODO# # implement coordinate-grabbing |
420 |
} |
421 |
} |
422 |
|
423 |
sub update_timers { |
424 |
my ($self, $timers) = @_; |
425 |
|
426 |
my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher}; |
427 |
|
428 |
for my $colour (COLOUR_BLACK, COLOUR_WHITE) { |
429 |
my $t = $timers->[$colour]; |
430 |
$self->{userpanel}[$colour]->set_timer ( |
431 |
$running && $colour == $self->{whosemove} && $t->[0], |
432 |
$t->[1] || $self->{rules}{time} |
433 |
+ ($self->{rules}{timesys} == TIMESYS_BYO_YOMI |
434 |
&& $self->{rules}{interval} * $self->{rules}{count}), |
435 |
$t->[2] || $self->{rules}{count}); |
436 |
} |
437 |
} |
438 |
|
439 |
sub update_board { |
440 |
my ($self) = @_; |
441 |
return unless $self->{path}; |
442 |
|
443 |
$self->{board_label}->set_text ("Move " . ($self->{showmove} - 1)); |
444 |
|
445 |
$self->{cur_board} = new KGS::Game::Board $self->{size}; |
446 |
$self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]); |
447 |
|
448 |
$self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) |
449 |
for COLOUR_WHITE, COLOUR_BLACK; |
450 |
|
451 |
if ($self->{rules}{ruleset} == RULESET_JAPANESE) { |
452 |
if ($self->{curnode}{move} == 0) { |
453 |
$self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK; |
454 |
} else { |
455 |
$self->{whosemove} = 1 - $self->{cur_board}{last}; |
456 |
} |
457 |
} else { |
458 |
# Chinese, Aga, NZ all have manual placement |
459 |
if ($self->{curnode}{move} < $self->{handicap}) { |
460 |
$self->{whosemove} = COLOUR_BLACK; |
461 |
} elsif ($self->{curnode}{move} == $self->{handicap}) { |
462 |
$self->{whosemove} = COLOUR_WHITE; |
463 |
} else { |
464 |
$self->{whosemove} = 1 - $self->{cur_board}{last}; |
465 |
} |
466 |
} |
467 |
|
468 |
my $start_time = $self->{rules}{time}; |
469 |
|
470 |
if ($self->{showmove} == @{$self->{path}}) { |
471 |
$self->{timers} = [ |
472 |
[$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}], |
473 |
[$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}], |
474 |
]; |
475 |
$self->update_timers ($self->{timers}); |
476 |
} else { |
477 |
$self->update_timers ([ |
478 |
[0, @{$self->{cur_board}{timer}[0]}], |
479 |
[0, @{$self->{cur_board}{timer}[1]}], |
480 |
]); |
481 |
} |
482 |
|
483 |
$self->{board}->set_board ($self->{cur_board}); |
484 |
|
485 |
if ($self->{cur_board}{score}) { |
486 |
$self->{score_inlay} ||= $self->{chat}->new_inlay; |
487 |
$self->{score_inlay}->clear; |
488 |
$self->{score_inlay}->append_text ("\n<header>Scoring</header>" |
489 |
. "\n<score>" |
490 |
. "White: $self->{cur_board}{score}[COLOUR_WHITE], " |
491 |
. "Black: $self->{cur_board}{score}[COLOUR_BLACK]" |
492 |
. "</score>"); |
493 |
} elsif ($self->{score_inlay}) { |
494 |
(delete $self->{score_inlay})->clear; |
495 |
} |
496 |
|
497 |
$self->update_cursor; |
498 |
} |
499 |
|
500 |
sub event_update_tree { |
501 |
my ($self) = @_; |
502 |
|
503 |
(delete $self->{undo_inlay})->clear |
504 |
if $self->{undo_inlay}; |
505 |
|
506 |
$self->{path} = $self->get_path; |
507 |
|
508 |
if ($self->{moveadj}) { |
509 |
my $upper = $self->{moveadj}->upper; |
510 |
my $pos = $self->{moveadj}->get_value; |
511 |
my $move = scalar @{$self->{path}}; |
512 |
|
513 |
$self->{moveadj}->upper ($move); |
514 |
|
515 |
$self->{moveadj}->changed; |
516 |
if ($pos == $upper) { |
517 |
$self->{moveadj}->value ($move); |
518 |
$self->{moveadj}->value_changed; |
519 |
} |
520 |
} |
521 |
} |
522 |
|
523 |
sub event_update_comments { |
524 |
my ($self, $node, $comment, $newnode) = @_; |
525 |
$self->SUPER::event_update_comments($node, $comment, $newnode); |
526 |
|
527 |
my $text; |
528 |
|
529 |
$text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>" |
530 |
if $newnode; |
531 |
|
532 |
for (split /\n/, $comment) { |
533 |
$text .= "\n"; |
534 |
if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) { |
535 |
$text .= "<user>" . (util::toxml $1) . "</user>:"; |
536 |
} |
537 |
|
538 |
# coords only for 19x19 so far |
539 |
$_ = util::toxml $_; |
540 |
s{ |
541 |
( |
542 |
\b |
543 |
(?:[bw])? |
544 |
[, ]{0,2} |
545 |
[a-hj-t] # valid for upto 19x19 |
546 |
\s? |
547 |
[1-9]?[0-9] |
548 |
\b |
549 |
) |
550 |
}{ |
551 |
"<coord>$1</coord>"; |
552 |
}sgexi; |
553 |
|
554 |
$text .= $_; |
555 |
} |
556 |
|
557 |
$self->{chat}->append_text ($text); |
558 |
} |
559 |
|
560 |
sub event_join { |
561 |
my ($self) = @_; |
562 |
|
563 |
$self->SUPER::event_join (@_); |
564 |
$self->init_tree; |
565 |
$self->event_update_game; |
566 |
} |
567 |
|
568 |
sub event_part { |
569 |
my ($self) = @_; |
570 |
|
571 |
$self->SUPER::event_part; |
572 |
$self->destroy; |
573 |
} |
574 |
|
575 |
sub event_move { |
576 |
my ($self, $pass) = @_; |
577 |
|
578 |
sound::play 1, $pass ? "pass" : "move"; |
579 |
} |
580 |
|
581 |
sub event_update_game { |
582 |
my ($self) = @_; |
583 |
|
584 |
$self->SUPER::event_update_game; |
585 |
|
586 |
return unless $self->{joined}; |
587 |
|
588 |
$self->{colour} = $self->player_colour ($self->{conn}{name}); |
589 |
|
590 |
my $title = defined $self->{channel} |
591 |
? $self->owner->as_string . " " . $self->opponent_string |
592 |
: "Game Window"; |
593 |
$self->set_title ("KGS Game $title"); |
594 |
$self->{title}->set_text ($title); |
595 |
|
596 |
$self->{user}[COLOUR_BLACK] = $self->{black}; |
597 |
$self->{user}[COLOUR_WHITE] = $self->{white}; |
598 |
|
599 |
# show board |
600 |
if ($self->is_inprogress) { |
601 |
if (!$self->{boardbox}->parent) { |
602 |
$self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size}); |
603 |
$self->{left}->add ($self->{boardbox}); |
604 |
$self->{board}->signal_connect (button_release => sub { |
605 |
if ($_[1] == 1) { |
606 |
$self->{board_click}->($_[2], $_[3]) if $self->{board_click}; |
607 |
} |
608 |
}); |
609 |
} |
610 |
if (my $ch = delete $self->{challenge}) { |
611 |
$_->{inlay}->destroy for values %$ch; |
612 |
} |
613 |
$self->update_cursor; |
614 |
} |
615 |
|
616 |
$self->{left}->show_all; |
617 |
|
618 |
$self->{rules_inlay}->refresh; |
619 |
|
620 |
} |
621 |
|
622 |
sub draw_setup { |
623 |
my ($self, $inlay) = @_; |
624 |
|
625 |
return unless $self->{joined}; |
626 |
|
627 |
my $rules = $self->{rules}; |
628 |
|
629 |
my $text = ""; |
630 |
|
631 |
$text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>" |
632 |
if $self->{teacher}; |
633 |
|
634 |
$text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>" |
635 |
if $self->{owner}->is_valid; |
636 |
|
637 |
if ($self->is_inprogress) { |
638 |
$text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>" |
639 |
. " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>"; |
640 |
} |
641 |
$text .= "\nType: " . util::toxml $gametype{$self->type}; |
642 |
|
643 |
$text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; |
644 |
|
645 |
$text .= "\nTime: "; |
646 |
|
647 |
if ($rules->{timesys} == TIMESYS_NONE) { |
648 |
$text .= "UNLIMITED"; |
649 |
} elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { |
650 |
$text .= util::format_time $rules->{time}; |
651 |
$text .= " ABS"; |
652 |
} elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { |
653 |
$text .= util::format_time $rules->{time}; |
654 |
$text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; |
655 |
} elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
656 |
$text .= util::format_time $rules->{time}; |
657 |
$text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
658 |
} |
659 |
|
660 |
$text .= "\nFlags:"; |
661 |
$text .= " private" if $self->is_private; |
662 |
$text .= " started" if $self->is_inprogress; |
663 |
$text .= " adjourned" if $self->is_adjourned; |
664 |
$text .= " scored" if $self->is_scored; |
665 |
$text .= " saved" if $self->is_saved; |
666 |
|
667 |
if ($self->is_inprogress) { |
668 |
$text .= "\nHandicap: " . $self->{handicap}; |
669 |
$text .= "\nKomi: " . $self->{komi}; |
670 |
$text .= "\nSize: " . $self->size_string; |
671 |
} |
672 |
|
673 |
if ($self->is_scored) { |
674 |
$text .= "\nResult: " . $self->score_string; |
675 |
} |
676 |
|
677 |
$inlay->append_text ("<infoblock>$text</infoblock>"); |
678 |
|
679 |
} |
680 |
|
681 |
sub event_update_rules { |
682 |
my ($self, $rules) = @_; |
683 |
|
684 |
$self->{rules} = $rules; |
685 |
|
686 |
if ($self->{user}) { |
687 |
$self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules) |
688 |
for COLOUR_BLACK, COLOUR_WHITE; |
689 |
} |
690 |
|
691 |
sound::play 3, "gamestart"; |
692 |
$self->{rules_inlay}->refresh; |
693 |
} |
694 |
|
695 |
sub event_resign_game { |
696 |
my ($self, $player) = @_; |
697 |
|
698 |
sound::play 3, "resign"; |
699 |
$self->{chat}->append_text ("\n<infoblock><header>Resign</header>" |
700 |
. "\n<user>" |
701 |
. (util::toxml $self->{user}[$msg->{player}]->as_string) |
702 |
. "</user> resigned.</infoblock>"); |
703 |
} |
704 |
|
705 |
sub event_out_of_time { |
706 |
my ($self, $player) = @_; |
707 |
|
708 |
sound::play 3, "timewin"; |
709 |
$self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>" |
710 |
. "\n<user>" |
711 |
. (util::toxml $self->{user}[$msg->{player}]->as_string) |
712 |
. "</user> ran out of time and lost.</infoblock>"); |
713 |
} |
714 |
|
715 |
sub event_done { |
716 |
my ($self) = @_; |
717 |
|
718 |
warn sprintf "EVENT DONE @{$self->{done}} %x\n", $self->{doneid};#d# |
719 |
|
720 |
if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) { |
721 |
sound::play 2, "ring" unless $inlay->{count}; |
722 |
$self->{chat}->append_text ("\n<infoblock><header>Press Done</header>" |
723 |
. "\nYour opponent pressed done. Now it's up to you."); |
724 |
} |
725 |
if ($self->{doneid} & 0x80000000) { |
726 |
sound::play 2, "warning" unless $inlay->{count}; |
727 |
$self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>" |
728 |
. "\nYour opponent changed the board.."); |
729 |
} |
730 |
|
731 |
$self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]); |
732 |
|
733 |
$self->{chat}->set_end; |
734 |
} |
735 |
|
736 |
sub inject_final_result { |
737 |
my ($self, $msg) = @_; |
738 |
|
739 |
$self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>" |
740 |
. "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) |
741 |
. "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
742 |
. "</infoblock>" |
743 |
); |
744 |
} |
745 |
|
746 |
sub inject_set_gametime { |
747 |
my ($self, $msg) = @_; |
748 |
|
749 |
$self->{timers} = [ |
750 |
[$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}], |
751 |
[$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}], |
752 |
]; |
753 |
|
754 |
$self->update_timers ($self->{timers}) |
755 |
if $self->{showmove} == @{$self->{path}}; |
756 |
} |
757 |
|
758 |
sub inject_req_undo { |
759 |
my ($self, $msg) = @_; |
760 |
|
761 |
my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay; |
762 |
return if $inlay->{ignore}; |
763 |
|
764 |
sound::play 2, "warning" unless $inlay->{count}; |
765 |
$inlay->{count}++; |
766 |
|
767 |
$inlay->clear; |
768 |
$inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n"); |
769 |
$inlay->append_button ("Grant", sub { |
770 |
(delete $self->{undo_inlay})->clear; |
771 |
$self->send (grant_undo => channel => $self->{channel}); |
772 |
}); |
773 |
$inlay->append_button ("Ignore", sub { |
774 |
$inlay->clear; |
775 |
$inlay->{ignore} = 1; |
776 |
# but leave inlay, so further undo requests get counted |
777 |
}); |
778 |
|
779 |
$self->{chat}->set_end; |
780 |
} |
781 |
|
782 |
sub inject_new_game { |
783 |
my ($self, $msg) = @_; |
784 |
|
785 |
if ($msg->{cid} != $self->{cid}) { |
786 |
$self->part; |
787 |
warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d# |
788 |
} |
789 |
|
790 |
$self->{chat}->append_text ("\n<header>Game successfully created on server.</header>"); |
791 |
delete $self->{cid}; |
792 |
} |
793 |
|
794 |
sub draw_challenge { |
795 |
my ($self, $id) = @_; |
796 |
|
797 |
my $info = $self->{challenge}{$id}; |
798 |
my $inlay = $info->{inlay}; |
799 |
my $rules = $info->{rules}; |
800 |
|
801 |
my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;; |
802 |
my $opponent = $as_black ? $info->{white} : $info->{black}; |
803 |
|
804 |
my ($size, $time, $interval, $count, $type); |
805 |
|
806 |
if (!$self->{channel}) { |
807 |
$inlay->append_text ("\nNotes: "); |
808 |
$inlay->append_entry (\$info->{notes}, 20, ""); |
809 |
$inlay->append_text ("\nGlobal Offer: "); |
810 |
$inlay->append_optionmenu (\$info->{flags}, |
811 |
0 => "No", |
812 |
2 => "Yes", |
813 |
); |
814 |
} else { |
815 |
$inlay->append_text ("\nNotes: " . util::toxml $info->{notes}); |
816 |
} |
817 |
|
818 |
$inlay->append_text ("\nType: "); |
819 |
$type = $inlay->append_optionmenu ( |
820 |
\$info->{gametype}, |
821 |
GAMETYPE_DEMONSTRATION , "Demonstration (not yet)", |
822 |
GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)", |
823 |
GAMETYPE_TEACHING , "Teaching (not yet)", |
824 |
GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)", |
825 |
GAMETYPE_SIMUL , "Simul (not yet!)", |
826 |
GAMETYPE_FREE , "Free", |
827 |
GAMETYPE_RATED , "Rated", |
828 |
sub { |
829 |
$size->set_history (2) if $_[0] eq GAMETYPE_RATED; |
830 |
}, |
831 |
); |
832 |
|
833 |
if ($self->{channel}) { |
834 |
$inlay->append_text ("\nMy Colour: "); |
835 |
$inlay->append_optionmenu ( |
836 |
\$as_black, |
837 |
0 => "White", |
838 |
1 => "Black", |
839 |
sub { |
840 |
if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) { |
841 |
($info->{black}, $info->{white}) = ($info->{white}, $info->{black}); |
842 |
} |
843 |
} |
844 |
); |
845 |
} |
846 |
|
847 |
$inlay->append_text ("\nRuleset: "); |
848 |
$inlay->append_optionmenu ( |
849 |
\$info->{rules}{ruleset}, |
850 |
RULESET_JAPANESE , "Japanese", |
851 |
RULESET_CHINESE , "Chinese", |
852 |
RULESET_AGA , "AGA", |
853 |
RULESET_NEW_ZEALAND, "New Zealand", |
854 |
); |
855 |
|
856 |
$inlay->append_text ("\nSize: "); |
857 |
$size = $inlay->append_optionmenu ( |
858 |
\$info->{rules}{size}, |
859 |
(9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38), |
860 |
sub { |
861 |
$type->set_history (5) # reset to free |
862 |
if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED; |
863 |
}, |
864 |
); |
865 |
|
866 |
if ($self->{channel}) { |
867 |
$inlay->append_text ("\nHandicap: "); |
868 |
$inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); |
869 |
|
870 |
$inlay->append_text ("\nKomi: "); |
871 |
$inlay->append_entry (\$info->{rules}{komi}, 5); |
872 |
} |
873 |
|
874 |
$inlay->append_text ("\nTimesys: "); |
875 |
$inlay->append_optionmenu ( |
876 |
\$info->{rules}{timesys}, |
877 |
&TIMESYS_NONE => "None", |
878 |
&TIMESYS_ABSOLUTE => "Absolute", |
879 |
&TIMESYS_BYO_YOMI => "Byo Yomi", |
880 |
&TIMESYS_CANADIAN => "Canadian", |
881 |
sub { |
882 |
my ($new) = @_; |
883 |
|
884 |
if ($new eq TIMESYS_NONE) { |
885 |
$time->hide; |
886 |
$interval->hide; |
887 |
$count->hide; |
888 |
} else { |
889 |
$time->show; |
890 |
$time->set_text ($self->{app}{defaults}{time}); |
891 |
if ($new eq TIMESYS_ABSOLUTE) { |
892 |
$interval->hide; |
893 |
$count->hide; |
894 |
} else { |
895 |
$interval->show; |
896 |
$count->show; |
897 |
if ($new eq TIMESYS_BYO_YOMI) { |
898 |
$interval->set_text ($self->{app}{defaults}{byo_time}); |
899 |
$count->set_text ($self->{app}{defaults}{byo_period}); |
900 |
} elsif ($new eq TIMESYS_CANADIAN) { |
901 |
$interval->set_text ($self->{app}{defaults}{can_time}); |
902 |
$count->set_text ($self->{app}{defaults}{can_period}); |
903 |
} |
904 |
} |
905 |
} |
906 |
} |
907 |
); |
908 |
|
909 |
$inlay->append_text ("\nMain Time: "); |
910 |
$time = $inlay->append_entry (\$info->{rules}{time}, 5); |
911 |
$inlay->append_text ("\nInterval: "); |
912 |
$interval = $inlay->append_entry (\$info->{rules}{interval}, 3); |
913 |
$inlay->append_text ("\nPeriods/Stones: "); |
914 |
$count = $inlay->append_entry (\$info->{rules}{count}, 2); |
915 |
|
916 |
$inlay->append_text ("\n"); |
917 |
|
918 |
if (!$self->{channel}) { |
919 |
$inlay->append_button ("Create Challenge", sub { |
920 |
$inlay->clear; |
921 |
$self->{cid} = $self->{conn}->alloc_clientid; |
922 |
$self->send (new_game => |
923 |
channel => delete $self->{roomid}, |
924 |
gametype => $info->{gametype}, |
925 |
cid => $self->{cid}, |
926 |
flags => $info->{flags}, |
927 |
rules => $info->{rules}, |
928 |
notes => $info->{notes}, |
929 |
); |
930 |
}); |
931 |
} else { |
932 |
$inlay->append_button ("OK", sub { |
933 |
$inlay->clear; |
934 |
$self->send (challenge => |
935 |
channel => $self->{channel}, |
936 |
black => $info->{black}, |
937 |
white => $info->{white}, |
938 |
gametype => $info->{gametype}, |
939 |
cid => $info->{cid}, |
940 |
rules => $info->{rules}, |
941 |
); |
942 |
}); |
943 |
if (exists $self->{challenge}{""}) { |
944 |
$inlay->append_button ("Reject", sub { |
945 |
$inlay->clear; |
946 |
$self->send (reject_challenge => |
947 |
channel => $self->{channel}, |
948 |
name => $opponent->{name}, |
949 |
gametype => $info->{gametype}, |
950 |
cid => $info->{cid}, |
951 |
rules => $info->{rules}, |
952 |
); |
953 |
}); |
954 |
} |
955 |
} |
956 |
} |
957 |
|
958 |
sub draw_users { |
959 |
my ($self, $inlay) = @_; |
960 |
|
961 |
for (sort keys %{$self->{users}}) { |
962 |
$inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); |
963 |
} |
964 |
} |
965 |
|
966 |
sub event_challenge { |
967 |
my ($self, $info) = @_; |
968 |
|
969 |
my $as_black = $info->{black}->{name} eq $self->{conn}{name}; |
970 |
my $opponent = $as_black ? $info->{white} : $info->{black}; |
971 |
|
972 |
my $id = $opponent->{name}; |
973 |
|
974 |
$self->{challenge}{$id} = $info; |
975 |
$self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay ( |
976 |
exists $self->{challenge}{""} |
977 |
? "Challenge from $opponent->{name}" |
978 |
: "Challenge to $opponent->{name}", |
979 |
sub { |
980 |
$self->{challenge}{$id}{inlay} = $_[0]; |
981 |
$self->draw_challenge ($id); |
982 |
}, |
983 |
!exists $self->{challenge}{""} # only open when not offerer |
984 |
); |
985 |
} |
986 |
|
987 |
1; |
988 |
|