… | |
… | |
71 | } |
71 | } |
72 | |
72 | |
73 | sub refresh { |
73 | sub refresh { |
74 | my ($self, $timestamp) = @_; |
74 | my ($self, $timestamp) = @_; |
75 | my $timer = $self->{time} + $self->{start} - $timestamp; |
75 | my $timer = $self->{time} + $self->{start} - $timestamp; |
76 | |
76 | |
77 | # we round the timer value slightly... the protocol isn't exact anyways, |
77 | # we round the timer value slightly... the protocol isn't exact anyways, |
78 | # and this gives smoother timers ;) |
78 | # and this gives smoother timers ;) |
79 | my @format = $self->{format}->(int ($timer + 0.4)); |
79 | my $timer2 = int $timer + 0.4; |
|
|
80 | |
|
|
81 | if ($timer2 <= 0) { |
|
|
82 | $timer2 = 0 if $timer2 < 0; |
|
|
83 | $self->set_text ("TIME OUT"); |
|
|
84 | } else { |
80 | $self->set_text ($self->{format}->(int ($timer + 0.4))); |
85 | $self->set_text ($self->{format}->($timer2)); |
|
|
86 | } |
81 | |
87 | |
82 | $timer - int $timer; |
88 | $timer - int $timer; |
83 | } |
89 | } |
84 | |
90 | |
85 | sub set_time { |
91 | sub set_time { |
86 | my ($self, $time) = @_; |
92 | my ($self, $time, $moves) = @_; |
87 | |
93 | |
88 | # we ignore requests to re-set the time of a running clock. |
94 | # we ignore requests to re-set the time of a running clock. |
89 | # this is the easiest way to ensure that commentary etc. |
95 | # this is the easiest way to ensure that commentary etc. |
90 | # doesn't re-set the clock. yes, this is frickle design, |
96 | # doesn't re-set the clock. yes, this is frickle design, |
91 | # but I think the protocol is to blame here, which gives |
97 | # but I think the protocol is to blame here, which gives |
92 | # very little time information. (cgoban2 also has had quite |
98 | # very little time information. (cgoban2 also has had quite |
93 | # a lot of small time update problems...) |
99 | # a lot of small time update problems...) |
94 | unless ($self->{timeout}) { |
100 | unless ($self->{timeout}) { |
95 | $self->{set}->($time->[0], $time->[1]); |
101 | $self->{set}->($time, $moves); |
96 | $self->refresh ($self->{start}); |
102 | $self->refresh ($self->{start}); |
97 | } |
103 | } |
98 | } |
104 | } |
99 | |
105 | |
100 | sub start { |
106 | sub start { |
… | |
… | |
167 | } |
173 | } |
168 | |
174 | |
169 | $self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); |
175 | $self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); |
170 | } |
176 | } |
171 | |
177 | |
172 | sub set_state { |
178 | sub set_captures { |
173 | my ($self, $captures, $timer, $when) = @_; |
179 | my ($self, $captures) = @_; |
|
|
180 | |
|
|
181 | $self->{info}->set_text ("$captures pris."); |
|
|
182 | } |
|
|
183 | |
|
|
184 | sub set_timer { |
|
|
185 | my ($self, $when, $time, $moves) = @_; |
174 | |
186 | |
175 | $self->{clock}->stop unless $when; |
187 | $self->{clock}->stop unless $when; |
176 | $self->{clock}->set_time ($timer); |
188 | $self->{clock}->set_time ($time, $moves); |
177 | $self->{clock}->start ($when) if $when; |
189 | $self->{clock}->start ($when) if $when; |
178 | |
|
|
179 | $self->{info}->set_text ("$captures pris."); |
|
|
180 | } |
190 | } |
181 | |
191 | |
182 | package game; |
192 | package game; |
183 | |
193 | |
184 | use Scalar::Util qw(weaken); |
194 | use Scalar::Util qw(weaken); |
185 | |
195 | |
186 | use KGS::Constants; |
196 | use KGS::Constants; |
187 | use KGS::Game::Board; |
197 | use KGS::Game::Board; |
188 | |
198 | |
189 | use Gtk2::GoBoard; |
199 | use Gtk2::GoBoard; |
|
|
200 | use Gtk2::GoBoard::Constants; |
|
|
201 | |
|
|
202 | use base KGS::Game; |
|
|
203 | use base KGS::Listener::Game; |
190 | |
204 | |
191 | use Glib::Object::Subclass |
205 | use Glib::Object::Subclass |
192 | Gtk2::Window; |
206 | Gtk2::Window; |
193 | |
|
|
194 | use base KGS::Listener::Game; |
|
|
195 | use base KGS::Game; |
|
|
196 | |
207 | |
197 | use POSIX qw(ceil); |
208 | use POSIX qw(ceil); |
198 | |
209 | |
199 | sub new { |
210 | sub new { |
200 | my ($self, %arg) = @_; |
211 | my ($self, %arg) = @_; |
201 | $self = $self->Glib::Object::new; |
212 | $self = $self->Glib::Object::new; |
202 | $self->{$_} = delete $arg{$_} for keys %arg; |
213 | $self->{$_} = delete $arg{$_} for keys %arg; |
203 | |
214 | |
204 | $self->listen ($self->{conn}); |
|
|
205 | |
|
|
206 | gtk::state $self, "game::window", undef, window_size => [600, 500]; |
215 | gtk::state $self, "game::window", undef, window_size => [600, 500]; |
207 | |
216 | |
208 | $self->signal_connect (delete_event => sub { $self->part; 1 }); |
|
|
209 | $self->signal_connect (destroy => sub { |
217 | $self->signal_connect (destroy => sub { |
210 | $self->unlisten; |
218 | $self->unlisten; |
211 | delete $self->{app}{game}{$self->{channel}}; |
219 | delete $self->{app}{game}{$self->{channel}}; |
212 | %{$_[0]} = (); |
220 | %{$_[0]} = (); |
213 | });#d# |
221 | });#d# |
… | |
… | |
223 | |
231 | |
224 | $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
232 | $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
225 | |
233 | |
226 | # board box (aspect/canvas) |
234 | # board box (aspect/canvas) |
227 | |
235 | |
228 | $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); |
236 | #$self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); |
|
|
237 | |
|
|
238 | # RIGHT PANE |
|
|
239 | |
|
|
240 | $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1); |
|
|
241 | $hpane->set (position_set => 1); |
|
|
242 | |
|
|
243 | $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0); |
229 | |
244 | |
230 | { |
245 | { |
231 | $frame->add (my $vbox = new Gtk2::VBox); |
246 | $frame->add (my $vbox = new Gtk2::VBox); |
232 | $vbox->add ($self->{title} = new Gtk2::Label $title); |
247 | $vbox->add ($self->{title} = new Gtk2::Label $title); |
233 | |
248 | |
… | |
… | |
239 | |
254 | |
240 | $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); |
255 | $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); |
241 | $scale->set_draw_value (0); |
256 | $scale->set_draw_value (0); |
242 | $scale->set_digits (0); |
257 | $scale->set_digits (0); |
243 | |
258 | |
244 | $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); |
259 | $self->{moveadj}->signal_connect (value_changed => sub { |
|
|
260 | $self->{showmove} = int $self->{moveadj}->get_value; |
|
|
261 | $self->update_board; |
|
|
262 | }); |
245 | } |
263 | } |
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 | # $sw->add ($self->{userlist} = new userlist); |
|
|
258 | |
|
|
259 | $self->{vpane}->add (my $vbox = new Gtk2::VBox); |
|
|
260 | |
264 | |
261 | $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
265 | $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
262 | |
266 | |
263 | $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) |
267 | $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) |
264 | for COLOUR_WHITE, COLOUR_BLACK; |
268 | for COLOUR_WHITE, COLOUR_BLACK; |
|
|
269 | |
|
|
270 | $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
|
|
271 | |
|
|
272 | $buttonbox->add ($self->{button_pass} = |
|
|
273 | Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0)); |
|
|
274 | $self->{button_pass}->signal_connect (clicked => sub { |
|
|
275 | $self->{board_click}->(255, 255) if $self->{board_click}; |
|
|
276 | }); |
|
|
277 | $buttonbox->add ($self->{button_undo} = |
|
|
278 | Gtk2::Button->Glib::Object::new (label => "Undo", no_show_all => 1, visible => 0)); |
|
|
279 | $self->{button_undo}->signal_connect (clicked => sub { |
|
|
280 | $self->send (req_undo => channel => $self->{channel}); |
|
|
281 | }); |
|
|
282 | $buttonbox->add ($self->{button_resign} = |
|
|
283 | Gtk2::Button->Glib::Object::new (label => "Resign", no_show_all => 1, visible => 0)); |
|
|
284 | $self->{button_resign}->signal_connect (clicked => sub { |
|
|
285 | $self->send (resign_game => channel => $self->{channel}, player => $self->{colour}); |
|
|
286 | }); |
265 | |
287 | |
266 | $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); |
288 | $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); |
267 | |
289 | |
268 | $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Rules", sub { $self->draw_rules (@_) }, 1); |
290 | $self->set_channel ($self->{channel}); |
269 | $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 0); |
|
|
270 | |
291 | |
271 | $self->{chat}->signal_connect (command => sub { |
292 | $self->show_all; |
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 | |
293 | |
280 | $self; |
294 | $self; |
|
|
295 | } |
|
|
296 | |
|
|
297 | sub set_channel { |
|
|
298 | my ($self, $channel) = @_; |
|
|
299 | |
|
|
300 | $self->{channel} = $channel; |
|
|
301 | |
|
|
302 | if ($self->{channel} > 0) { |
|
|
303 | $self->listen ($self->{conn}); |
|
|
304 | |
|
|
305 | $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1); |
|
|
306 | $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1); |
|
|
307 | |
|
|
308 | $self->signal_connect (delete_event => sub { $self->part; 1 }); |
|
|
309 | $self->{chat}->signal_connect (command => sub { |
|
|
310 | my ($chat, $cmd, $arg) = @_; |
|
|
311 | if ($cmd eq "rsave") { |
|
|
312 | Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d# |
|
|
313 | } else { |
|
|
314 | $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self); |
|
|
315 | } |
|
|
316 | }); |
|
|
317 | } |
281 | } |
318 | } |
282 | |
319 | |
283 | sub event_update_users { |
320 | sub event_update_users { |
284 | my ($self, $add, $update, $remove) = @_; |
321 | my ($self, $add, $update, $remove) = @_; |
285 | |
322 | |
286 | # $self->{userlist}->update ($add, $update, $remove); |
323 | # $self->{userlist}->update ($add, $update, $remove); |
287 | |
324 | |
|
|
325 | $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy |
|
|
326 | for @$remove; |
|
|
327 | |
288 | $self->{users_inlay}->refresh; |
328 | $self->{users_inlay}->refresh; |
289 | |
329 | |
290 | my %important; |
330 | my %important; |
|
|
331 | $important{$self->{black}{name}}++; |
|
|
332 | $important{$self->{white}{name}}++; |
291 | $important{$self->{user1}{name}}++; |
333 | $important{$self->{owner}{name}}++; |
292 | $important{$self->{user2}{name}}++; |
|
|
293 | $important{$self->{user3}{name}}++; |
|
|
294 | |
334 | |
295 | if (my @users = grep $important{$_->{name}}, @$add) { |
335 | if (my @users = grep $important{$_->{name}}, @$add) { |
296 | $self->{chat}->append_text ("\n<header>Joins:</header>"); |
336 | $self->{chat}->append_text ("\n<header>Joins:</header>"); |
297 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
337 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
298 | } |
338 | } |
299 | if (my @users = grep $important{$_->{name}}, @$remove) { |
339 | if (my @users = grep $important{$_->{name}}, @$remove) { |
300 | $self->{chat}->append_text ("\n<header>Parts:</header>"); |
340 | $self->{chat}->append_text ("\n<header>Parts:</header>"); |
301 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
341 | $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
302 | } |
342 | } |
303 | |
|
|
304 | } |
343 | } |
305 | |
344 | |
306 | sub join { |
345 | sub join { |
307 | my ($self) = @_; |
346 | my ($self) = @_; |
308 | return if $self->{joined}; |
347 | return if $self->{joined}; |
309 | |
348 | |
310 | $self->SUPER::join; |
349 | $self->SUPER::join; |
311 | } |
350 | } |
312 | |
351 | |
|
|
352 | sub update_cursor { |
|
|
353 | my ($self) = @_; |
|
|
354 | |
|
|
355 | my $running = $self->{showmove} == @{$self->{path}} && $self->is_active; |
|
|
356 | |
|
|
357 | delete $self->{board_click}; |
|
|
358 | |
|
|
359 | if ($self->{teacher} eq $self->{app}{conn}) { |
|
|
360 | #TODO# # teaching mode not implemented |
|
|
361 | $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1); |
|
|
362 | $self->{button_undo}->hide; |
|
|
363 | $self->{button_resign}->hide; |
|
|
364 | $self->{board}->set (cursor => undef); |
|
|
365 | |
|
|
366 | } elsif ($running && $self->{colour} != COLOUR_NONE) { |
|
|
367 | # during game |
|
|
368 | $self->{button_undo}->show; |
|
|
369 | $self->{button_resign}->show; |
|
|
370 | |
|
|
371 | if ($self->{cur_board}{score}) { |
|
|
372 | # during scoring |
|
|
373 | $self->{button_pass}->set (label => "Done", sensitive => 1, visible => 1); |
|
|
374 | $self->{board}->set (cursor => sub { |
|
|
375 | $_[0] & (MARK_B | MARK_W) |
|
|
376 | ? $_[0] ^ MARK_GRAYED |
|
|
377 | : $_[0]; |
|
|
378 | }); |
|
|
379 | $self->{board_click} = sub { |
|
|
380 | if ($_[0] == 255) { |
|
|
381 | $self->{button_pass}->sensitive (0); |
|
|
382 | $self->done; |
|
|
383 | } else { |
|
|
384 | $self->send (mark_dead => |
|
|
385 | channel => $self->{channel}, |
|
|
386 | x => $_[0], |
|
|
387 | y => $_[1], |
|
|
388 | dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED), |
|
|
389 | ); |
|
|
390 | } |
|
|
391 | }; |
|
|
392 | |
|
|
393 | } elsif (1 - $self->{colour} == $self->{cur_board}{last}) { |
|
|
394 | # normal move |
|
|
395 | $self->{button_pass}->set (label => "Pass", sensitive => 1, visible => 1); |
|
|
396 | $self->{board}->set (cursor => sub { |
|
|
397 | # if is_valid_move oder so#TODO# |
|
|
398 | $_[0] & (MARK_B | MARK_W) |
|
|
399 | ? $_[0] |
|
|
400 | : $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B); |
|
|
401 | }); |
|
|
402 | $self->{board_click} = sub { |
|
|
403 | $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); |
|
|
404 | $self->{board}->set (cursor => undef); |
|
|
405 | delete $self->{board_click}; |
|
|
406 | $self->{button_pass}->sensitive (0); |
|
|
407 | }; |
|
|
408 | } else { |
|
|
409 | $self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1); |
|
|
410 | } |
|
|
411 | } else { |
|
|
412 | $self->{button_undo}->hide; |
|
|
413 | $self->{button_resign}->hide; |
|
|
414 | $self->{button_pass}->hide; |
|
|
415 | $self->{board}->set (cursor => undef); |
|
|
416 | #TODO# # implement coordinate-grabbing |
|
|
417 | } |
|
|
418 | } |
|
|
419 | |
|
|
420 | sub update_timers { |
|
|
421 | my ($self, $timers) = @_; |
|
|
422 | |
|
|
423 | my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher}; |
|
|
424 | |
|
|
425 | for my $colour (COLOUR_BLACK, COLOUR_WHITE) { |
|
|
426 | my $t = $timers->[$colour]; |
|
|
427 | $self->{userpanel}[$colour]->set_timer ( |
|
|
428 | $running && $self->{lastmove_colour} == 1 - $colour && $t->[0], |
|
|
429 | $t->[1], $t->[2]); |
|
|
430 | } |
|
|
431 | } |
|
|
432 | |
313 | sub update_board { |
433 | sub update_board { |
314 | my ($self) = @_; |
434 | my ($self) = @_; |
315 | return unless $self->{path}; |
435 | return unless $self->{path}; |
316 | |
436 | |
317 | my $move = int $self->{moveadj}->get_value; |
|
|
318 | |
|
|
319 | my $running = $move == @{$self->{path}}; |
|
|
320 | |
|
|
321 | $self->{board_label}->set_text ("Move " . ($move - 1)); |
437 | $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1)); |
322 | |
438 | |
323 | $self->{cur_board} = new KGS::Game::Board $self->{size}; |
439 | $self->{cur_board} = new KGS::Game::Board $self->{size}; |
324 | $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); |
440 | $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]); |
325 | |
441 | |
|
|
442 | $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) |
326 | for my $colour (COLOUR_WHITE, COLOUR_BLACK) { |
443 | for COLOUR_WHITE, COLOUR_BLACK; |
327 | $self->{userpanel}[$colour]->set_state ( |
444 | |
328 | $self->{cur_board}{captures}[$colour], |
445 | if ($self->{showmove} == @{$self->{path}}) { |
|
|
446 | $self->{timers} = [ |
|
|
447 | [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}], |
|
|
448 | [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}], |
|
|
449 | ]; |
|
|
450 | $self->update_timers ($self->{timers}); |
|
|
451 | } else { |
|
|
452 | $self->update_timers ([ |
329 | $self->{cur_board}{timer}[$colour], |
453 | [0, @{$self->{cur_board}{timer}[0]}], |
330 | ($running && $self->{lastmove_colour} == !$colour) |
454 | [0, @{$self->{cur_board}{timer}[1]}], |
331 | ? $self->{lastmove_time} : 0 |
|
|
332 | ); |
455 | ]); |
333 | } |
456 | } |
334 | |
457 | |
335 | $self->{board}->set_board ($self->{cur_board}); |
458 | $self->{board}->set_board ($self->{cur_board}); |
|
|
459 | |
|
|
460 | $self->update_cursor; |
336 | } |
461 | } |
337 | |
462 | |
338 | sub event_update_tree { |
463 | sub event_update_tree { |
339 | my ($self) = @_; |
464 | my ($self) = @_; |
340 | |
465 | |
… | |
… | |
394 | |
519 | |
395 | sub event_join { |
520 | sub event_join { |
396 | my ($self) = @_; |
521 | my ($self) = @_; |
397 | |
522 | |
398 | $self->SUPER::event_join (@_); |
523 | $self->SUPER::event_join (@_); |
|
|
524 | $self->init_tree; |
399 | $self->event_update_game; |
525 | $self->event_update_game; |
400 | $self->show_all; |
|
|
401 | } |
526 | } |
402 | |
527 | |
403 | sub event_part { |
528 | sub event_part { |
404 | my ($self) = @_; |
529 | my ($self) = @_; |
405 | |
530 | |
… | |
… | |
408 | } |
533 | } |
409 | |
534 | |
410 | sub event_move { |
535 | sub event_move { |
411 | my ($self, $pass) = @_; |
536 | my ($self, $pass) = @_; |
412 | sound::play 1, $pass ? "pass" : "move"; |
537 | sound::play 1, $pass ? "pass" : "move"; |
|
|
538 | |
|
|
539 | if ($self->{undo_inlay}) { |
|
|
540 | (delete $self->{undo_inlay})->clear; |
|
|
541 | } |
413 | } |
542 | } |
414 | |
543 | |
415 | sub event_update_game { |
544 | sub event_update_game { |
416 | my ($self) = @_; |
545 | my ($self) = @_; |
|
|
546 | |
417 | $self->SUPER::event_update_game; |
547 | $self->SUPER::event_update_game; |
418 | |
548 | |
419 | return unless $self->{joined}; |
549 | return unless $self->{joined}; |
420 | |
550 | |
|
|
551 | $self->{colour} = $self->player_colour ($self->{conn}{name}); |
|
|
552 | |
421 | my $title = defined $self->{channel} |
553 | my $title = defined $self->{channel} |
422 | ? $self->owner->as_string . " " . $self->opponent_string |
554 | ? $self->owner->as_string . " " . $self->opponent_string |
423 | : "Game Window"; |
555 | : "Game Window"; |
424 | $self->set_title("KGS Game $title"); |
556 | $self->set_title ("KGS Game $title"); |
425 | $self->{title}->set_text ($title); |
557 | $self->{title}->set_text ($title); |
426 | |
558 | |
427 | $self->{user}[COLOUR_BLACK] = $self->{user1}; |
559 | $self->{user}[COLOUR_BLACK] = $self->{black}; |
428 | $self->{user}[COLOUR_WHITE] = $self->{user2}; |
560 | $self->{user}[COLOUR_WHITE] = $self->{white}; |
429 | |
561 | |
430 | # show board |
562 | # show board |
431 | if ($self->is_inprogress) { |
563 | if ($self->is_inprogress) { |
432 | $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent; |
564 | if (!$self->{boardbox}->parent) { |
|
|
565 | $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size}); |
|
|
566 | $self->{left}->add ($self->{boardbox}); |
|
|
567 | $self->{board}->signal_connect (button_release => sub { |
|
|
568 | if ($_[1] == 1) { |
|
|
569 | $self->{board_click}->($_[2], $_[3]) if $self->{board_click}; |
|
|
570 | } |
|
|
571 | }); |
|
|
572 | } |
433 | if (my $ch = delete $self->{challenge}) { |
573 | if (my $ch = delete $self->{challenge}) { |
434 | (delete $_->{inlay})->clear for values %$ch; |
574 | $_->{inlay}->destroy for values %$ch; |
435 | } |
575 | } |
|
|
576 | $self->update_cursor; |
436 | } |
577 | } |
437 | |
578 | |
438 | $self->{left}->show_all; |
579 | $self->{left}->show_all; |
439 | |
580 | |
440 | # view text |
581 | $self->{rules_inlay}->refresh; |
441 | |
582 | |
442 | eval { #d# |
583 | } |
443 | my @ga; |
|
|
444 | $ga[0] = "\nType: " . (util::toxml $gametype{$self->type}) |
|
|
445 | . " (" . (util::toxml $gameopt{$self->option}) . ")"; |
|
|
446 | $ga[1] = "\nFlags:"; |
|
|
447 | $ga[1] .= " started" if $self->is_inprogress; |
|
|
448 | $ga[1] .= " adjourned" if $self->is_adjourned; |
|
|
449 | $ga[1] .= " scored" if $self->is_scored; |
|
|
450 | $ga[1] .= " saved" if $self->is_saved; |
|
|
451 | |
584 | |
|
|
585 | sub draw_setup { |
|
|
586 | my ($self, $inlay) = @_; |
|
|
587 | |
|
|
588 | return unless $self->{joined}; |
|
|
589 | |
|
|
590 | my $rules = $self->{rules}; |
|
|
591 | |
|
|
592 | my $text = ""; |
|
|
593 | |
|
|
594 | $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>" |
|
|
595 | if $self->{teacher}; |
|
|
596 | |
452 | $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" |
597 | $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>" |
453 | if $self->{user3}->is_inprogress; |
598 | if $self->{owner}->is_valid; |
454 | |
|
|
455 | $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>" |
|
|
456 | . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>" |
|
|
457 | if $self->is_inprogress; |
|
|
458 | |
599 | |
459 | if ($self->is_inprogress) { |
600 | if ($self->is_inprogress) { |
460 | $ga[4] = "\nHandicap: " . $self->{handicap}; |
601 | $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>" |
461 | $ga[5] = "\nKomi: " . $self->{komi}; |
602 | . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>"; |
462 | $ga[6] = "\nSize: " . $self->size_string; |
|
|
463 | } |
|
|
464 | |
|
|
465 | if ($self->is_scored) { |
|
|
466 | $ga[7] = "\nResult: " . $self->score_string; |
|
|
467 | } |
|
|
468 | |
|
|
469 | $text = "\n<infoblock><header>Game Update</header>"; |
|
|
470 | for (0..7) { |
|
|
471 | if ($self->{gatext}[$_] ne $ga[$_]) { |
|
|
472 | $text .= $ga[$_]; |
|
|
473 | } |
|
|
474 | } |
|
|
475 | $text .= "</infoblock>"; |
|
|
476 | |
|
|
477 | $self->{gatext} = \@ga; |
|
|
478 | }; |
603 | } |
479 | |
604 | $text .= "\nType: " . util::toxml $gametype{$self->type}; |
480 | $self->{chat}->append_text ($text); |
|
|
481 | } |
|
|
482 | |
|
|
483 | sub draw_rules { |
|
|
484 | my ($self, $inlay) = @_; |
|
|
485 | |
|
|
486 | my $rules = $self->{rules}; |
|
|
487 | |
|
|
488 | my $text = ""; |
|
|
489 | |
605 | |
490 | $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; |
606 | $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; |
491 | |
607 | |
492 | $text .= "\nTime: "; |
608 | $text .= "\nTime: "; |
493 | |
609 | |
… | |
… | |
502 | } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
618 | } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
503 | $text .= util::format_time $rules->{time}; |
619 | $text .= util::format_time $rules->{time}; |
504 | $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
620 | $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
505 | } |
621 | } |
506 | |
622 | |
|
|
623 | $text .= "\nFlags:"; |
|
|
624 | $text .= " private" if $self->is_private; |
|
|
625 | $text .= " started" if $self->is_inprogress; |
|
|
626 | $text .= " adjourned" if $self->is_adjourned; |
|
|
627 | $text .= " scored" if $self->is_scored; |
|
|
628 | $text .= " saved" if $self->is_saved; |
|
|
629 | |
|
|
630 | if ($self->is_inprogress) { |
|
|
631 | $text .= "\nHandicap: " . $self->{handicap}; |
|
|
632 | $text .= "\nKomi: " . $self->{komi}; |
|
|
633 | $text .= "\nSize: " . $self->size_string; |
|
|
634 | } |
|
|
635 | |
|
|
636 | if ($self->is_scored) { |
|
|
637 | $text .= "\nResult: " . $self->score_string; |
|
|
638 | } |
|
|
639 | |
507 | $inlay->append_text ("<infoblock>$text</infoblock>"); |
640 | $inlay->append_text ("<infoblock>$text</infoblock>"); |
|
|
641 | |
508 | } |
642 | } |
509 | |
643 | |
510 | sub event_update_rules { |
644 | sub event_update_rules { |
511 | my ($self, $rules) = @_; |
645 | my ($self, $rules) = @_; |
512 | |
646 | |
… | |
… | |
520 | sound::play 3, "gamestart"; |
654 | sound::play 3, "gamestart"; |
521 | |
655 | |
522 | $self->{rules_inlay}->refresh; |
656 | $self->{rules_inlay}->refresh; |
523 | } |
657 | } |
524 | |
658 | |
525 | sub inject_resign_game { |
659 | sub event_resign_game { |
526 | my ($self, $msg) = @_; |
660 | my ($self, $player) = @_; |
527 | |
661 | |
528 | sound::play 3, "resign"; |
662 | sound::play 3, "resign"; |
529 | |
|
|
530 | $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" |
663 | $self->{chat}->append_text ("\n<infoblock><header>Resign</header>" |
531 | . "\n<user>" |
664 | . "\n<user>" |
532 | . (util::toxml $self->{user}[$msg->{player}]->as_string) |
665 | . (util::toxml $self->{user}[$msg->{player}]->as_string) |
533 | . "</user> resigned.</infoblock>"); |
666 | . "</user> resigned.</infoblock>"); |
|
|
667 | } |
|
|
668 | |
|
|
669 | sub event_out_of_time { |
|
|
670 | my ($self, $player) = @_; |
|
|
671 | |
|
|
672 | sound::play 3, "timewin"; |
|
|
673 | $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>" |
|
|
674 | . "\n<user>" |
|
|
675 | . (util::toxml $self->{user}[$msg->{player}]->as_string) |
|
|
676 | . "</user> ran out of time and lost.</infoblock>"); |
|
|
677 | } |
|
|
678 | |
|
|
679 | sub event_done { |
|
|
680 | my ($self) = @_; |
|
|
681 | |
|
|
682 | if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) { |
|
|
683 | $self->{chat}->append_text ("\n<infoblock><header>Done</header>" |
|
|
684 | . "\nYour opponent pressed done."); |
|
|
685 | } |
534 | } |
686 | } |
535 | |
687 | |
536 | sub inject_final_result { |
688 | sub inject_final_result { |
537 | my ($self, $msg) = @_; |
689 | my ($self, $msg) = @_; |
538 | |
690 | |
… | |
… | |
541 | . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
693 | . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
542 | . "</infoblock>" |
694 | . "</infoblock>" |
543 | ); |
695 | ); |
544 | } |
696 | } |
545 | |
697 | |
|
|
698 | sub inject_set_gametime { |
|
|
699 | my ($self, $msg) = @_; |
|
|
700 | |
|
|
701 | $self->{timers} = [ |
|
|
702 | [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}], |
|
|
703 | [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}], |
|
|
704 | ]; |
|
|
705 | |
|
|
706 | print "SGT\n";#d# |
|
|
707 | $self->update_timers ($self->{timers}) |
|
|
708 | if $self->{showmove} == @{$self->{path}}; |
|
|
709 | } |
|
|
710 | |
|
|
711 | sub inject_req_undo { |
|
|
712 | my ($self, $msg) = @_; |
|
|
713 | |
|
|
714 | my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay; |
|
|
715 | return if $inlay->{ignore}; |
|
|
716 | |
|
|
717 | sound::play 3, "warning" unless $inlay->{count}; |
|
|
718 | $inlay->{count}++; |
|
|
719 | |
|
|
720 | $inlay->clear; |
|
|
721 | $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n"); |
|
|
722 | $inlay->append_button ("Grant", sub { |
|
|
723 | $inlay->clear; |
|
|
724 | $self->send (grant_undo => channel => $self->{channel}); |
|
|
725 | }); |
|
|
726 | $inlay->append_button ("Ignore", sub { |
|
|
727 | $inlay->clear; |
|
|
728 | $inlay->{ignore} = 1; |
|
|
729 | # but leave inlay, so further undo requests get counted |
|
|
730 | }); |
|
|
731 | |
|
|
732 | $self->{chat}->set_end; |
|
|
733 | } |
|
|
734 | |
|
|
735 | sub inject_new_game { |
|
|
736 | my ($self, $msg) = @_; |
|
|
737 | |
|
|
738 | $self->{chat}->append_text ("\n<header>ACK from server ($msg->{cid} == $self->{cid})</header>"); |
|
|
739 | delete $self->{cid}; |
|
|
740 | } |
|
|
741 | |
546 | sub draw_challenge { |
742 | sub draw_challenge { |
547 | my ($self, $c) = @_; |
743 | my ($self, $id) = @_; |
548 | |
744 | |
|
|
745 | my $info = $self->{challenge}{$id}; |
549 | my $inlay = $c->{inlay}; |
746 | my $inlay = $info->{inlay}; |
550 | my $challenge = $c->{challenge}; |
|
|
551 | my $rules = $challenge->{rules}; |
747 | my $rules = $info->{rules}; |
552 | |
748 | |
553 | my $as_black = $challenge->{user1}{name} eq $self->{conn}{name}; |
749 | my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;; |
554 | my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1}; |
750 | my $opponent = $as_black ? $info->{white} : $info->{black}; |
555 | |
751 | |
556 | $inlay->append_text ("\n<challenge>Challenge to <user>" . $opponent->as_string . "</user></challenge>"); |
752 | my ($size, $time, $interval, $count); |
|
|
753 | |
|
|
754 | if (!$self->{channel}) { |
|
|
755 | $inlay->append_text ("\nNotes: "); |
|
|
756 | $inlay->append_entry (\$info->{notes}, 20, ""); |
|
|
757 | $inlay->append_text ("\nGlobal Offer: "); |
|
|
758 | $inlay->append_optionmenu (\$info->{flags}, |
|
|
759 | 0 => "No", |
|
|
760 | 2 => "Yes", |
|
|
761 | ); |
|
|
762 | } else { |
|
|
763 | $inlay->append_text ("\nNotes: " . util::toxml $info->{notes}); |
|
|
764 | } |
|
|
765 | |
|
|
766 | $inlay->append_text ("\nType: "); |
|
|
767 | $inlay->append_optionmenu ( |
|
|
768 | \$info->{gametype}, |
|
|
769 | GAMETYPE_DEMONSTRATION , "Demonstration", |
|
|
770 | GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P)", |
|
|
771 | GAMETYPE_TEACHING , "Teaching", |
|
|
772 | GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P)", |
|
|
773 | GAMETYPE_SIMUL , "Simul (not yet!)", |
|
|
774 | GAMETYPE_FREE , "Free", |
|
|
775 | GAMETYPE_RATED , "Rated", |
|
|
776 | sub { |
|
|
777 | $size->set_history (2) if $_[0] eq GAMETYPE_RATED; |
|
|
778 | }, |
|
|
779 | ); |
|
|
780 | |
|
|
781 | if ($self->{channel}) { |
|
|
782 | $inlay->append_text ("\nMy Colour: "); |
|
|
783 | $inlay->append_optionmenu ( |
|
|
784 | \$as_black, |
|
|
785 | 0 => "White", |
|
|
786 | 1 => "Black", |
|
|
787 | sub { |
|
|
788 | if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) { |
|
|
789 | ($info->{black}, $info->{white}) = ($info->{white}, $info->{black}); |
|
|
790 | } |
|
|
791 | } |
|
|
792 | ); |
|
|
793 | } |
|
|
794 | |
|
|
795 | $inlay->append_text ("\nRuleset: "); |
|
|
796 | $inlay->append_optionmenu ( |
|
|
797 | \$info->{rules}{ruleset}, |
|
|
798 | RULESET_JAPANESE , "Japanese", |
|
|
799 | RULESET_CHINESE , "Chinese", |
|
|
800 | RULESET_AGA , "AGA", |
|
|
801 | RULESET_NEW_ZEALAND, "New Zealand", |
|
|
802 | ); |
|
|
803 | |
|
|
804 | $inlay->append_text ("\nSize: "); |
|
|
805 | $size = $inlay->append_optionmenu ( |
|
|
806 | \$info->{rules}{size}, 9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38 |
|
|
807 | ); |
|
|
808 | |
|
|
809 | if ($self->{channel}) { |
557 | $inlay->append_text ("\nHandicap: $rules->{handicap}"); |
810 | $inlay->append_text ("\nHandicap: "); |
|
|
811 | $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); |
558 | |
812 | |
559 | #bless( ( |
813 | $inlay->append_text ("\nKomi: "); |
560 | # gametype => 3, |
814 | $inlay->append_entry (\$info->{rules}{komi}, 5); |
561 | # user1 => bless( { |
815 | } |
562 | # flags => 2633, |
816 | |
563 | # name => 'dorkusx' |
817 | $inlay->append_text ("\nTimesys: "); |
564 | # }, 'KGS::User' ), |
818 | $inlay->append_optionmenu ( |
565 | # rules => bless( { |
819 | \$info->{rules}{timesys}, |
566 | # count => 5, |
820 | &TIMESYS_NONE => "None", |
567 | # time => 900, |
821 | &TIMESYS_ABSOLUTE => "Absolute", |
568 | # timesys => 2, |
822 | &TIMESYS_BYO_YOMI => "Byo Yomi", |
569 | # interval => 30, |
823 | &TIMESYS_CANADIAN => "Canadian", |
570 | # komi => '6.5', |
824 | sub { |
571 | # size => 19, |
825 | my ($new) = @_; |
572 | # ruleset => 0, |
826 | |
573 | # handicap => 0 |
827 | if ($new eq TIMESYS_NONE) { |
574 | # }, 'KGS::Rules' ), |
828 | $time->hide; |
575 | # user2 => bless( { |
829 | $interval->hide; |
576 | # flags => 436220808, |
830 | $count->hide; |
577 | # name => 'Nerdamus' |
831 | } else { |
578 | # }, 'KGS::User' ) |
832 | $time->show; |
579 | # ), 'KGS::Challenge' ) |
833 | $time->set_text ($self->{app}{defaults}{time}); |
|
|
834 | if ($new eq TIMESYS_ABSOLUTE) { |
|
|
835 | $interval->hide; |
|
|
836 | $count->hide; |
|
|
837 | } else { |
|
|
838 | $interval->show; |
|
|
839 | $count->show; |
|
|
840 | if ($new eq TIMESYS_BYO_YOMI) { |
|
|
841 | $interval->set_text ($self->{app}{defaults}{byo_time}); |
|
|
842 | $count->set_text ($self->{app}{defaults}{byo_period}); |
|
|
843 | } elsif ($new eq TIMESYS_CANADIAN) { |
|
|
844 | $interval->set_text ($self->{app}{defaults}{can_time}); |
|
|
845 | $count->set_text ($self->{app}{defaults}{can_period}); |
|
|
846 | } |
|
|
847 | } |
|
|
848 | } |
|
|
849 | } |
|
|
850 | ); |
|
|
851 | |
|
|
852 | $inlay->append_text ("\nMain Time: "); |
|
|
853 | $time = $inlay->append_entry (\$info->{rules}{time}, 5); |
|
|
854 | $inlay->append_text ("\nInterval: "); |
|
|
855 | $interval = $inlay->append_entry (\$info->{rules}{interval}, 3); |
|
|
856 | $inlay->append_text ("\nPeriods/Stones: "); |
|
|
857 | $count = $inlay->append_entry (\$info->{rules}{count}, 2); |
|
|
858 | |
|
|
859 | $inlay->append_text ("\n"); |
|
|
860 | |
|
|
861 | if (!$self->{channel}) { |
|
|
862 | $inlay->append_button ("Create Challenge", sub { |
|
|
863 | $inlay->clear; |
|
|
864 | $self->{cid} = $self->{conn}->alloc_clientid; |
|
|
865 | $self->send (new_game => |
|
|
866 | channel => delete $self->{roomid}, |
|
|
867 | gametype => $info->{gametype}, |
|
|
868 | cid => $self->{cid}, |
|
|
869 | flags => $info->{flags}, |
|
|
870 | rules => $info->{rules}, |
|
|
871 | notes => $info->{notes}, |
|
|
872 | ); |
|
|
873 | }); |
|
|
874 | } else { |
|
|
875 | $inlay->append_button ("OK", sub { |
|
|
876 | $inlay->clear; |
|
|
877 | $self->send (challenge => |
|
|
878 | channel => $self->{channel}, |
|
|
879 | black => $info->{black}, |
|
|
880 | white => $info->{white}, |
|
|
881 | gametype => $info->{gametype}, |
|
|
882 | cid => $info->{cid}, |
|
|
883 | rules => $info->{rules}, |
|
|
884 | ); |
|
|
885 | }); |
|
|
886 | if (exists $self->{challenge}{""}) { |
|
|
887 | $inlay->append_button ("Reject", sub { |
|
|
888 | $inlay->clear; |
|
|
889 | $self->send (reject_challenge => |
|
|
890 | channel => $self->{channel}, |
|
|
891 | name => $opponent->{name}, |
|
|
892 | gametype => $info->{gametype}, |
|
|
893 | cid => $info->{cid}, |
|
|
894 | rules => $info->{rules}, |
|
|
895 | ); |
|
|
896 | }); |
|
|
897 | } |
|
|
898 | } |
580 | } |
899 | } |
581 | |
900 | |
582 | sub draw_users { |
901 | sub draw_users { |
583 | my ($self, $inlay) = @_; |
902 | my ($self, $inlay) = @_; |
584 | |
903 | |
… | |
… | |
586 | $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); |
905 | $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); |
587 | } |
906 | } |
588 | } |
907 | } |
589 | |
908 | |
590 | sub event_challenge { |
909 | sub event_challenge { |
591 | my ($self, $challenge) = @_; |
910 | my ($self, $info) = @_; |
592 | |
911 | |
593 | my $as_black = $challenge->{user1}{name} eq $self->{conn}{name}; |
912 | my $as_black = $info->{black}->{name} eq $self->{conn}{name}; |
594 | my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1}; |
913 | my $opponent = $as_black ? $info->{white} : $info->{black}; |
595 | |
914 | |
596 | my $c = $self->{challenge}{$opponent->{name}} ||= {}; |
915 | my $id = $opponent->{name}; |
597 | |
916 | |
598 | $c->{inlay} ||= $self->{chat}->new_inlay; |
917 | $self->{challenge}{$id} = $info; |
599 | $c->{challenge} = $challenge; |
918 | $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay ( |
600 | |
919 | exists $self->{challenge}{""} |
|
|
920 | ? "Challenge from $opponent->{name}" |
|
|
921 | : "Challenge to $opponent->{name}", |
|
|
922 | sub { |
|
|
923 | $self->{challenge}{$id}{inlay} = $_[0]; |
601 | $self->draw_challenge ($c); |
924 | $self->draw_challenge ($id); |
602 | |
925 | }, |
603 | # require KGS::Listener::Debug; |
926 | !exists $self->{challenge}{""} # only open when not offerer |
604 | # $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge)); |
927 | ); |
605 | } |
928 | } |
606 | |
929 | |
607 | 1; |
930 | 1; |
608 | |
931 | |