--- deliantra/server/ext/login.ext 2007/01/08 22:32:10 1.20 +++ deliantra/server/ext/login.ext 2008/01/07 01:55:19 1.78 @@ -1,11 +1,11 @@ -#! perl +#! perl # mandatory # login handling use Fcntl; use Coro::AIO; -my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; +our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; # paranoia function to overwrite a string-in-place sub nuke_str { @@ -16,7 +16,7 @@ my ($ns, $flags, $text) = @_; my $current = $Coro::current; - $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); + $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); Coro::schedule while ref $current; $current @@ -42,8 +42,8 @@ $ns->send_drawinfo ( "That player is already logged in on this server. " . "If you want to create a new player, choose another name. " - . "If you are already a registered player, make sure nobody " - . "else is using your account at this time. If you lost your conenction " + . "If you have already a registered, make sure nobody " + . "else is using your account at this time. If you lost your connection " . "then the server will likely timeout within a minute. If you still " . "cannot log-in after a minute, you are still logged in. Make sure " . "you do not have another client running. If you use windows, reboot, " @@ -54,12 +54,74 @@ 1 } -sub check_clean_save { +sub safe_spot($$$) { + my ($m, $x, $y) = @_; + + return 0; + warn join ":", $m->at ($x, $y);#d# + warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; + return 0; + + scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) +} + +sub enter_map { my ($pl) = @_; - unless (delete $pl->{clean_save}) { - #d#TODO + my $ob = $pl->ob; + + my ($map, $x, $y) + = $ob->{_link_pos} + ? @{delete $ob->{_link_pos}} + : ($pl->maplevel, $ob->x, $ob->y); + + $ob->enter_link; + + my $m = cf::map::find $map; + my $time = delete $pl->{unclean_save}; + + if ($time && $m && !safe_spot $m, $x, $y) { + if ($time < $m->{instantiate_time}) { + # the map was reset in the meantime + my $age = $cf::RUNTIME - $time; + + warn $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d# + + ($map, $x, $y) = $pl->savebed; + + if ($age >= $MAX_DISCONNECT_TIME) { + $ob->message ( + "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " + . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " + . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... " + . "H", + cf::NDI_RED + ); + # kill them. + # reminds me of the famous badness 10000 syndrome... + $ob->stats->hp (-10000); #] if they survive this they deserved to live + $pl->killer ("a cave-in"); + } else { + $ob->message ( + "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " + . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " + . "Better use a savebed next time, much worse things could have happened... " + . "H", + cf::NDI_RED + ); + } + } else { + $ob->message ( + "You didn't use a bed to reality 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. " + . "H", + cf::NDI_RED + ); + } } + + $ob->goto ($map, $x, $y); } # delete a player directory, be non-blocking AND synchronous... @@ -67,18 +129,67 @@ sub nuke_playerdir { my ($user) = @_; - aio_stat "$PLAYERDIR/$user"; - system "cd \Q$PLAYERDIR\E " - . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " - . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; + my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; + + cf::fork_call { + rename "$PLAYERDIR/$user", $temp; + system "rm", "-rf", $temp; + }; } -sub addme { +cf::client->attach (on_addme => sub { my ($ns) = @_; - $ns->destroy if $ns->pl; + if (!$ns->facecache) + { + $ns->send_drawinfo (< Configure => Map & Image => Cache Images. +*** jcrossclient: your client is broken, use CFPlus or gcfclient. +*** +*** +EOF + if ($ns->version =~ /jcrossclient/) { + # let them, for now + } else { + $ns->flush; + return $ns->destroy; + } + + # $ns->facecache = true; + } + + if ($ns->mapmode < cf::Map1aCmd) { + $ns->send_drawinfo (<flush; + return $ns->destroy; + } + + $ns->pl and return $ns->destroy; $ns->async (sub { + $Coro::current->{desc} = "addme init"; + my ($user, $pass); $ns->send_packet ("addme_success"); @@ -101,7 +212,7 @@ . "Chose another, or wait till the other session has ended.", cf::NDI_RED ); - } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { + } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { last; } else { $ns->send_drawinfo ( @@ -111,10 +222,13 @@ cf::NDI_RED ); } + Coro::Timer::sleep 0.4; } check_playing $ns, $user and next; + $Coro::current->{desc} = "addme($user) pass"; + $ns->send_drawinfo ( "Welcome $user, please enter your password now. " . "New users should now choose a password. " @@ -131,6 +245,7 @@ . "that cannot be too much to ask for :)", cf::NDI_RED ); + Coro::Timer::sleep 0.4; } # lock this username for the remainder of this login session @@ -146,6 +261,8 @@ check_playing $ns, $user and next; + $Coro::current->{desc} = "addme($user) check"; + # try to read the user file and check the password if (my $pl = cf::player::find $user) { aio_stat $pl->path and next; @@ -157,8 +274,7 @@ # password matches, wonderful my $pl = cf::player::find $user or next; $pl->connect ($ns); - check_clean_save $pl; - $pl->{clean_save} = 1; + enter_map $pl; last; } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; @@ -193,9 +309,23 @@ ); next; } + } else { + # unable to load the playerfile: + # check wether the player dir exists, which means the file is corrupted or + # something very similar. + if (!aio_stat cf::player::playerdir $user) { + $ns->send_drawinfo ( + "Unable to retrieve this player. It might be a locked or broken account. " + . "If this is your account, ask a dungeon master for assistance. " + . "Otherwise choose a different login name.", + cf::NDI_RED + ); + next; + } } # the rest of this function is character creation + $Coro::current->{desc} = "addme($user) chargen"; # just to make sure nothing is left over nuke_playerdir $user; @@ -209,6 +339,7 @@ "The passwords do not match, please try again.", cf::NDI_RED ); + Coro::Timer::sleep 0.5; next; } @@ -218,9 +349,10 @@ $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); nuke_str $pass; $pl->connect ($ns); - my $ob = $pl->ob; + $ob->goto ($pl->maplevel, $ob->x, $ob->y); + while () { $ob->update_stats; $pl->save_stats; @@ -239,22 +371,52 @@ } else { $ob->roll_stats; } + + Coro::Timer::sleep 0.2; } $ob->set_animation (2); $ob->add_statbonus; - $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); - $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, - "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); + while () { + $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); + my $msg = $ob->msg; + $msg =~ s/(?<=\S)\n(?=\S)/ /g; + $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE); + + my $res = query $ns, cf::CS_QUERY_SINGLECHAR, + "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; + + last if $res =~ /[dD]/; + + $pl->chargen_race_next; + Coro::Timer::sleep 0.2; + } - $ns->state (cf::ST_CHANGE_CLASS); - delete $pl->{deny_save};#d# too early + $pl->chargen_race_done; + + while () { + my $res = query $ns, cf::CS_QUERY_SINGLECHAR, + "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; + + if ($res =~ /^[fF]/) { + $pl->gender (1); + last; + } elsif ($res =~ /^[mM]/) { + $pl->gender (0); + last; + } + Coro::Timer::sleep 0.2; + } + + $ob->reply (undef, "Welcome to Deliantra!"); + + delete $pl->{deny_save}; last; } }); -} +}); cf::register_command quit => sub { my ($ob, $arg) = @_; @@ -280,7 +442,7 @@ "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED); $pl->ns->flush; - $pl->quit_character; + cf::async { $pl->quit_character }; } }); }; @@ -296,6 +458,7 @@ # update respawn position $pl->savebed ($bed->map->path, $bed->x, $bed->y); + cf::async { $pl->save }; $pl->killer ("left"); $ob->check_score; @@ -329,24 +492,21 @@ $_->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; - delete $pl->{clean_save}; + $pl->{unclean_save} = $cf::RUNTIME; } }, ); -cf::client->attach ( - on_addme => \&addme, -); - ############################################################################# -our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs -our $SAVE_TIMEOUT = 200; # save players every n seconds -our $SAVE_INTERVAL = 1.1; # save at max. one player every $SAVE_INTERVAL +our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs +our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds our $SCHEDULER = cf::async_ext { + $Coro::current->{desc} = "player scheduler"; + while () { - Coro::Timer::sleep $SCHEDULE_INTERVAL; + Coro::EV::timer_once $SCHEDULE_INTERVAL; # this weird form of iteration over values is used because # the hash changes underneath us frequently, and for @@ -361,28 +521,30 @@ eval { if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { $pl->save; - Coro::Timer::sleep $SAVE_INTERVAL; - } - 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; - my $b_ = $ob->refcnt; - my $pl_ref = $pl->refcnt_cnt; - my $ob_ref = $ob->refcnt_cnt; - - if ($pl_ref == 2 && $ob_ref == 1) { - warn "player-scheduler destroy ", $ob->name;#d# - #delete $cf::PLAYER{$ob->name}; - ## pl_ref == one from object + one from cf::PLAYER - ## ob_ref == one from simply being an object - #$ob->destroy; - #$pl->destroy; - } else { - warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# + unless ($pl->active || $pl->ns) { + # check refcounts, this is tricky and needs to be adjusted to fit server internals + my $ob = $pl->ob; + + my $pl_ref = $pl->refcnt_cnt; + my $ob_ref = $ob->refcnt_cnt; + + ## pl_ref == $pl + ob->contr + %cf::PLAYER + ## ob_ref == $ob + pl->observe + simply being an object + if ($pl_ref == 3 && $ob_ref == 3) { + warn "player-scheduler destroy ", $ob->name;#d# + + # remove from sight and get fresh "copies" + $pl = delete $cf::PLAYER{$ob->name}; + $ob = $pl->ob; + + $pl->destroy; # destroys $ob + } else { + my $a_ = $pl->refcnt;#d# + my $b_ = $ob->refcnt;#d# + + warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref ob $ob_ref (C pl $a_ ob $b_)\n";#d# + } } } };