1 |
pcg |
1.36 |
use utf8; |
2 |
|
|
|
3 |
pcg |
1.82 |
use Scalar::Util (); |
4 |
|
|
|
5 |
root |
1.111 |
### GO CLOCK WIDGET ######################################################### |
6 |
|
|
|
7 |
pcg |
1.23 |
package game::goclock; |
8 |
|
|
|
9 |
pcg |
1.37 |
# Lo and Behold! I admit it! The rounding stuff etc.. in goclock |
10 |
|
|
# is completely borked. |
11 |
|
|
|
12 |
pcg |
1.23 |
use Time::HiRes (); |
13 |
|
|
|
14 |
|
|
use KGS::Constants; |
15 |
|
|
|
16 |
pcg |
1.82 |
use Glib::Object::Subclass |
17 |
|
|
Gtk2::Label; |
18 |
pcg |
1.23 |
|
19 |
pcg |
1.82 |
sub INIT_INSTANCE { |
20 |
|
|
my $self = shift; |
21 |
pcg |
1.23 |
|
22 |
pcg |
1.84 |
$self->signal_connect (destroy => sub { $_[0]->stop }); |
23 |
pcg |
1.83 |
|
24 |
pcg |
1.23 |
$self->{set} = sub { }; |
25 |
pcg |
1.86 |
$self->{format} = sub { "???" }; |
26 |
pcg |
1.82 |
} |
27 |
pcg |
1.23 |
|
28 |
root |
1.106 |
sub FINALIZE_INSTANCE { |
29 |
|
|
my $self = shift; |
30 |
|
|
|
31 |
|
|
$self->stop; |
32 |
|
|
} |
33 |
|
|
|
34 |
pcg |
1.38 |
sub configure { |
35 |
pcg |
1.23 |
my ($self, $timesys, $main, $interval, $count) = @_; |
36 |
|
|
|
37 |
|
|
if ($timesys == TIMESYS_ABSOLUTE) { |
38 |
pcg |
1.105 |
$self->{format} = sub { |
39 |
root |
1.113 |
if ($_[0] < 0) { |
40 |
pcg |
1.105 |
"TIMEOUT"; |
41 |
|
|
} else { |
42 |
|
|
util::format_time $_[0]; |
43 |
|
|
} |
44 |
|
|
}; |
45 |
pcg |
1.23 |
|
46 |
|
|
} elsif ($timesys == TIMESYS_BYO_YOMI) { |
47 |
|
|
my $low = $interval * $count; |
48 |
|
|
|
49 |
|
|
$self->{format} = sub { |
50 |
root |
1.113 |
if ($_[0] < 0) { |
51 |
pcg |
1.105 |
"TIMEOUT"; |
52 |
|
|
} elsif ($_[0] > $low) { |
53 |
pcg |
1.37 |
util::format_time $_[0] - $low; |
54 |
pcg |
1.23 |
} else { |
55 |
pcg |
1.34 |
sprintf "%s (%d)", |
56 |
pcg |
1.37 |
util::format_time int (($_[0] - 1) % $interval + 1), |
57 |
pcg |
1.43 |
($_[0] - 1) / $interval; |
58 |
pcg |
1.23 |
} |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
} elsif ($timesys == TIMESYS_CANADIAN) { |
62 |
|
|
$self->{format} = sub { |
63 |
root |
1.113 |
if ($_[0] < 0) { |
64 |
pcg |
1.105 |
"TIMEOUT"; |
65 |
|
|
} elsif (!$self->{moves}) { |
66 |
pcg |
1.37 |
util::format_time $_[0] - $low; |
67 |
pcg |
1.23 |
} else { |
68 |
pcg |
1.38 |
my $time = int (($_[0] - 1) % $interval + 1); |
69 |
|
|
|
70 |
pcg |
1.62 |
sprintf "%s/%d =%d", |
71 |
pcg |
1.37 |
util::format_time $time, |
72 |
pcg |
1.34 |
$self->{moves}, |
73 |
pcg |
1.75 |
$self->{moves} > 1 |
74 |
|
|
? $time / $self->{moves} |
75 |
|
|
: $interval; |
76 |
pcg |
1.23 |
} |
77 |
|
|
}; |
78 |
|
|
|
79 |
|
|
} else { |
80 |
|
|
# none, or unknown |
81 |
pcg |
1.105 |
$self->{format} = sub { "-" } |
82 |
pcg |
1.23 |
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
sub refresh { |
86 |
|
|
my ($self, $timestamp) = @_; |
87 |
pcg |
1.38 |
my $timer = $self->{time} + $self->{start} - $timestamp; |
88 |
pcg |
1.102 |
|
89 |
pcg |
1.36 |
# we round the timer value slightly... the protocol isn't exact anyways, |
90 |
|
|
# and this gives smoother timers ;) |
91 |
pcg |
1.102 |
my $timer2 = int $timer + 0.4; |
92 |
|
|
|
93 |
pcg |
1.105 |
$self->set_text ($self->{format}->($timer2)); |
94 |
pcg |
1.23 |
|
95 |
|
|
$timer - int $timer; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
sub set_time { |
99 |
root |
1.106 |
my ($self, $start, $time, $moves) = @_; |
100 |
|
|
|
101 |
|
|
$self->{time} = $time; |
102 |
|
|
$self->{moves} = $moves; |
103 |
pcg |
1.23 |
|
104 |
root |
1.106 |
if ($start) { |
105 |
|
|
$self->{start} = $start; |
106 |
|
|
$self->start; |
107 |
|
|
} else { |
108 |
|
|
$self->stop; |
109 |
pcg |
1.27 |
$self->refresh ($self->{start}); |
110 |
|
|
} |
111 |
pcg |
1.23 |
} |
112 |
|
|
|
113 |
|
|
sub start { |
114 |
root |
1.106 |
my ($self) = @_; |
115 |
pcg |
1.23 |
|
116 |
pcg |
1.36 |
$self->stop; |
117 |
pcg |
1.23 |
|
118 |
|
|
my $timeout; $timeout = sub { |
119 |
pcg |
1.36 |
my $next = $self->refresh (Time::HiRes::time) * 1000; |
120 |
pcg |
1.27 |
$next += 1000 if $next < 0; |
121 |
pcg |
1.23 |
$self->{timeout} = add Glib::Timeout $next, $timeout; |
122 |
|
|
0; |
123 |
|
|
}; |
124 |
|
|
|
125 |
|
|
$timeout->(); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
sub stop { |
129 |
|
|
my ($self) = @_; |
130 |
|
|
|
131 |
|
|
remove Glib::Source delete $self->{timeout} if $self->{timeout}; |
132 |
pcg |
1.24 |
} |
133 |
|
|
|
134 |
root |
1.111 |
### USER PANEL ############################################################## |
135 |
|
|
|
136 |
pcg |
1.23 |
package game::userpanel; |
137 |
|
|
|
138 |
root |
1.113 |
use KGS::Constants; |
139 |
|
|
|
140 |
pcg |
1.82 |
use Glib::Object::Subclass |
141 |
root |
1.113 |
Gtk2::Frame, |
142 |
pcg |
1.82 |
properties => [ |
143 |
root |
1.113 |
Glib::ParamSpec->IV ("colour", "colour", "User Colour", |
144 |
|
|
COLOUR_BLACK, COLOUR_WHITE, COLOUR_BLACK, |
145 |
|
|
[qw(construct-only readable writable)]), |
146 |
pcg |
1.82 |
]; |
147 |
pcg |
1.23 |
|
148 |
pcg |
1.82 |
sub INIT_INSTANCE { |
149 |
|
|
my ($self) = @_; |
150 |
pcg |
1.23 |
|
151 |
pcg |
1.82 |
$self->add (my $vbox = new Gtk2::VBox); |
152 |
pcg |
1.26 |
|
153 |
root |
1.113 |
$vbox->pack_start (($self->{name} = new Gtk2::Label "-"), 1, 1, 0); |
154 |
|
|
$vbox->pack_start (($self->{info} = new Gtk2::Label "-"), 1, 1, 0); |
155 |
|
|
$vbox->pack_start (($self->{clock} = new game::goclock), 1, 1, 0); |
156 |
pcg |
1.23 |
|
157 |
pcg |
1.32 |
$vbox->add ($self->{imagebox} = new Gtk2::VBox); |
158 |
|
|
|
159 |
pcg |
1.23 |
$self; |
160 |
|
|
} |
161 |
|
|
|
162 |
pcg |
1.37 |
sub configure { |
163 |
pcg |
1.69 |
my ($self, $app, $user, $rules) = @_; |
164 |
pcg |
1.23 |
|
165 |
pcg |
1.38 |
if ($self->{name}->get_text ne $user->as_string) { |
166 |
|
|
$self->{name}->set_text ($user->as_string); |
167 |
pcg |
1.26 |
|
168 |
pcg |
1.32 |
$self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
169 |
|
|
$self->{imagebox}->add (gtk::image_from_data undef); |
170 |
pcg |
1.33 |
$self->{imagebox}->show_all; |
171 |
pcg |
1.32 |
|
172 |
pcg |
1.52 |
if ($user->has_pic) { |
173 |
|
|
# the big picture... |
174 |
pcg |
1.69 |
$app->userpic ($user->{name}, sub { |
175 |
pcg |
1.52 |
return unless $self->{imagebox}; |
176 |
pcg |
1.69 |
|
177 |
pcg |
1.52 |
if ($_[0]) { |
178 |
|
|
$self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; |
179 |
|
|
$self->{imagebox}->add (gtk::image_from_data $_[0]); |
180 |
|
|
$self->{imagebox}->show_all; |
181 |
|
|
} |
182 |
|
|
}); |
183 |
|
|
} |
184 |
pcg |
1.26 |
} |
185 |
|
|
|
186 |
pcg |
1.38 |
$self->{clock}->configure (@{$rules}{qw(timesys time interval count)}); |
187 |
pcg |
1.23 |
} |
188 |
|
|
|
189 |
pcg |
1.102 |
sub set_captures { |
190 |
|
|
my ($self, $captures) = @_; |
191 |
|
|
|
192 |
|
|
$self->{info}->set_text ("$captures pris."); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
sub set_timer { |
196 |
root |
1.106 |
my ($self, $start, $time, $moves) = @_; |
197 |
pcg |
1.23 |
|
198 |
root |
1.106 |
$self->{clock}->set_time ($start, $time, $moves); |
199 |
pcg |
1.23 |
} |
200 |
|
|
|
201 |
root |
1.111 |
### GAME WINDOW ############################################################# |
202 |
|
|
|
203 |
pcg |
1.1 |
package game; |
204 |
|
|
|
205 |
pcg |
1.83 |
use Scalar::Util qw(weaken); |
206 |
|
|
|
207 |
pcg |
1.1 |
use KGS::Constants; |
208 |
|
|
use KGS::Game::Board; |
209 |
|
|
|
210 |
pcg |
1.74 |
use Gtk2::GoBoard; |
211 |
pcg |
1.97 |
use Gtk2::GoBoard::Constants; |
212 |
pcg |
1.74 |
|
213 |
pcg |
1.102 |
use base KGS::Game; |
214 |
|
|
use base KGS::Listener::Game; |
215 |
|
|
|
216 |
pcg |
1.83 |
use Glib::Object::Subclass |
217 |
|
|
Gtk2::Window; |
218 |
|
|
|
219 |
pcg |
1.15 |
use POSIX qw(ceil); |
220 |
|
|
|
221 |
pcg |
1.1 |
sub new { |
222 |
pcg |
1.83 |
my ($self, %arg) = @_; |
223 |
|
|
$self = $self->Glib::Object::new; |
224 |
|
|
$self->{$_} = delete $arg{$_} for keys %arg; |
225 |
pcg |
1.1 |
|
226 |
pcg |
1.83 |
gtk::state $self, "game::window", undef, window_size => [600, 500]; |
227 |
pcg |
1.1 |
|
228 |
pcg |
1.87 |
$self->signal_connect (destroy => sub { |
229 |
|
|
$self->unlisten; |
230 |
|
|
delete $self->{app}{game}{$self->{channel}}; |
231 |
|
|
%{$_[0]} = (); |
232 |
|
|
});#d# |
233 |
pcg |
1.1 |
|
234 |
pcg |
1.83 |
$self->add (my $hpane = new Gtk2::HPaned); |
235 |
|
|
gtk::state $hpane, "game::hpane", undef, position => 500; |
236 |
pcg |
1.1 |
|
237 |
pcg |
1.36 |
# LEFT PANE |
238 |
|
|
|
239 |
pcg |
1.83 |
$hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0); |
240 |
pcg |
1.31 |
|
241 |
pcg |
1.83 |
$hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
242 |
pcg |
1.1 |
|
243 |
pcg |
1.31 |
# board box (aspect/canvas) |
244 |
|
|
|
245 |
pcg |
1.91 |
# 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); |
251 |
pcg |
1.1 |
|
252 |
|
|
{ |
253 |
pcg |
1.66 |
$frame->add (my $vbox = new Gtk2::VBox); |
254 |
root |
1.113 |
$vbox->add ($self->{title} = new Gtk2::Label "-"); |
255 |
pcg |
1.66 |
|
256 |
|
|
$vbox->add (my $hbox = new Gtk2::HBox); |
257 |
|
|
|
258 |
root |
1.113 |
$hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 0, 0); |
259 |
pcg |
1.1 |
|
260 |
pcg |
1.63 |
$self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0; |
261 |
pcg |
1.2 |
|
262 |
pcg |
1.66 |
$hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0); |
263 |
pcg |
1.2 |
$scale->set_draw_value (0); |
264 |
pcg |
1.30 |
$scale->set_digits (0); |
265 |
pcg |
1.1 |
|
266 |
pcg |
1.104 |
$self->{moveadj}->signal_connect (value_changed => sub { |
267 |
|
|
$self->{showmove} = int $self->{moveadj}->get_value; |
268 |
|
|
$self->update_board; |
269 |
|
|
}); |
270 |
pcg |
1.1 |
} |
271 |
pcg |
1.31 |
|
272 |
pcg |
1.83 |
$vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0); |
273 |
pcg |
1.82 |
|
274 |
pcg |
1.83 |
$hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_) |
275 |
|
|
for COLOUR_WHITE, COLOUR_BLACK; |
276 |
pcg |
1.100 |
|
277 |
|
|
$vbox->pack_start ((my $buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
278 |
|
|
|
279 |
pcg |
1.101 |
$buttonbox->add ($self->{button_pass} = |
280 |
pcg |
1.102 |
Gtk2::Button->Glib::Object::new (label => "Pass", no_show_all => 1, visible => 0)); |
281 |
pcg |
1.101 |
$self->{button_pass}->signal_connect (clicked => sub { |
282 |
|
|
$self->{board_click}->(255, 255) if $self->{board_click}; |
283 |
|
|
}); |
284 |
pcg |
1.102 |
$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 |
|
|
}); |
294 |
pcg |
1.1 |
|
295 |
pcg |
1.86 |
$vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0); |
296 |
pcg |
1.73 |
|
297 |
pcg |
1.96 |
$self->set_channel ($self->{channel}); |
298 |
pcg |
1.86 |
|
299 |
pcg |
1.96 |
$self->show_all; |
300 |
pcg |
1.1 |
|
301 |
|
|
$self; |
302 |
|
|
} |
303 |
|
|
|
304 |
pcg |
1.96 |
sub 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 }); |
316 |
|
|
$self->{chat}->signal_connect (command => sub { |
317 |
|
|
my ($chat, $cmd, $arg) = @_; |
318 |
|
|
if ($cmd eq "rsave") { |
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 |
|
|
} |
323 |
|
|
}); |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
|
|
327 |
root |
1.111 |
### JOIN/LEAVE ############################################################## |
328 |
|
|
|
329 |
|
|
sub join { |
330 |
|
|
my ($self) = @_; |
331 |
|
|
return if $self->{joined}; |
332 |
|
|
|
333 |
|
|
$self->SUPER::join; |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
sub event_join { |
337 |
|
|
my ($self) = @_; |
338 |
|
|
|
339 |
|
|
$self->SUPER::event_join (@_); |
340 |
|
|
$self->init_tree; |
341 |
|
|
$self->event_update_game; |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
sub event_part { |
345 |
|
|
my ($self) = @_; |
346 |
|
|
|
347 |
|
|
$self->SUPER::event_part; |
348 |
|
|
$self->destroy; |
349 |
|
|
} |
350 |
|
|
|
351 |
root |
1.112 |
sub event_quit { |
352 |
|
|
my ($self) = @_; |
353 |
|
|
|
354 |
|
|
$self->SUPER::event_quit; |
355 |
|
|
$self->destroy; |
356 |
|
|
} |
357 |
|
|
|
358 |
root |
1.111 |
### USERS ################################################################### |
359 |
|
|
|
360 |
|
|
sub draw_users { |
361 |
|
|
my ($self, $inlay) = @_; |
362 |
|
|
|
363 |
|
|
for (sort keys %{$self->{users}}) { |
364 |
|
|
$inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>"); |
365 |
|
|
} |
366 |
|
|
} |
367 |
|
|
|
368 |
pcg |
1.1 |
sub event_update_users { |
369 |
pcg |
1.18 |
my ($self, $add, $update, $remove) = @_; |
370 |
pcg |
1.1 |
|
371 |
pcg |
1.88 |
# $self->{userlist}->update ($add, $update, $remove); |
372 |
pcg |
1.70 |
|
373 |
pcg |
1.97 |
$self->{challenge}{$_->{name}} && (delete $self->{challenge}{$_->{name}})->{inlay}->destroy |
374 |
|
|
for @$remove; |
375 |
|
|
|
376 |
pcg |
1.88 |
$self->{users_inlay}->refresh; |
377 |
pcg |
1.57 |
|
378 |
|
|
my %important; |
379 |
pcg |
1.98 |
$important{$self->{black}{name}}++; |
380 |
|
|
$important{$self->{white}{name}}++; |
381 |
|
|
$important{$self->{owner}{name}}++; |
382 |
pcg |
1.57 |
|
383 |
|
|
if (my @users = grep $important{$_->{name}}, @$add) { |
384 |
pcg |
1.73 |
$self->{chat}->append_text ("\n<header>Joins:</header>"); |
385 |
|
|
$self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
386 |
pcg |
1.57 |
} |
387 |
|
|
if (my @users = grep $important{$_->{name}}, @$remove) { |
388 |
pcg |
1.73 |
$self->{chat}->append_text ("\n<header>Parts:</header>"); |
389 |
|
|
$self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users; |
390 |
pcg |
1.57 |
} |
391 |
pcg |
1.1 |
} |
392 |
|
|
|
393 |
root |
1.111 |
### GAME INFO ############################################################### |
394 |
|
|
|
395 |
|
|
sub draw_setup { |
396 |
|
|
my ($self, $inlay) = @_; |
397 |
|
|
|
398 |
|
|
return unless $self->{joined}; |
399 |
|
|
|
400 |
|
|
my $rules = $self->{rules}; |
401 |
|
|
|
402 |
|
|
my $text = ""; |
403 |
|
|
|
404 |
|
|
$text .= "\nTeacher: <user>" . (util::toxml $self->{teacher}) . "</user>" |
405 |
|
|
if $self->{teacher}; |
406 |
|
|
|
407 |
|
|
$text .= "\nOwner: <user>" . (util::toxml $self->{owner}->as_string) . "</user>" |
408 |
|
|
if $self->{owner}->is_valid; |
409 |
|
|
|
410 |
|
|
if ($self->is_inprogress) { |
411 |
|
|
$text .= "\nPlayers: <user>" . (util::toxml $self->{white}->as_string) . "</user>" |
412 |
|
|
. " vs. <user>" . (util::toxml $self->{black}->as_string) . "</user>"; |
413 |
|
|
} |
414 |
|
|
$text .= "\nType: " . util::toxml $gametype{$self->type}; |
415 |
|
|
|
416 |
|
|
$text .= "\nRuleset: " . $ruleset{$rules->{ruleset}}; |
417 |
|
|
|
418 |
|
|
$text .= "\nTime: "; |
419 |
|
|
|
420 |
|
|
if ($rules->{timesys} == TIMESYS_NONE) { |
421 |
|
|
$text .= "UNLIMITED"; |
422 |
|
|
} elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) { |
423 |
|
|
$text .= util::format_time $rules->{time}; |
424 |
|
|
$text .= " ABS"; |
425 |
|
|
} elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) { |
426 |
|
|
$text .= util::format_time $rules->{time}; |
427 |
|
|
$text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count}; |
428 |
|
|
} elsif ($rules->{timesys} == TIMESYS_CANADIAN) { |
429 |
|
|
$text .= util::format_time $rules->{time}; |
430 |
|
|
$text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count}; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
$text .= "\nFlags:"; |
434 |
|
|
$text .= " private" if $self->is_private; |
435 |
|
|
$text .= " started" if $self->is_inprogress; |
436 |
|
|
$text .= " adjourned" if $self->is_adjourned; |
437 |
|
|
$text .= " scored" if $self->is_scored; |
438 |
|
|
$text .= " saved" if $self->is_saved; |
439 |
|
|
|
440 |
|
|
if ($self->is_inprogress) { |
441 |
|
|
$text .= "\nHandicap: " . $self->{handicap}; |
442 |
|
|
$text .= "\nKomi: " . $self->{komi}; |
443 |
|
|
$text .= "\nSize: " . $self->size_string; |
444 |
|
|
} |
445 |
|
|
|
446 |
|
|
if ($self->is_scored) { |
447 |
|
|
$text .= "\nResult: " . $self->score_string; |
448 |
|
|
} |
449 |
|
|
|
450 |
|
|
$inlay->append_text ("<infoblock>$text</infoblock>"); |
451 |
|
|
|
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
sub event_update_game { |
455 |
pcg |
1.1 |
my ($self) = @_; |
456 |
pcg |
1.21 |
|
457 |
root |
1.111 |
$self->SUPER::event_update_game; |
458 |
|
|
|
459 |
|
|
return unless $self->{joined}; |
460 |
|
|
|
461 |
|
|
$self->{colour} = $self->player_colour ($self->{conn}{name}); |
462 |
|
|
|
463 |
|
|
$self->{user}[COLOUR_BLACK] = $self->{black}; |
464 |
|
|
$self->{user}[COLOUR_WHITE] = $self->{white}; |
465 |
|
|
|
466 |
|
|
# show board |
467 |
|
|
if ($self->is_inprogress) { |
468 |
root |
1.113 |
if (!$self->{board}) { |
469 |
|
|
$self->{left}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size}); |
470 |
root |
1.111 |
$self->{board}->signal_connect (button_release => sub { |
471 |
root |
1.114 |
return unless $self->{cur_board}; |
472 |
root |
1.111 |
if ($_[1] == 1) { |
473 |
|
|
$self->{board_click}->($_[2], $_[3]) if $self->{board_click}; |
474 |
|
|
} |
475 |
|
|
}); |
476 |
root |
1.113 |
$self->{board}->show_all; |
477 |
root |
1.111 |
} |
478 |
|
|
if (my $ch = delete $self->{challenge}) { |
479 |
|
|
$_->{inlay}->destroy for values %$ch; |
480 |
|
|
} |
481 |
|
|
$self->update_cursor; |
482 |
|
|
} |
483 |
|
|
|
484 |
root |
1.113 |
my $title = defined $self->{channel} |
485 |
|
|
? $self->owner->as_string . " " . $self->opponent_string |
486 |
|
|
: "Game Window"; |
487 |
|
|
$self->set_title ("KGS Game $title"); |
488 |
|
|
$self->{title}->set_text ($title); # title gets redrawn wrongly |
489 |
root |
1.111 |
|
490 |
|
|
$self->{rules_inlay}->refresh; |
491 |
root |
1.113 |
|
492 |
|
|
if (exists $self->{teacher}) { |
493 |
|
|
$self->{teacher_inlay} ||= $self->{chat}->new_inlay; |
494 |
|
|
$self->{teacher_inlay}->clear; |
495 |
|
|
$self->{teacher_inlay}->append_text ("\n<header>Teacher:</header> <user>" |
496 |
|
|
. (util::toxml $self->{teacher}) . "</user>"); |
497 |
|
|
} elsif ($self->{teacher_inlay}) { |
498 |
|
|
(delete $self->{teacher_inlay})->clear; |
499 |
|
|
} |
500 |
root |
1.114 |
|
501 |
|
|
$self->update_cursor; |
502 |
root |
1.111 |
} |
503 |
|
|
|
504 |
|
|
sub event_update_rules { |
505 |
|
|
my ($self, $rules) = @_; |
506 |
|
|
|
507 |
|
|
$self->{rules} = $rules; |
508 |
|
|
|
509 |
|
|
if ($self->{user}) { |
510 |
root |
1.113 |
# todo. gets drawn wrongly |
511 |
|
|
|
512 |
root |
1.111 |
$self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules) |
513 |
|
|
for COLOUR_BLACK, COLOUR_WHITE; |
514 |
|
|
} |
515 |
|
|
|
516 |
|
|
sound::play 3, "gamestart"; |
517 |
|
|
$self->{rules_inlay}->refresh; |
518 |
|
|
} |
519 |
|
|
|
520 |
|
|
### BOARD DISPLAY ########################################################### |
521 |
|
|
|
522 |
|
|
sub update_timers { |
523 |
|
|
my ($self, $timers) = @_; |
524 |
|
|
|
525 |
|
|
my $running = $self->{showmove} == @{$self->{path}} && !$self->{teacher}; |
526 |
|
|
|
527 |
|
|
for my $colour (COLOUR_BLACK, COLOUR_WHITE) { |
528 |
|
|
my $t = $timers->[$colour]; |
529 |
|
|
$self->{userpanel}[$colour]->set_timer ( |
530 |
|
|
$running && $colour == $self->{whosemove} && $t->[0], |
531 |
|
|
$t->[1] || $self->{rules}{time} |
532 |
|
|
+ ($self->{rules}{timesys} == TIMESYS_BYO_YOMI |
533 |
|
|
&& $self->{rules}{interval} * $self->{rules}{count}), |
534 |
root |
1.113 |
$t->[2]); |
535 |
root |
1.111 |
} |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
sub inject_set_gametime { |
539 |
|
|
my ($self, $msg) = @_; |
540 |
|
|
|
541 |
|
|
$self->{timers} = [ |
542 |
|
|
[$msg->{NOW}, $msg->{black_time}, $msg->{black_moves}], |
543 |
|
|
[$msg->{NOW}, $msg->{white_time}, $msg->{white_moves}], |
544 |
|
|
]; |
545 |
|
|
|
546 |
|
|
$self->update_timers ($self->{timers}) |
547 |
|
|
if $self->{showmove} == @{$self->{path}}; |
548 |
pcg |
1.1 |
} |
549 |
|
|
|
550 |
pcg |
1.97 |
sub update_cursor { |
551 |
|
|
my ($self) = @_; |
552 |
|
|
|
553 |
root |
1.114 |
return unless $self->{cur_board}; |
554 |
|
|
|
555 |
pcg |
1.104 |
my $running = $self->{showmove} == @{$self->{path}} && $self->is_active; |
556 |
pcg |
1.99 |
|
557 |
|
|
delete $self->{board_click}; |
558 |
|
|
|
559 |
|
|
if ($self->{teacher} eq $self->{app}{conn}) { |
560 |
|
|
#TODO# # teaching mode not implemented |
561 |
root |
1.107 |
$self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); |
562 |
pcg |
1.102 |
$self->{button_undo}->hide; |
563 |
|
|
$self->{button_resign}->hide; |
564 |
pcg |
1.101 |
$self->{board}->set (cursor => undef); |
565 |
|
|
|
566 |
|
|
} elsif ($running && $self->{colour} != COLOUR_NONE) { |
567 |
|
|
# during game |
568 |
pcg |
1.102 |
$self->{button_undo}->show; |
569 |
|
|
$self->{button_resign}->show; |
570 |
pcg |
1.101 |
|
571 |
|
|
if ($self->{cur_board}{score}) { |
572 |
|
|
# during scoring |
573 |
root |
1.107 |
$self->{button_pass}->set (label => "Done", visible => 1, sensitive => 1); |
574 |
pcg |
1.101 |
$self->{board}->set (cursor => sub { |
575 |
|
|
$_[0] & (MARK_B | MARK_W) |
576 |
|
|
? $_[0] ^ MARK_GRAYED |
577 |
|
|
: $_[0]; |
578 |
|
|
}); |
579 |
|
|
$self->{board_click} = sub { |
580 |
|
|
if ($_[0] == 255) { |
581 |
|
|
$self->{button_pass}->sensitive (0); |
582 |
|
|
$self->done; |
583 |
|
|
} else { |
584 |
|
|
$self->send (mark_dead => |
585 |
|
|
channel => $self->{channel}, |
586 |
|
|
x => $_[0], |
587 |
|
|
y => $_[1], |
588 |
|
|
dead => !($self->{cur_board}{board}[$_[0]][$_[1]] & MARK_GRAYED), |
589 |
|
|
); |
590 |
|
|
} |
591 |
|
|
}; |
592 |
|
|
|
593 |
root |
1.106 |
} elsif ($self->{colour} == $self->{whosemove}) { |
594 |
pcg |
1.101 |
# normal move |
595 |
root |
1.107 |
$self->{button_pass}->set (label => "Pass", visible => 1, sensitive => 1); |
596 |
pcg |
1.101 |
$self->{board}->set (cursor => sub { |
597 |
root |
1.114 |
$self->{cur_board} |
598 |
|
|
&& $self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2], |
599 |
|
|
$self->{rules}{ruleset} == RULESET_NEW_ZEALAND) |
600 |
|
|
? $_[0] | MARK_GRAYED | ($self->{colour} == COLOUR_WHITE ? MARK_W : MARK_B) |
601 |
|
|
: $_[0]; |
602 |
pcg |
1.101 |
}); |
603 |
|
|
$self->{board_click} = sub { |
604 |
root |
1.114 |
return unless |
605 |
|
|
$self->{cur_board}->is_valid_move ($self->{colour}, $_[1], $_[2], |
606 |
|
|
$self->{rules}{ruleset} == RULESET_NEW_ZEALAND); |
607 |
pcg |
1.101 |
$self->send (game_move => channel => $self->{channel}, x => $_[0], y => $_[1]); |
608 |
|
|
$self->{board}->set (cursor => undef); |
609 |
|
|
delete $self->{board_click}; |
610 |
|
|
$self->{button_pass}->sensitive (0); |
611 |
|
|
}; |
612 |
|
|
} else { |
613 |
|
|
$self->{button_pass}->set (label => "Pass", sensitive => 0, visible => 1); |
614 |
root |
1.106 |
$self->{board}->set (cursor => undef); |
615 |
pcg |
1.101 |
} |
616 |
|
|
} else { |
617 |
pcg |
1.102 |
$self->{button_undo}->hide; |
618 |
|
|
$self->{button_resign}->hide; |
619 |
pcg |
1.101 |
$self->{button_pass}->hide; |
620 |
|
|
$self->{board}->set (cursor => undef); |
621 |
pcg |
1.99 |
#TODO# # implement coordinate-grabbing |
622 |
pcg |
1.97 |
} |
623 |
|
|
} |
624 |
|
|
|
625 |
pcg |
1.23 |
sub update_board { |
626 |
|
|
my ($self) = @_; |
627 |
root |
1.114 |
|
628 |
pcg |
1.23 |
return unless $self->{path}; |
629 |
|
|
|
630 |
root |
1.106 |
if ($self->{rules}{ruleset} == RULESET_JAPANESE) { |
631 |
|
|
if ($self->{curnode}{move} == 0) { |
632 |
|
|
$self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK; |
633 |
|
|
} else { |
634 |
|
|
$self->{whosemove} = 1 - $self->{cur_board}{last}; |
635 |
|
|
} |
636 |
|
|
} else { |
637 |
|
|
# Chinese, Aga, NZ all have manual placement |
638 |
|
|
if ($self->{curnode}{move} < $self->{handicap}) { |
639 |
|
|
$self->{whosemove} = COLOUR_BLACK; |
640 |
|
|
} elsif ($self->{curnode}{move} == $self->{handicap}) { |
641 |
root |
1.109 |
$self->{whosemove} = $self->{handicap} ? COLOUR_WHITE : COLOUR_BLACK; |
642 |
root |
1.106 |
} else { |
643 |
|
|
$self->{whosemove} = 1 - $self->{cur_board}{last}; |
644 |
|
|
} |
645 |
|
|
} |
646 |
|
|
|
647 |
root |
1.114 |
$self->{board_label}->set_text ("Move " . ($self->{showmove} - 1)); |
648 |
|
|
|
649 |
|
|
$self->{cur_board} = new KGS::Game::Board $self->{size}; |
650 |
|
|
$self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $self->{showmove} - 1]]); |
651 |
|
|
|
652 |
|
|
$self->update_cursor; |
653 |
|
|
|
654 |
|
|
$self->{userpanel}[$_]->set_captures ($self->{cur_board}{captures}[$_]) |
655 |
|
|
for COLOUR_WHITE, COLOUR_BLACK; |
656 |
|
|
|
657 |
root |
1.106 |
my $start_time = $self->{rules}{time}; |
658 |
|
|
|
659 |
pcg |
1.104 |
if ($self->{showmove} == @{$self->{path}}) { |
660 |
|
|
$self->{timers} = [ |
661 |
|
|
[$self->{lastmove_time}, @{$self->{cur_board}{timer}[0]}], |
662 |
|
|
[$self->{lastmove_time}, @{$self->{cur_board}{timer}[1]}], |
663 |
|
|
]; |
664 |
|
|
$self->update_timers ($self->{timers}); |
665 |
pcg |
1.103 |
} else { |
666 |
pcg |
1.104 |
$self->update_timers ([ |
667 |
|
|
[0, @{$self->{cur_board}{timer}[0]}], |
668 |
|
|
[0, @{$self->{cur_board}{timer}[1]}], |
669 |
|
|
]); |
670 |
pcg |
1.103 |
} |
671 |
|
|
|
672 |
pcg |
1.64 |
$self->{board}->set_board ($self->{cur_board}); |
673 |
pcg |
1.97 |
|
674 |
pcg |
1.105 |
if ($self->{cur_board}{score}) { |
675 |
|
|
$self->{score_inlay} ||= $self->{chat}->new_inlay; |
676 |
|
|
$self->{score_inlay}->clear; |
677 |
|
|
$self->{score_inlay}->append_text ("\n<header>Scoring</header>" |
678 |
|
|
. "\n<score>" |
679 |
|
|
. "White: $self->{cur_board}{score}[COLOUR_WHITE], " |
680 |
|
|
. "Black: $self->{cur_board}{score}[COLOUR_BLACK]" |
681 |
|
|
. "</score>"); |
682 |
|
|
} elsif ($self->{score_inlay}) { |
683 |
|
|
(delete $self->{score_inlay})->clear; |
684 |
|
|
} |
685 |
pcg |
1.23 |
} |
686 |
|
|
|
687 |
pcg |
1.1 |
sub event_update_tree { |
688 |
|
|
my ($self) = @_; |
689 |
|
|
|
690 |
root |
1.106 |
(delete $self->{undo_inlay})->clear |
691 |
|
|
if $self->{undo_inlay}; |
692 |
|
|
|
693 |
pcg |
1.1 |
$self->{path} = $self->get_path; |
694 |
pcg |
1.30 |
|
695 |
|
|
if ($self->{moveadj}) { |
696 |
|
|
my $upper = $self->{moveadj}->upper; |
697 |
|
|
my $pos = $self->{moveadj}->get_value; |
698 |
pcg |
1.48 |
my $move = scalar @{$self->{path}}; |
699 |
pcg |
1.30 |
|
700 |
pcg |
1.48 |
$self->{moveadj}->upper ($move); |
701 |
pcg |
1.30 |
|
702 |
|
|
$self->{moveadj}->changed; |
703 |
|
|
if ($pos == $upper) { |
704 |
pcg |
1.48 |
$self->{moveadj}->value ($move); |
705 |
|
|
$self->{moveadj}->value_changed; |
706 |
pcg |
1.30 |
} |
707 |
|
|
} |
708 |
pcg |
1.29 |
} |
709 |
|
|
|
710 |
|
|
sub event_update_comments { |
711 |
|
|
my ($self, $node, $comment, $newnode) = @_; |
712 |
|
|
$self->SUPER::event_update_comments($node, $comment, $newnode); |
713 |
|
|
|
714 |
pcg |
1.30 |
my $text; |
715 |
|
|
|
716 |
pcg |
1.34 |
$text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>" |
717 |
pcg |
1.29 |
if $newnode; |
718 |
|
|
|
719 |
pcg |
1.30 |
for (split /\n/, $comment) { |
720 |
|
|
$text .= "\n"; |
721 |
pcg |
1.40 |
if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) { |
722 |
pcg |
1.30 |
$text .= "<user>" . (util::toxml $1) . "</user>:"; |
723 |
|
|
} |
724 |
pcg |
1.40 |
|
725 |
|
|
# coords only for 19x19 so far |
726 |
|
|
$_ = util::toxml $_; |
727 |
pcg |
1.39 |
s{ |
728 |
|
|
( |
729 |
|
|
\b |
730 |
|
|
(?:[bw])? |
731 |
|
|
[, ]{0,2} |
732 |
pcg |
1.41 |
[a-hj-t] # valid for upto 19x19 |
733 |
pcg |
1.40 |
\s? |
734 |
pcg |
1.42 |
[1-9]?[0-9] |
735 |
pcg |
1.40 |
\b |
736 |
pcg |
1.39 |
) |
737 |
|
|
}{ |
738 |
|
|
"<coord>$1</coord>"; |
739 |
|
|
}sgexi; |
740 |
|
|
|
741 |
pcg |
1.30 |
$text .= $_; |
742 |
|
|
} |
743 |
|
|
|
744 |
pcg |
1.73 |
$self->{chat}->append_text ($text); |
745 |
pcg |
1.1 |
} |
746 |
|
|
|
747 |
|
|
sub event_move { |
748 |
|
|
my ($self, $pass) = @_; |
749 |
root |
1.107 |
|
750 |
pcg |
1.1 |
sound::play 1, $pass ? "pass" : "move"; |
751 |
pcg |
1.25 |
} |
752 |
|
|
|
753 |
root |
1.111 |
### GAMEPLAY EVENTS ######################################################### |
754 |
pcg |
1.38 |
|
755 |
pcg |
1.100 |
sub event_resign_game { |
756 |
|
|
my ($self, $player) = @_; |
757 |
pcg |
1.54 |
|
758 |
|
|
sound::play 3, "resign"; |
759 |
pcg |
1.73 |
$self->{chat}->append_text ("\n<infoblock><header>Resign</header>" |
760 |
pcg |
1.39 |
. "\n<user>" |
761 |
pcg |
1.38 |
. (util::toxml $self->{user}[$msg->{player}]->as_string) |
762 |
root |
1.113 |
. "</user> resigned." |
763 |
|
|
. "\n<user>" |
764 |
|
|
. (util::toxml $self->{user}[1 - $msg->{player}]->as_string) |
765 |
|
|
. "</user> wins the game." |
766 |
|
|
. "</infoblock>"); |
767 |
pcg |
1.38 |
} |
768 |
|
|
|
769 |
root |
1.113 |
sub event_time_win { |
770 |
pcg |
1.100 |
my ($self, $player) = @_; |
771 |
|
|
|
772 |
|
|
sound::play 3, "timewin"; |
773 |
pcg |
1.104 |
$self->{chat}->append_text ("\n<infoblock><header>Out of Time</header>" |
774 |
pcg |
1.100 |
. "\n<user>" |
775 |
root |
1.113 |
. (util::toxml $self->{user}[1 - $msg->{player}]->as_string) |
776 |
|
|
. "</user> ran out of time and lost." |
777 |
|
|
. "\n<user>" |
778 |
pcg |
1.100 |
. (util::toxml $self->{user}[$msg->{player}]->as_string) |
779 |
root |
1.113 |
. "</user> wins the game." |
780 |
|
|
. "</infoblock>"); |
781 |
|
|
} |
782 |
|
|
|
783 |
|
|
sub event_owner_left { |
784 |
|
|
my ($self) = @_; |
785 |
|
|
|
786 |
|
|
sound::play 2, "info"; |
787 |
|
|
$self->{chat}->append_text ("\n<infoblock><header>Owner left</header>" |
788 |
|
|
. "\nThe owner of this game left.</infoblock>"); |
789 |
|
|
} |
790 |
|
|
|
791 |
|
|
sub event_teacher_left { |
792 |
|
|
my ($self) = @_; |
793 |
|
|
|
794 |
|
|
sound::play 2, "info"; |
795 |
|
|
$self->{chat}->append_text ("\n<infoblock><header>Teacher left</header>" |
796 |
|
|
. "\nThe teacher left the game.</infoblock>"); |
797 |
pcg |
1.100 |
} |
798 |
|
|
|
799 |
pcg |
1.102 |
sub event_done { |
800 |
|
|
my ($self) = @_; |
801 |
|
|
|
802 |
|
|
if ($self->{done}[1 - $self->{colour}] && !$self->{done}[$self->{colour}]) { |
803 |
root |
1.108 |
sound::play 2, "info" unless $inlay->{count}; |
804 |
root |
1.107 |
$self->{chat}->append_text ("\n<infoblock><header>Press Done</header>" |
805 |
|
|
. "\nYour opponent pressed done. Now it's up to you."); |
806 |
pcg |
1.102 |
} |
807 |
root |
1.107 |
if ($self->{doneid} & 0x80000000) { |
808 |
root |
1.108 |
sound::play 2, "info" unless $inlay->{count}; |
809 |
root |
1.107 |
$self->{chat}->append_text ("\n<infoblock><header>Press Done Again</header>" |
810 |
root |
1.108 |
. "\nThe board has changed."); |
811 |
root |
1.107 |
} |
812 |
|
|
|
813 |
|
|
$self->{button_pass}->sensitive (!$self->{done}[$self->{colour}]); |
814 |
|
|
|
815 |
|
|
$self->{chat}->set_end; |
816 |
pcg |
1.102 |
} |
817 |
|
|
|
818 |
pcg |
1.38 |
sub inject_final_result { |
819 |
|
|
my ($self, $msg) = @_; |
820 |
|
|
|
821 |
pcg |
1.73 |
$self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>" |
822 |
pcg |
1.38 |
. "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string) |
823 |
|
|
. "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string) |
824 |
pcg |
1.42 |
. "</infoblock>" |
825 |
pcg |
1.38 |
); |
826 |
pcg |
1.37 |
} |
827 |
|
|
|
828 |
pcg |
1.98 |
sub inject_req_undo { |
829 |
|
|
my ($self, $msg) = @_; |
830 |
|
|
|
831 |
|
|
my $inlay = $self->{undo_inlay} ||= $self->{chat}->new_inlay; |
832 |
|
|
return if $inlay->{ignore}; |
833 |
|
|
|
834 |
root |
1.107 |
sound::play 2, "warning" unless $inlay->{count}; |
835 |
pcg |
1.98 |
$inlay->{count}++; |
836 |
pcg |
1.101 |
|
837 |
pcg |
1.98 |
$inlay->clear; |
838 |
|
|
$inlay->append_text ("\n<undo>Undo requested ($inlay->{count} times)</undo>\n"); |
839 |
|
|
$inlay->append_button ("Grant", sub { |
840 |
root |
1.106 |
(delete $self->{undo_inlay})->clear; |
841 |
pcg |
1.98 |
$self->send (grant_undo => channel => $self->{channel}); |
842 |
|
|
}); |
843 |
|
|
$inlay->append_button ("Ignore", sub { |
844 |
|
|
$inlay->clear; |
845 |
|
|
$inlay->{ignore} = 1; |
846 |
|
|
# but leave inlay, so further undo requests get counted |
847 |
|
|
}); |
848 |
|
|
|
849 |
pcg |
1.101 |
$self->{chat}->set_end; |
850 |
pcg |
1.98 |
} |
851 |
|
|
|
852 |
pcg |
1.97 |
sub inject_new_game { |
853 |
|
|
my ($self, $msg) = @_; |
854 |
|
|
|
855 |
root |
1.107 |
if ($msg->{cid} != $self->{cid}) { |
856 |
|
|
$self->part; |
857 |
|
|
warn "ERROR: challenge id mismatch, PLEASE REPORT, especially the circumstances (many games open? etc..)\n";#d# |
858 |
|
|
} |
859 |
|
|
|
860 |
|
|
$self->{chat}->append_text ("\n<header>Game successfully created on server.</header>"); |
861 |
pcg |
1.97 |
delete $self->{cid}; |
862 |
|
|
} |
863 |
|
|
|
864 |
root |
1.111 |
### CHALLENGE HANDLING ###################################################### |
865 |
|
|
|
866 |
pcg |
1.86 |
sub draw_challenge { |
867 |
pcg |
1.97 |
my ($self, $id) = @_; |
868 |
pcg |
1.86 |
|
869 |
pcg |
1.98 |
my $info = $self->{challenge}{$id}; |
870 |
|
|
my $inlay = $info->{inlay}; |
871 |
|
|
my $rules = $info->{rules}; |
872 |
pcg |
1.97 |
|
873 |
pcg |
1.98 |
my $as_black = $info->{black}{name} eq $self->{conn}{name} ? 1 : 0;; |
874 |
|
|
my $opponent = $as_black ? $info->{white} : $info->{black}; |
875 |
pcg |
1.86 |
|
876 |
root |
1.106 |
my ($size, $time, $interval, $count, $type); |
877 |
pcg |
1.97 |
|
878 |
|
|
if (!$self->{channel}) { |
879 |
|
|
$inlay->append_text ("\nNotes: "); |
880 |
pcg |
1.98 |
$inlay->append_entry (\$info->{notes}, 20, ""); |
881 |
pcg |
1.97 |
$inlay->append_text ("\nGlobal Offer: "); |
882 |
pcg |
1.98 |
$inlay->append_optionmenu (\$info->{flags}, |
883 |
pcg |
1.97 |
0 => "No", |
884 |
|
|
2 => "Yes", |
885 |
|
|
); |
886 |
|
|
} else { |
887 |
pcg |
1.98 |
$inlay->append_text ("\nNotes: " . util::toxml $info->{notes}); |
888 |
pcg |
1.97 |
} |
889 |
|
|
|
890 |
|
|
$inlay->append_text ("\nType: "); |
891 |
root |
1.106 |
$type = $inlay->append_optionmenu ( |
892 |
pcg |
1.98 |
\$info->{gametype}, |
893 |
root |
1.106 |
GAMETYPE_DEMONSTRATION , "Demonstration (not yet)", |
894 |
|
|
GAMETYPE_DEMONSTRATION | GAMETYPE_PRIVATE, "Demonstration (P) (not yet)", |
895 |
|
|
GAMETYPE_TEACHING , "Teaching (not yet)", |
896 |
|
|
GAMETYPE_TEACHING | GAMETYPE_PRIVATE, "Teaching (P) (not yet)", |
897 |
pcg |
1.97 |
GAMETYPE_SIMUL , "Simul (not yet!)", |
898 |
|
|
GAMETYPE_FREE , "Free", |
899 |
|
|
GAMETYPE_RATED , "Rated", |
900 |
|
|
sub { |
901 |
|
|
$size->set_history (2) if $_[0] eq GAMETYPE_RATED; |
902 |
|
|
}, |
903 |
|
|
); |
904 |
|
|
|
905 |
|
|
if ($self->{channel}) { |
906 |
|
|
$inlay->append_text ("\nMy Colour: "); |
907 |
|
|
$inlay->append_optionmenu ( |
908 |
|
|
\$as_black, |
909 |
|
|
0 => "White", |
910 |
|
|
1 => "Black", |
911 |
|
|
sub { |
912 |
pcg |
1.98 |
if ($info->{$_[0] ? "black" : "white"}{name} ne $self->{conn}{name}) { |
913 |
|
|
($info->{black}, $info->{white}) = ($info->{white}, $info->{black}); |
914 |
pcg |
1.97 |
} |
915 |
|
|
} |
916 |
|
|
); |
917 |
|
|
} |
918 |
pcg |
1.86 |
|
919 |
pcg |
1.97 |
$inlay->append_text ("\nRuleset: "); |
920 |
|
|
$inlay->append_optionmenu ( |
921 |
pcg |
1.98 |
\$info->{rules}{ruleset}, |
922 |
pcg |
1.97 |
RULESET_JAPANESE , "Japanese", |
923 |
|
|
RULESET_CHINESE , "Chinese", |
924 |
|
|
RULESET_AGA , "AGA", |
925 |
|
|
RULESET_NEW_ZEALAND, "New Zealand", |
926 |
|
|
); |
927 |
|
|
|
928 |
|
|
$inlay->append_text ("\nSize: "); |
929 |
|
|
$size = $inlay->append_optionmenu ( |
930 |
root |
1.106 |
\$info->{rules}{size}, |
931 |
|
|
(9 => 9, 13 => 13, 19 => 19, map +($_, $_), 2..38), |
932 |
|
|
sub { |
933 |
|
|
$type->set_history (5) # reset to free |
934 |
|
|
if $_[0] != 19 && $info->{gametype} == GAMETYPE_RATED; |
935 |
|
|
}, |
936 |
pcg |
1.97 |
); |
937 |
|
|
|
938 |
|
|
if ($self->{channel}) { |
939 |
|
|
$inlay->append_text ("\nHandicap: "); |
940 |
pcg |
1.98 |
$inlay->append_optionmenu (\$info->{rules}{handicap}, map +($_, $_), 0..9); |
941 |
pcg |
1.97 |
|
942 |
|
|
$inlay->append_text ("\nKomi: "); |
943 |
pcg |
1.98 |
$inlay->append_entry (\$info->{rules}{komi}, 5); |
944 |
pcg |
1.97 |
} |
945 |
|
|
|
946 |
|
|
$inlay->append_text ("\nTimesys: "); |
947 |
|
|
$inlay->append_optionmenu ( |
948 |
pcg |
1.98 |
\$info->{rules}{timesys}, |
949 |
pcg |
1.97 |
&TIMESYS_NONE => "None", |
950 |
|
|
&TIMESYS_ABSOLUTE => "Absolute", |
951 |
|
|
&TIMESYS_BYO_YOMI => "Byo Yomi", |
952 |
|
|
&TIMESYS_CANADIAN => "Canadian", |
953 |
|
|
sub { |
954 |
|
|
my ($new) = @_; |
955 |
|
|
|
956 |
|
|
if ($new eq TIMESYS_NONE) { |
957 |
|
|
$time->hide; |
958 |
|
|
$interval->hide; |
959 |
|
|
$count->hide; |
960 |
|
|
} else { |
961 |
|
|
$time->show; |
962 |
|
|
$time->set_text ($self->{app}{defaults}{time}); |
963 |
|
|
if ($new eq TIMESYS_ABSOLUTE) { |
964 |
|
|
$interval->hide; |
965 |
|
|
$count->hide; |
966 |
|
|
} else { |
967 |
|
|
$interval->show; |
968 |
|
|
$count->show; |
969 |
|
|
if ($new eq TIMESYS_BYO_YOMI) { |
970 |
|
|
$interval->set_text ($self->{app}{defaults}{byo_time}); |
971 |
|
|
$count->set_text ($self->{app}{defaults}{byo_period}); |
972 |
|
|
} elsif ($new eq TIMESYS_CANADIAN) { |
973 |
|
|
$interval->set_text ($self->{app}{defaults}{can_time}); |
974 |
|
|
$count->set_text ($self->{app}{defaults}{can_period}); |
975 |
|
|
} |
976 |
|
|
} |
977 |
|
|
} |
978 |
|
|
} |
979 |
|
|
); |
980 |
|
|
|
981 |
|
|
$inlay->append_text ("\nMain Time: "); |
982 |
pcg |
1.98 |
$time = $inlay->append_entry (\$info->{rules}{time}, 5); |
983 |
pcg |
1.97 |
$inlay->append_text ("\nInterval: "); |
984 |
pcg |
1.98 |
$interval = $inlay->append_entry (\$info->{rules}{interval}, 3); |
985 |
pcg |
1.97 |
$inlay->append_text ("\nPeriods/Stones: "); |
986 |
pcg |
1.98 |
$count = $inlay->append_entry (\$info->{rules}{count}, 2); |
987 |
pcg |
1.97 |
|
988 |
|
|
$inlay->append_text ("\n"); |
989 |
|
|
|
990 |
|
|
if (!$self->{channel}) { |
991 |
|
|
$inlay->append_button ("Create Challenge", sub { |
992 |
|
|
$inlay->clear; |
993 |
|
|
$self->{cid} = $self->{conn}->alloc_clientid; |
994 |
|
|
$self->send (new_game => |
995 |
pcg |
1.98 |
channel => delete $self->{roomid}, |
996 |
|
|
gametype => $info->{gametype}, |
997 |
|
|
cid => $self->{cid}, |
998 |
|
|
flags => $info->{flags}, |
999 |
|
|
rules => $info->{rules}, |
1000 |
|
|
notes => $info->{notes}, |
1001 |
pcg |
1.97 |
); |
1002 |
|
|
}); |
1003 |
|
|
} else { |
1004 |
|
|
$inlay->append_button ("OK", sub { |
1005 |
|
|
$inlay->clear; |
1006 |
|
|
$self->send (challenge => |
1007 |
pcg |
1.98 |
channel => $self->{channel}, |
1008 |
|
|
black => $info->{black}, |
1009 |
|
|
white => $info->{white}, |
1010 |
|
|
gametype => $info->{gametype}, |
1011 |
|
|
cid => $info->{cid}, |
1012 |
|
|
rules => $info->{rules}, |
1013 |
pcg |
1.97 |
); |
1014 |
|
|
}); |
1015 |
|
|
if (exists $self->{challenge}{""}) { |
1016 |
|
|
$inlay->append_button ("Reject", sub { |
1017 |
|
|
$inlay->clear; |
1018 |
|
|
$self->send (reject_challenge => |
1019 |
pcg |
1.98 |
channel => $self->{channel}, |
1020 |
|
|
name => $opponent->{name}, |
1021 |
|
|
gametype => $info->{gametype}, |
1022 |
|
|
cid => $info->{cid}, |
1023 |
|
|
rules => $info->{rules}, |
1024 |
pcg |
1.97 |
); |
1025 |
|
|
}); |
1026 |
|
|
} |
1027 |
|
|
} |
1028 |
pcg |
1.86 |
} |
1029 |
|
|
|
1030 |
root |
1.111 |
sub new_game_challenge { |
1031 |
|
|
my ($self) = @_; |
1032 |
|
|
|
1033 |
|
|
my $d = $self->{app}{defaults}; |
1034 |
|
|
|
1035 |
|
|
$self->{challenge}{""} = { |
1036 |
|
|
gametype => $d->{gametype}, |
1037 |
|
|
flags => 0, |
1038 |
|
|
notes => $d->{stones}, |
1039 |
|
|
rules => { |
1040 |
|
|
ruleset => $d->{ruleset}, |
1041 |
|
|
size => $d->{size}, |
1042 |
|
|
timesys => $d->{timesys}, |
1043 |
|
|
time => $d->{time}, |
1044 |
|
|
interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time}, |
1045 |
|
|
count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones}, |
1046 |
|
|
}, |
1047 |
pcg |
1.88 |
|
1048 |
root |
1.111 |
inlay => $self->{chat}->new_inlay, |
1049 |
|
|
}; |
1050 |
|
|
$self->draw_challenge (""); |
1051 |
pcg |
1.88 |
} |
1052 |
|
|
|
1053 |
pcg |
1.68 |
sub event_challenge { |
1054 |
pcg |
1.98 |
my ($self, $info) = @_; |
1055 |
pcg |
1.68 |
|
1056 |
pcg |
1.98 |
my $as_black = $info->{black}->{name} eq $self->{conn}{name}; |
1057 |
|
|
my $opponent = $as_black ? $info->{white} : $info->{black}; |
1058 |
pcg |
1.86 |
|
1059 |
pcg |
1.97 |
my $id = $opponent->{name}; |
1060 |
root |
1.108 |
|
1061 |
|
|
sound::play 2, "info"; |
1062 |
pcg |
1.86 |
|
1063 |
pcg |
1.98 |
$self->{challenge}{$id} = $info; |
1064 |
|
|
$self->{challenge}{$id}{inlay} = $self->{chat}->new_switchable_inlay ( |
1065 |
|
|
exists $self->{challenge}{""} |
1066 |
root |
1.110 |
? "Challenge from " . $opponent->as_string |
1067 |
|
|
: "Challenge to " . $opponent->as_string, |
1068 |
pcg |
1.97 |
sub { |
1069 |
|
|
$self->{challenge}{$id}{inlay} = $_[0]; |
1070 |
|
|
$self->draw_challenge ($id); |
1071 |
|
|
}, |
1072 |
pcg |
1.101 |
!exists $self->{challenge}{""} # only open when not offerer |
1073 |
pcg |
1.97 |
); |
1074 |
pcg |
1.68 |
} |
1075 |
|
|
|
1076 |
pcg |
1.1 |
1; |
1077 |
|
|
|