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.87 by root, Wed May 28 05:12:01 2008 UTC vs.
Revision 1.95 by root, Wed Dec 17 16:43:38 2008 UTC

13} 13}
14 14
15sub query { 15sub query {
16 my ($ns, $flags, $text) = @_; 16 my ($ns, $flags, $text) = @_;
17 17
18 my $current = $Coro::current; 18 $ns->query ($flags, $text, Coro::rouse_cb);
19 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); 19 Coro::rouse_wait
20 Coro::schedule while ref $current;
21
22 $current
23} 20}
24 21
25sub can_cleanup { 22sub can_cleanup {
26 my ($pl, $mtime) = @_; 23 my ($pl, $mtime) = @_;
27 24
73} 70}
74 71
75sub enter_map { 72sub enter_map {
76 my ($pl) = @_; 73 my ($pl) = @_;
77 74
75 warn $pl->ob->name, ": enter map 1\n";#d#
78 my $ob = $pl->ob; 76 my $ob = $pl->ob;
79 77
80 my ($map, $x, $y) 78 my ($map, $x, $y)
81 = $ob->{_link_pos} 79 = $ob->{_link_pos}
82 ? @{delete $ob->{_link_pos}} 80 ? @{delete $ob->{_link_pos}}
83 : ($pl->maplevel, $ob->x, $ob->y); 81 : ($pl->maplevel, $ob->x, $ob->y);
84 82
83 warn $pl->ob->name, ": enter map 2\n";#d#
85 $ob->enter_link; 84 $ob->enter_link;
85 warn $pl->ob->name, ": enter map 3\n";#d#
86 86
87 my $m = cf::map::find $map; 87 my $m = cf::map::find $map;
88 my $time = delete $pl->{unclean_save}; 88 my $time = delete $pl->{unclean_save};
89 warn $pl->ob->name, ": enter map 4\n";#d#
89 90
90 if ($time && $m) { 91 if ($time && $m) {
91 if ($time < $m->{instantiate_time}) { 92 if ($time < $m->{instantiate_time}) {
92 # the map was reset in the meantime 93 # the map was reset in the meantime
93 my $age = $cf::RUNTIME - $time; 94 my $age = $cf::RUNTIME - $time;
126 cf::NDI_RED 127 cf::NDI_RED
127 ); 128 );
128 } 129 }
129 } 130 }
130 131
132 warn $pl->ob->name, ": enter map 5\n";#d#
131 $ob->goto ($map, $x, $y); 133 #$ob->goto ($map, $x, $y);
134 $ob->goto ($map, $x, $y, sub {
135 warn $pl->ob->name, ": enter map check\n";#d#
136 $_[0]
137 }, sub {
138 warn $pl->ob->name, ": enter map done\n";#d#
139 });
140 warn $pl->ob->name, ": enter map 6\n";#d#
141
132} 142}
133 143
134# delete a player directory, be non-blocking AND synchronous... 144# delete a player directory, be non-blocking AND synchronous...
135# (thats hard, so we crap out and fork). 145# (thats hard, so we crap out and fork).
136sub nuke_playerdir { 146sub nuke_playerdir {
209 cf::NDI_BLUE 219 cf::NDI_BLUE
210 ); 220 );
211 221
212 # read username 222 # read username
213 while () { 223 while () {
214 $user = query $ns, 0, "What is your name?\n:"; 224 $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:";
215 225
216 if ($cf::LOGIN_LOCK{$user}) { 226 if ($cf::LOGIN_LOCK{$user}) {
217 $ns->send_drawinfo ( 227 $ns->send_drawinfo (
218 "That username is currently used in another login session. " 228 "That username is currently used in another login session. "
219 . "Chose another, or wait till the other session has ended.", 229 . "Chose another, or wait till the other session has ended.",
220 cf::NDI_RED 230 cf::NDI_RED
221 ); 231 );
222 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { 232 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z/) {
223 last; 233 last;
224 } else { 234 } else {
225 $ns->send_drawinfo ( 235 $ns->send_drawinfo (
226 "Your username contains illegal characters " 236 "Your username contains illegal characters "
227 . "(only a-z, A-Z and 0-9 are allowed), " 237 . "(only a-z, A-Z and 0-9 are allowed), "
228 . "or is not between 3 and 18 characters in length.", 238 . "or is not between 3 and 20 characters in length.",
229 cf::NDI_RED 239 cf::NDI_RED
230 ); 240 );
231 } 241 }
232 Coro::Timer::sleep 0.4; 242 Coro::Timer::sleep 0.4;
233 } 243 }
279 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { 289 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
280 nuke_str $pass; 290 nuke_str $pass;
281 # password matches, wonderful 291 # password matches, wonderful
282 my $pl = cf::player::find $user or next; 292 my $pl = cf::player::find $user or next;
283 $pl->connect ($ns); 293 $pl->connect ($ns);
294 $pl->ob->flag (cf::FLAG_DEBUG, 1);#d# temp
284 enter_map $pl; 295 enter_map $pl;
285 last; 296 last;
286 } elsif (can_cleanup $pl, $mtime) { 297 } elsif (can_cleanup $pl, $mtime) {
287 Coro::Timer::sleep 1; 298 Coro::Timer::sleep 1;
288 299
419 Coro::Timer::sleep 0.2; 430 Coro::Timer::sleep 0.2;
420 } 431 }
421 432
422 $ob->reply (undef, "Welcome to Deliantra!"); 433 $ob->reply (undef, "Welcome to Deliantra!");
423 434
435 # XXX: Workaround for delayed client ext protocol handshake
436 $pl->esrv_new_player;
437
424 delete $pl->{deny_save}; 438 delete $pl->{deny_save};
425 439
426 last; 440 last;
427 } 441 }
428 }); 442 });
429}); 443});
430 444
431cf::register_command quit => sub { 445cf::register_command quit => sub {
432 my ($ob, $arg) = @_; 446 my ($ob, $arg) = @_;
433 447
434 $ob->reply (undef, 448 $ob->send_msg (undef,
435 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " 449 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
436 . "If you are sure you want to do this, then use the quit_character command instead of quit.", 450 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
437 cf::NDI_UNIQUE | cf::NDI_RED); 451 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
438}; 452};
439 453
440cf::register_command quit_character => sub { 454cf::register_command quit_character => sub {
441 my ($ob, $arg) = @_; 455 my ($ob, $arg) = @_;
442 456
443 my $pl = $ob->contr; 457 my $pl = $ob->contr;
444 458
445 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { 459 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
446 if ($_[0] !~ /^[yY]/) { 460 if ($_[0] !~ /^[yY]/) {
447 $ob->reply (undef, 461 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
448 "Ok, not not quitting then.",
449 cf::NDI_UNIQUE | cf::NDI_RED);
450 } else { 462 } else {
451 $ob->reply (undef, 463 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
452 "Ok, quitting, hope to see you again.",
453 cf::NDI_UNIQUE | cf::NDI_RED);
454 $pl->ns->flush; 464 $pl->ns->flush;
455 cf::async { $pl->quit_character }; 465 cf::async { $pl->quit_character };
456 } 466 }
457 }); 467 });
458}; 468};
471 cf::async { $pl->save }; 481 cf::async { $pl->save };
472 482
473 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; 483 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy;
474 $ob->check_score; 484 $ob->check_score;
475 485
476 $ob->reply (undef, "In the future, you will wake up here when you die."); 486 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY);
477 487
478 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 488 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
479 if ($_[0] !~ /^[yY]/) { 489 if ($_[0] !~ /^[yY]/) {
480 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 490 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
481 $pl->deactivate; 491 $pl->deactivate;
506 unless safe_spot $pl; 516 unless safe_spot $pl;
507 } 517 }
508 }, 518 },
509); 519);
510 520
511#############################################################################
512 521
513our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs
514our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds
515
516our $SCHEDULER = cf::async_ext {
517 $Coro::current->{desc} = "player scheduler";
518
519 while () {
520 Coro::EV::timer_once $SCHEDULE_INTERVAL;
521
522 # this weird form of iteration over values is used because
523 # the hash changes underneath us frequently, and for
524 # keeps a direct reference to the value without (in 5.8 perls)
525 # keeping a reference, so this is prone to crashes or worse.
526 my @players = keys %cf::PLAYER;
527 for (@players) {
528 my $pl = $cf::PLAYER{$_}
529 or next;
530 $pl->valid or next;
531
532 eval {
533 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
534 $pl->save;
535
536 unless ($pl->active || $pl->ns) {
537 # check refcounts, this is tricky and needs to be adjusted to fit server internals
538 my $ob = $pl->ob;
539
540 my $pl_ref = $pl->refcnt_cnt;
541 my $ob_ref = $ob->refcnt_cnt;
542
543 ## pl_ref == $pl + ob->contr + %cf::PLAYER
544 ## ob_ref == $ob + pl->observe + simply being an object
545 if ($pl_ref == 3 && $ob_ref == 3) {
546 warn "player-scheduler destroy ", $ob->name;#d#
547
548 # remove from sight and get fresh "copies"
549 $pl = delete $cf::PLAYER{$ob->name};
550 $ob = $pl->ob;
551
552 $pl->destroy; # destroys $ob
553 } else {
554 my $a_ = $pl->refcnt;#d#
555 my $b_ = $ob->refcnt;#d#
556
557 warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref/3 ob $ob_ref/3 (C pl $a_/1 ob $b_/2)\n";#d#
558 }
559 }
560 }
561 };
562 warn $@ if $@;
563 cf::cede_to_tick;
564 };
565 }
566};
567
568$SCHEDULER->prio (1);
569

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines