--- deliantra/server/ext/login.ext 2007/01/08 19:25:53 1.19 +++ deliantra/server/ext/login.ext 2008/12/17 16:43:38 1.95 @@ -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 { @@ -15,11 +15,8 @@ sub query { my ($ns, $flags, $text) = @_; - my $current = $Coro::current; - $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); - Coro::schedule while ref $current; - - $current + $ns->query ($flags, $text, Coro::rouse_cb); + Coro::rouse_wait } sub can_cleanup { @@ -42,8 +39,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 +51,94 @@ 1 } -sub check_clean_save { +sub safe_spot($) { my ($pl) = @_; + + my $ob = $pl->ob; + + my $m = $ob->map + or return; + my $x = $ob->x; + my $y = $ob->y; + +# return 0;#d# +# 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) = @_; + + warn $pl->ob->name, ": enter map 1\n";#d# + my $ob = $pl->ob; + + my ($map, $x, $y) + = $ob->{_link_pos} + ? @{delete $ob->{_link_pos}} + : ($pl->maplevel, $ob->x, $ob->y); + + warn $pl->ob->name, ": enter map 2\n";#d# + $ob->enter_link; + warn $pl->ob->name, ": enter map 3\n";#d# + + my $m = cf::map::find $map; + my $time = delete $pl->{unclean_save}; + warn $pl->ob->name, ": enter map 4\n";#d# + + if ($time && $m) { + 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# + + 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 + my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; + } else { + ($map, $x, $y) = $pl->savebed; - unless (delete $pl->{clean_save}) { - #d#TODO + $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 + ); + } } + + warn $pl->ob->name, ": enter map 5\n";#d# + #$ob->goto ($map, $x, $y); + $ob->goto ($map, $x, $y, sub { + warn $pl->ob->name, ": enter map check\n";#d# + $_[0] + }, sub { + warn $pl->ob->name, ": enter map done\n";#d# + }); + warn $pl->ob->name, ": enter map 6\n";#d# + } # delete a player directory, be non-blocking AND synchronous... @@ -67,18 +146,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 deliantra 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"); @@ -93,7 +221,7 @@ # read username while () { - $user = query $ns, 0, "What is your name?\n:"; + $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; if ($cf::LOGIN_LOCK{$user}) { $ns->send_drawinfo ( @@ -101,20 +229,23 @@ . "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,19}\z/) { last; } else { $ns->send_drawinfo ( "Your username contains illegal characters " . "(only a-z, A-Z and 0-9 are allowed), " - . "or is not between 3 and 18 characters in length.", + . "or is not between 3 and 20 characters in length.", 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 +262,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 +278,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 +291,8 @@ # password matches, wonderful my $pl = cf::player::find $user or next; $pl->connect ($ns); - check_clean_save $pl; - $pl->{clean_save} = 1; + $pl->ob->flag (cf::FLAG_DEBUG, 1);#d# temp + enter_map $pl; last; } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; @@ -193,9 +327,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 +357,7 @@ "The passwords do not match, please try again.", cf::NDI_RED ); + Coro::Timer::sleep 0.5; next; } @@ -218,9 +367,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,30 +389,66 @@ } else { $ob->roll_stats; } + + Coro::Timer::sleep 0.05; } $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]/; - $ns->state (cf::ST_CHANGE_CLASS); - delete $pl->{deny_save};#d# too early + $pl->chargen_race_next; + Coro::Timer::sleep 0.2; + } + + # create the playerdir, if necessary, as chargen_race_done did it before + # presumably because of unique maps + aio_mkdir playerdir $pl, 0770; + $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!"); + + # XXX: Workaround for delayed client ext protocol handshake + $pl->esrv_new_player; + + delete $pl->{deny_save}; 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); + $ob->send_msg (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::NDI_REPLY); }; cf::register_command quit_character => sub { @@ -272,15 +458,11 @@ $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); + $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); } else { - $ob->reply (undef, - "Ok, quitting, hope to see you again.", - cf::NDI_UNIQUE | cf::NDI_RED); + $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); $pl->ns->flush; - $pl->quit_character; + cf::async { $pl->quit_character }; } }); }; @@ -296,11 +478,12 @@ # update respawn position $pl->savebed ($bed->map->path, $bed->x, $bed->y); + cf::async { $pl->save }; - $pl->killer ("left"); + my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; $ob->check_score; - $ob->reply (undef, "In the future, you will wake up here when you die."); + $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { if ($_[0] !~ /^[yY]/) { @@ -329,68 +512,10 @@ $_->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 + unless safe_spot $pl; } }, ); -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 = 0.1; # save at max. one player every $SAVE_INTERVAL - -our $SCHEDULER = cf::async_ext { - while () { - Coro::Timer::sleep $SCHEDULE_INTERVAL; - - # 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) { - $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# - } - } - }; - warn $@ if $@; - Coro::cede; - }; - } -}; - -$SCHEDULER->prio (1);