ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
(Generate patch)

Comparing deliantra/server/ext/login.ext (file contents):
Revision 1.20 by root, Mon Jan 8 22:32:10 2007 UTC vs.
Revision 1.62 by root, Sat Jul 14 14:33:29 2007 UTC

1#! perl 1#! perl # mandatory
2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7
8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9 7
10# paranoia function to overwrite a string-in-place 8# paranoia function to overwrite a string-in-place
11sub nuke_str { 9sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0] 10 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13} 11}
14 12
15sub query { 13sub query {
16 my ($ns, $flags, $text) = @_; 14 my ($ns, $flags, $text) = @_;
17 15
18 my $current = $Coro::current; 16 my $current = $Coro::current;
19 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); 17 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] });
20 Coro::schedule while ref $current; 18 Coro::schedule while ref $current;
21 19
22 $current 20 $current
23} 21}
24 22
40 return unless cf::player::find_active $user; 38 return unless cf::player::find_active $user;
41 39
42 $ns->send_drawinfo ( 40 $ns->send_drawinfo (
43 "That player is already logged in on this server. " 41 "That player is already logged in on this server. "
44 . "If you want to create a new player, choose another name. " 42 . "If you want to create a new player, choose another name. "
45 . "If you are already a registered player, make sure nobody " 43 . "If you have already a registered, make sure nobody "
46 . "else is using your account at this time. If you lost your conenction " 44 . "else is using your account at this time. If you lost your connection "
47 . "then the server will likely timeout within a minute. If you still " 45 . "then the server will likely timeout within a minute. If you still "
48 . "cannot log-in after a minute, you are still logged in. Make sure " 46 . "cannot log-in after a minute, you are still logged in. Make sure "
49 . "you do not have another client running. If you use windows, reboot, " 47 . "you do not have another client running. If you use windows, reboot, "
50 . "this will fix anything.", 48 . "this will fix anything.",
51 cf::NDI_RED 49 cf::NDI_RED
52 ); 50 );
53 51
54 1 52 1
55} 53}
56 54
57sub check_clean_save { 55sub enter_map {
58 my ($pl) = @_; 56 my ($pl) = @_;
59 57
60 unless (delete $pl->{clean_save}) { 58 my $ob = $pl->ob;
61 #d#TODO 59
60 my ($map, $x, $y)
61 = $ob->{_link_pos}
62 ? @{delete $ob->{_link_pos}}
63 : ($pl->maplevel, $ob->x, $ob->y);
64
65 $ob->enter_link;
66
67 if (my $time = delete $pl->{unclean_save}) {
68 if (my $m = cf::map::find $map) {
69 if ($time < $m->{instantiate_time} || 1) {#d#
70 # the map was reset in the meantime
71 my $age = $cf::RUNTIME - $time;
72 warn $ob->name, " map reset after logout, logout age $age\n";#d#
73
74 # for now, just go back to savebed
75 ($map, $x, $y) = $pl->savebed;
76
77 $pl->ns->send_drawinfo (
78 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. "
79 . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. "
80 . "Better use a savebed next time, much worse things could have happened...",
81 cf::NDI_RED
82 );
83 } else {
84 $pl->ns->send_drawinfo (
85 "You didn't use a bed to reality to leave this realm. This is very dangerous, "
86 . "as lots of things could happen when you leave by other means, such as cave-ins, "
87 . "or monsters suddenly snapping your body. Better use a savebed next time.",
88 cf::NDI_RED
89 );
90 }
91 }
62 } 92 }
93
94 $ob->goto ($map, $x, $y);
63} 95}
64 96
65# delete a player directory, be non-blocking AND synchronous... 97# delete a player directory, be non-blocking AND synchronous...
66# (thats hard, so we crap out and fork). 98# (thats hard, so we crap out and fork).
67sub nuke_playerdir { 99sub nuke_playerdir {
71 system "cd \Q$PLAYERDIR\E " 103 system "cd \Q$PLAYERDIR\E "
72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 104 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 105 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
74} 106}
75 107
76sub addme { 108cf::client->attach (on_addme => sub {
77 my ($ns) = @_; 109 my ($ns) = @_;
78 110
79 $ns->destroy if $ns->pl; 111 if (!$ns->facecache)
112 {
113 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
114
115
116***
117*** WARNING:
118*** Your client does not support face/image caching,
119*** or it has been disabled. Face caching is mandatory
120*** so please enable it or use a newer client.
121***
122*** Look at your client preferences:
123***
124*** CFPlus: all known versions automatically enable the facecache.
125*** cfclient: use the -cache commandline option.
126*** cfclient: map will not redraw automatically (bug).
127*** gcfclient: use -cache commandline option, or enable
128*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.
129*** jcrossclient: your client is broken, use CFPlus or gcfclient.
130***
131***
132EOF
133 if ($ns->version =~ /jcrossclient/) {
134 # let them, for now
135 } else {
136 $ns->flush;
137 return $ns->destroy;
138 }
139
140 # $ns->facecache = true;
141 }
142
143 if ($ns->mapmode < cf::Map1aCmd) {
144 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
145
146
147***
148*** WARNING:
149*** Your client is too old. Please upgrade to a newer version.
150EOF
151
152 $ns->flush;
153 return $ns->destroy;
154 }
155
156 $ns->pl and return $ns->destroy;
80 157
81 $ns->async (sub { 158 $ns->async (sub {
82 my ($user, $pass); 159 my ($user, $pass);
83 160
84 $ns->send_packet ("addme_success"); 161 $ns->send_packet ("addme_success");
99 $ns->send_drawinfo ( 176 $ns->send_drawinfo (
100 "That username is currently used in another login session. " 177 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.", 178 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED 179 cf::NDI_RED
103 ); 180 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { 181 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) {
105 last; 182 last;
106 } else { 183 } else {
107 $ns->send_drawinfo ( 184 $ns->send_drawinfo (
108 "Your username contains illegal characters " 185 "Your username contains illegal characters "
109 . "(only a-z, A-Z and 0-9 are allowed), " 186 . "(only a-z, A-Z and 0-9 are allowed), "
110 . "or is not between 3 and 18 characters in length.", 187 . "or is not between 3 and 18 characters in length.",
111 cf::NDI_RED 188 cf::NDI_RED
112 ); 189 );
113 } 190 }
191 Coro::Timer::sleep 0.4;
114 } 192 }
115 193
116 check_playing $ns, $user and next; 194 check_playing $ns, $user and next;
117 195
118 $ns->send_drawinfo ( 196 $ns->send_drawinfo (
129 $ns->send_drawinfo ( 207 $ns->send_drawinfo (
130 "Try to use at least three characters as your password please, " 208 "Try to use at least three characters as your password please, "
131 . "that cannot be too much to ask for :)", 209 . "that cannot be too much to ask for :)",
132 cf::NDI_RED 210 cf::NDI_RED
133 ); 211 );
212 Coro::Timer::sleep 0.4;
134 } 213 }
135 214
136 # lock this username for the remainder of this login session 215 # lock this username for the remainder of this login session
137 if ($cf::LOGIN_LOCK{$user}) { 216 if ($cf::LOGIN_LOCK{$user}) {
138 $ns->send_drawinfo ( 217 $ns->send_drawinfo (
155 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { 234 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
156 nuke_str $pass; 235 nuke_str $pass;
157 # password matches, wonderful 236 # password matches, wonderful
158 my $pl = cf::player::find $user or next; 237 my $pl = cf::player::find $user or next;
159 $pl->connect ($ns); 238 $pl->connect ($ns);
160 check_clean_save $pl; 239 enter_map $pl;
161 $pl->{clean_save} = 1;
162 last; 240 last;
163 } elsif (can_cleanup $pl, $mtime) { 241 } elsif (can_cleanup $pl, $mtime) {
164 Coro::Timer::sleep 1; 242 Coro::Timer::sleep 1;
165 243
166 $ns->send_drawinfo ( 244 $ns->send_drawinfo (
191 . "(check for Numlock and other semi-obvious error sources).", 269 . "(check for Numlock and other semi-obvious error sources).",
192 cf::NDI_RED 270 cf::NDI_RED
193 ); 271 );
194 next; 272 next;
195 } 273 }
274 } else {
275 # unable to load the playerfile:
276 # check wether the player dir exists, which means the file is corrupted or
277 # something very similar.
278 if (!aio_stat cf::player::playerdir $user) {
279 $ns->send_drawinfo (
280 "Unable to retrieve this player. It might be a locked or broken account. "
281 . "If this is your account, ask a dungeon master for assistance. "
282 . "Otherwise choose a different login name.",
283 cf::NDI_RED
284 );
285 next;
286 }
196 } 287 }
197 288
198 # the rest of this function is character creation 289 # the rest of this function is character creation
199 290
200 # just to make sure nothing is left over 291 # just to make sure nothing is left over
207 nuke_str $pass2; 298 nuke_str $pass2;
208 $ns->send_drawinfo ( 299 $ns->send_drawinfo (
209 "The passwords do not match, please try again.", 300 "The passwords do not match, please try again.",
210 cf::NDI_RED 301 cf::NDI_RED
211 ); 302 );
303 Coro::Timer::sleep 0.5;
212 next; 304 next;
213 } 305 }
214 306
215 nuke_str $pass2; 307 nuke_str $pass2;
216 308
217 my $pl = cf::player::new $user; 309 my $pl = cf::player::new $user;
218 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); 310 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
219 nuke_str $pass; 311 nuke_str $pass;
220 $pl->connect ($ns); 312 $pl->connect ($ns);
221
222 my $ob = $pl->ob; 313 my $ob = $pl->ob;
314
315 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
223 316
224 while () { 317 while () {
225 $ob->update_stats; 318 $ob->update_stats;
226 $pl->save_stats; 319 $pl->save_stats;
227 320
237 $ob->swap_stats ($res - 1, $swap - 1); 330 $ob->swap_stats ($res - 1, $swap - 1);
238 } 331 }
239 } else { 332 } else {
240 $ob->roll_stats; 333 $ob->roll_stats;
241 } 334 }
335
336 Coro::Timer::sleep 0.2;
242 } 337 }
243 338
244 $ob->set_animation (2); 339 $ob->set_animation (2);
245 $ob->add_statbonus; 340 $ob->add_statbonus;
246 341
247 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 342 while () {
248 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 343 $ns->send_msg (-1, "chargen-race-title", ucfirst $pl->title);
344 my $msg = $ob->msg;
345 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
346 $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg);
347
348 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
249 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); 349 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
250 350
251 $ns->state (cf::ST_CHANGE_CLASS); 351 last if $res =~ /[dD]/;
352
353 $pl->chargen_race_next;
354 Coro::Timer::sleep 0.2;
355 }
356
357 $pl->chargen_race_done;
358
359 while () {
360 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
361 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
362
363 if ($res =~ /^[fF]/) {
364 $pl->gender (1);
365 last;
366 } elsif ($res =~ /^[mM]/) {
367 $pl->gender (0);
368 last;
369 }
370 Coro::Timer::sleep 0.2;
371 }
372
373 $ob->reply (undef, "Welcome to Crossfire!");
374
252 delete $pl->{deny_save};#d# too early 375 delete $pl->{deny_save};
253 376
254 last; 377 last;
255 } 378 }
256 }); 379 });
257} 380});
258 381
259cf::register_command quit => sub { 382cf::register_command quit => sub {
260 my ($ob, $arg) = @_; 383 my ($ob, $arg) = @_;
261 384
262 $ob->reply (undef, 385 $ob->reply (undef,
278 } else { 401 } else {
279 $ob->reply (undef, 402 $ob->reply (undef,
280 "Ok, quitting, hope to see you again.", 403 "Ok, quitting, hope to see you again.",
281 cf::NDI_UNIQUE | cf::NDI_RED); 404 cf::NDI_UNIQUE | cf::NDI_RED);
282 $pl->ns->flush; 405 $pl->ns->flush;
283 $pl->quit_character; 406 cf::async { $pl->quit_character };
284 } 407 }
285 }); 408 });
286}; 409};
287 410
288cf::object->attach ( 411cf::object->attach (
294 417
295 my $pl = $ob->contr; 418 my $pl = $ob->contr;
296 419
297 # update respawn position 420 # update respawn position
298 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 421 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
422 cf::async { $pl->save };
299 423
300 $pl->killer ("left"); 424 $pl->killer ("left");
301 $ob->check_score; 425 $ob->check_score;
302 426
303 $ob->reply (undef, "In the future, you will wake up here when you die."); 427 $ob->reply (undef, "In the future, you will wake up here when you die.");
327 451
328 if ($cleanly) { 452 if ($cleanly) {
329 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 453 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
330 } else { 454 } else {
331 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 455 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
332 delete $pl->{clean_save}; 456 $pl->{unclean_save} = $cf::RUNTIME;
333 } 457 }
334 }, 458 },
335); 459);
336 460
337cf::client->attach (
338 on_addme => \&addme,
339);
340
341############################################################################# 461#############################################################################
342 462
343our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs 463our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
344our $SAVE_TIMEOUT = 200; # save players every n seconds 464our $SAVE_TIMEOUT = 20; # save players every n seconds
345our $SAVE_INTERVAL = 1.1; # save at max. one player every $SAVE_INTERVAL
346 465
347our $SCHEDULER = cf::async_ext { 466our $SCHEDULER = cf::async_ext {
467 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
348 while () { 468 while () {
349 Coro::Timer::sleep $SCHEDULE_INTERVAL; 469 $schedule_interval->next;
350 470
351 # this weird form of iteration over values is used because 471 # this weird form of iteration over values is used because
352 # the hash changes underneath us frequently, and for 472 # the hash changes underneath us frequently, and for
353 # keeps a direct reference to the value without (in 5.8 perls) 473 # keeps a direct reference to the value without (in 5.8 perls)
354 # keeping a reference, so this is prone to crashes or worse. 474 # keeping a reference, so this is prone to crashes or worse.
358 or next; 478 or next;
359 $pl->valid or next; 479 $pl->valid or next;
360 480
361 eval { 481 eval {
362 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { 482 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
483 cf::wait_for_tick_begin;
363 $pl->save; 484 $pl->save;
364 Coro::Timer::sleep $SAVE_INTERVAL;
365 }
366 485
367 unless ($pl->active) { 486 unless ($pl->active || $pl->ns) {
368 # check refcounts, this is tricky and needs to be adjusted to fit server internals 487 # check refcounts, this is tricky and needs to be adjusted to fit server internals
369 my $ob = $pl->ob; 488 my $ob = $pl->ob;
370 Scalar::Util::weaken $pl; 489
371 Scalar::Util::weaken $ob;
372 my $a_ = $pl->refcnt;
373 my $b_ = $ob->refcnt;
374 my $pl_ref = $pl->refcnt_cnt; 490 my $pl_ref = $pl->refcnt_cnt;
375 my $ob_ref = $ob->refcnt_cnt; 491 my $ob_ref = $ob->refcnt_cnt;
376 492
493 ## pl_ref == $pl + ob->contr + %cf::PLAYER
494 ## ob_ref == $ob + pl->observe + simply being an object
377 if ($pl_ref == 2 && $ob_ref == 1) { 495 if ($pl_ref == 3 && $ob_ref == 3) {
378 warn "player-scheduler destroy ", $ob->name;#d# 496 warn "player-scheduler destroy ", $ob->name;#d#
497
498 # remove from sight and get fresh "copies"
379 #delete $cf::PLAYER{$ob->name}; 499 $pl = delete $cf::PLAYER{$ob->name};
380 ## pl_ref == one from object + one from cf::PLAYER 500 $ob = $pl->ob;
381 ## ob_ref == one from simply being an object 501
382 #$ob->destroy;
383 #$pl->destroy; 502 $pl->destroy; # destroys $ob
384 } else { 503 } else {
504 my $a_ = $pl->refcnt;#d#
505 my $b_ = $ob->refcnt;#d#
506
385 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# 507 warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref ob $ob_ref (C pl $a_ ob $b_)\n";#d#
508 }
386 } 509 }
387 } 510 }
388 }; 511 };
389 warn $@ if $@; 512 warn $@ if $@;
390 Coro::cede; 513 Coro::cede;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines