… | |
… | |
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; |
163 | $pl->{clean_save} = 1; |
161 | $pl->{clean_save} = 1; |
164 | last; |
162 | last; |
165 | } elsif (can_cleanup $buf, $mtime) { |
163 | } elsif (can_cleanup $pl, $mtime) { |
166 | Coro::Timer::sleep 1; |
164 | Coro::Timer::sleep 1; |
167 | |
165 | |
168 | $ns->send_drawinfo ( |
166 | $ns->send_drawinfo ( |
169 | "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, " |
170 | . "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 " |
… | |
… | |
178 | |
176 | |
179 | # check if the file hasn't changed |
177 | # check if the file hasn't changed |
180 | aio_stat cf::player::path $user and next; |
178 | aio_stat cf::player::path $user and next; |
181 | $mtime == (stat _)[9] or next; |
179 | $mtime == (stat _)[9] or next; |
182 | |
180 | |
183 | nuke_playerdir $user; |
181 | $pl->quit_character; |
184 | |
182 | |
185 | # fall through to creation |
183 | # fall through to creation |
186 | } else { |
184 | } else { |
187 | nuke_str $pass; |
185 | nuke_str $pass; |
188 | |
186 | |
… | |
… | |
296 | |
294 | |
297 | my $pl = $ob->contr; |
295 | my $pl = $ob->contr; |
298 | |
296 | |
299 | # update respawn position |
297 | # update respawn position |
300 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
298 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
|
|
299 | cf::async { $pl->save }; |
301 | |
300 | |
302 | $pl->killer ("left"); |
301 | $pl->killer ("left"); |
303 | $ob->check_score; |
302 | $ob->check_score; |
304 | |
303 | |
305 | $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."); |
… | |
… | |
340 | on_addme => \&addme, |
339 | on_addme => \&addme, |
341 | ); |
340 | ); |
342 | |
341 | |
343 | ############################################################################# |
342 | ############################################################################# |
344 | |
343 | |
345 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
344 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
346 | our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory |
|
|
347 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
345 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
348 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
346 | our $SAVE_INTERVAL = 0.5; # save at max. one player every $SAVE_INTERVAL |
349 | |
347 | |
350 | our $SCHEDULER = cf::async_ext { |
348 | our $SCHEDULER = cf::async_ext { |
351 | while () { |
349 | while () { |
352 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
350 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
353 | |
351 | |
… | |
… | |
362 | $pl->valid or next; |
360 | $pl->valid or next; |
363 | |
361 | |
364 | eval { |
362 | eval { |
365 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
363 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
366 | $pl->save; |
364 | $pl->save; |
|
|
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; |
|
|
372 | my $b_ = $ob->refcnt; |
|
|
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 | } |
367 | Coro::Timer::sleep $SAVE_INTERVAL; |
391 | Coro::Timer::sleep $SAVE_INTERVAL; |
368 | } |
392 | } |
369 | my $ob = $pl->ob; |
|
|
370 | Scalar::Util::weaken $pl; # 2 == from object + from perl |
|
|
371 | Scalar::Util::weaken $ob; # 2 == one from being an object + ??? |
|
|
372 | my $a_ = $pl->refcnt; |
|
|
373 | my $b_ = $ob->refcnt; |
|
|
374 | my $a = $pl->refcnt_cnt; |
|
|
375 | my $b = $ob->refcnt_cnt; |
|
|
376 | warn "rc $a,$a_ $b,$b_\n";#d# |
|
|
377 | }; |
393 | }; |
378 | warn $@ if $@; |
394 | warn $@ if $@; |
379 | Coro::cede; |
395 | Coro::cede; |
380 | }; |
396 | }; |
381 | } |
397 | } |