ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
(Generate patch)

Comparing kgsueme/kgsueme/game.pl (file contents):
Revision 1.31 by pcg, Mon Jun 2 12:39:20 2003 UTC vs.
Revision 1.137 by root, Sat Jun 12 06:14:36 2004 UTC

1use utf8;
2
3use Scalar::Util ();
4
5### GO CLOCK WIDGET #########################################################
6
1package game::goclock; 7package game::goclock;
2 8
9# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
10# is completely borked.
11
3use Time::HiRes (); 12use Time::HiRes ();
4 13
5use KGS::Constants; 14use KGS::Constants;
6 15
7use base gtk::widget; 16use Glib::Object::Subclass
17 Gtk2::Label;
8 18
9sub new { 19sub INIT_INSTANCE {
10 my $class = shift; 20 my $self = shift;
11 my $self = $class->SUPER::new(@_);
12 21
13 $self->{widget} = new Gtk2::Label; 22 $self->signal_connect (destroy => sub { $_[0]->stop });
14 23
15 $self->{set} = sub { }; 24 $self->{set} = sub { };
16 $self->{format} = sub { "ERROR" }; 25 $self->{format} = sub { "???" };
26}
17 27
28sub FINALIZE_INSTANCE {
29 my $self = shift;
30
18 $self; 31 $self->stop;
19} 32}
20 33
21sub append_text { 34sub configure {
22 my ($self, $text) = @_;
23
24 $self->{buffer}->insert ($self->{buffer}->get_end_iter, $text);
25}
26
27sub format_time {
28 my ($time) = @_;
29
30 $time > 60*60
31 ? sprintf "%d:%02d:%02d", $time / (60 * 60), $time / 60 % 60, $time % 60
32 : sprintf "%d:%02d", $time / 60 % 60, $time % 60;
33}
34
35sub set_rules {
36 my ($self, $timesys, $main, $interval, $count) = @_; 35 my ($self, $timesys, $main, $interval, $count) = @_;
37 36
38 if ($timesys == TIMESYS_ABSOLUTE) { 37 if ($timesys == TIMESYS_ABSOLUTE) {
39 $self->{set} = sub { $self->{time} = $_[0] }; 38 $self->{format} = sub {
40 $self->{format} = sub { format_time $_[0] }; 39 if ($_[0] < 0) {
40 "TIMEOUT";
41 } else {
42 util::format_time $_[0];
43 }
44 };
41 45
42 } elsif ($timesys == TIMESYS_BYO_YOMI) { 46 } elsif ($timesys == TIMESYS_BYO_YOMI) {
43 my $low = $interval * $count; 47 my $low = $interval * $count;
44 48
45 $self->{set} = sub { $self->{time} = $_[0] };
46
47 $self->{format} = sub { 49 $self->{format} = sub {
50 if ($_[0] < 0) {
51 "TIMEOUT";
48 if ($_[0] > $low) { 52 } elsif ($_[0] > $low) {
49 format_time $_[0] - $low; 53 util::format_time $_[0] - $low;
50 } else { 54 } else {
51 sprintf "%s (%d)", (format_time int ($_[0] % $interval) || $interval), $_[0] / $interval; 55 sprintf "%s (%d)",
56 util::format_time int (($_[0] - 1) % $interval + 1),
57 ($_[0] - 1) / $interval;
52 } 58 }
53 }; 59 };
54 60
55 } elsif ($timesys == TIMESYS_CANADIAN) { 61 } elsif ($timesys == TIMESYS_CANADIAN) {
56 my $low = $interval;
57
58 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
59
60 $self->{format} = sub { 62 $self->{format} = sub {
61 if ($_[0] > $low) { 63 if ($_[0] < 0) {
64 "TIMEOUT";
65 } elsif (!$self->{moves}) {
62 format_time $_[0] - $low; 66 util::format_time $_[0] - $low;
63 } else { 67 } else {
64 sprintf "%s / %d", (format_time int($_[0] % $interval) || $interval), $self->{moves}; 68 my $time = int (($_[0] - 1) % $interval + 1);
69
70 sprintf "%s/%d =%d",
71 util::format_time $time,
72 $self->{moves},
73 $self->{moves} > 1
74 ? $time / $self->{moves}
75 : $interval;
65 } 76 }
66 }; 77 };
67 78
68 } else { 79 } else {
69 # none, or unknown 80 # none, or unknown
70 $self->{set} = sub { };
71 $self->{format} = sub { "---" } 81 $self->{format} = sub { "-" }
72 } 82 }
73} 83}
74 84
75sub refresh { 85sub refresh {
76 my ($self, $timestamp) = @_; 86 my ($self, $timestamp) = @_;
77 my $timer = $self->{time} + $self->{start} - $timestamp; 87 my $timer = $self->{time} + $self->{start} - $timestamp;
88
89 # we round the timer value slightly... the protocol isn't exact anyways,
90 # and this gives smoother timers ;)
91 my $timer2 = int $timer + 0.4;
92
78 $self->{widget}->set_text ($self->{format}->($timer)); 93 $self->set_text ($self->{format}->($timer2));
79 94
80 $timer - int $timer; 95 $timer - int $timer;
81} 96}
82 97
83sub set_time { 98sub set_time {
84 my ($self, $time) = @_; 99 my ($self, $start, $time, $moves) = @_;
85 100
86 # we ignore requests to re-set the time of a running clock. 101 $self->{time} = $time;
87 # this is the easiest way to ensure that commentary etc. 102 $self->{moves} = $moves;
88 # doesn't re-set the clock. yes, this is frickle design, 103
89 # but I think the protoocl is to blame here, which gives 104 if ($start) {
90 # very little time information. (cgoban2 also has had quite 105 $self->{start} = $start;
91 # a lot of small time update problems...) 106 $self->start;
92 unless ($self->{timeout}) { 107 } else {
93 $self->{set}->($time->[0], $time->[1]); 108 $self->stop;
94 $self->refresh ($self->{start}); 109 $self->refresh ($self->{start});
95 } 110 }
96} 111}
97 112
98sub start { 113sub start {
99 my ($self) = @_; 114 my ($self) = @_;
100 115
101 return if $self->{timeout}; 116 $self->stop;
102
103 # this is correct, since we assume the last message triggered a start
104 $self->{start} = $KGS::Protocol::NOW;
105 117
106 my $timeout; $timeout = sub { 118 my $timeout; $timeout = sub {
107 # -100 means we run the timer a bit earlier to avoid 10.99 => 10s roundings.
108 # we "could" cheat by precalculating the time, but I feel uneasy about both
109 # ways to cheat.
110 my $next = int ($self->refresh (Time::HiRes::time) * 1000) - 100; 119 my $next = $self->refresh (Time::HiRes::time) * 1000;
111 $next += 1000 if $next < 0; 120 $next += 1000 if $next < 0;
112 $self->{timeout} = add Glib::Timeout $next, $timeout; 121 $self->{timeout} = add Glib::Timeout $next, $timeout;
113 0; 122 0;
114 }; 123 };
115 124
120 my ($self) = @_; 129 my ($self) = @_;
121 130
122 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 131 remove Glib::Source delete $self->{timeout} if $self->{timeout};
123} 132}
124 133
125sub destroy { 134### USER PANEL ##############################################################
126 my ($self) = @_;
127 $self->stop;
128 $self->SUPER::destroy;
129}
130 135
131package game::userpanel; 136package game::userpanel;
132 137
133use base gtk::widget; 138use KGS::Constants;
134 139
135sub new { 140use Glib::Object::Subclass
136 my $class = shift; 141 Gtk2::Frame,
137 my $self = $class->SUPER::new(@_); 142 properties => [
143 Glib::ParamSpec->IV ("colour", "colour", "User Colour",
144 COLOUR_BLACK, COLOUR_WHITE, COLOUR_BLACK,
145 [qw(construct-only readable writable)]),
146 ];
138 147
139 $self->{widget} = new Gtk2::HBox; 148sub INIT_INSTANCE {
149 my ($self) = @_;
140 150
151 $self->add ($self->{window} = my $window = new Gtk2::EventBox); # for bg
152
141 $self->{widget}->add (my $vbox = new Gtk2::VBox); 153 $window->add (my $vbox = new Gtk2::VBox);
142 154
143 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 155 $vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0);
156 $vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0);
157 $vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0);
158
144 $vbox->add ($self->{info} = new Gtk2::Label ""); 159 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
145 $vbox->add (($self->{clock} = new game::goclock)->widget);
146 160
147 $self; 161 $self;
148} 162}
149 163
150sub set_rules { 164sub SET_PROPERTY {
165 my ($self, $pspec, $value) = @_;
166
167 $self->{$pspec->get_name} = $value;
168
169 $self->set_name ("userpanel-$self->{colour}");
170}
171
172sub configure {
151 my ($self, $rules) = @_; 173 my ($self, $app, $user, $rules) = @_;
152 174
153 if ($self->{name}->get_text ne $rules->{player}[$self->{colour}]) { 175 if ($self->{name}->get_text ne $user->as_string) {
154 $self->{name}->set_text ($rules->{player}[$self->{colour}]); 176 $self->{name}->set_text ($user->as_string);
155 177
178 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
179 unless ($::config{suppress_userpic}) {
180 $self->{imagebox}->add (gtk::image_from_data undef);
181 }
182 $self->{imagebox}->show_all;
183
184 if ($user->has_pic) {
156 # the big picture... 185 # the big picture...
157 appwin::userpic ($rules->{player}[$self->{colour}], sub { 186 $app->userpic ($user->{name}, sub {
187 return unless $self->{imagebox};
188
189 if ($_[0]) {
190 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
191 unless ($::config{suppress_userpic}) {
158 $self->{widget}->add (gtk::image_from_data $_[0]) if $_[0]; 192 $self->{imagebox}->add (gtk::image_from_data $_[0]);
193 }
159 $self->{widget}->show_all; 194 $self->{imagebox}->show_all;
160 # undef => show sth. funny 195 }
196 });
161 }); 197 }
162 } 198 }
163 199
164 $self->{clock}->set_rules (@{$rules->{rules}}{qw(timesys time interval count)}); 200 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
165} 201}
166 202
167sub set_state { 203sub set_captures {
168 my ($self, $captures, $timer, $running) = @_; 204 my ($self, $captures) = @_;
169
170 $self->{clock}->stop unless $running;
171 $self->{clock}->set_time ($timer);
172 $self->{clock}->start if $running;
173 205
174 $self->{info}->set_text ("$captures pris."); 206 $self->{info}->set_text ("$captures pris.");
175} 207}
176 208
209sub set_timer {
210 my ($self, $start, $time, $moves) = @_;
211
212 $self->{clock}->set_time ($start, $time, $moves);
213}
214
215### GAME WINDOW #############################################################
216
177package game; 217package game;
218
219use Scalar::Util qw(weaken);
178 220
179use KGS::Constants; 221use KGS::Constants;
180use KGS::Game::Board; 222use KGS::Game::Board;
181 223
224use Gtk2::GoBoard;
225use Gtk2::GoBoard::Constants;
226
227use base KGS::Game;
182use base KGS::Listener::Game; 228use base KGS::Listener::Game;
183use base KGS::Game;
184 229
185use base gtk::widget; 230use Glib::Object::Subclass
231 Gtk2::Window;
186 232
187use POSIX qw(ceil); 233use POSIX qw(ceil);
188 234
189sub new { 235sub new {
190 my $self = shift; 236 my ($self, %arg) = @_;
191 $self = $self->SUPER::new(@_);
192 237
193 $self->listen($self->{conn}); 238 $self = $self->Glib::Object::new;
239 $self->{$_} = delete $arg{$_} for keys %arg;
194 240
195 $self->{window} = new Gtk2::Window 'toplevel';
196 my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
197 $self->{window}->set_title("KGS Game $title");
198 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500]; 241 gtk::state $self, "game::window", undef, window_size => [620, 460];
242 $self->set (allow_shrink => 1);
199 243
200 $self->{window}->signal_connect(delete_event => sub { 244 $self->signal_connect (destroy => sub {
201 $self->part; 245 $self->unlisten;
202 $self->destroy; 246 delete $self->{app}{game}{$self->{channel}};
203 1; 247 %{$_[0]} = ();
204 }); 248 });#d#
205 249
206 $self->{window}->add($self->{hpane} = new Gtk2::HPaned); 250 $self->add (my $hpane = new Gtk2::HPaned);
207 gtk::state $self->{hpane}, "game::hpane", undef, position => 500; 251 gtk::state $hpane, "game::hpane", undef, position => 420;
208 252
253 # LEFT PANE
254
209 $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 1); 255 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
210 256
211 $self->{boardbox} = new Gtk2::VBox;
212
213 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); 257 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
214 258
215 # challenge
216
217 $self->{challenge} = new challenge channel => $self->{channel};
218
219 # board box (aspect/canvas) 259 # board box (aspect/canvas)
220 260
261 # RIGHT PANE
262
263 $hpane->pack2 ((my $vbox = new Gtk2::VBox), 1, 1);
264 $hpane->set (position_set => 1);
265
221 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 266 $vbox->pack_start ((my $frame = new Gtk2::Frame), 0, 1, 0);
222 267
223 { 268 {
224 $frame->add(my $vbox = new Gtk2::VBox); 269 $frame->add (my $vbox = new Gtk2::VBox);
225 $vbox->add($self->{title} = new Gtk2::Label $title); 270 $vbox->add ($self->{title} = new Gtk2::Label "-");
271 $self->{title}->set (visible => 0, no_show_all => 1); # workaround for refresh-bug
226 272
273 $vbox->add (my $hbox = new Gtk2::HBox);
274
275 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0);
276
227 $self->{moveadj} = new Gtk2::Adjustment 0, 0, 0, 1, 1, 0; 277 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
228 278
229 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj}); 279 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
230 $scale->set_draw_value (0); 280 $scale->set_draw_value (0);
231 $scale->set_digits (0); 281 $scale->set_digits (0);
232 282
233 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 283 $self->{moveadj}->signal_connect (value_changed => sub {
284 $self->{showmove} = int $self->{moveadj}->get_value;
285 $self->update_board;
286 });
234 } 287 }
235 288
236 $self->{boardbox}->pack_start((my $aspect_frame = new Gtk2::AspectFrame "", 0.5, 0.5, 1, 0), 1, 1, 0);
237 $aspect_frame->set (border_width => 0, shadow_type => 'none', label_xalign => 0.5);
238 $self->{board_label} = $aspect_frame->get_label_widget;
239
240 $aspect_frame->add($self->{canvas} = new Gtk2::DrawingArea);
241 $self->{canvas}->double_buffered (0) if $::config->{conserve_memory};
242
243 $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
244 $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
245
246 # RIGHT PANE
247
248 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0);
249 $self->{hpane}->set(position_set => 1);
250 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
251
252 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
253 $sw->set_policy("automatic", "always");
254
255 $sw->add(($self->{userlist} = new userlist)->widget);
256
257 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
258
259 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0); 289 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
290
260 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget); 291 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
261 $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget); 292 for COLOUR_WHITE, COLOUR_BLACK;
293
294 $vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
295
296 $buttonbox->add ($self->{button_pass} =
297 Gtk2::Button->Glib::Object::new (label => "Pass", visible => 0, no_show_all => 1));
298 $self->{button_pass}->signal_connect (clicked => sub {
299 $self->{board_click}->(255, 255) if $self->{board_click};
300 });
301 $buttonbox->add ($self->{button_undo} =
302 Gtk2::Button->Glib::Object::new (label => "Undo", visible => 0, no_show_all => 1));
303 $self->{button_undo}->signal_connect (clicked => sub {
304 $self->send (req_undo => channel => $self->{channel});
305 });
306 $buttonbox->add ($self->{button_resign} =
307 Gtk2::Button->Glib::Object::new (label => "Resign", visible => 0, no_show_all => 1));
308 $self->{button_resign}->signal_connect (clicked => sub {
309 $self->send (resign_game => channel => $self->{channel}, player => $self->{colour});
310 });
262 311
263 $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0); 312 $vbox->pack_start (($self->{chat} = new chat app => $self->{app}), 1, 1, 0);
264 313
265 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
266 $self->{entry}->signal_connect(activate => sub { 314 $self->{chat}->signal_connect (tag_event => sub {
267 my $text = $self->{entry}->get_text; 315 my (undef, $tag, $event, $content) = @_;
268 $self->say($text) if $text =~ /\S/;
269 $self->{entry}->set_text("");
270 }); 316 });
271 317
318 $self->set_channel ($self->{channel});
319
320 $self->show_all;
321
322 $self;
323}
324
325sub set_channel {
326 my ($self, $channel) = @_;
327
328 $self->{channel} = $channel;
329
330 if ($self->{channel} > 0) {
331 $self->listen ($self->{conn});
332
333 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Setup:", sub { $self->draw_setup (@_) }, 1);
334 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 1);
335
336 $self->signal_connect (delete_event => sub { $self->part; 1 });
337 $self->{chat}->signal_connect (command => sub {
338 my ($chat, $cmd, $arg) = @_;
339 if ($cmd eq "rsave") {
340 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
341 } else {
342 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
343 }
344 });
345 }
346}
347
348### JOIN/LEAVE ##############################################################
349
350sub join {
351 my ($self) = @_;
352 return if $self->{joined};
353
354 $self->SUPER::join;
355}
356
357sub part {
358 my ($self) = @_;
359
360 $self->hide;
361 $self->SUPER::part;
362}
363
364sub event_join {
365 my ($self) = @_;
366
367 $self->SUPER::event_join (@_);
368 $self->init_tree;
272 $self->event_update_game; 369 $self->event_update_game;
273 $self; 370}
371
372sub event_part {
373 my ($self) = @_;
374
375 $self->SUPER::event_part;
376 $self->destroy;
377}
378
379sub event_quit {
380 my ($self) = @_;
381
382 $self->SUPER::event_quit;
383 $self->destroy;
384}
385
386### USERS ###################################################################
387
388sub draw_users {
389 my ($self, $inlay) = @_;
390
391 for (sort keys %{$self->{users}}) {
392 $inlay->append_text ("\t<user>" . $self->{users}{$_}->as_string . "</user>");
393 }
274} 394}
275 395
276sub event_update_users { 396sub event_update_users {
277 my ($self, $add, $update, $remove) = @_; 397 my ($self, $add, $update, $remove) = @_;
278 398
279 $self->{userlist}->update ($add, $update, $remove); 399# $self->{userlist}->update ($add, $update, $remove);
280}
281 400
282sub join { 401 $self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy
402 for @$remove;
403
404 $self->{users_inlay}->refresh;
405
406 my %important;
407 $important{$self->{black}{name}}++;
408 $important{$self->{white}{name}}++;
409 $important{$self->{owner}{name}}++;
410
411 if (my @users = grep $important{$_->{name}}, @$add) {
412 $self->{chat}->append_text ("\n<leader>Joins:</leader>");
413 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
414 }
415 if (my @users = grep $important{$_->{name}}, @$remove) {
416 $self->{chat}->append_text ("\n<leader>Parts:</leader>");
417 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
418 }
419}
420
421### GAME INFO ###############################################################
422
423sub draw_setup {
283 my ($self) = @_; 424 my ($self, $inlay) = @_;
425
284 return if $self->{joined}; 426 return unless $self->{joined};
285 427
286 $self->SUPER::join; 428 my $rules = $self->{rules};
287 429
288 $self->{window}->show_all; 430 my $text = "";
289}
290 431
291sub part { 432 $text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>"
292 my ($self) = @_; 433 if $self->{teacher};
293 434
294 $self->SUPER::part; 435 $text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>"
295 $self->destroy; 436 if $self->{owner}->is_valid;
296}
297 437
298sub configure_event { 438 if ($self->is_inprogress) {
299 my ($widget, $event, $self) = @_; 439 $text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>"
300 delete $self->{stack}; 440 . " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>";
301 delete $self->{pixbuf}; 441 }
302 delete $self->{board_shown}; 442 $text .= "\nType: " . util::toxml $gametype{$self->type};
303 delete $self->{background}; 443
304 $self->repaint_board; 444 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
445
446 $text .= "\nTime: ";
447
448 if ($rules->{timesys} == TIMESYS_NONE) {
449 $text .= "UNLIMITED";
450 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
451 $text .= util::format_time $rules->{time};
452 $text .= " ABS";
453 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
454 $text .= util::format_time $rules->{time};
455 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
456 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
457 $text .= util::format_time $rules->{time};
458 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
459 }
305 0; 460
306} 461 $text .= "\nFlags:";
462 $text .= " private" if $self->is_private;
463 $text .= " started" if $self->is_inprogress;
464 $text .= " adjourned" if $self->is_adjourned;
465 $text .= " scored" if $self->is_scored;
466 $text .= " saved" if $self->is_saved;
307 467
308sub expose_event { 468 if ($self->is_inprogress) {
309 my ($widget, $event, $self) = @_; 469 $text .= "\nHandicap: " . $self->{handicap};
470 $text .= "\nKomi: " . $self->{komi};
471 $text .= "\nSize: " . $self->size_string;
472 }
310 473
311 $self->{pixbuf} or return; 474 if ($self->is_scored) {
475 $text .= "\nResult: " . $self->score_string;
476 }
312 477
313 my $area = $event->area; 478 $inlay->append_text ("<infoblock>$text</infoblock>");
314 479
315 $self->redraw ($area); 480}
316 481
482sub event_update_game {
483 my ($self) = @_;
484
485 $self->SUPER::event_update_game;
486
487 return unless $self->{joined};
488
489 $self->{colour} = $self->player_colour ($self->{conn}{name});
317 0; 490
318} 491 $self->{user}[COLOUR_BLACK] = $self->{black};
492 $self->{user}[COLOUR_WHITE] = $self->{white};
319 493
320# something Gtk2 fixed 494 # show board
321sub INTERP_NEAREST (){ 'nearest' } 495 if ($self->is_inprogress) {
322sub INTERP_TILES (){ 'tiles' } 496 if (!$self->{board}) {
323sub INTERP_BILINEAR (){ 'bilinear' } 497 $self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
324sub INTERP_HYPER (){ 'hyper' } 498 $self->{board}->signal_connect (button_release => sub {
325 499 return unless $self->{cur_board};
326sub new_pixbuf { 500 if ($_[1] == 1) {
327 my ($w, $h, $alpha, $fill) = @_; 501 $self->{board_click}->($_[2], $_[3]) if $self->{board_click};
328
329 my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
330 $pixbuf->fill ($fill) if defined $fill;
331
332 $pixbuf;
333}
334
335sub scale_pixbuf {
336 my ($src, $w, $h, $mode) = @_;
337
338 my $dst = new_pixbuf $w, $h, 1;
339
340 $src->scale(
341 $dst, 0, 0, $w, $h, 0, 0,
342 $w / $src->get_width, $h / $src->get_height,
343 $mode,
344 );
345
346 $dst;
347}
348
349# create a stack of stones
350sub create_stack {
351 my ($self, $mark, $size, $rand) = @_;
352
353 my $shadow = $size * 0.05;
354
355 my $c = \$self->{stack}{$mark};
356 unless ($$c) {
357 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
358 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
359
360 # zeroeth the shadow
361 if ($mark & (MARK_B | MARK_W)) {
362 # the -0.5's are a mystery to me
363 $::shadow_img->composite (
364 $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
365 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
366 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
367 ); 502 }
368 } 503 });
504 $self->{board}->show_all;
505 }
506 if (my $ch = delete $self->{challenge}) {
507 $_->{inlay}->destroy for values %$ch;
508 }
509 $self->update_cursor;
510 }
369 511
370 # first the big stones (handicap stones different for effect) 512 my $title = defined $self->{channel}
371 for ([MARK_B, $mark & MARK_MOVE ? 255 : 192], 513 ? $self->owner->as_string . " " . $self->opponent_string
372 [MARK_W, $mark & MARK_MOVE ? 255 : 192], 514 : "Game Window";
373 [MARK_GRAY_B, 128], 515 $self->set_title ("KGS Game $title");
374 [MARK_GRAY_W, 128]) { 516 $self->{title}->set_text ($title); # title gets redrawn wrongly
375 my ($mask, $alpha) = @$_; 517 $self->{title}->show; # workaround for refresh-bug
376 if ($mark & $mask) { 518
377 $stone->composite ( 519 $self->{rules_inlay}->refresh;
378 $base, 0, 0, $size, $size, 0, 0, 520
379 $size / $stone->get_width, $size / $stone->get_height, 521 if (exists $self->{teacher}) {
380 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha 522 $self->{teacher_inlay} ||= $self->{chat}->new_inlay;
523 $self->{teacher_inlay}->clear;
524 $self->{teacher_inlay}->append_text ("\n<header>Teacher:</header> <user>"
525 . (util::toxml $self->{teacher}) . "</user>");
526 } elsif ($self->{teacher_inlay}) {
527 (delete $self->{teacher_inlay})->clear;
528 }
529
530 $self->update_cursor;
531}
532
533sub event_update_rules {
534 my ($self, $rules) = @_;
535
536 $self->{rules} = $rules;
537
538 if ($self->{user}) {
539 # todo. gets drawn wrongly
540
541 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
542 for COLOUR_BLACK, COLOUR_WHITE;
543 }
544
545 sound::play 3, "gamestart";
546 $self->{rules_inlay}->refresh;
547}
548
549### BOARD DISPLAY ###########################################################
550
551sub update_timers {
552 my ($self, $timers) = @_;
553
554 my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher};
555
556 for my $colour (COLOUR_BLACK, COLOUR_WHITE) {
557 my $t = $timers->[$colour];
558 $self->{userpanel}[$colour]->set_timer (
559 $running && $colour == $self->{whosemove} && $t->[0],
560 $t->[1] || $self->{rules}{time}
561 + ($self->{rules}{timesys} == TIMESYS_BYO_YOMI
562 && $self->{rules}{interval} * $self->{rules}{count}),
563 $t->[2]);
564 }
565}
566
567sub inject_set_gametime {
568 my ($self, $msg) = @_;
569
570 $self->{timers} = [
571 [$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}],
572 [$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}],
573 ];
574
575 $self->update_timers ($self->{timers})
576 if $self->{showmove} == @{$self->{path}};
577}
578
579sub update_cursor {
580 my ($self) = @_;
581
582 return unless $self->{cur_board};
583
584 if ($self->{rules}{ruleset} == RULESET_JAPANESE) {
585 if ($self->{curnode}{move} == 0) {
586 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
587 } else {
588 $self->{whosemove} = 1 - $self->{cur_board}{last};
589 }
590 } else {
591 # Chinese, Aga, NZ all have manual placement
592 if ($self->{curnode}{move} < $self->{handicap}) {
593 $self->{whosemove} = COLOUR_BLACK;
594 } elsif ($self->{curnode}{move} == $self->{handicap}) {
595 $self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK;
596 } else {
597 $self->{whosemove} = 1 - $self->{cur_board}{last};
598 }
599 }
600
601 my $running = $self->{showmove} == @{$self->{path}} && $self->is_active;
602
603 delete $self->{board_click};
604
605 if ($self->{teacher} eq $self->{app}{conn}) {
606 #TODO# # teaching mode not implemented
607 $self->{button_pass}->set (label => "Pass", sensitive => 1);
608 $self->{button_pass}->show;
609 $self->{button_undo}->hide;
610 $self->{button_resign}->hide;
611 $self->{board}->set (cursor => undef);
612
613 } elsif ($running && $self->{colour} != COLOUR_NONE) {
614 # during game
615 $self->{button_undo}->show;
616 $self->{button_resign}->show;
617
618 if ($self->{cur_board}{score}) {
619 # during scoring
620 $self->{button_pass}->set (label => "Done", sensitive => 1);
621 $self->{button_pass}->show;
622 $self->{board}->set (cursor => sub {
623 $_[0] & (MARK_B | MARK_W)
624 ? $_[0] ^ MARK_GRAYED
625 : $_[0];
626 });
627 $self->{board_click} = sub {
628 if ($_[0] == 255) {
629 $self->{button_pass}->sensitive (0);
630 $self->done;
631 } else {
632 $self->send (mark_dead =>
633 channel => $self->{channel},
634 x => $_[0],
635 y => $_[1],
636 dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED),
381 ); 637 );
382 } 638 }
383 } 639 };
384 640
385 # then the small stones 641 } elsif ($self->{colour} == $self->{whosemove}) {
386 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]], 642 # normal move
387 [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) { 643 $self->{button_pass}->set (label => "Pass", sensitive => 1);
388 my ($mask, $img) = @$_; 644 $self->{button_pass}->show;
389 if ($mark & $mask) { 645 $self->{board}->set (cursor => sub {
390 $img->composite ( 646 $self->{cur_board}
391 $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2, 647 && $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2],
392 $size / $img->get_width / 2, $size / $img->get_height / 2, 648 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND)
393 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224 649 ? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B)
394 ); 650 : $_[0];
651 });
652 $self->{board_click} = sub {
653 return unless
654 $self->{cur_board}->is_valid_move ($self->{colour}, $_[0], $_[1],
655 $self->{rules}{ruleset} == RULESET_NEW_ZEALAND);
656 $self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]);
657 $self->{board}->set (cursor => undef);
658 delete $self->{board_click};
659 $self->{button_pass}->sensitive (0);
660 };
661 } else {
662 $self->{button_pass}->set (label => "Pass", sensitive => 0);
663 $self->{button_pass}->show;
664 $self->{board}->set (cursor => undef);
665 }
666 } else {
667 $self->{button_undo}->hide;
668 $self->{button_resign}->hide;
669 $self->{button_pass}->hide;
670 $self->{board}->set (cursor => undef);
671 #TODO# # implement coordinate-grabbing
672 }
673}
674
675sub update_board {
676 my ($self) = @_;
677
678 return unless $self->{path};
679
680 $self->{board_label}->set_text ("Move " . ($self->{showmove} - 1));
681
682 $self->{cur_board} = new KGS::Game::Board $self->{size};
683 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]);
684
685 $self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_])
686 for COLOUR_WHITE, COLOUR_BLACK;
687
688 $self->{board}->set_board ($self->{cur_board});
689
690 if ($self->{cur_board}{score}) {
691 $self->{score_inlay} ||= $self->{chat}->new_inlay;
692 $self->{score_inlay}->clear;
693 $self->{score_inlay}->append_text ("\n<header>Scoring</header>"
694 . "\n<score>"
695 . "White: $self->{cur_board}{score}[COLOUR_WHITE], "
696 . "Black: $self->{cur_board}{score}[COLOUR_BLACK]"
697 . "</score>");
698 } elsif ($self->{score_inlay}) {
699 (delete $self->{score_inlay})->clear;
700 }
701
702 $self->update_cursor;
703
704 if ($self->{showmove} == @{$self->{path}}) {
705 $self->{timers} = [
706 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}],
707 [$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}],
708 ];
709 $self->update_timers ($self->{timers});
710 } else {
711 $self->update_timers ([
712 [0, @{$self->{cur_board}{timer}[0]}],
713 [0, @{$self->{cur_board}{timer}[1]}],
714 ]);
715 }
716
717}
718
719sub event_update_tree {
720 my ($self) = @_;
721
722 (delete $self->{undo_inlay})->clear
723 if $self->{undo_inlay};
724
725 $self->{path} = $self->get_path;
726
727 if ($self->{moveadj}) {
728 my $upper = $self->{moveadj}->upper;
729 my $pos = $self->{moveadj}->get_value;
730 my $move = scalar @{$self->{path}};
731
732 $self->{moveadj}->upper ($move);
733
734 $self->{moveadj}->changed;
735 if ($pos == $upper) {
736 $self->{moveadj}->value ($move);
737 $self->{moveadj}->value_changed;
738 }
739 }
740}
741
742sub event_update_comments {
743 my ($self, $node, $comment, $newnode) = @_;
744 $self->SUPER::event_update_comments ($node, $comment, $newnode);
745
746 my $text;
747
748 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
749 if $newnode;
750
751 for (split /\n/, $comment) {
752 $text .= "\n";
753 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
754 $text .= "<user>" . (util::toxml $1) . "</user>:";
755 }
756
757 # coords only for 19x19 so far
758 $_ = util::toxml $_;
759 s{
760 (
761 \b
762 (?:[bw])?
763 [, ]{0,2}
764 [a-hj-t] # valid for upto 19x19
765 \s?
766 [1-9]?[0-9]
767 \b
768 )
769 }{
770 "<coord>$1</coord>";
771 }sgexi;
772
773 $text .= $_;
774 }
775
776 $self->{chat}->append_text ($text);
777}
778
779sub event_move {
780 my ($self, $pass) = @_;
781
782 sound::play 1, $pass ? "pass" : "move";
783}
784
785### GAMEPLAY EVENTS #########################################################
786
787sub event_resign_game {
788 my ($self, $player) = @_;
789
790 sound::play 3, "resign";
791 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
792 . "\n<user>"
793 . (util::toxml $self->{user}[$player]->as_string)
794 . "</user> resigned."
795 . "\n<user>"
796 . (util::toxml $self->{user}[1 - $player]->as_string)
797 . "</user> wins the game."
798 . "</infoblock>");
799}
800
801sub event_out_of_time {
802 my ($self, $player) = @_;
803
804 sound::play 3, "timewin";
805 $self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>"
806 . "\n<user>"
807 . (util::toxml $self->{user}[$msg->{player}]->as_string)
808 . "</user> ran out of time and lost."
809 . "\n<user>"
810 . (util::toxml $self->{user}[1 - $msg->{player}]->as_string)
811 . "</user> wins the game."
812 . "</infoblock>");
813}
814
815sub event_owner_left {
816 my ($self) = @_;
817
818 $self->{chat}->append_text ("\n<infoblock><header>Owner left</header>"
819 . "\nThe owner of this game left.</infoblock>");
820}
821
822sub event_teacher_left {
823 my ($self) = @_;
824
825 $self->{chat}->append_text ("\n<infoblock><header>Teacher left</header>"
826 . "\nThe teacher left the game.</infoblock>");
827}
828
829sub event_done {
830 my ($self) = @_;
831
832 if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) {
833 sound::play 2, "info" unless $inlay->{count};
834 $self->{chat}->append_text ("\n<infoblock><header>Press Done</header>"
835 . "\nYour opponent pressed done. Now it's up to you.");
836 }
837 if ($self->{doneid} & 0x80000000) {
838 sound::play 2, "info" unless $inlay->{count};
839 $self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>"
840 . "\nThe board has changed.");
841 }
842
843 $self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]);
844
845 $self->{chat}->set_end;
846}
847
848sub inject_final_result {
849 my ($self, $msg) = @_;
850
851 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
852 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
853 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
854 . "</infoblock>"
855 );
856}
857
858sub inject_req_undo {
859 my ($self, $msg) = @_;
860
861 my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay;
862 return if $inlay->{ignore};
863
864 sound::play 2, "warning" unless $inlay->{count};
865 $inlay->{count}++;
866
867 $inlay->clear;
868 $inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n");
869 $inlay->append_button ("Grant", sub {
870 (delete $self->{undo_inlay})->clear;
871 $self->send (grant_undo => channel => $self->{channel});
872 });
873 $inlay->append_button ("Ignore", sub {
874 $inlay->clear;
875 $inlay->{ignore} = 1;
876 # but leave inlay, so further undo requests get counted
877 });
878
879 $self->{chat}->set_end;
880}
881
882sub inject_new_game {
883 my ($self, $msg) = @_;
884
885 if ($msg->{cid} != $self->{cid}) {
886 $self->part;
887 warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d#
888 }
889
890 $self->{chat}->append_text ("\n<header>Game successfully created on server.</header>");
891 delete $self->{cid};
892}
893
894### CHALLENGE HANDLING ######################################################
895
896sub draw_challenge {
897 my ($self, $id) = @_;
898
899 my $info = $self->{challenge}{$id};
900 my $inlay = $info->{inlay};
901 my $rules = $info->{rules};
902
903 my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;;
904 my $opponent = $as_black ? $info->{white} : $info->{black};
905
906 my ($size, $time, $interval, $count, $type);
907
908 if (!$self->{channel}) {
909 $inlay->append_text ("\nNotes: ");
910 $inlay->append_widget (gtk::textentry \$info->{notes}, 20, "");
911 $inlay->append_text ("\nGlobal Offer: ");
912 $inlay->append_optionmenu (\$info->{flags},
913 0 => "No",
914 2 => "Yes",
915 );
916 } else {
917 $inlay->append_text ("\nNotes: " . util::toxml $info->{notes});
918 }
919
920 $inlay->append_text ("\nType: ");
921 $type = $inlay->append_optionmenu (
922 \$info->{gametype},
923 GAMETYPE_DEMONSTRATION , "Demonstration (not yet)",
924 GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)",
925 GAMETYPE_TEACHING , "Teaching (not yet)",
926 GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)",
927 GAMETYPE_SIMUL , "Simul (not yet!)",
928 GAMETYPE_FREE , "Free",
929 GAMETYPE_RATED , "Rated",
930 sub {
931 $size->set_history (2) if $_[0] eq GAMETYPE_RATED;
932 },
933 );
934
935 if ($self->{channel}) {
936 $inlay->append_text ("\nMy Colour: ");
937 $inlay->append_optionmenu (
938 \$as_black,
939 0 => "White",
940 1 => "Black",
941 sub {
942 if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) {
943 ($info->{black}, $info->{white}) = ($info->{white}, $info->{black});
395 } 944 }
396 } 945 }
946 );
947 }
397 948
398 # and lastly any markers 949 $inlay->append_text ("\nRuleset: ");
399 my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B)); 950 $inlay->append_optionmenu (
951 \$info->{rules}{ruleset},
952 RULESET_JAPANESE , "Japanese",
953 RULESET_CHINESE , "Chinese",
954 RULESET_AGA , "AGA",
955 RULESET_NEW_ZEALAND, "New Zealand",
956 );
400 957
401 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 958 $inlay->append_text ("\nSize: ");
402 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 959 $size = $inlay->append_optionmenu (
403 [MARK_SQUARE, $::square_img[$dark_bg]]) { 960 \$info->{rules}{size},
404 my ($mask, $img) = @$_; 961 (9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38),
405 if ($mark & $mask) { 962 sub {
406 $img->composite ( 963 $type->set_history (5) # reset to free
407 $base, 0, 0, $size, $size, 0, 0, 964 if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED;
408 $size / $img->get_width, $size / $img->get_height,
409 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
410 );
411 }
412 }
413
414 push @$$c, $base;
415 } 965 },
416 } 966 );
417 967
418 $$c->[$rand % @$$c]; 968 if ($self->{channel}) {
419} 969 $inlay->append_text ("\nHandicap: ");
970 $inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9);
420 971
421sub pixbuf_text { 972 $inlay->append_text ("\nKomi: ");
422 my ($pixbuf, $colour, $x, $y, $height, $text) = @_; 973 $inlay->append_widget (gtk::numentry \$info->{rules}{komi}, 5);
423
424 my @c = grep $_,
425 map $::font[$colour][$::fontmap{$_}],
426 split //, $text;
427
428 if (@c) {
429 my $spacing = $height * 0.1;
430 my $s = $height / List::Util::max map $_->get_height, @c;
431 my $W = List::Util::sum map $_->get_width, @c;
432
433 $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
434 $y -= $height * 0.5;
435
436 for (@c) {
437 my $w = $_->get_width * $s;
438 # +2 == don't fight the rounding
439 $_->composite ($pixbuf,
440 $x, $y, $w+2, $height+2, $x, $y, $s, $s,
441 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
442
443 $x += $w + $spacing;
444 }
445 }
446}
447
448sub pixbuf_rect {
449 my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
450 # we fake lines by... a horrible method :/
451 my $colour_pb = new_pixbuf 1, 1, 0, $colour;
452 $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
453 INTERP_NEAREST, $alpha);
454}
455
456sub repaint_board {
457 my ($self) = @_;
458 my $canvas = $self->{canvas};
459 my $expose_area = undef;
460
461 return $expose_area unless $self->{board};
462
463 my ($w, $h) = ($canvas->allocation->values)[2,3];
464
465 die "FATAL: board aspect ratio != 1" unless $w == $h;
466 974 }
467 my $s = $w;
468 975
469 return unless $s >= 200; 976 $inlay->append_text ("\nTimesys: ");
470 977 $inlay->append_optionmenu (
471 my $size = $self->{size}; 978 \$info->{rules}{timesys},
979 &TIMESYS_NONE => "None",
980 &TIMESYS_ABSOLUTE => "Absolute",
981 &TIMESYS_BYO_YOMI => "Byo Yomi",
982 &TIMESYS_CANADIAN => "Canadian",
983 sub {
984 my ($new) = @_;
472 985
473 # we leave enough space for the shadows.. I like smaller stones, and we 986 if ($new eq TIMESYS_NONE) {
474 # do no need to do the nifty recursive screen updates that goban2 does 987 $time->hide;
475 my $border = int ($s / ($size + 3) * 0.5); 988 $interval->hide;
476 my $s2 = $s - $border * 2; 989 $count->hide;
477 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
478 my $ofs = int ($edge / 2);
479
480 my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
481
482 my $pixbuf;
483
484 my $oldboard;
485
486 if ($self->{background}) {
487 if ($oldboard = $self->{board_shown}) {
488 $pixbuf = $self->{pixbuf};
489 } else { 990 } else {
490 $pixbuf = $self->{background}->copy; 991 $time->show;
491 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s; 992 $time->set_text ($self->{app}{defaults}{time});
492 } 993 if ($new eq TIMESYS_ABSOLUTE) {
493 } else { 994 $interval->hide;
494 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s; 995 $count->hide;
495
496 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
497
498 if ($s < $bw && $s < $bh) {
499 $pixbuf = new_pixbuf $s, $s, 0;
500 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
501 } else { 996 } else {
502 $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES; 997 $interval->show;
503 } 998 $count->show;
504 999 if ($new eq TIMESYS_BYO_YOMI) {
505 my $linew = int ($s / 40 / $size); 1000 $interval->set_text ($self->{app}{defaults}{byo_time});
506 1001 $count->set_text ($self->{app}{defaults}{byo_period});
507 # ornamental border... we have time to waste :/ 1002 } elsif ($new eq TIMESYS_CANADIAN) {
508 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255; 1003 $interval->set_text ($self->{app}{defaults}{can_time});
509 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255; 1004 $count->set_text ($self->{app}{defaults}{can_period});
510 pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
511 pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
512
513 for my $i (1 .. $size) {
514 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
515 pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
516
517 # 38 max, but we allow a bit more
518 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
519 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];
520
521 pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
522 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
523 pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
524 pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
525
526 $a++;
527 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
528 }
529
530 # hoshi points
531 my $hoshi = sub {
532 my ($x, $y) = @_;
533 my $hs = int ($edge / 4) | 1;
534 $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2;
535
536 # we use the shadow mask... not perfect, but I want to finish this
537 $::shadow_img->composite ($pixbuf,
538 $x, $y, $hs + 1, $hs + 1, $x, $y,
539 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
540 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
541 };
542
543 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
544 $hoshi->($h1, $h1);
545 $hoshi->($size - $h1 + 1, $h1);
546 $hoshi->($h1, $size - $h1 + 1);
547 $hoshi->($size - $h1 + 1, $size - $h1 + 1);
548
549 if ($size % 2) { # on odd boards, also the remaining 5
550 my $h2 = ($size + 1) / 2;
551 if ($size > 10) {
552 $hoshi->($h1, $h2);
553 $hoshi->($size - $h1 + 1, $h2);
554 $hoshi->($h2, $size - $h1 + 1);
555 $hoshi->($h2, $h1);
556 }
557 # the tengen
558 $hoshi->($h2, $h2);
559 }
560
561 unless ($::config->{conserve_memory} > 1) {
562 $self->{background} = $pixbuf;
563 $pixbuf = $pixbuf->copy;
564 }
565 }
566
567 $self->{pixbuf} = $pixbuf;
568
569 # hoshi-points(!)#d#
570 # caching of empty board gfx(!)#d#
571
572 for my $x (1 .. $size) {
573 for my $y (1 .. $size) {
574 my $rand = ($x ^ $y ^ 0x5555);
575
576 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
577
578 if ($::config->{randomize}) {
579 $dx += ($rand % 7) - 3;
580 $dy += ($rand / 3 % 7) - 3;
581 }
582
583 my $shadow = $edge * 0.05;
584 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
585
586 my $mark = $self->{board}{board}[$x-1][$y-1];
587 my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
588
589 if ($oldboard) {
590 next if $old == $mark; # no change
591
592 $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
593 $expose_area = $expose_area
594 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
595 : new Gtk2::Gdk::Rectangle @$area;
596 }
597
598 if ($mark) {
599 my $pb = $self->create_stack($mark, $edge, $rand);
600
601 $pb->composite ($pixbuf, @$area,
602 $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
603
604 # labels are handled here because they are quite rare
605 if ($mark & MARK_LABEL) {
606 my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
607
608 if ($white) {
609 pixbuf_text $pixbuf, 0,
610 $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
611 $self->{board}{label}[$x-1][$y-1];
612 } 1005 }
613 pixbuf_text $pixbuf, $white,
614 $k[$x], $k[$y], $ofs * 0.7,
615 $self->{board}{label}[$x-1][$y-1];
616 } 1006 }
617 } 1007 }
618 } 1008 }
619 } 1009 );
620 1010
621 $self->{board_shown} = Storable::dclone $self->{board}; 1011 $inlay->append_text ("\nMain Time: ");
622 #d# save 1012 $time = $inlay->append_widget (gtk::timeentry \$info->{rules}{time}, 5);
623 #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable"; 1013 $inlay->append_text ("\nInterval: ");
1014 $interval = $inlay->append_widget (gtk::timeentry \$info->{rules}{interval}, 5);
1015 $inlay->append_text ("\nPeriods/Stones: ");
1016 $count = $inlay->append_widget (gtk::numentry \$info->{rules}{count}, 5);
624 1017
625 $expose_area; 1018 $inlay->append_text ("\n");
626}
627 1019
628sub redraw {
629 my ($self, $area) = @_;
630
631 if ($area && $self->{pixbuf}) {
632 my ($x, $y, $w, $h) = $area->values;
633
634 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
635 $x, $y, $x, $y, $w, $h,
636 "normal", 0, 0);
637 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
638 $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
639 }
640}
641
642sub update_board {
643 my ($self) = @_;
644 return unless $self->{path};
645
646 my $move = int $self->{moveadj}->get_value;
647
648 my $running = $move == @{$self->{path}};
649
650 $self->{board_label}->set_text ("Move $move");
651
652 $self->{board} = new KGS::Game::Board $self->{size};
653 $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
654
655 $self->{userpanel}[WHITE]->set_state ($self->{board}{captures}[WHITE],
656 $self->{board}{timer}[WHITE],
657 $running && $self->{board}{last} == BLACK);
658 $self->{userpanel}[BLACK]->set_state ($self->{board}{captures}[BLACK],
659 $self->{board}{timer}[BLACK],
660 $running && $self->{board}{last} == WHITE);
661
662 $self->redraw ($self->repaint_board);
663}
664
665sub event_update_tree {
666 my ($self) = @_;
667
668 $self->{path} = $self->get_path;
669
670 $self->{userpanel}[WHITE]->set_rules ($self->{path}[0]); # should be onload only
671 $self->{userpanel}[BLACK]->set_rules ($self->{path}[0]); # should be onload only
672
673 if ($self->{moveadj}) { 1020 if (!$self->{channel}) {
674 my $upper = $self->{moveadj}->upper; 1021 $inlay->append_button ("Create Challenge", sub {
675 my $pos = $self->{moveadj}->get_value; 1022 $inlay->clear;
676 1023 $self->{cid} = $self->{conn}->alloc_clientid;
677 $self->{moveadj}->upper (scalar @{$self->{path}}); 1024 $self->send (new_game =>
1025 channel => delete $self->{roomid},
1026 gametype => $info->{gametype},
1027 cid => $self->{cid},
1028 flags => $info->{flags},
1029 rules => $info->{rules},
1030 notes => $info->{notes},
1031 );
678 1032 });
679 $self->{moveadj}->changed;
680 if ($pos == $upper) {
681 $self->{moveadj}->set_value (scalar @{$self->{path}});
682 } else { 1033 } else {
683 $self->{moveadj}->value_changed; 1034 $inlay->append_button ("OK", sub {
1035 $inlay->clear;
1036 $self->send (challenge =>
1037 channel => $self->{channel},
1038 black => $info->{black},
1039 white => $info->{white},
1040 gametype => $info->{gametype},
1041 cid => $info->{cid},
1042 rules => $info->{rules},
1043 );
1044 });
1045 if (exists $self->{challenge}{""}) {
1046 $inlay->append_button ("Reject", sub {
1047 $inlay->clear;
1048 $self->send (reject_challenge =>
1049 channel => $self->{channel},
1050 name => $opponent->{name},
1051 gametype => $info->{gametype},
1052 cid => $info->{cid},
1053 rules => $info->{rules},
1054 );
1055 });
684 } 1056 }
685 } 1057 }
686} 1058}
687 1059
688sub event_update_comments { 1060sub new_game_challenge {
689 my ($self, $node, $comment, $newnode) = @_; 1061 my ($self) = @_;
690 $self->SUPER::event_update_comments($node, $comment, $newnode);
691 1062
692 my $text; 1063 my $d = $self->{app}{defaults};
693 1064
694 $text .= "\n<header>Move <move>$node->{move}</move>, <header>Node <node>$node->{id}</node></header>" 1065 $self->{challenge}{""} = {
695 if $newnode; 1066 gametype => $d->{gametype},
696 1067 flags => 0,
697 for (split /\n/, $comment) { 1068 notes => $d->{stones},
698 $text .= "\n"; 1069 rules => {
699 if ($_ =~ s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) { 1070 ruleset => $d->{ruleset},
700 $text .= "<user>" . (util::toxml $1) . "</user>:"; 1071 size => $d->{size},
1072 timesys => $d->{timesys},
1073 time => $d->{time},
1074 interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time},
1075 count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones},
701 } 1076 },
702 $text .= $_; 1077
1078 inlay => $self->{chat}->new_inlay,
703 } 1079 };
704 1080 $self->draw_challenge ("");
705 $self->{text}->append_text ($text);
706} 1081}
707 1082
708sub event_join { 1083sub event_challenge {
709 my ($self) = @_;
710 $self->SUPER::event_join;
711}
712
713sub event_part {
714 my ($self) = @_;
715 $self->SUPER::event_part;
716}
717
718sub event_move {
719 my ($self, $pass) = @_; 1084 my ($self, $info) = @_;
720 sound::play 1, $pass ? "pass" : "move";
721}
722 1085
723sub event_update_game { 1086 my $as_black = $info->{black}->{name} eq $self->{conn}{name};
724 my ($self) = @_; 1087 my $opponent = $as_black ? $info->{white} : $info->{black};
725 $self->SUPER::event_update_game;
726 warn "GAME UPDATE ".join (":", %$self);
727 warn "SAVED ".$self->is_saved;
728 warn "SCORED ".$self->is_scored;
729 warn "ADJ ".$self->is_adjourned;
730 warn "VALID ".$self->is_valid;
731 warn "MOVES ".$self->moves;
732 warn "TYPE ".$self->type;
733 1088
734 $self->{left}->remove ($_) for $self->{left}->get_children; 1089 my $id = $opponent->{name};
735 if ($self->is_valid) {
736 $self->{left}->add ($self->{boardbox});
737 (delete $self->{challenge})->destroy if $self->{challenge};
738 } else {
739 $self->{left}->add ($self->{challenge});
740 }
741 $self->{left}->show_all;
742}
743 1090
744sub destroy { 1091 sound::play 2, "info";
745 my ($self) = @_; 1092
746 (delete $self->{userpanel}[WHITE])->destroy if $self->{userpanel}[WHITE]; 1093 $self->{challenge}{$id} = $info;
747 (delete $self->{userpanel}[BLACK])->destroy if $self->{userpanel}[BLACK]; 1094 $self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay (
748 $self->SUPER::destroy; 1095 exists $self->{challenge}{""}
749 delete $appwin::gamelist->{game}{$self->{channel}}; 1096 ? "Challenge from " . $opponent->as_string
1097 : "Challenge to " . $opponent->as_string,
1098 sub {
1099 $self->{challenge}{$id}{inlay} = $_[0];
1100 $self->draw_challenge ($id);
1101 },
1102 !exists $self->{challenge}{""} # only open when not offerer
1103 );
750} 1104}
751 1105
7521; 11061;
753 1107

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines