--- deliantra/server/ext/login.ext 2007/01/04 16:19:31 1.9 +++ deliantra/server/ext/login.ext 2007/01/22 03:44:41 1.27 @@ -1,4 +1,4 @@ -#! perl +#! perl # MANDATORY # login handling @@ -23,10 +23,10 @@ } sub can_cleanup { - my ($playerfile, $mtime) = @_; + my ($pl, $mtime) = @_; my $age = time - $mtime; - my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; + my $level = $pl->ob->level; ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 @@ -37,7 +37,7 @@ sub check_playing { my ($ns, $user) = @_; - return unless cf::player::find $user; + return unless cf::player::find_active $user; $ns->send_drawinfo ( "That player is already logged in on this server. " @@ -54,6 +54,20 @@ 1 } +sub check_clean_save { + my ($pl) = @_; + + if (my $time = delete $pl->{unclean_save}) { + $pl->ns->send_drawinfo ( + "You didn't use a savebed to leave this realm. This is very dangerous, " + . "as lots of things could happen when you leave by other means, such as cave-ins, " + . "or monsters suddenly snapping your body. Better use a savebed next time.", + cf::NDI_RED + ); + #d#TODO + } +} + # delete a player directory, be non-blocking AND synchronous... # (thats hard, so we crap out and fork). sub nuke_playerdir { @@ -70,7 +84,7 @@ $ns->destroy if $ns->pl; - $ns->coro (sub { + $ns->async (sub { my ($user, $pass); $ns->send_packet ("addme_success"); @@ -125,9 +139,6 @@ ); } - my $dir = "$PLAYERDIR/$user"; - my $plfile = "$dir/$user.pl"; - # lock this username for the remainder of this login session if ($cf::LOGIN_LOCK{$user}) { $ns->send_drawinfo ( @@ -142,21 +153,19 @@ check_playing $ns, $user and next; # try to read the user file and check the password - if (my $fh = aio_open $plfile, O_RDONLY, 0) { - my $mtime = (stat $fh)[9]; - - 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; - $buf =~ /^password (\S+)$/m or next; - my $hash = $1; + if (my $pl = cf::player::find $user) { + aio_stat $pl->path and next; + my $mtime = (stat _)[9]; + my $hash = $pl->password; - if ($hash eq crypt $pass, $hash) { + if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { nuke_str $pass; # password matches, wonderful - my $pl = cf::player::load $plfile or next; - $pl->enable_save (1); + my $pl = cf::player::find $user or next; $pl->connect ($ns); + check_clean_save $pl; last; - } elsif (can_cleanup $buf, $mtime) { + } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; $ns->send_drawinfo ( @@ -171,10 +180,10 @@ or next; # check if the file hasn't changed - aio_stat $plfile and next; + aio_stat cf::player::path $user and next; $mtime == (stat _)[9] or next; - nuke_playerdir $user; + $pl->quit_character; # fall through to creation } else { @@ -210,8 +219,7 @@ nuke_str $pass2; - my $pl = cf::player::create; - $pl->ob->name ($user); + my $pl = cf::player::new $user; $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); nuke_str $pass; $pl->connect ($ns); @@ -246,13 +254,42 @@ "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); $ns->state (cf::ST_CHANGE_CLASS); - $pl->enable_save (1);#d# too early + delete $pl->{deny_save};#d# too early last; } }); } +cf::register_command quit => sub { + my ($ob, $arg) = @_; + + $ob->reply (undef, + "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " + . "If you are sure you want to do this, then use the quit_character command instead of quit.", + cf::NDI_UNIQUE | cf::NDI_RED); +}; + +cf::register_command quit_character => sub { + my ($ob, $arg) = @_; + + my $pl = $ob->contr; + + $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { + if ($_[0] !~ /^[yY]/) { + $ob->reply (undef, + "Ok, not not quitting then.", + cf::NDI_UNIQUE | cf::NDI_RED); + } else { + $ob->reply (undef, + "Ok, quitting, hope to see you again.", + cf::NDI_UNIQUE | cf::NDI_RED); + $pl->ns->flush; + $pl->quit_character; + } + }); +}; + cf::object->attach ( type => cf::SAVEBED, on_apply => sub { @@ -260,21 +297,24 @@ return cf::override 0 unless $ob->type == cf::PLAYER; + my $pl = $ob->contr; + # update respawn position - $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); + $pl->savebed ($bed->map->path, $bed->x, $bed->y); + cf::async { $pl->save }; - $ob->contr->killer ("left"); + $pl->killer ("left"); $ob->check_score; $ob->reply (undef, "In the future, you will wake up here when you die."); - $ob->contr->save (1); - $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { + $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { if ($_[0] !~ /^[yY]/) { - $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); - $ob->contr->ns->destroy; + $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); + $pl->deactivate; + $pl->ns->destroy; } else { - $ob->contr->enable_save (1); + cf::async { $pl->save }; } }); }, @@ -295,6 +335,7 @@ $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; } else { $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; + $pl->{unclean_save} = $cf::RUNTIME; } }, ); @@ -303,3 +344,63 @@ on_addme => \&addme, ); +############################################################################# + +our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs +our $SAVE_TIMEOUT = 20; # save players every n seconds + +our $SCHEDULER = cf::async_ext { + my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL); + while () { + $schedule_interval->next; + + # this weird form of iteration over values is used because + # the hash changes underneath us frequently, and for + # keeps a direct reference to the value without (in 5.8 perls) + # keeping a reference, so this is prone to crashes or worse. + my @players = keys %cf::PLAYER; + for (@players) { + my $pl = $cf::PLAYER{$_} + or next; + $pl->valid or next; + + eval { + if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { + $cf::WAIT_FOR_TICK_ONE->wait; + $pl->save; + + unless ($pl->active) { + # check refcounts, this is tricky and needs to be adjusted to fit server internals + my $ob = $pl->ob; + Scalar::Util::weaken $pl; + Scalar::Util::weaken $ob; + my $a_ = $pl->refcnt;#d# + my $b_ = $ob->refcnt;#d# + my $pl_ref = $pl->refcnt_cnt; + my $ob_ref = $ob->refcnt_cnt; + + ## pl_ref == one from object + one from cf::PLAYER + ## ob_ref == one from simply being an object + if ($pl_ref == 2 && $ob_ref == 1) { + warn "player-scheduler destroy ", $ob->name;#d# + + # remove from sight and get fresh "copies" + $pl = delete $cf::PLAYER{$ob->name}; + $ob = $pl->ob; + + $ob->destroy; + $pl->destroy; + } else { + warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# + } + } + } + }; + warn $@ if $@; + Coro::cede; + }; + } +}; + +$SCHEDULER->prio (1); +