--- kgsueme/kgsueme/game.pl 2003/06/18 00:24:30 1.60 +++ kgsueme/kgsueme/game.pl 2004/06/06 08:01:09 1.134 @@ -1,5 +1,9 @@ use utf8; +use Scalar::Util (); + +### GO CLOCK WIDGET ######################################################### + package game::goclock; # Lo and Behold! I admit it! The rounding stuff etc.. in goclock @@ -9,34 +13,43 @@ use KGS::Constants; -use base gtk::widget; +use Glib::Object::Subclass + Gtk2::Label; -sub new { - my $class = shift; - my $self = $class->SUPER::new(@_); +sub INIT_INSTANCE { + my $self = shift; - $self->{widget} = new Gtk2::Label; + $self->signal_connect (destroy => sub { $_[0]->stop }); $self->{set} = sub { }; - $self->{format} = sub { "ERROR" }; + $self->{format} = sub { "???" }; +} - $self; +sub FINALIZE_INSTANCE { + my $self = shift; + + $self->stop; } sub configure { my ($self, $timesys, $main, $interval, $count) = @_; if ($timesys == TIMESYS_ABSOLUTE) { - $self->{set} = sub { $self->{time} = $_[0] }; - $self->{format} = sub { util::format_time $_[0] }; + $self->{format} = sub { + if ($_[0] < 0) { + "TIMEOUT"; + } else { + util::format_time $_[0]; + } + }; } elsif ($timesys == TIMESYS_BYO_YOMI) { my $low = $interval * $count; - $self->{set} = sub { $self->{time} = $_[0] }; - $self->{format} = sub { - if ($_[0] > $low) { + if ($_[0] < 0) { + "TIMEOUT"; + } elsif ($_[0] > $low) { util::format_time $_[0] - $low; } else { sprintf "%s (%d)", @@ -46,63 +59,62 @@ }; } elsif ($timesys == TIMESYS_CANADIAN) { - $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] }; - $self->{format} = sub { - if (!$self->{moves}) { + if ($_[0] < 0) { + "TIMEOUT"; + } elsif (!$self->{moves}) { util::format_time $_[0] - $low; } else { my $time = int (($_[0] - 1) % $interval + 1); - sprintf "%s/%d [%d]", + sprintf "%s/%d =%d", util::format_time $time, $self->{moves}, - $time / ($self->{moves} || 1); - + $self->{moves} > 1 + ? $time / $self->{moves} + : $interval; } }; } else { # none, or unknown - $self->{set} = sub { }; - $self->{format} = sub { "---" } + $self->{format} = sub { "-" } } } sub refresh { my ($self, $timestamp) = @_; my $timer = $self->{time} + $self->{start} - $timestamp; - + # we round the timer value slightly... the protocol isn't exact anyways, # and this gives smoother timers ;) - my @format = $self->{format}->(int ($timer + 0.4)); - $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4))); + my $timer2 = int $timer + 0.4; + + $self->set_text ($self->{format}->($timer2)); $timer - int $timer; } sub set_time { - my ($self, $time) = @_; + my ($self, $start, $time, $moves) = @_; - # we ignore requests to re-set the time of a running clock. - # this is the easiest way to ensure that commentary etc. - # doesn't re-set the clock. yes, this is frickle design, - # but I think the protocol is to blame here, which gives - # very little time information. (cgoban2 also has had quite - # a lot of small time update problems...) - unless ($self->{timeout}) { - $self->{set}->($time->[0], $time->[1]); + $self->{time} = $time; + $self->{moves} = $moves; + + if ($start) { + $self->{start} = $start; + $self->start; + } else { + $self->stop; $self->refresh ($self->{start}); } } sub start { - my ($self, $when) = @_; + my ($self) = @_; $self->stop; - $self->{start} = $when; - my $timeout; $timeout = sub { my $next = $self->refresh (Time::HiRes::time) * 1000; $next += 1000 if $next < 0; @@ -119,50 +131,66 @@ remove Glib::Source delete $self->{timeout} if $self->{timeout}; } -sub destroy { - my ($self) = @_; - $self->stop; - $self->SUPER::destroy; -} +### USER PANEL ############################################################## package game::userpanel; -use base gtk::widget; +use KGS::Constants; -sub new { - my $class = shift; - my $self = $class->SUPER::new(@_); +use Glib::Object::Subclass + Gtk2::Frame, + properties => [ + Glib::ParamSpec->IV ("colour", "colour", "User Colour", + COLOUR_BLACK, COLOUR_WHITE, COLOUR_BLACK, + [qw(construct-only readable writable)]), + ]; - $self->{widget} = new Gtk2::HBox; +sub INIT_INSTANCE { + my ($self) = @_; - $self->{widget}->add (my $vbox = new Gtk2::VBox); + $self->add ($self->{window} = my $window = new Gtk2::EventBox); # for bg - $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); - $vbox->add ($self->{info} = new Gtk2::Label ""); - $vbox->add (($self->{clock} = new game::goclock)->widget); + $window->add (my $vbox = new Gtk2::VBox); + + $vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0); + $vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0); + $vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0); $vbox->add ($self->{imagebox} = new Gtk2::VBox); $self; } +sub SET_PROPERTY { + my ($self, $pspec, $value) = @_; + + $self->{$pspec->get_name} = $value; + + $self->set_name ("userpanel-$self->{colour}"); +} + sub configure { - my ($self, $user, $rules) = @_; + my ($self, $app, $user, $rules) = @_; if ($self->{name}->get_text ne $user->as_string) { $self->{name}->set_text ($user->as_string); $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; - $self->{imagebox}->add (gtk::image_from_data undef); + unless ($::config{suppress_userpic}) { + $self->{imagebox}->add (gtk::image_from_data undef); + } $self->{imagebox}->show_all; if ($user->has_pic) { # the big picture... - appwin::userpic ($user->{name}, sub { + $app->userpic ($user->{name}, sub { return unless $self->{imagebox}; + if ($_[0]) { $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; - $self->{imagebox}->add (gtk::image_from_data $_[0]); + unless ($::config{suppress_userpic}) { + $self->{imagebox}->add (gtk::image_from_data $_[0]); + } $self->{imagebox}->show_all; } }); @@ -172,513 +200,525 @@ $self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); } -sub set_state { - my ($self, $captures, $timer, $when) = @_; - - $self->{clock}->stop unless $when; - $self->{clock}->set_time ($timer); - $self->{clock}->start ($when) if $when; +sub set_captures { + my ($self, $captures) = @_; $self->{info}->set_text ("$captures pris."); } +sub set_timer { + my ($self, $start, $time, $moves) = @_; + + $self->{clock}->set_time ($start, $time, $moves); +} + +### GAME WINDOW ############################################################# + package game; +use Scalar::Util qw(weaken); + use KGS::Constants; use KGS::Game::Board; -use base KGS::Listener::Game; +use Gtk2::GoBoard; +use Gtk2::GoBoard::Constants; + use base KGS::Game; +use base KGS::Listener::Game; -use base gtk::widget; +use Glib::Object::Subclass + Gtk2::Window; use POSIX qw(ceil); sub new { - my $self = shift; - $self = $self->SUPER::new(@_); + my ($self, %arg) = @_; - $self->listen($self->{conn}); + $self = $self->Glib::Object::new; + $self->{$_} = delete $arg{$_} for keys %arg; - $self->{window} = new Gtk2::Window 'toplevel'; - gtk::state $self->{window}, "game::window", undef, window_size => [600, 500]; + gtk::state $self, "game::window", undef, window_size => [620, 460]; + $self->set (allow_shrink => 1); - $self->{window}->signal_connect(delete_event => sub { - $self->part; - $self->destroy; - 1; - }); + $self->signal_connect (destroy => sub { + $self->unlisten; + delete $self->{app}{game}{$self->{channel}}; + %{$_[0]} = (); + });#d# - $self->{window}->add($self->{hpane} = new Gtk2::HPaned); - gtk::state $self->{hpane}, "game::hpane", undef, position => 500; + $self->add (my $hpane = new Gtk2::HPaned); + gtk::state $hpane, "game::hpane", undef, position => 420; # LEFT PANE - $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 0); + $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0); - $self->{boardbox} = new Gtk2::VBox; - - $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); - - # challenge + $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); - $self->{challenge} = new challenge channel => $self->{channel}; - # board box (aspect/canvas) - $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); - - { - $frame->add(my $vbox = new Gtk2::VBox); - $vbox->add($self->{title} = new Gtk2::Label $title); + # RIGHT PANE - $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 1, 0; + $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1); + $hpane->set (position_set => 1); - $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj}); - $scale->set_draw_value (0); - $scale->set_digits (0); + $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0); - $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); - } + { + $frame->add (my $vbox = new Gtk2::VBox); + $vbox->add ($self->{title} = new Gtk2::Label "-"); - $self->{boardbox}->pack_start((my $aspect_frame = new Gtk2::AspectFrame "", 0.5, 0.5, 1, 0), 1, 1, 0); - $aspect_frame->set (border_width => 0, shadow_type => 'none', label_xalign => 0.5); - $self->{board_label} = $aspect_frame->get_label_widget; + $vbox->add (my $hbox = new Gtk2::HBox); - $aspect_frame->add($self->{canvas} = new Gtk2::DrawingArea); - $self->{canvas}->double_buffered (0);# if $::config->{conserve_memory}; + $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0); - $self->{canvas}->signal_connect(configure_event => \&configure_event, $self); - $self->{canvas}->signal_connect(expose_event => \&expose_event, $self); + $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0; - # RIGHT PANE + $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); + $scale->set_draw_value (0); + $scale->set_digits (0); - $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 1, 1); - $self->{hpane}->set(position_set => 1); - gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80; + $self->{moveadj}->signal_connect (value_changed => sub { + $self->{showmove} = int $self->{moveadj}->get_value; + $self->update_board; + }); + } - $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow); - $sw->set_policy("automatic", "always"); + $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); - $sw->add(($self->{userlist} = new userlist)->widget); + $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) + for COLOUR_WHITE, COLOUR_BLACK; - $self->{vpane}->add(my $vbox = new Gtk2::VBox); + $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0); - $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); - $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget); - $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget); + $buttonbox->add ($self->{button_pass} = + Gtk2::Button->Glib::Object::new (label => "Pass", visible => 0)); + $self->{button_pass}->signal_connect (clicked => sub { + $self->{board_click}->(255, 255) if $self->{board_click}; + }); + eval { $self->{button_pass}->set (no_show_all => 1) }; # workaround for gtk+-2.2 + $buttonbox->add ($self->{button_undo} = + Gtk2::Button->Glib::Object::new (label => "Undo", visible => 0)); + $self->{button_undo}->signal_connect (clicked => sub { + $self->send (req_undo => channel => $self->{channel}); + }); + eval { $self->{button_undo}->set (no_show_all => 1) }; # workaround for gtk+-2.2 + $buttonbox->add ($self->{button_resign} = + Gtk2::Button->Glib::Object::new (label => "Resign", visible => 0)); + $self->{button_resign}->signal_connect (clicked => sub { + $self->send (resign_game => channel => $self->{channel}, player => $self->{colour}); + }); + eval { $self->{button_resign}->set (no_show_all => 1) }; # workaround for gtk+-2.2 - $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0); + $vbox->pack_start (($self->{chat} = new chat), 1, 1, 0); - $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); - $self->{entry}->signal_connect(activate => sub { - my $text = $self->{entry}->get_text; - $self->say($text) if $text =~ /\S/; - $self->{entry}->set_text(""); - }); + $self->set_channel ($self->{channel}); + + $self->show_all; - $self->event_update_game; $self; } -sub event_update_users { - my ($self, $add, $update, $remove) = @_; +sub set_channel { + my ($self, $channel) = @_; - $self->{userlist}->update ($add, $update, $remove); + $self->{channel} = $channel; - my %important; - $important{$self->{user1}{name}}++; - $important{$self->{user2}{name}}++; - $important{$self->{user3}{name}}++; + if ($self->{channel} > 0) { + $self->listen ($self->{conn}); - if (my @users = grep $important{$_->{name}}, @$add) { - $self->{text}->append_text ("\n
Joins:
"); - $self->{text}->append_text (" " . $_->as_string . "") for @users; - } - if (my @users = grep $important{$_->{name}}, @$remove) { - $self->{text}->append_text ("\n
Parts:
"); - $self->{text}->append_text (" " . $_->as_string . "") for @users; + $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1); + $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1); + + $self->signal_connect (delete_event => sub { $self->part; 1 }); + $self->{chat}->signal_connect (command => sub { + my ($chat, $cmd, $arg) = @_; + if ($cmd eq "rsave") { + Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d# + } else { + $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self); + } + }); } - } +### JOIN/LEAVE ############################################################## + sub join { my ($self) = @_; return if $self->{joined}; $self->SUPER::join; - - $self->{window}->show_all; } sub part { my ($self) = @_; + $self->hide; $self->SUPER::part; - $self->destroy; } -sub configure_event { - my ($widget, $event, $self) = @_; - delete $self->{stack}; - delete $self->{pixbuf}; - delete $self->{board_shown}; - delete $self->{background}; - $self->repaint_board; - 0; -} - -sub expose_event { - my ($widget, $event, $self) = @_; - - $self->{pixbuf} or return; - - $self->redraw ($event->area->values); +sub event_join { + my ($self) = @_; - 0; + $self->SUPER::event_join (@_); + $self->init_tree; + $self->event_update_game; } -# something Gtk2 fixed -sub INTERP_NEAREST (){ 'nearest' } -sub INTERP_TILES (){ 'tiles' } -sub INTERP_BILINEAR (){ 'bilinear' } -sub INTERP_HYPER (){ 'hyper' } +sub event_part { + my ($self) = @_; -sub new_pixbuf { - my ($w, $h, $alpha, $fill) = @_; + $self->SUPER::event_part; + $self->destroy; +} - my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; - $pixbuf->fill ($fill) if defined $fill; +sub event_quit { + my ($self) = @_; - $pixbuf; + $self->SUPER::event_quit; + $self->destroy; } -sub scale_pixbuf { - my ($src, $w, $h, $mode) = @_; - - my $dst = new_pixbuf $w, $h, 1; +### USERS ################################################################### - $src->scale( - $dst, 0, 0, $w, $h, 0, 0, - $w / $src->get_width, $h / $src->get_height, - $mode, - ); +sub draw_users { + my ($self, $inlay) = @_; - $dst; + for (sort keys %{$self->{users}}) { + $inlay->append_text (" " . $self->{users}{$_}->as_string . ""); + } } -# create a stack of stones -sub create_stack { - my ($self, $mark, $size, $rand) = @_; - - my $shadow = $size * 0.05; - - my $c = \$self->{stack}{$mark}; - unless ($$c) { - for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) { - my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000; - - # zeroeth the shadow - if ($mark & (MARK_B | MARK_W)) { - # the -0.5's are a mystery to me - $::shadow_img->composite ( - $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5, - $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192 - ); - } +sub event_update_users { + my ($self, $add, $update, $remove) = @_; - # first the big stones (handicap stones could be different) - for ([MARK_B, $mark & MARK_MOVE ? 255 : 255], - [MARK_W, $mark & MARK_MOVE ? 255 : 255], - [MARK_GRAY_B, 128], - [MARK_GRAY_W, 128]) { - my ($mask, $alpha) = @$_; - if ($mark & $mask) { - $stone->composite ( - $base, 0, 0, $size, $size, 0, 0, - $size / $stone->get_width, $size / $stone->get_height, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha - ); - } - } +# $self->{userlist}->update ($add, $update, $remove); - # then the small stones - for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]], - [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) { - my ($mask, $img) = @$_; - if ($mark & $mask) { - $img->composite ( - $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2, - $size / $img->get_width / 2, $size / $img->get_height / 2, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224 - ); - } - } + $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy + for @$remove; - # and lastly any markers - my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B)); + $self->{users_inlay}->refresh; - for ([MARK_CIRCLE, $::circle_img[$dark_bg]], - [MARK_TRIANGLE, $::triangle_img[$dark_bg]], - [MARK_SQUARE, $::square_img[$dark_bg]]) { - my ($mask, $img) = @$_; - if ($mark & $mask) { - $img->composite ( - $base, 0, 0, $size, $size, 0, 0, - $size / $img->get_width, $size / $img->get_height, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192 - ); - } - } + my %important; + $important{$self->{black}{name}}++; + $important{$self->{white}{name}}++; + $important{$self->{owner}{name}}++; - push @$$c, $base; - } + if (my @users = grep $important{$_->{name}}, @$add) { + $self->{chat}->append_text ("\nJoins:"); + $self->{chat}->append_text (" " . $_->as_string . "") for @users; + } + if (my @users = grep $important{$_->{name}}, @$remove) { + $self->{chat}->append_text ("\nParts:"); + $self->{chat}->append_text (" " . $_->as_string . "") for @users; } - - $$c->[$rand % @$$c]; } -sub pixbuf_text { - my ($pixbuf, $colour, $x, $y, $height, $text) = @_; +### GAME INFO ############################################################### - my @c = grep $_, - map $::font[$colour][$::fontmap{$_}], - split //, $text; +sub draw_setup { + my ($self, $inlay) = @_; - if (@c) { - my $spacing = $height * 0.1; - my $s = $height / List::Util::max map $_->get_height, @c; - my $W = List::Util::sum map $_->get_width, @c; + return unless $self->{joined}; - $x -= ($W * $s + $spacing * (@c - 1)) * 0.5; - $y -= $height * 0.5; + my $rules = $self->{rules}; - for (@c) { - my $w = $_->get_width * $s; - # +2 == don't fight the rounding - $_->composite ($pixbuf, - $x, $y, $w+2, $height+2, $x, $y, $s, $s, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255); + my $text = ""; - $x += $w + $spacing; - } - } -} + $text .= "\nTeacher: " . (util::toxml $self->{teacher}) . "" + if $self->{teacher}; -sub pixbuf_rect { - my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_; - # we fake lines by... a horrible method :/ - my $colour_pb = new_pixbuf 1, 1, 0, $colour; - $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1, - INTERP_NEAREST, $alpha); -} + $text .= "\nOwner: " . (util::toxml $self->{owner}->as_string) . "" + if $self->{owner}->is_valid; -sub repaint_board { - my ($self) = @_; - my $canvas = $self->{canvas}; + if ($self->is_inprogress) { + $text .= "\nPlayers: " . (util::toxml $self->{white}->as_string) . "" + . " vs. " . (util::toxml $self->{black}->as_string) . ""; + } + $text .= "\nType: " . util::toxml $gametype{$self->type}; - return unless $self->{board}; + $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; - my ($w, $h) = ($canvas->allocation->values)[2,3]; + $text .= "\nTime: "; - die "FATAL: board aspect ratio != 1" unless $w == $h; + if ($rules->{timesys} == TIMESYS_NONE) { + $text .= "UNLIMITED"; + } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { + $text .= util::format_time $rules->{time}; + $text .= " ABS"; + } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { + $text .= util::format_time $rules->{time}; + $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; + } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { + $text .= util::format_time $rules->{time}; + $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; + } - my $s = $w; + $text .= "\nFlags:"; + $text .= " private" if $self->is_private; + $text .= " started" if $self->is_inprogress; + $text .= " adjourned" if $self->is_adjourned; + $text .= " scored" if $self->is_scored; + $text .= " saved" if $self->is_saved; + + if ($self->is_inprogress) { + $text .= "\nHandicap: " . $self->{handicap}; + $text .= "\nKomi: " . $self->{komi}; + $text .= "\nSize: " . $self->size_string; + } - return unless $s >= 200; - - my $size = $self->{size}; + if ($self->is_scored) { + $text .= "\nResult: " . $self->score_string; + } - # we leave enough space for the shadows.. I like smaller stones, and we - # do no need to do the nifty recursive screen updates that goban2 does - my $border = int ($s / ($size + 3) * 0.5); - my $s2 = $s - $border * 2; - my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0); - my $ofs = int ($edge / 2); + $inlay->append_text ("$text"); - my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size; +} - my $pixbuf; +sub event_update_game { + my ($self) = @_; - my $oldboard; + $self->SUPER::event_update_game; - if ($self->{background}) { - if ($oldboard = $self->{board_shown}) { - $pixbuf = $self->{pixbuf}; - } else { - $pixbuf = $self->{background}->copy; - $self->{canvas}->queue_draw_area (0, 0, $s, $s); - } - } else { - $self->{canvas}->queue_draw_area (0, 0, $s, $s); + return unless $self->{joined}; - my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); + $self->{colour} = $self->player_colour ($self->{conn}{name}); + + $self->{user}[COLOUR_BLACK] = $self->{black}; + $self->{user}[COLOUR_WHITE] = $self->{white}; - if ($s < $bw && $s < $bh) { - $pixbuf = new_pixbuf $s, $s, 0; - $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0); - } else { - $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES; + # show board + if ($self->is_inprogress) { + if (!$self->{board}) { + $self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size}); + $self->{board}->signal_connect (button_release => sub { + return unless $self->{cur_board}; + if ($_[1] == 1) { + $self->{board_click}->($_[2], $_[3]) if $self->{board_click}; + } + }); + $self->{board}->show_all; } + if (my $ch = delete $self->{challenge}) { + $_->{inlay}->destroy for values %$ch; + } + $self->update_cursor; + } - my $linew = int ($s / 40 / $size); - - # ornamental border... we have time to waste :/ - pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255; - pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255; - pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255; - pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255; - - for my $i (1 .. $size) { - pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192; - pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192; - - # 38 max, but we allow a bit more - my $label = (qw(- A B C D E F G H J K L M N O P Q R S T U V W X Y Z - AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ))[$i]; - - pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label; - pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label; - pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1; - pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1; + my $title = defined $self->{channel} + ? $self->owner->as_string . " " . $self->opponent_string + : "Game Window"; + $self->set_title ("KGS Game $title"); + $self->{title}->set_text ($title); # title gets redrawn wrongly + + $self->{rules_inlay}->refresh; + + if (exists $self->{teacher}) { + $self->{teacher_inlay} ||= $self->{chat}->new_inlay; + $self->{teacher_inlay}->clear; + $self->{teacher_inlay}->append_text ("\n
Teacher:
" + . (util::toxml $self->{teacher}) . ""); + } elsif ($self->{teacher_inlay}) { + (delete $self->{teacher_inlay})->clear; + } - $a++; - $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... - } + $self->update_cursor; +} - # hoshi points - my $hoshi = sub { - my ($x, $y) = @_; - my $hs = int ($edge / 4) | 1; - $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2; +sub event_update_rules { + my ($self, $rules) = @_; - # we use the shadow mask... not perfect, but I want to finish this - $::shadow_img->composite ($pixbuf, - $x, $y, $hs + 1, $hs + 1, $x, $y, - $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, - $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255); - }; + $self->{rules} = $rules; - my $h1 = $size < 10 ? 3 : 4; # corner / edge offset - $hoshi->($h1, $h1); - $hoshi->($size - $h1 + 1, $h1); - $hoshi->($h1, $size - $h1 + 1); - $hoshi->($size - $h1 + 1, $size - $h1 + 1); - - if ($size % 2) { # on odd boards, also the remaining 5 - my $h2 = ($size + 1) / 2; - if ($size > 10) { - $hoshi->($h1, $h2); - $hoshi->($size - $h1 + 1, $h2); - $hoshi->($h2, $size - $h1 + 1); - $hoshi->($h2, $h1); - } - # the tengen - $hoshi->($h2, $h2); - } + if ($self->{user}) { + # todo. gets drawn wrongly - unless ($::config->{conserve_memory} > 1) { - $self->{background} = $pixbuf; - $pixbuf = $pixbuf->copy; - } + $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules) + for COLOUR_BLACK, COLOUR_WHITE; } - $self->{pixbuf} = $pixbuf; + sound::play 3, "gamestart"; + $self->{rules_inlay}->refresh; +} - for my $x (1 .. $size) { - for my $y (1 .. $size) { - my $rand = ($x ^ $y ^ 0x5555); +### BOARD DISPLAY ########################################################### - my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs); +sub update_timers { + my ($self, $timers) = @_; - if ($::config->{randomize}) { - $dx += ($rand % 7) - 3; - $dy += ($rand / 3 % 7) - 3; - } + my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher}; - my $shadow = $edge * 0.05; - my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow]; - - my $mark = $self->{board}{board}[$x-1][$y-1]; - my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0; + for my $colour (COLOUR_BLACK, COLOUR_WHITE) { + my $t = $timers->[$colour]; + $self->{userpanel}[$colour]->set_timer ( + $running && $colour == $self->{whosemove} && $t->[0], + $t->[1] || $self->{rules}{time} + + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI + && $self->{rules}{interval} * $self->{rules}{count}), + $t->[2]); + } +} - if ($oldboard) { - next if $old == $mark; # no change +sub inject_set_gametime { + my ($self, $msg) = @_; - $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy); - } + $self->{timers} = [ + [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}], + [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}], + ]; - if ($mark) { - my $pb = $self->create_stack($mark, $edge, $rand); + $self->update_timers ($self->{timers}) + if $self->{showmove} == @{$self->{path}}; +} - $pb->composite ($pixbuf, @$area, - $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255); +sub update_cursor { + my ($self) = @_; - # labels are handled here because they are quite rare - if ($mark & MARK_LABEL) { - my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1; - - if ($white) { - pixbuf_text $pixbuf, 0, - $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7, - $self->{board}{label}[$x-1][$y-1]; - } - pixbuf_text $pixbuf, $white, - $k[$x], $k[$y], $ofs * 0.7, - $self->{board}{label}[$x-1][$y-1]; - } - } + return unless $self->{cur_board}; - if ($oldboard) { - $self->redraw (@$area); - } + if ($self->{rules}{ruleset} == RULESET_JAPANESE) { + if ($self->{curnode}{move} == 0) { + $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK; + } else { + $self->{whosemove} = 1 - $self->{cur_board}{last}; + } + } else { + # Chinese, Aga, NZ all have manual placement + if ($self->{curnode}{move} < $self->{handicap}) { + $self->{whosemove} = COLOUR_BLACK; + } elsif ($self->{curnode}{move} == $self->{handicap}) { + $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK; + } else { + $self->{whosemove} = 1 - $self->{cur_board}{last}; } } - $self->{board_shown} = Storable::dclone $self->{board}; -} + my $running = $self->{showmove} == @{$self->{path}} && $self->is_active; -sub redraw { - my ($self, $x, $y, $w, $h) = @_; + delete $self->{board_click}; + + if ($self->{teacher} eq $self->{app}{conn}) { + #TODO# # teaching mode not implemented + $self->{button_pass}->set (label => "Pass", sensitive => 1); + $self->{button_pass}->show; + $self->{button_undo}->hide; + $self->{button_resign}->hide; + $self->{board}->set (cursor => undef); + + } elsif ($running && $self->{colour} != COLOUR_NONE) { + # during game + $self->{button_undo}->show; + $self->{button_resign}->show; + + if ($self->{cur_board}{score}) { + # during scoring + $self->{button_pass}->set (label => "Done", sensitive => 1); + $self->{button_pass}->show; + $self->{board}->set (cursor => sub { + $_[0] & (MARK_B | MARK_W) + ? $_[0] ^ MARK_GRAYED + : $_[0]; + }); + $self->{board_click} = sub { + if ($_[0] == 255) { + $self->{button_pass}->sensitive (0); + $self->done; + } else { + $self->send (mark_dead => + channel => $self->{channel}, + x => $_[0], + y => $_[1], + dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED), + ); + } + }; - if ($self->{pixbuf}) { - $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf}, - $x, $y, $x, $y, $w, $h, - "normal", 0, 0); - $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0, - $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE; + } elsif ($self->{colour} == $self->{whosemove}) { + # normal move + $self->{button_pass}->set (label => "Pass", sensitive => 1); + $self->{button_pass}->show; + $self->{board}->set (cursor => sub { + $self->{cur_board} + && $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2], + $self->{rules}{ruleset} == RULESET_NEW_ZEALAND) + ? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B) + : $_[0]; + }); + $self->{board_click} = sub { + return unless + $self->{cur_board}->is_valid_move ($self->{colour}, $_[0], $_[1], + $self->{rules}{ruleset} == RULESET_NEW_ZEALAND); + $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); + $self->{board}->set (cursor => undef); + delete $self->{board_click}; + $self->{button_pass}->sensitive (0); + }; + } else { + $self->{button_pass}->set (label => "Pass", sensitive => 0); + $self->{button_pass}->show; + $self->{board}->set (cursor => undef); + } + } else { + $self->{button_undo}->hide; + $self->{button_resign}->hide; + $self->{button_pass}->hide; + $self->{board}->set (cursor => undef); + #TODO# # implement coordinate-grabbing } } sub update_board { my ($self) = @_; + return unless $self->{path}; - my $move = int $self->{moveadj}->get_value; + $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1)); - my $running = $move == @{$self->{path}}; + $self->{cur_board} = new KGS::Game::Board $self->{size}; + $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]); - $self->{board_label}->set_text ("Move " . ($move - 1)); + $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) + for COLOUR_WHITE, COLOUR_BLACK; - $self->{board} = new KGS::Game::Board $self->{size}; - $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]); + $self->{board}->set_board ($self->{cur_board}); + + if ($self->{cur_board}{score}) { + $self->{score_inlay} ||= $self->{chat}->new_inlay; + $self->{score_inlay}->clear; + $self->{score_inlay}->append_text ("\n
Scoring
" + . "\n" + . "White: $self->{cur_board}{score}[COLOUR_WHITE], " + . "Black: $self->{cur_board}{score}[COLOUR_BLACK]" + . ""); + } elsif ($self->{score_inlay}) { + (delete $self->{score_inlay})->clear; + } - for my $colour (WHITE, BLACK) { - $self->{userpanel}[$colour]->set_state ( - $self->{board}{captures}[$colour], - $self->{board}{timer}[$colour], - ($running && $self->{lastmove_colour} == !$colour) - ? $self->{lastmove_time} : 0 - ); + $self->update_cursor; + + if ($self->{showmove} == @{$self->{path}}) { + $self->{timers} = [ + [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}], + [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}], + ]; + $self->update_timers ($self->{timers}); + } else { + $self->update_timers ([ + [0, @{$self->{cur_board}{timer}[0]}], + [0, @{$self->{cur_board}{timer}[1]}], + ]); } - $self->repaint_board; } sub event_update_tree { my ($self) = @_; + (delete $self->{undo_inlay})->clear + if $self->{undo_inlay}; + $self->{path} = $self->get_path; if ($self->{moveadj}) { @@ -698,7 +738,7 @@ sub event_update_comments { my ($self, $node, $comment, $newnode) = @_; - $self->SUPER::event_update_comments($node, $comment, $newnode); + $self->SUPER::event_update_comments ($node, $comment, $newnode); my $text; @@ -730,144 +770,334 @@ $text .= $_; } - $self->{text}->append_text ($text); + $self->{chat}->append_text ($text); } -sub event_join { - my ($self) = @_; - $self->SUPER::event_join; +sub event_move { + my ($self, $pass) = @_; + + sound::play 1, $pass ? "pass" : "move"; } -sub event_part { - my ($self) = @_; - $self->SUPER::event_part; - $self->destroy; +### GAMEPLAY EVENTS ######################################################### + +sub event_resign_game { + my ($self, $player) = @_; + + sound::play 3, "resign"; + $self->{chat}->append_text ("\n
Resign
" + . "\n" + . (util::toxml $self->{user}[$player]->as_string) + . " resigned." + . "\n" + . (util::toxml $self->{user}[1 - $player]->as_string) + . " wins the game." + . "
"); } -sub event_move { - my ($self, $pass) = @_; - sound::play 1, $pass ? "pass" : "move"; +sub event_out_of_time { + my ($self, $player) = @_; + + sound::play 3, "timewin"; + $self->{chat}->append_text ("\n
Out of Time
" + . "\n" + . (util::toxml $self->{user}[$msg->{player}]->as_string) + . " ran out of time and lost." + . "\n" + . (util::toxml $self->{user}[1 - $msg->{player}]->as_string) + . " wins the game." + . "
"); } -sub event_update_game { +sub event_owner_left { my ($self) = @_; - $self->SUPER::event_update_game; - my $title = $self->{channel} ? $self->owner->as_string . " " . $self->opponent_string : "Game Window"; - $self->{window}->set_title("KGS Game $title"); - $self->{title}->set_text ($title); + $self->{chat}->append_text ("\n
Owner left
" + . "\nThe owner of this game left.
"); +} - $self->{user}[BLACK] = $self->{user1}; - $self->{user}[WHITE] = $self->{user2}; +sub event_teacher_left { + my ($self) = @_; - # show board - - $self->{left}->remove ($_) for $self->{left}->get_children; - if ($self->is_valid) { - $self->{left}->add ($self->{boardbox}); - (delete $self->{challenge})->destroy if $self->{challenge}; - } else { - $self->{left}->add ($self->{challenge}->widget); - } - $self->{left}->show_all; + $self->{chat}->append_text ("\n
Teacher left
" + . "\nThe teacher left the game.
"); +} - # view text - - my @ga; - $ga[0] = "\nType: " . (util::toxml $gametype{$self->type}) - . " (" . (util::toxml $gameopt{$self->option}) . ")"; - $ga[1] = "\nFlags:"; - $ga[1] .= " valid" if $self->is_valid; - $ga[1] .= " adjourned" if $self->is_adjourned; - $ga[1] .= " scored" if $self->is_scored; - $ga[1] .= " saved" if $self->is_saved; - - $ga[2] = "\nOwner: " . (util::toxml $self->{user3}->as_string) . "" if $self->{user3}->is_valid; - - $ga[3] = "\nPlayers: " . (util::toxml $self->{user2}->as_string) . "" - . " vs. " . (util::toxml $self->{user1}->as_string) . "" - if $self->is_valid; - - if ($self->is_valid) { - $ga[4] = "\nHandicap: " . $self->{handicap}; - $ga[5] = "\nKomi: " . $self->{komi}; - $ga[6] = "\nSize: " . $self->size_string; - } +sub event_done { + my ($self) = @_; - if ($self->is_scored) { - $ga[7] = "\nResult: " . $self->score_string; + if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) { + sound::play 2, "info" unless $inlay->{count}; + $self->{chat}->append_text ("\n
Press Done
" + . "\nYour opponent pressed done. Now it's up to you."); } - - $text = "\n
Game Update
"; - for (0..7) { - if ($self->{gatext}[$_] ne $ga[$_]) { - $text .= $ga[$_]; - } + if ($self->{doneid} & 0x80000000) { + sound::play 2, "info" unless $inlay->{count}; + $self->{chat}->append_text ("\n
Press Done Again
" + . "\nThe board has changed."); } - $text .= "
"; - $self->{gatext} = \@ga; - - $self->{text}->append_text ($text); + $self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]); + + $self->{chat}->set_end; } -sub event_update_rules { - my ($self, $rules) = @_; +sub inject_final_result { + my ($self, $msg) = @_; - $self->{userpanel}[$_]->configure ($self->{user}[$_], $rules) - for BLACK, WHITE; + $self->{chat}->append_text ("\n
Game Over
" + . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) + . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) + . "
" + ); +} - sound::play 3, "gamestart"; +sub inject_req_undo { + my ($self, $msg) = @_; - my $text = "\n
Game Rules
"; + my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay; + return if $inlay->{ignore}; - $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; + sound::play 2, "warning" unless $inlay->{count}; + $inlay->{count}++; - $text .= "\nTime: "; + $inlay->clear; + $inlay->append_text ("\nUndo requested ($inlay->{count} times)\n"); + $inlay->append_button ("Grant", sub { + (delete $self->{undo_inlay})->clear; + $self->send (grant_undo => channel => $self->{channel}); + }); + $inlay->append_button ("Ignore", sub { + $inlay->clear; + $inlay->{ignore} = 1; + # but leave inlay, so further undo requests get counted + }); - if ($rules->{timesys} == TIMESYS_NONE) { - $text .= "UNLIMITED"; - } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { - $text .= util::format_time $rules->{time}; - $text .= " ABS"; - } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { - $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count}; - $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; - } elsif ($rules->{timesys} == TIMESYS_CANADIAN) { - $text .= util::format_time $rules->{time}; - $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; - } - - $self->{text}->append_text ("$text"); + $self->{chat}->set_end; } -sub inject_resign_game { +sub inject_new_game { my ($self, $msg) = @_; - sound::play 3, "resign"; + if ($msg->{cid} != $self->{cid}) { + $self->part; + warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d# + } - $self->{text}->append_text ("\n
Resign
" - . "\n" - . (util::toxml $self->{user}[$msg->{player}]->as_string) - . " resigned.
"); + $self->{chat}->append_text ("\n
Game successfully created on server.
"); + delete $self->{cid}; } -sub inject_final_result { - my ($self, $msg) = @_; +### CHALLENGE HANDLING ###################################################### - $self->{text}->append_text ("\n
Game Over
" - . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) - . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) - . "
" - ); +sub draw_challenge { + my ($self, $id) = @_; + + my $info = $self->{challenge}{$id}; + my $inlay = $info->{inlay}; + my $rules = $info->{rules}; + + my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;; + my $opponent = $as_black ? $info->{white} : $info->{black}; + + my ($size, $time, $interval, $count, $type); + + if (!$self->{channel}) { + $inlay->append_text ("\nNotes: "); + $inlay->append_widget (gtk::textentry \$info->{notes}, 20, ""); + $inlay->append_text ("\nGlobal Offer: "); + $inlay->append_optionmenu (\$info->{flags}, + 0 => "No", + 2 => "Yes", + ); + } else { + $inlay->append_text ("\nNotes: " . util::toxml $info->{notes}); + } + + $inlay->append_text ("\nType: "); + $type = $inlay->append_optionmenu ( + \$info->{gametype}, + GAMETYPE_DEMONSTRATION , "Demonstration (not yet)", + GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)", + GAMETYPE_TEACHING , "Teaching (not yet)", + GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)", + GAMETYPE_SIMUL , "Simul (not yet!)", + GAMETYPE_FREE , "Free", + GAMETYPE_RATED , "Rated", + sub { + $size->set_history (2) if $_[0] eq GAMETYPE_RATED; + }, + ); + + if ($self->{channel}) { + $inlay->append_text ("\nMy Colour: "); + $inlay->append_optionmenu ( + \$as_black, + 0 => "White", + 1 => "Black", + sub { + if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) { + ($info->{black}, $info->{white}) = ($info->{white}, $info->{black}); + } + } + ); + } + + $inlay->append_text ("\nRuleset: "); + $inlay->append_optionmenu ( + \$info->{rules}{ruleset}, + RULESET_JAPANESE , "Japanese", + RULESET_CHINESE , "Chinese", + RULESET_AGA , "AGA", + RULESET_NEW_ZEALAND, "New Zealand", + ); + + $inlay->append_text ("\nSize: "); + $size = $inlay->append_optionmenu ( + \$info->{rules}{size}, + (9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38), + sub { + $type->set_history (5) # reset to free + if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED; + }, + ); + + if ($self->{channel}) { + $inlay->append_text ("\nHandicap: "); + $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); + + $inlay->append_text ("\nKomi: "); + $inlay->append_widget (gtk::numentry \$info->{rules}{komi}, 5); + } + + $inlay->append_text ("\nTimesys: "); + $inlay->append_optionmenu ( + \$info->{rules}{timesys}, + &TIMESYS_NONE => "None", + &TIMESYS_ABSOLUTE => "Absolute", + &TIMESYS_BYO_YOMI => "Byo Yomi", + &TIMESYS_CANADIAN => "Canadian", + sub { + my ($new) = @_; + + if ($new eq TIMESYS_NONE) { + $time->hide; + $interval->hide; + $count->hide; + } else { + $time->show; + $time->set_text ($self->{app}{defaults}{time}); + if ($new eq TIMESYS_ABSOLUTE) { + $interval->hide; + $count->hide; + } else { + $interval->show; + $count->show; + if ($new eq TIMESYS_BYO_YOMI) { + $interval->set_text ($self->{app}{defaults}{byo_time}); + $count->set_text ($self->{app}{defaults}{byo_period}); + } elsif ($new eq TIMESYS_CANADIAN) { + $interval->set_text ($self->{app}{defaults}{can_time}); + $count->set_text ($self->{app}{defaults}{can_period}); + } + } + } + } + ); + + $inlay->append_text ("\nMain Time: "); + $time = $inlay->append_widget (gtk::timeentry \$info->{rules}{time}, 5); + $inlay->append_text ("\nInterval: "); + $interval = $inlay->append_widget (gtk::timeentry \$info->{rules}{interval}, 5); + $inlay->append_text ("\nPeriods/Stones: "); + $count = $inlay->append_widget (gtk::numentry \$info->{rules}{count}, 5); + + $inlay->append_text ("\n"); + + if (!$self->{channel}) { + $inlay->append_button ("Create Challenge", sub { + $inlay->clear; + $self->{cid} = $self->{conn}->alloc_clientid; + $self->send (new_game => + channel => delete $self->{roomid}, + gametype => $info->{gametype}, + cid => $self->{cid}, + flags => $info->{flags}, + rules => $info->{rules}, + notes => $info->{notes}, + ); + }); + } else { + $inlay->append_button ("OK", sub { + $inlay->clear; + $self->send (challenge => + channel => $self->{channel}, + black => $info->{black}, + white => $info->{white}, + gametype => $info->{gametype}, + cid => $info->{cid}, + rules => $info->{rules}, + ); + }); + if (exists $self->{challenge}{""}) { + $inlay->append_button ("Reject", sub { + $inlay->clear; + $self->send (reject_challenge => + channel => $self->{channel}, + name => $opponent->{name}, + gametype => $info->{gametype}, + cid => $info->{cid}, + rules => $info->{rules}, + ); + }); + } + } } -sub destroy { +sub new_game_challenge { my ($self) = @_; - $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy - for BLACK, WHITE; - $self->SUPER::destroy; - delete $appwin::gamelist->{game}{$self->{channel}}; + + my $d = $self->{app}{defaults}; + + $self->{challenge}{""} = { + gametype => $d->{gametype}, + flags => 0, + notes => $d->{stones}, + rules => { + ruleset => $d->{ruleset}, + size => $d->{size}, + timesys => $d->{timesys}, + time => $d->{time}, + interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time}, + count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones}, + }, + + inlay => $self->{chat}->new_inlay, + }; + $self->draw_challenge (""); +} + +sub event_challenge { + my ($self, $info) = @_; + + my $as_black = $info->{black}->{name} eq $self->{conn}{name}; + my $opponent = $as_black ? $info->{white} : $info->{black}; + + my $id = $opponent->{name}; + + sound::play 2, "info"; + + $self->{challenge}{$id} = $info; + $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay ( + exists $self->{challenge}{""} + ? "Challenge from " . $opponent->as_string + : "Challenge to " . $opponent->as_string, + sub { + $self->{challenge}{$id}{inlay} = $_[0]; + $self->draw_challenge ($id); + }, + !exists $self->{challenge}{""} # only open when not offerer + ); } 1;