… | |
… | |
21 | |
21 | |
22 | $current |
22 | $current |
23 | } |
23 | } |
24 | |
24 | |
25 | sub can_cleanup { |
25 | sub can_cleanup { |
26 | my ($playerfile, $mtime) = @_; |
26 | my ($pl, $mtime) = @_; |
27 | |
27 | |
28 | my $age = time - $mtime; |
28 | my $age = time - $mtime; |
29 | my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; |
29 | my $level = $pl->ob->level; |
30 | |
30 | |
31 | ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
31 | ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
32 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
32 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
33 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
33 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
34 | || $age > 700 * 86400 # 2 years for everybody else |
34 | || $age > 700 * 86400 # 2 years for everybody else |
… | |
… | |
145 | local $cf::LOGIN_LOCK{$user} = 1; |
145 | local $cf::LOGIN_LOCK{$user} = 1; |
146 | |
146 | |
147 | check_playing $ns, $user and next; |
147 | check_playing $ns, $user and next; |
148 | |
148 | |
149 | # try to read the user file and check the password |
149 | # try to read the user file and check the password |
150 | if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { |
150 | if (my $pl = cf::player::find $user) { |
|
|
151 | aio_stat $pl->path and next; |
151 | my $mtime = (stat $fh)[9]; |
152 | my $mtime = (stat _)[9]; |
152 | |
|
|
153 | 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
|
|
154 | $buf =~ /^password (\S+)$/m or next; |
|
|
155 | my $hash = $1; |
153 | my $hash = $pl->password; |
156 | |
154 | |
157 | if ($hash eq crypt $pass, $hash) { |
155 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
158 | nuke_str $pass; |
156 | nuke_str $pass; |
159 | # password matches, wonderful |
157 | # password matches, wonderful |
160 | my $pl = cf::player::find $user or next; |
158 | my $pl = cf::player::find $user or next; |
161 | $pl->connect ($ns); |
159 | $pl->connect ($ns); |
162 | check_clean_save $pl; |
160 | check_clean_save $pl; |
|
|
161 | $pl->{clean_save} = 1; |
163 | last; |
162 | last; |
164 | } elsif (can_cleanup $buf, $mtime) { |
163 | } elsif (can_cleanup $pl, $mtime) { |
165 | Coro::Timer::sleep 1; |
164 | Coro::Timer::sleep 1; |
166 | |
165 | |
167 | $ns->send_drawinfo ( |
166 | $ns->send_drawinfo ( |
168 | "Player exists, but password does not match. If this is your account, " |
167 | "Player exists, but password does not match. If this is your account, " |
169 | . "please try again. If not, you can now decide to take over this account " |
168 | . "please try again. If not, you can now decide to take over this account " |
… | |
… | |
177 | |
176 | |
178 | # check if the file hasn't changed |
177 | # check if the file hasn't changed |
179 | aio_stat cf::player::path $user and next; |
178 | aio_stat cf::player::path $user and next; |
180 | $mtime == (stat _)[9] or next; |
179 | $mtime == (stat _)[9] or next; |
181 | |
180 | |
182 | nuke_playerdir $user; |
181 | $pl->quit_character; |
183 | |
182 | |
184 | # fall through to creation |
183 | # fall through to creation |
185 | } else { |
184 | } else { |
186 | nuke_str $pass; |
185 | nuke_str $pass; |
187 | |
186 | |
… | |
… | |
255 | last; |
254 | last; |
256 | } |
255 | } |
257 | }); |
256 | }); |
258 | } |
257 | } |
259 | |
258 | |
260 | # "Quitting will delete your character PERMANENTLY. If you are sure you want to do this, then use the quit_character command instead of quit." |
259 | cf::register_command quit => sub { |
261 | # "Quitting will delete your character.\nAre you sure you want to quit (y/n):" |
260 | my ($ob, $arg) = @_; |
262 | # quit, quit_character, save |
261 | |
|
|
262 | $ob->reply (undef, |
|
|
263 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
|
|
264 | . "If you are sure you want to do this, then use the quit_character command instead of quit.", |
|
|
265 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
266 | }; |
|
|
267 | |
|
|
268 | cf::register_command quit_character => sub { |
|
|
269 | my ($ob, $arg) = @_; |
|
|
270 | |
|
|
271 | my $pl = $ob->contr; |
|
|
272 | |
|
|
273 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
|
|
274 | if ($_[0] !~ /^[yY]/) { |
|
|
275 | $ob->reply (undef, |
|
|
276 | "Ok, not not quitting then.", |
|
|
277 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
278 | } else { |
|
|
279 | $ob->reply (undef, |
|
|
280 | "Ok, quitting, hope to see you again.", |
|
|
281 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
282 | $pl->ns->flush; |
|
|
283 | $pl->quit_character; |
|
|
284 | } |
|
|
285 | }); |
|
|
286 | }; |
263 | |
287 | |
264 | cf::object->attach ( |
288 | cf::object->attach ( |
265 | type => cf::SAVEBED, |
289 | type => cf::SAVEBED, |
266 | on_apply => sub { |
290 | on_apply => sub { |
267 | my ($bed, $ob) = @_; |
291 | my ($bed, $ob) = @_; |
268 | |
292 | |
269 | return cf::override 0 unless $ob->type == cf::PLAYER; |
293 | return cf::override 0 unless $ob->type == cf::PLAYER; |
270 | |
294 | |
271 | my $pl = $ob->pl; |
295 | my $pl = $ob->contr; |
272 | |
296 | |
273 | # update respawn position |
297 | # update respawn position |
274 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
298 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
|
|
299 | cf::async { $pl->save }; |
275 | |
300 | |
276 | $pl->killer ("left"); |
301 | $pl->killer ("left"); |
277 | $ob->check_score; |
302 | $ob->check_score; |
278 | |
303 | |
279 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
304 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
… | |
… | |
282 | if ($_[0] !~ /^[yY]/) { |
307 | if ($_[0] !~ /^[yY]/) { |
283 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
308 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
284 | $pl->deactivate; |
309 | $pl->deactivate; |
285 | $pl->ns->destroy; |
310 | $pl->ns->destroy; |
286 | } else { |
311 | } else { |
287 | cf::async { |
312 | cf::async { $pl->save }; |
288 | $pl->{clean_save} = 1; |
|
|
289 | $pl->save; |
|
|
290 | }; |
|
|
291 | } |
313 | } |
292 | }); |
314 | }); |
293 | }, |
315 | }, |
294 | ); |
316 | ); |
295 | |
317 | |
… | |
… | |
306 | |
328 | |
307 | if ($cleanly) { |
329 | if ($cleanly) { |
308 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
330 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
309 | } else { |
331 | } else { |
310 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
332 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
333 | delete $pl->{clean_save}; |
311 | } |
334 | } |
312 | }, |
335 | }, |
313 | ); |
336 | ); |
314 | |
337 | |
315 | cf::client->attach ( |
338 | cf::client->attach ( |
316 | on_addme => \&addme, |
339 | on_addme => \&addme, |
317 | ); |
340 | ); |
318 | |
341 | |
319 | ############################################################################# |
342 | ############################################################################# |
320 | |
343 | |
321 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
344 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
322 | our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory |
|
|
323 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
345 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
324 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
|
|
325 | |
346 | |
326 | our $SCHEDULER = cf::async_ext { |
347 | our $SCHEDULER = cf::async_ext { |
327 | while () { |
348 | while () { |
328 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
349 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
329 | |
350 | |
… | |
… | |
337 | or next; |
358 | or next; |
338 | $pl->valid or next; |
359 | $pl->valid or next; |
339 | |
360 | |
340 | eval { |
361 | eval { |
341 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
362 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
363 | $cf::WAIT_FOR_TICK_ONE->wait; |
342 | $pl->save; |
364 | $pl->save; |
343 | Coro::Timer::sleep $SAVE_INTERVAL; |
365 | |
|
|
366 | unless ($pl->active) { |
|
|
367 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
368 | my $ob = $pl->ob; |
|
|
369 | Scalar::Util::weaken $pl; |
|
|
370 | Scalar::Util::weaken $ob; |
|
|
371 | my $a_ = $pl->refcnt;#d# |
|
|
372 | my $b_ = $ob->refcnt;#d# |
|
|
373 | my $pl_ref = $pl->refcnt_cnt; |
|
|
374 | my $ob_ref = $ob->refcnt_cnt; |
|
|
375 | |
|
|
376 | ## pl_ref == one from object + one from cf::PLAYER |
|
|
377 | ## ob_ref == one from simply being an object |
|
|
378 | if ($pl_ref == 2 && $ob_ref == 1) { |
|
|
379 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
380 | |
|
|
381 | # remove from sight and get fresh "copies" |
|
|
382 | $pl = delete $cf::PLAYER{$ob->name}; |
|
|
383 | $ob = $pl->ob; |
|
|
384 | |
|
|
385 | $ob->destroy; |
|
|
386 | $pl->destroy; |
|
|
387 | } else { |
|
|
388 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
|
|
389 | } |
|
|
390 | } |
344 | } |
391 | } |
345 | }; |
392 | }; |
346 | warn $@ if $@; |
393 | warn $@ if $@; |
347 | Coro::cede; |
394 | Coro::cede; |
348 | }; |
395 | }; |