--- deliantra/server/ext/login.ext 2006/12/22 16:34:00 1.1 +++ deliantra/server/ext/login.ext 2007/07/14 19:27:59 1.64 @@ -1,29 +1,32 @@ -#! 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; -# testbed for coroutines in crossfire : +# paranoia function to overwrite a string-in-place +sub nuke_str { + substr $_[0], 0, (length $_[0]), "x" x length $_[0] +} sub query { 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 } 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 @@ -34,13 +37,13 @@ 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. " . "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, " @@ -51,6 +54,64 @@ 1 } +sub enter_map { + my ($pl) = @_; + + my $ob = $pl->ob; + + my ($map, $x, $y) + = $ob->{_link_pos} + ? @{delete $ob->{_link_pos}} + : ($pl->maplevel, $ob->x, $ob->y); + + $ob->enter_link; + + if (my $time = delete $pl->{unclean_save}) { + if (my $m = cf::map::find $map) { + 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# + + # for now, just go back to savebed + ($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 + } 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... # (thats hard, so we crap out and fork). sub nuke_playerdir { @@ -62,12 +123,57 @@ . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; } -sub on_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; + } - $ns->coro (sub { + if ($ns->mapmode < cf::Map1aCmd) { + $ns->send_drawinfo (<flush; + return $ns->destroy; + } + + $ns->pl and return $ns->destroy; + + $ns->async (sub { my ($user, $pass); $ns->send_packet ("addme_success"); @@ -83,13 +189,24 @@ # read username while () { $user = query $ns, 0, "What is your name?\n:"; - last if $user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/; - $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.", - cf::NDI_RED - ); + + if ($cf::LOGIN_LOCK{$user}) { + $ns->send_drawinfo ( + "That username is currently used in another login session. " + . "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}\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.", + cf::NDI_RED + ); + } + Coro::Timer::sleep 0.4; } check_playing $ns, $user and next; @@ -110,53 +227,59 @@ . "that cannot be too much to ask for :)", cf::NDI_RED ); + Coro::Timer::sleep 0.4; } - check_playing $ns, $user and next; + # lock this username for the remainder of this login session + if ($cf::LOGIN_LOCK{$user}) { + $ns->send_drawinfo ( + "That username is currently used in another login session. " + . "Chose another, or wait till the other session has ended.", + cf::NDI_RED + ); + next; + } + local $cf::LOGIN_LOCK{$user} = 1; - my $dir = "$PLAYERDIR/$user"; - my $plfile = "$dir/$user.pl"; + 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]; + if (my $pl = cf::player::find $user) { + aio_stat $pl->path and next; + my $mtime = (stat _)[9]; + my $hash = $pl->password; - 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; - $buf =~ /^password (\S+)$/m or next; - my $hash = $1; - - check_playing $ns, $user and next; - - 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); + enter_map $pl; last; - } - - if (can_cleanup $buf, $mtime) { + } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; $ns->send_drawinfo ( - "Player exists, but password does not match. If this is you account, " - . "please try again. If not, you can now opt to take over this account " + "Player exists, but password does not match. If this is your account, " + . "please try again. If not, you can now decide to take over this account " . "because it has not been in-use for some time.", cf::NDI_RED ); + #TODO: nuke_str (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ or next; # check if the file hasn't changed - aio_stat $plfile or next; + aio_stat cf::player::path $user and next; $mtime == (stat _)[9] or next; - # nuke playerdir, this might block, but it needs to be atomic - nuke_playerdir $user; + $pl->quit_character; # fall through to creation } else { + nuke_str $pass; + Coro::Timer::sleep 1; $ns->send_drawinfo ( @@ -166,19 +289,49 @@ ); 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 - check_playing $ns, $user and next; + # just to make sure nothing is left over nuke_playerdir $user; - my $pl = cf::player::create; - $pl->ob->name ($user); + my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; + + if ($pass2 ne $pass) { + nuke_str $pass; + nuke_str $pass2; + $ns->send_drawinfo ( + "The passwords do not match, please try again.", + cf::NDI_RED + ); + Coro::Timer::sleep 0.5; + next; + } + + nuke_str $pass2; + + 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); my $ob = $pl->ob; + $ob->goto ($pl->maplevel, $ob->x, $ob->y); + while () { $ob->update_stats; $pl->save_stats; @@ -197,21 +350,81 @@ } 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 (-1, "chargen-race-title", ucfirst $pl->title); + my $msg = $ob->msg; + $msg =~ s/(?<=\S)\n(?=\S)/ /g; + $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg); + + 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; + } + + $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 Crossfire!"); - $ns->state (cf::ST_CHANGE_CLASS); + 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); +}; + +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; + cf::async { $pl->quit_character }; + } + }); +}; cf::object->attach ( type => cf::SAVEBED, @@ -220,26 +433,105 @@ return cf::override 0 unless $ob->type == cf::PLAYER; - my $ns = $ob->contr->ns; + 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 }; - #TODO? - #strcpy (pl->contr->killer, "left"); - #check_score (pl); /* Always check score */ + $pl->killer ("left"); + $ob->check_score; $ob->reply (undef, "In the future, you will wake up here when you die."); - $ob->contr->save (); - $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { - if ($_[0] !~ /^[yY]/) { - $ob->contr->save (1); - $ns->destroy (); - } + $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { + if ($_[0] !~ /^[yY]/) { + $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); + $pl->deactivate; + $pl->ns->destroy; + } else { + cf::async { $pl->save }; + } }); }, ); -cf::client->attach (package => __PACKAGE__); +cf::player->attach ( + on_login => sub { + my ($pl) = @_; + my $name = $pl->ob->name; + + $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; + }, + on_logout => sub { + my ($pl, $cleanly) = @_; + my $name = $pl->ob->name; + + if ($cleanly) { + $_->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; + } + }, +); + +############################################################################# + +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_begin; + $pl->save; + + 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# + } + } + } + }; + warn $@ if $@; + Coro::cede; + }; + } +}; + +$SCHEDULER->prio (1);