… | |
… | |
158 | nuke_str $pass; |
158 | nuke_str $pass; |
159 | # password matches, wonderful |
159 | # password matches, wonderful |
160 | my $pl = cf::player::find $user or next; |
160 | my $pl = cf::player::find $user or next; |
161 | $pl->connect ($ns); |
161 | $pl->connect ($ns); |
162 | check_clean_save $pl; |
162 | check_clean_save $pl; |
|
|
163 | $pl->{clean_save} = 1; |
163 | last; |
164 | last; |
164 | } elsif (can_cleanup $buf, $mtime) { |
165 | } elsif (can_cleanup $buf, $mtime) { |
165 | Coro::Timer::sleep 1; |
166 | Coro::Timer::sleep 1; |
166 | |
167 | |
167 | $ns->send_drawinfo ( |
168 | $ns->send_drawinfo ( |
… | |
… | |
291 | on_apply => sub { |
292 | on_apply => sub { |
292 | my ($bed, $ob) = @_; |
293 | my ($bed, $ob) = @_; |
293 | |
294 | |
294 | return cf::override 0 unless $ob->type == cf::PLAYER; |
295 | return cf::override 0 unless $ob->type == cf::PLAYER; |
295 | |
296 | |
296 | my $pl = $ob->pl; |
297 | my $pl = $ob->contr; |
297 | |
298 | |
298 | # update respawn position |
299 | # update respawn position |
299 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
300 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
300 | |
301 | |
301 | $pl->killer ("left"); |
302 | $pl->killer ("left"); |
… | |
… | |
307 | if ($_[0] !~ /^[yY]/) { |
308 | if ($_[0] !~ /^[yY]/) { |
308 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
309 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
309 | $pl->deactivate; |
310 | $pl->deactivate; |
310 | $pl->ns->destroy; |
311 | $pl->ns->destroy; |
311 | } else { |
312 | } else { |
312 | cf::async { |
313 | cf::async { $pl->save }; |
313 | $pl->{clean_save} = 1; |
|
|
314 | $pl->save; |
|
|
315 | }; |
|
|
316 | } |
314 | } |
317 | }); |
315 | }); |
318 | }, |
316 | }, |
319 | ); |
317 | ); |
320 | |
318 | |
… | |
… | |
331 | |
329 | |
332 | if ($cleanly) { |
330 | if ($cleanly) { |
333 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
331 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
334 | } else { |
332 | } else { |
335 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
333 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
334 | delete $pl->{clean_save}; |
336 | } |
335 | } |
337 | }, |
336 | }, |
338 | ); |
337 | ); |
339 | |
338 | |
340 | cf::client->attach ( |
339 | cf::client->attach ( |
… | |
… | |
342 | ); |
341 | ); |
343 | |
342 | |
344 | ############################################################################# |
343 | ############################################################################# |
345 | |
344 | |
346 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
345 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
347 | our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory |
|
|
348 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
346 | our $SAVE_TIMEOUT = 200; # save players every n seconds |
349 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
347 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
350 | |
348 | |
351 | our $SCHEDULER = cf::async_ext { |
349 | our $SCHEDULER = cf::async_ext { |
352 | while () { |
350 | while () { |
353 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
351 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
… | |
… | |
365 | eval { |
363 | eval { |
366 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
364 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
367 | $pl->save; |
365 | $pl->save; |
368 | Coro::Timer::sleep $SAVE_INTERVAL; |
366 | Coro::Timer::sleep $SAVE_INTERVAL; |
369 | } |
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 | } |
370 | }; |
390 | }; |
371 | warn $@ if $@; |
391 | warn $@ if $@; |
372 | Coro::cede; |
392 | Coro::cede; |
373 | }; |
393 | }; |
374 | } |
394 | } |