--- deliantra/server/ext/login.ext 2007/01/08 14:11:05 1.17 +++ deliantra/server/ext/login.ext 2007/04/27 17:16:13 1.47 @@ -1,11 +1,10 @@ -#! perl +#! perl # MANDATORY # login handling use Fcntl; use Coro::AIO; - -my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; +use List::Util qw(min max); # paranoia function to overwrite a string-in-place sub nuke_str { @@ -16,17 +15,17 @@ 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 @@ -57,7 +56,13 @@ sub check_clean_save { my ($pl) = @_; - unless (delete $pl->{clean_save}) { + 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 } } @@ -73,10 +78,182 @@ . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; } +sub send_capabilities { + my ($ns) = @_; + + return unless $ns->extcmd; + + $ns->ext_event (capabilities => + # id, name, flags (1 == 2d), edge length + tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32]], + ); +} + +sub setup { + my ($ns, $args) = @_; + + # run through the cmds of setup + # syntax is setup ... + # + # we send the status of the cmd back, or a FALSE is the cmd is the server unknown + # The client then must sort this out + + my %setup = split / +/, $args; + while (my ($k, $v) = each %setup) { + if ($k eq "sound") { + $ns->sound ($v); + + } elsif ($k eq "exp64") { + $setup{$k} = 1; + + } elsif ($k eq "spellmon") { + $ns->monitor_spells ($v); + + } elsif ($k eq "darkness") { + $ns->darkness ($v); + + } elsif ($k eq "map1cmd") { + $ns->mapmode (cf::Map1Cmd) if $v > 0; + + } elsif ($k eq "map1acmd") { + $ns->mapmode (cf::Map1aCmd) if $v > 0; + + } elsif ($k eq "map2cmd") { + # gcfclient bug, map1acmd is sent too late + $ns->mapmode (cf::Map1aCmd); + $setup{$k} = "FALSE"; + + } elsif ($k eq "newmapcmd") { + $ns->newmapcmd ($v); + + } elsif ($k eq "mapinfocmd") { + $ns->mapinfocmd ($v); + + } elsif ($k eq "extcmd") { + $ns->extcmd ($v > 0); + send_capabilities $ns; + + } elsif ($k eq "extmap") { + $ns->extmap ($v); + + } elsif ($k eq "facecache") { + if (!$v) { + $v = 1; + $setup{$k} = $v; + $ns->send_drawinfo ("(trying to forcefully enable facecaching)", cf::NDI_RED); + } + + $ns->facecache ($v); + + } elsif ($k eq "faceset") { + $ns->faceset (0); + $setup{$k} = 0; + # $ns->image2 (1) + + } elsif ($k eq "tileset") { + $setup{$k} = $ns->faceset ($v & 1); + + } elsif ($k eq "itemcmd") { + # Version of the item protocol command to use. Currently, + # only supported versions are 1 and 2. Using a numeric + # value will make it very easy to extend this in the future. + $ns->itemcmd ($v) if $v >= 1 && $v <= 2; + + $setup{$k} = $ns->itemcmd; + + } elsif ($k eq "mapsize") { + my ($x, $y) = split /x/, $v; + + $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, ($x - 1) | 1); + $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, ($y - 1) | 1); + + $setup{$k} = "${x}x${y}"; + + } elsif ($k eq "extendedMapInfos") { + $ns->ext_mapinfos ($v); + + } elsif ($k eq "extendedTextInfos") { + $ns->has_readable_type ($v); + + } elsif ($k eq "smoothing") { # cfplus-style smoothing + $ns->smoothing ($v); + + } elsif ($k eq "fxix") { + $ns->fxix ($v); + + } elsif ($k eq "msg") { + $ns->can_msg ($v); + + } elsif ($k eq "excmd") { + # we support it + + } else { + # other commands: + # sexp: no idea, probably for oudated servers + # tick: more stupidity, server should send a tick per tick + + $setup{$k} = "FALSE"; + } + } + + $ns->send_packet (join " ", setup => %setup); + + cf::datalog setup => + request => $args, + reply => \%setup, + client => $ns->version, + ; +} + sub addme { 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 { my ($user, $pass); @@ -101,7 +278,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 ( @@ -147,22 +324,19 @@ check_playing $ns, $user and next; # try to read the user file and check the password - if (my $fh = aio_open cf::player::path $user, 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::find $user or next; $pl->connect ($ns); check_clean_save $pl; - $pl->{clean_save} = 1; last; - } elsif (can_cleanup $buf, $mtime) { + } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; $ns->send_drawinfo ( @@ -180,7 +354,7 @@ aio_stat cf::player::path $user and next; $mtime == (stat _)[9] or next; - nuke_playerdir $user; + $pl->quit_character; # fall through to creation } else { @@ -195,6 +369,19 @@ ); 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 @@ -246,12 +433,23 @@ $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]/; - $ns->state (cf::ST_CHANGE_CLASS); - delete $pl->{deny_save};#d# too early + $pl->chargen_race_next; + } + + $pl->chargen_race_done; + + delete $pl->{deny_save}; last; } @@ -282,7 +480,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 }; } }); }; @@ -298,6 +496,7 @@ # update respawn position $pl->savebed ($bed->map->path, $bed->x, $bed->y); + cf::async { $pl->save }; $pl->killer ("left"); $ob->check_score; @@ -331,24 +530,25 @@ $_->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, + on_setup => \&setup, ); ############################################################################# -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 $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 () { - Coro::Timer::sleep $SCHEDULE_INTERVAL; + $schedule_interval->next; # this weird form of iteration over values is used because # the hash changes underneath us frequently, and for @@ -362,29 +562,33 @@ eval { if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { + cf::wait_for_tick_begin; $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) { + # 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, " pp$pl_ref,pc$a_ op$ob_ref,oc$b_\n";#d# + } } } };