… | |
… | |
13 | } |
13 | } |
14 | |
14 | |
15 | sub query { |
15 | sub query { |
16 | my ($ns, $flags, $text) = @_; |
16 | my ($ns, $flags, $text) = @_; |
17 | |
17 | |
18 | my $current = $Coro::current; |
18 | $ns->query ($flags, $text, Coro::rouse_cb); |
19 | $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); |
19 | Coro::rouse_wait |
20 | Coro::schedule while ref $current; |
|
|
21 | |
|
|
22 | $current |
|
|
23 | } |
20 | } |
24 | |
21 | |
25 | sub can_cleanup { |
22 | sub can_cleanup { |
26 | my ($pl, $mtime) = @_; |
23 | my ($pl, $mtime) = @_; |
27 | |
24 | |
… | |
… | |
222 | cf::NDI_BLUE |
219 | cf::NDI_BLUE |
223 | ); |
220 | ); |
224 | |
221 | |
225 | # read username |
222 | # read username |
226 | while () { |
223 | while () { |
227 | $user = query $ns, 0, "What is your name?\n:"; |
224 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
228 | |
225 | |
229 | if ($cf::LOGIN_LOCK{$user}) { |
226 | if ($cf::LOGIN_LOCK{$user}) { |
230 | $ns->send_drawinfo ( |
227 | $ns->send_drawinfo ( |
231 | "That username is currently used in another login session. " |
228 | "That username is currently used in another login session. " |
232 | . "Chose another, or wait till the other session has ended.", |
229 | . "Chose another, or wait till the other session has ended.", |
233 | cf::NDI_RED |
230 | cf::NDI_RED |
234 | ); |
231 | ); |
235 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { |
232 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z/) { |
236 | last; |
233 | last; |
237 | } else { |
234 | } else { |
238 | $ns->send_drawinfo ( |
235 | $ns->send_drawinfo ( |
239 | "Your username contains illegal characters " |
236 | "Your username contains illegal characters " |
240 | . "(only a-z, A-Z and 0-9 are allowed), " |
237 | . "(only a-z, A-Z and 0-9 are allowed), " |
241 | . "or is not between 3 and 18 characters in length.", |
238 | . "or is not between 3 and 20 characters in length.", |
242 | cf::NDI_RED |
239 | cf::NDI_RED |
243 | ); |
240 | ); |
244 | } |
241 | } |
245 | Coro::Timer::sleep 0.4; |
242 | Coro::Timer::sleep 0.4; |
246 | } |
243 | } |
… | |
… | |
292 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
289 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
293 | nuke_str $pass; |
290 | nuke_str $pass; |
294 | # password matches, wonderful |
291 | # password matches, wonderful |
295 | my $pl = cf::player::find $user or next; |
292 | my $pl = cf::player::find $user or next; |
296 | $pl->connect ($ns); |
293 | $pl->connect ($ns); |
|
|
294 | $pl->ob->flag (cf::FLAG_DEBUG, 1);#d# temp |
297 | enter_map $pl; |
295 | enter_map $pl; |
298 | last; |
296 | last; |
299 | } elsif (can_cleanup $pl, $mtime) { |
297 | } elsif (can_cleanup $pl, $mtime) { |
300 | Coro::Timer::sleep 1; |
298 | Coro::Timer::sleep 1; |
301 | |
299 | |
… | |
… | |
432 | Coro::Timer::sleep 0.2; |
430 | Coro::Timer::sleep 0.2; |
433 | } |
431 | } |
434 | |
432 | |
435 | $ob->reply (undef, "Welcome to Deliantra!"); |
433 | $ob->reply (undef, "Welcome to Deliantra!"); |
436 | |
434 | |
|
|
435 | # XXX: Workaround for delayed client ext protocol handshake |
|
|
436 | $pl->esrv_new_player; |
|
|
437 | |
437 | delete $pl->{deny_save}; |
438 | delete $pl->{deny_save}; |
438 | |
439 | |
439 | last; |
440 | last; |
440 | } |
441 | } |
441 | }); |
442 | }); |
… | |
… | |
519 | unless safe_spot $pl; |
520 | unless safe_spot $pl; |
520 | } |
521 | } |
521 | }, |
522 | }, |
522 | ); |
523 | ); |
523 | |
524 | |
524 | ############################################################################# |
|
|
525 | |
525 | |
526 | our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs |
|
|
527 | our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds |
|
|
528 | |
|
|
529 | our $SCHEDULER = cf::async_ext { |
|
|
530 | $Coro::current->{desc} = "player scheduler"; |
|
|
531 | |
|
|
532 | while () { |
|
|
533 | Coro::EV::timer_once $SCHEDULE_INTERVAL; |
|
|
534 | |
|
|
535 | # this weird form of iteration over values is used because |
|
|
536 | # the hash changes underneath us frequently, and for |
|
|
537 | # keeps a direct reference to the value without (in 5.8 perls) |
|
|
538 | # keeping a reference, so this is prone to crashes or worse. |
|
|
539 | my @players = keys %cf::PLAYER; |
|
|
540 | for (@players) { |
|
|
541 | my $pl = $cf::PLAYER{$_} |
|
|
542 | or next; |
|
|
543 | $pl->valid or next; |
|
|
544 | |
|
|
545 | eval { |
|
|
546 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
547 | $pl->save; |
|
|
548 | |
|
|
549 | unless ($pl->active || $pl->ns) { |
|
|
550 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
551 | my $ob = $pl->ob; |
|
|
552 | |
|
|
553 | my $pl_ref = $pl->refcnt_cnt; |
|
|
554 | my $ob_ref = $ob->refcnt_cnt; |
|
|
555 | |
|
|
556 | ## pl_ref == $pl + ob->contr + %cf::PLAYER |
|
|
557 | ## ob_ref == $ob + pl->observe + simply being an object |
|
|
558 | if ($pl_ref == 3 && $ob_ref == 3) { |
|
|
559 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
560 | |
|
|
561 | # remove from sight and get fresh "copies" |
|
|
562 | $pl = delete $cf::PLAYER{$ob->name}; |
|
|
563 | $ob = $pl->ob; |
|
|
564 | |
|
|
565 | $pl->destroy; # destroys $ob |
|
|
566 | } else { |
|
|
567 | my $a_ = $pl->refcnt;#d# |
|
|
568 | my $b_ = $ob->refcnt;#d# |
|
|
569 | |
|
|
570 | warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref/3 ob $ob_ref/3 (C pl $a_/1 ob $b_/2)\n";#d# |
|
|
571 | } |
|
|
572 | } |
|
|
573 | } |
|
|
574 | }; |
|
|
575 | warn $@ if $@; |
|
|
576 | cf::cede_to_tick; |
|
|
577 | }; |
|
|
578 | } |
|
|
579 | }; |
|
|
580 | |
|
|
581 | $SCHEDULER->prio (1); |
|
|
582 | |
|
|