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.60 by pcg, Wed Jun 18 00:24:30 2003 UTC vs.
Revision 1.111 by root, Mon May 31 17:14:25 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines