… | |
… | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | |
7 | |
8 | my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
8 | my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
9 | |
9 | |
10 | # testbed for coroutines in crossfire : |
10 | # paranoia function to overwrite a string-in-place |
|
|
11 | sub nuke_str { |
|
|
12 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
|
|
13 | } |
11 | |
14 | |
12 | sub query { |
15 | sub query { |
13 | my ($ns, $flags, $text) = @_; |
16 | my ($ns, $flags, $text) = @_; |
14 | |
17 | |
15 | my $current = $Coro::current; |
18 | my $current = $Coro::current; |
… | |
… | |
32 | } |
35 | } |
33 | |
36 | |
34 | sub check_playing { |
37 | sub check_playing { |
35 | my ($ns, $user) = @_; |
38 | my ($ns, $user) = @_; |
36 | |
39 | |
37 | return unless cf::player::find $user; |
40 | return unless cf::player::find_active $user; |
38 | |
41 | |
39 | $ns->send_drawinfo ( |
42 | $ns->send_drawinfo ( |
40 | "That player is already logged in on this server. " |
43 | "That player is already logged in on this server. " |
41 | . "If you want to create a new player, choose another name. " |
44 | . "If you want to create a new player, choose another name. " |
42 | . "If you are already a registered player, make sure nobody " |
45 | . "If you are already a registered player, make sure nobody " |
… | |
… | |
49 | ); |
52 | ); |
50 | |
53 | |
51 | 1 |
54 | 1 |
52 | } |
55 | } |
53 | |
56 | |
|
|
57 | sub check_clean_save { |
|
|
58 | my ($pl) = @_; |
|
|
59 | |
|
|
60 | unless (delete $pl->{clean_save}) { |
|
|
61 | #d#TODO |
|
|
62 | } |
|
|
63 | } |
|
|
64 | |
54 | # delete a player directory, be non-blocking AND synchronous... |
65 | # delete a player directory, be non-blocking AND synchronous... |
55 | # (thats hard, so we crap out and fork). |
66 | # (thats hard, so we crap out and fork). |
56 | sub nuke_playerdir { |
67 | sub nuke_playerdir { |
57 | my ($user) = @_; |
68 | my ($user) = @_; |
58 | |
69 | |
… | |
… | |
60 | system "cd \Q$PLAYERDIR\E " |
71 | system "cd \Q$PLAYERDIR\E " |
61 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
72 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
62 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
73 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
63 | } |
74 | } |
64 | |
75 | |
65 | sub on_addme { |
76 | sub addme { |
66 | my ($ns) = @_; |
77 | my ($ns) = @_; |
67 | |
78 | |
68 | $ns->destroy if $ns->pl; |
79 | $ns->destroy if $ns->pl; |
69 | |
80 | |
70 | $ns->coro (sub { |
81 | $ns->async (sub { |
71 | my ($user, $pass); |
82 | my ($user, $pass); |
72 | |
83 | |
73 | $ns->send_packet ("addme_success"); |
84 | $ns->send_packet ("addme_success"); |
74 | |
85 | |
75 | for (;;) { |
86 | for (;;) { |
… | |
… | |
120 | . "that cannot be too much to ask for :)", |
131 | . "that cannot be too much to ask for :)", |
121 | cf::NDI_RED |
132 | cf::NDI_RED |
122 | ); |
133 | ); |
123 | } |
134 | } |
124 | |
135 | |
125 | my $dir = "$PLAYERDIR/$user"; |
|
|
126 | my $plfile = "$dir/$user.pl"; |
|
|
127 | |
|
|
128 | # lock this username for the remainder of this login session |
136 | # lock this username for the remainder of this login session |
129 | if ($cf::LOGIN_LOCK{$user}) { |
137 | if ($cf::LOGIN_LOCK{$user}) { |
130 | $ns->send_drawinfo ( |
138 | $ns->send_drawinfo ( |
131 | "That username is currently used in another login session. " |
139 | "That username is currently used in another login session. " |
132 | . "Chose another, or wait till the other session has ended.", |
140 | . "Chose another, or wait till the other session has ended.", |
… | |
… | |
137 | local $cf::LOGIN_LOCK{$user} = 1; |
145 | local $cf::LOGIN_LOCK{$user} = 1; |
138 | |
146 | |
139 | check_playing $ns, $user and next; |
147 | check_playing $ns, $user and next; |
140 | |
148 | |
141 | # try to read the user file and check the password |
149 | # try to read the user file and check the password |
142 | if (my $fh = aio_open $plfile, O_RDONLY, 0) { |
150 | if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { |
143 | my $mtime = (stat $fh)[9]; |
151 | my $mtime = (stat $fh)[9]; |
144 | |
152 | |
145 | 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
153 | 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
146 | $buf =~ /^password (\S+)$/m or next; |
154 | $buf =~ /^password (\S+)$/m or next; |
147 | my $hash = $1; |
155 | my $hash = $1; |
148 | |
156 | |
149 | if ($hash eq crypt $pass, $hash) { |
157 | if ($hash eq crypt $pass, $hash) { |
|
|
158 | nuke_str $pass; |
150 | # password matches, wonderful |
159 | # password matches, wonderful |
151 | my $pl = cf::player::load $plfile or next; |
160 | my $pl = cf::player::find $user or next; |
152 | $pl->enable_save (1); |
|
|
153 | $pl->connect ($ns); |
161 | $pl->connect ($ns); |
|
|
162 | check_clean_save $pl; |
|
|
163 | $pl->{clean_save} = 1; |
154 | last; |
164 | last; |
155 | } elsif (can_cleanup $buf, $mtime) { |
165 | } elsif (can_cleanup $buf, $mtime) { |
156 | Coro::Timer::sleep 1; |
166 | Coro::Timer::sleep 1; |
157 | |
167 | |
158 | $ns->send_drawinfo ( |
168 | $ns->send_drawinfo ( |
… | |
… | |
160 | . "please try again. If not, you can now decide to take over this account " |
170 | . "please try again. If not, you can now decide to take over this account " |
161 | . "because it has not been in-use for some time.", |
171 | . "because it has not been in-use for some time.", |
162 | cf::NDI_RED |
172 | cf::NDI_RED |
163 | ); |
173 | ); |
164 | |
174 | |
|
|
175 | #TODO: nuke_str |
165 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
176 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
166 | or next; |
177 | or next; |
167 | |
178 | |
168 | # check if the file hasn't changed |
179 | # check if the file hasn't changed |
169 | aio_stat $plfile and next; |
180 | aio_stat cf::player::path $user and next; |
170 | $mtime == (stat _)[9] or next; |
181 | $mtime == (stat _)[9] or next; |
171 | |
182 | |
172 | nuke_playerdir $user; |
183 | nuke_playerdir $user; |
173 | |
184 | |
174 | # fall through to creation |
185 | # fall through to creation |
175 | } else { |
186 | } else { |
|
|
187 | nuke_str $pass; |
|
|
188 | |
176 | Coro::Timer::sleep 1; |
189 | Coro::Timer::sleep 1; |
177 | |
190 | |
178 | $ns->send_drawinfo ( |
191 | $ns->send_drawinfo ( |
179 | "Wrong username or password. Please try again " |
192 | "Wrong username or password. Please try again " |
180 | . "(check for Numlock and other semi-obvious error sources).", |
193 | . "(check for Numlock and other semi-obvious error sources).", |
… | |
… | |
190 | nuke_playerdir $user; |
203 | nuke_playerdir $user; |
191 | |
204 | |
192 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
205 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
193 | |
206 | |
194 | if ($pass2 ne $pass) { |
207 | if ($pass2 ne $pass) { |
|
|
208 | nuke_str $pass; |
|
|
209 | nuke_str $pass2; |
195 | $ns->send_drawinfo ( |
210 | $ns->send_drawinfo ( |
196 | "The passwords do not match, please try again.", |
211 | "The passwords do not match, please try again.", |
197 | cf::NDI_RED |
212 | cf::NDI_RED |
198 | ); |
213 | ); |
199 | next; |
214 | next; |
200 | } |
215 | } |
201 | |
216 | |
|
|
217 | nuke_str $pass2; |
|
|
218 | |
202 | my $pl = cf::player::create; |
219 | my $pl = cf::player::new $user; |
203 | $pl->ob->name ($user); |
|
|
204 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
220 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
|
|
221 | nuke_str $pass; |
205 | $pl->connect ($ns); |
222 | $pl->connect ($ns); |
206 | |
223 | |
207 | my $ob = $pl->ob; |
224 | my $ob = $pl->ob; |
208 | |
225 | |
209 | while () { |
226 | while () { |
… | |
… | |
232 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
249 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
233 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
250 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
234 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
251 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
235 | |
252 | |
236 | $ns->state (cf::ST_CHANGE_CLASS); |
253 | $ns->state (cf::ST_CHANGE_CLASS); |
237 | $pl->enable_save (1);#d# too early |
254 | delete $pl->{deny_save};#d# too early |
238 | |
255 | |
239 | last; |
256 | last; |
240 | } |
257 | } |
241 | }); |
258 | }); |
242 | } |
259 | } |
|
|
260 | |
|
|
261 | cf::register_command quit => sub { |
|
|
262 | my ($ob, $arg) = @_; |
|
|
263 | |
|
|
264 | $ob->reply (undef, |
|
|
265 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
|
|
266 | . "If you are sure you want to do this, then use the quit_character command instead of quit.", |
|
|
267 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
268 | }; |
|
|
269 | |
|
|
270 | cf::register_command quit_character => sub { |
|
|
271 | my ($ob, $arg) = @_; |
|
|
272 | |
|
|
273 | my $pl = $ob->contr; |
|
|
274 | |
|
|
275 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
|
|
276 | if ($_[0] !~ /^[yY]/) { |
|
|
277 | $ob->reply (undef, |
|
|
278 | "Ok, not not quitting then.", |
|
|
279 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
280 | } else { |
|
|
281 | $ob->reply (undef, |
|
|
282 | "Ok, quitting, hope to see you again.", |
|
|
283 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
284 | $pl->ns->flush; |
|
|
285 | $pl->quit_character; |
|
|
286 | } |
|
|
287 | }); |
|
|
288 | }; |
243 | |
289 | |
244 | cf::object->attach ( |
290 | cf::object->attach ( |
245 | type => cf::SAVEBED, |
291 | type => cf::SAVEBED, |
246 | on_apply => sub { |
292 | on_apply => sub { |
247 | my ($bed, $ob) = @_; |
293 | my ($bed, $ob) = @_; |
248 | |
294 | |
249 | return cf::override 0 unless $ob->type == cf::PLAYER; |
295 | return cf::override 0 unless $ob->type == cf::PLAYER; |
250 | |
296 | |
|
|
297 | my $pl = $ob->contr; |
|
|
298 | |
251 | # update respawn position |
299 | # update respawn position |
252 | $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); |
300 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
253 | |
301 | |
254 | $ob->contr->killer ("left"); |
302 | $pl->killer ("left"); |
255 | $ob->check_score; |
303 | $ob->check_score; |
256 | |
304 | |
257 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
305 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
258 | $ob->contr->save (1); |
|
|
259 | |
306 | |
260 | $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
307 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
261 | $ob->contr->enable_save (1); |
|
|
262 | if ($_[0] !~ /^[yY]/) { |
308 | if ($_[0] !~ /^[yY]/) { |
263 | $ob->contr->save (1); |
309 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
|
|
310 | $pl->deactivate; |
264 | $ob->contr->ns->destroy; |
311 | $pl->ns->destroy; |
|
|
312 | } else { |
|
|
313 | cf::async { $pl->save }; |
265 | } |
314 | } |
266 | }); |
315 | }); |
267 | }, |
316 | }, |
268 | ); |
317 | ); |
269 | |
318 | |
270 | cf::client->attach (package => __PACKAGE__); |
319 | cf::player->attach ( |
|
|
320 | on_login => sub { |
|
|
321 | my ($pl) = @_; |
|
|
322 | my $name = $pl->ob->name; |
271 | |
323 | |
|
|
324 | $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
325 | }, |
|
|
326 | on_logout => sub { |
|
|
327 | my ($pl, $cleanly) = @_; |
|
|
328 | my $name = $pl->ob->name; |
|
|
329 | |
|
|
330 | if ($cleanly) { |
|
|
331 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
332 | } else { |
|
|
333 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
334 | delete $pl->{clean_save}; |
|
|
335 | } |
|
|
336 | }, |
|
|
337 | ); |
|
|
338 | |
|
|
339 | cf::client->attach ( |
|
|
340 | on_addme => \&addme, |
|
|
341 | ); |
|
|
342 | |
|
|
343 | ############################################################################# |
|
|
344 | |
|
|
345 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
|
|
346 | our $SAVE_TIMEOUT = 200; # save players every n seconds |
|
|
347 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
|
|
348 | |
|
|
349 | our $SCHEDULER = cf::async_ext { |
|
|
350 | while () { |
|
|
351 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
|
|
352 | |
|
|
353 | # this weird form of iteration over values is used because |
|
|
354 | # the hash changes underneath us frequently, and for |
|
|
355 | # keeps a direct reference to the value without (in 5.8 perls) |
|
|
356 | # keeping a reference, so this is prone to crashes or worse. |
|
|
357 | my @players = keys %cf::PLAYER; |
|
|
358 | for (@players) { |
|
|
359 | my $pl = $cf::PLAYER{$_} |
|
|
360 | or next; |
|
|
361 | $pl->valid or next; |
|
|
362 | |
|
|
363 | eval { |
|
|
364 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
365 | $pl->save; |
|
|
366 | Coro::Timer::sleep $SAVE_INTERVAL; |
|
|
367 | } |
|
|
368 | |
|
|
369 | unless ($pl->active) { |
|
|
370 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
371 | my $ob = $pl->ob; |
|
|
372 | Scalar::Util::weaken $pl; |
|
|
373 | Scalar::Util::weaken $ob; |
|
|
374 | my $a_ = $pl->refcnt; |
|
|
375 | my $b_ = $ob->refcnt; |
|
|
376 | my $pl_ref = $pl->refcnt_cnt; |
|
|
377 | my $ob_ref = $ob->refcnt_cnt; |
|
|
378 | |
|
|
379 | if ($pl_ref == 2 && $ob_ref == 1) { |
|
|
380 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
381 | delete $cf::PLAYER{$ob->name}; |
|
|
382 | # pl_ref == one from object + one from cf::PLAYER |
|
|
383 | # ob_ref == one from simply being an object |
|
|
384 | $ob->destroy; |
|
|
385 | $pl->destroy; |
|
|
386 | } else { |
|
|
387 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
|
|
388 | } |
|
|
389 | } |
|
|
390 | }; |
|
|
391 | warn $@ if $@; |
|
|
392 | Coro::cede; |
|
|
393 | }; |
|
|
394 | } |
|
|
395 | }; |
|
|
396 | |
|
|
397 | $SCHEDULER->prio (1); |
|
|
398 | |