--- kgsueme/kgsueme/game.pl 2003/05/31 15:08:32 1.7
+++ kgsueme/kgsueme/game.pl 2004/05/21 15:32:02 1.87
@@ -1,453 +1,601 @@
-package game;
+use utf8;
-use KGS::Constants;
-use KGS::Game::Board;
+use Scalar::Util ();
-use base KGS::Listener::Game;
-use base KGS::Game;
+package game::goclock;
-use base gtk::widget;
+# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
+# is completely borked.
-sub new {
- my $self = shift;
- $self = $self->SUPER::new(@_);
+use Time::HiRes ();
- $self->listen($self->{conn});
+use KGS::Constants;
- $self->{window} = new Gtk2::Window 'toplevel';
- my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
- $self->{window}->set_title("KGS Game $title");
- gtk::state $self->{window}, "game::window", undef, window_size => [600, 500];
-
- $self->{window}->signal_connect(delete_event => sub {
- if ($self->{joined}) {
- $self->part;
- } else {
- $self->event_part;
- }
- 1;
- });
+use Glib::Object::Subclass
+ Gtk2::Label;
- $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
- gtk::state $self->{hpane}, "game::hpane", undef, position => 500;
+sub INIT_INSTANCE {
+ my $self = shift;
- $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
+ $self->signal_connect (destroy => sub { $_[0]->stop });
- $vbox->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
+ $self->{set} = sub { };
+ $self->{format} = sub { "???" };
+}
- {
- # grrr...
- $frame->add(my $vbox = new Gtk2::VBox);
- $vbox->add($self->{title} = new Gtk2::Label $title);
+sub configure {
+ my ($self, $timesys, $main, $interval, $count) = @_;
- $self->{moveadj} = new Gtk2::Adjustment 1, 0, 1, 0.01, 0.1, 0;
+ if ($timesys == TIMESYS_ABSOLUTE) {
+ $self->{set} = sub { $self->{time} = $_[0] };
+ $self->{format} = sub { util::format_time $_[0] };
- $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj});
- $scale->set_draw_value (0);
+ } elsif ($timesys == TIMESYS_BYO_YOMI) {
+ my $low = $interval * $count;
- $self->{moveadj}->signal_connect (value_changed => sub {
- return unless $self->{path};
+ $self->{set} = sub { $self->{time} = $_[0] };
- my $move = int (@{$self->{path}} * $_[0]->get_value);
+ $self->{format} = sub {
+ if ($_[0] > $low) {
+ util::format_time $_[0] - $low;
+ } else {
+ sprintf "%s (%d)",
+ util::format_time int (($_[0] - 1) % $interval + 1),
+ ($_[0] - 1) / $interval;
+ }
+ };
- $self->{board} = new KGS::Game::Board $self->{size};
- $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
+ } elsif ($timesys == TIMESYS_CANADIAN) {
+ $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
- $self->redraw ($self->repaint_board);
+ $self->{format} = sub {
+ if (!$self->{moves}) {
+ util::format_time $_[0] - $low;
+ } else {
+ my $time = int (($_[0] - 1) % $interval + 1);
+
+ sprintf "%s/%d =%d",
+ util::format_time $time,
+ $self->{moves},
+ $self->{moves} > 1
+ ? $time / $self->{moves}
+ : $interval;
+ }
+ };
- $self->{text}->set_text(KGS::Listener::Debug::dumpval([$self->{board}{time},$self->{board}{captures}]). $self->{board}{comment});
- });
+ } else {
+ # none, or unknown
+ $self->{set} = sub { };
+ $self->{format} = sub { "---" }
}
-
- $vbox->pack_start(($self->{canvas} = new Gtk2::DrawingArea), 1, 1, 0);
-
- $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
- $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
-
- $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0);
- $self->{hpane}->set(position_set => 1);
- gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
+}
- $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
- $sw->set_policy("automatic", "always");
+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->set_text ($self->{format}->(int ($timer + 0.4)));
+
+ $timer - int $timer;
+}
+
+sub set_time {
+ my ($self, $time) = @_;
+
+ # 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->refresh ($self->{start});
+ }
+}
- $sw->add(($self->{userlist} = new gtk::userlist)->widget);
+sub start {
+ my ($self, $when) = @_;
- $self->{vpane}->add(my $vbox = new Gtk2::VBox);
-
- $vbox->pack_start((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0);
- $sw->set_policy("never", "always");
+ $self->stop;
- $sw->add(($self->{text} = new gtk::text)->widget);
+ $self->{start} = $when;
- $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
- $self->{entry}->signal_connect(activate => sub {
- my $text = $self->{entry}->get_text;
- # add message
- $self->{entry}->set_text("");
- });
+ my $timeout; $timeout = sub {
+ my $next = $self->refresh (Time::HiRes::time) * 1000;
+ $next += 1000 if $next < 0;
+ $self->{timeout} = add Glib::Timeout $next, $timeout;
+ 0;
+ };
- $self;
+ $timeout->();
}
-sub event_update_users {
+sub stop {
my ($self) = @_;
- $self->{userlist}->update($self->{users});
+ remove Glib::Source delete $self->{timeout} if $self->{timeout};
}
-sub join {
+package game::userpanel;
+
+use Glib::Object::Subclass
+ Gtk2::HBox,
+ properties => [
+ Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
+ ];
+
+sub INIT_INSTANCE {
my ($self) = @_;
- $self->SUPER::join;
- $self->{window}->show_all;
+ $self->add (my $vbox = new Gtk2::VBox);
+
+ $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
+ $vbox->add ($self->{info} = new Gtk2::Label "");
+ $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
+
+ $vbox->add ($self->{imagebox} = new Gtk2::VBox);
+
+ $self;
}
-sub part {
- my ($self) = @_;
- $self->SUPER::part;
+sub configure {
+ my ($self, $app, $user, $rules) = @_;
- $self->{window}->hide;
+ 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);
+ $self->{imagebox}->show_all;
+
+ if ($user->has_pic) {
+ # the big picture...
+ $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]);
+ $self->{imagebox}->show_all;
+ }
+ });
+ }
+ }
+
+ $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
}
-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 set_state {
+ my ($self, $captures, $timer, $when) = @_;
+
+ $self->{clock}->stop unless $when;
+ $self->{clock}->set_time ($timer);
+ $self->{clock}->start ($when) if $when;
+
+ $self->{info}->set_text ("$captures pris.");
}
-sub expose_event {
- my ($widget, $event, $self) = @_;
+package game;
- $self->{pixbuf} or return;
+use Scalar::Util qw(weaken);
- my $area = $event->area;
- my ($ox, $oy, $s) = @{$self->{offsets}};
+use KGS::Constants;
+use KGS::Game::Board;
- $self->redraw (
- (new Gtk2::Gdk::Rectangle $area->x - $ox, $area->y - $oy, $area->width, $area->height)
- ->intersect(new Gtk2::Gdk::Rectangle 0, 0, $s, $s)
- );
+use Gtk2::GoBoard;
- 0;
-}
+use Glib::Object::Subclass
+ Gtk2::Window;
-# something Gtk2 fixed
-sub INTERP_NEAREST (){ 'nearest' }
-sub INTERP_TILES (){ 'tiles' }
-sub INTERP_BILINEAR (){ 'bilinear' }
-sub INTERP_HYPER (){ 'hyper' }
+use base KGS::Listener::Game;
+use base KGS::Game;
-sub new_pixbuf {
- my ($w, $h, $alpha, $fill) = @_;
+use POSIX qw(ceil);
- my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
- $pixbuf->fill ($fill) if defined $fill;
+sub new {
+ my ($self, %arg) = @_;
+ $self = $self->Glib::Object::new;
+ $self->{$_} = delete $arg{$_} for keys %arg;
- $pixbuf;
-}
+ $self->listen ($self->{conn});
-sub scale_pixbuf {
- my ($src, $w, $h, $mode) = @_;
+ gtk::state $self, "game::window", undef, window_size => [600, 500];
- my $dst = new_pixbuf $w, $h, 1;
+ $self->signal_connect (delete_event => sub { $self->part; 1 });
+ $self->signal_connect (destroy => sub {
+ $self->unlisten;
+ delete $self->{app}{game}{$self->{channel}};
+ %{$_[0]} = ();
+ });#d#
- $src->scale(
- $dst, 0, 0, $w, $h, 0, 0,
- $w / $src->get_width, $h / $src->get_height,
- $mode,
- );
+ $self->add (my $hpane = new Gtk2::HPaned);
+ gtk::state $hpane, "game::hpane", undef, position => 500;
- $dst;
-}
+ # LEFT PANE
-# create a stack of stones
-sub create_stack {
- my ($self, $mark, $size, $rand) = @_;
+ $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
+
+ $self->{boardbox} = new Gtk2::VBox;
- my $shadow = $size * 0.05;
+ $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
- 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;
+ # board box (aspect/canvas)
+
+ $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
- # zeroeth the shadow
- if ($mark & (MARK_B | MARK_W)) {
- $::black_img[0]->composite (
- $base, $shadow, $shadow, $size, $size, $shadow-0.5, $shadow-0.5,
- $size / $stone->get_width, $size / $stone->get_height,
- $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 128
- );
- }
+ {
+ $frame->add (my $vbox = new Gtk2::VBox);
+ $vbox->add ($self->{title} = new Gtk2::Label $title);
- # first the big stones (handicap stones different for effect)
- for ([MARK_B, $mark & MARK_MOVE ? 255 : 192],
- [MARK_W, $mark & MARK_MOVE ? 255 : 192],
- [MARK_GRAY_B, 128],
- [MARK_GRAY_W, 128]) {
- my ($mask, $alpha) = @$_;
- if ($mark & $mask) {
- $stone->composite (
- $base, 0, 0, $size, $size, -0.5, -0.5,
- $size / $stone->get_width, $size / $stone->get_height,
- $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
- );
- }
- }
+ $vbox->add (my $hbox = new Gtk2::HBox);
- # then the samll 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, ($size / 4) x2, (int ($size / 2 + 0.5)) x2, ($size / 4 - 0.5) x 2,
- $size / $img->get_width / 2, $size / $img->get_height / 2,
- $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
- );
- }
- }
+ $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0);
- # and lastly any markers
- my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
+ $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
- 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.5, -0.5,
- $size / $img->get_width, $size / $img->get_height,
- $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255
- );
- }
- }
+ $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
+ $scale->set_draw_value (0);
+ $scale->set_digits (0);
- push @$$c, $base;
- }
+ $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
}
- $$c->[$rand % @$$c];
-}
+ $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
+
+ # RIGHT PANE
+
+ $hpane->pack2 (($self->{vpane} = new Gtk2::VPaned), 1, 1);
+ $hpane->set (position_set => 1);
+ gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
+
+ $self->{vpane}->add (my $sw = new Gtk2::ScrolledWindow);
+ $sw->set_policy ("automatic", "always");
-sub pixbuf_text {
- my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
+ $sw->add ($self->{userlist} = new userlist);
- my @c = grep $_,
- map $::font[$colour][$::fontmap{$_}],
- split //, $text;
+ $self->{vpane}->add (my $vbox = new Gtk2::VBox);
- 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;
+ $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
- $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
- $y -= $height * 0.5;
+ $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
+ for COLOUR_WHITE, COLOUR_BLACK;
+
+ $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
- for (@c) {
- my $w = $_->get_width * $s;
- $_->composite ($pixbuf,
- $x, $y, $w+0.999, $height+0.999, $x, $y, $s, $s,
- $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
+ $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Rules", sub { $self->draw_rules (@_) }, 1);
- $x += $w + $spacing;
+ $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);
}
+ });
+
+ $self;
+}
+
+sub event_update_users {
+ my ($self, $add, $update, $remove) = @_;
+
+ return unless $self->{userlist};
+
+ $self->{userlist}->update ($add, $update, $remove);
+
+ my %important;
+ $important{$self->{user1}{name}}++;
+ $important{$self->{user2}{name}}++;
+ $important{$self->{user3}{name}}++;
+
+ if (my @users = grep $important{$_->{name}}, @$add) {
+ $self->{chat}->append_text ("\n");
+ $self->{chat}->append_text (" " . $_->as_string . "") for @users;
+ }
+ if (my @users = grep $important{$_->{name}}, @$remove) {
+ $self->{chat}->append_text ("\n");
+ $self->{chat}->append_text (" " . $_->as_string . "") for @users;
}
+
}
-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);
+sub join {
+ my ($self) = @_;
+ return if $self->{joined};
+
+ $self->SUPER::join;
}
-sub repaint_board {
+sub update_board {
my ($self) = @_;
- my $canvas = $self->{canvas};
- my $expose_area = undef;
+ return unless $self->{path};
- return $expose_area unless $self->{board};
+ my $move = int $self->{moveadj}->get_value;
- my ($w, $h) = ($canvas->allocation->values)[2,3];
-
- my $s = $w > $h ? $h : $w;
+ my $running = $move == @{$self->{path}};
- return unless $s > 128;
-
- $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
+ $self->{board_label}->set_text ("Move " . ($move - 1));
- my $size = $self->{size};
+ $self->{cur_board} = new KGS::Game::Board $self->{size};
+ $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
- my $border = int ($s / ($size + 3) * 0.5);
- my $s2 = $s - $border * 2;
- my $edge = int ($s2 / ($size + 1) * 0.95) - ($::config->{randomize} ? 3 : 0);
- my $ofs = int ($edge / 2);
+ for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
+ $self->{userpanel}[$colour]->set_state (
+ $self->{cur_board}{captures}[$colour],
+ $self->{cur_board}{timer}[$colour],
+ ($running && $self->{lastmove_colour} == !$colour)
+ ? $self->{lastmove_time} : 0
+ );
+ }
- my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
+ $self->{board}->set_board ($self->{cur_board});
+}
- my $pixbuf;
+sub event_update_tree {
+ my ($self) = @_;
- my $oldboard;
+ $self->{path} = $self->get_path;
- if ($self->{background}) {
- if ($oldboard = $self->{board_shown}) {
- $pixbuf = $self->{pixbuf};
- } else {
- $pixbuf = $self->{background}->copy;
- $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
+ if ($self->{moveadj}) {
+ my $upper = $self->{moveadj}->upper;
+ my $pos = $self->{moveadj}->get_value;
+ my $move = scalar @{$self->{path}};
+
+ $self->{moveadj}->upper ($move);
+
+ $self->{moveadj}->changed;
+ if ($pos == $upper) {
+ $self->{moveadj}->value ($move);
+ $self->{moveadj}->value_changed;
}
- } else {
- $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
-
- my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
+ }
+}
- 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;
+sub event_update_comments {
+ my ($self, $node, $comment, $newnode) = @_;
+ $self->SUPER::event_update_comments($node, $comment, $newnode);
+
+ my $text;
+
+ $text .= "\nMove $node->{move}, Node $node->{id}"
+ if $newnode;
+
+ for (split /\n/, $comment) {
+ $text .= "\n";
+ if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
+ $text .= "" . (util::toxml $1) . ":";
}
+
+ # coords only for 19x19 so far
+ $_ = util::toxml $_;
+ s{
+ (
+ \b
+ (?:[bw])?
+ [, ]{0,2}
+ [a-hj-t] # valid for upto 19x19
+ \s?
+ [1-9]?[0-9]
+ \b
+ )
+ }{
+ "$1";
+ }sgexi;
+
+ $text .= $_;
+ }
+
+ $self->{chat}->append_text ($text);
+}
+
+sub event_join {
+ my ($self) = @_;
+
+ $self->SUPER::event_join (@_);
+ $self->event_update_game;
+ $self->show_all;
+}
+
+sub event_part {
+ my ($self) = @_;
+
+ $self->SUPER::event_part;
+ $self->destroy;
+}
+
+sub event_move {
+ my ($self, $pass) = @_;
+ sound::play 1, $pass ? "pass" : "move";
+}
- my $linew = int ($s / 25 / $size);
+sub event_update_game {
+ my ($self) = @_;
+ $self->SUPER::event_update_game;
- # 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;
+ return unless $self->{joined};
- $a++;
- $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
+ 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);
+
+ $self->{user}[COLOUR_BLACK] = $self->{user1};
+ $self->{user}[COLOUR_WHITE] = $self->{user2};
+
+ # show board
+ if ($self->is_inprogress) {
+ $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent;
+ if (my $ch = delete $self->{challenge}) {
+ (delete $_->{inlay})->clear for values %$ch;
}
+ }
+
+ $self->{left}->show_all;
+
+ # view text
+
+ eval { #d#
+ my @ga;
+ $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
+ . " (" . (util::toxml $gameopt{$self->option}) . ")";
+ $ga[1] = "\nFlags:";
+ $ga[1] .= " started" if $self->is_inprogress;
+ $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_inprogress;
+
+ $ga[3] = "\nPlayers: " . (util::toxml $self->{user2}->as_string) . ""
+ . " vs. " . (util::toxml $self->{user1}->as_string) . ""
+ if $self->is_inprogress;
+
+ if ($self->is_inprogress) {
+ $ga[4] = "\nHandicap: " . $self->{handicap};
+ $ga[5] = "\nKomi: " . $self->{komi};
+ $ga[6] = "\nSize: " . $self->size_string;
+ }
- unless ($::config->{conserve_memory}) {
- $self->{background} = $pixbuf;
- $pixbuf = $pixbuf->copy;
+ if ($self->is_scored) {
+ $ga[7] = "\nResult: " . $self->score_string;
+ }
+
+ $text = "\n";
+ for (0..7) {
+ if ($self->{gatext}[$_] ne $ga[$_]) {
+ $text .= $ga[$_];
}
}
+ $text .= "";
+
+ $self->{gatext} = \@ga;
+ };
+
+ $self->{chat}->append_text ($text);
+}
- $self->{pixbuf} = $pixbuf;
+sub draw_rules {
+ my ($self, $inlay) = @_;
- # hoshi-points(!)#d#
- # caching of empty board gfx(!)#d#
+ my $rules = $self->{rules};
- for my $x (1 .. $size) {
- for my $y (1 .. $size) {
- my $rand = ($x ^ $y ^ 0x5555);
+ my $text = "";
- my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
+ $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
- if ($::config->{randomize}) {
- $dx += ($rand % 7) - 3;
- $dy += ($rand / 3 % 7) - 3;
- }
+ $text .= "\nTime: ";
- 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;
-
- if ($oldboard) {
- next if $old == $mark; # no change
-
- $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
- $expose_area = $expose_area
- ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
- : new Gtk2::Gdk::Rectangle @$area;
- }
+ 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};
+ }
+
+ $inlay->clear;
+ $inlay->append_text ("$text");
+}
- if ($mark) {
- my $pb = $self->create_stack($mark, $edge, $rand);
+sub event_update_rules {
+ my ($self, $rules) = @_;
- $pb->composite ($pixbuf, @$area,
- $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
+ $self->{rules} = $rules;
- # 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];
- }
-
- # old pixmap&mask-way. that was fast ;(
- #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 );
-
- #$gc->set_clip_mask ($bm);
- #$gc->set_clip_origin ($dx, $dy);
- #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge);
- }
- }
+ if ($self->{user}) {
+ $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
+ for COLOUR_BLACK, COLOUR_WHITE;
}
- $self->{board_shown} = Storable::dclone $self->{board};
- #d# save
- #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
+ sound::play 3, "gamestart";
- $expose_area;
+ $self->draw_rules ($self->{rules_inlay});
}
-sub redraw {
- my ($self, $area) = @_;
+sub inject_resign_game {
+ my ($self, $msg) = @_;
- if ($area && $self->{pixbuf}) {
- my ($x, $y, $w, $h) = $area->values;
- my ($ox, $oy, $s) = @{$self->{offsets}};
+ sound::play 3, "resign";
- $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
- $x, $y, $x + $ox, $y + $oy, $w, $h,
- "normal", 0, 0);
- $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
- $x + $ox - 1, $y + $oy - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
- }
+ $self->{chat}->append_text ("\n"
+ . "\n"
+ . (util::toxml $self->{user}[$msg->{player}]->as_string)
+ . " resigned.");
}
-sub event_update_tree {
- my ($self) = @_;
+sub inject_final_result {
+ my ($self, $msg) = @_;
- $self->{path} = $self->get_path;
- $self->{moveadj}->value_changed if $self->{moveadj};
+ $self->{chat}->append_text ("\n"
+ . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
+ . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
+ . ""
+ );
}
-sub event_part {
- my ($self) = @_;
- $self->SUPER::event_part;
- delete $appwin::gamelist->{game}{$self->{channel}};
- $self->destroy;
+sub draw_challenge {
+ my ($self, $c) = @_;
+
+ my $inlay = $c->{inlay};
+ my $challenge = $c->{challenge};
+ my $rules = $challenge->{rules};
+
+ my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
+ my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
+
+ $inlay->clear;
+ $inlay->append_text ("\nChallenge to " . $opponent->as_string . "");
+ $inlay->append_text ("\nHandicap: $rules->{handicap}");
+
+#bless( (
+# gametype => 3,
+# user1 => bless( {
+# flags => 2633,
+# name => 'dorkusx'
+# }, 'KGS::User' ),
+# rules => bless( {
+# count => 5,
+# time => 900,
+# timesys => 2,
+# interval => 30,
+# komi => '6.5',
+# size => 19,
+# ruleset => 0,
+# handicap => 0
+# }, 'KGS::Rules' ),
+# user2 => bless( {
+# flags => 436220808,
+# name => 'Nerdamus'
+# }, 'KGS::User' )
+# ), 'KGS::Challenge' )
}
-sub event_move {
- my ($self, $pass) = @_;
- sound::play 1, $pass ? "pass" : "move";
+sub event_challenge {
+ my ($self, $challenge) = @_;
+
+ my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
+ my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
+
+ my $c = $self->{challenge}{$opponent->{name}} ||= {};
+
+ $c->{inlay} ||= $self->{chat}->new_inlay;
+ $c->{challenge} = $challenge;
+
+ $self->draw_challenge ($c);
+
+# require KGS::Listener::Debug;
+# $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge));
}
1;