… | |
… | |
13 | } |
13 | } |
14 | |
14 | |
15 | sub query { |
15 | sub 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 | |
25 | sub can_cleanup { |
22 | sub can_cleanup { |
26 | my ($pl, $mtime) = @_; |
23 | my ($pl, $mtime) = @_; |
27 | |
24 | |
… | |
… | |
73 | } |
70 | } |
74 | |
71 | |
75 | sub enter_map { |
72 | sub 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; |
… | |
… | |
103 | cf::NDI_RED |
104 | cf::NDI_RED |
104 | ); |
105 | ); |
105 | # kill them. |
106 | # kill them. |
106 | # reminds me of the famous badness 10000 syndrome... |
107 | # reminds me of the famous badness 10000 syndrome... |
107 | $ob->stats->hp (-10000); #] if they survive this they deserved to live |
108 | $ob->stats->hp (-10000); #] if they survive this they deserved to live |
108 | my $killer = cf::arch::get "killer_login"; |
109 | my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; |
109 | $pl->killer ($killer); |
|
|
110 | $killer->destroy; |
|
|
111 | } else { |
110 | } else { |
112 | ($map, $x, $y) = $pl->savebed; |
111 | ($map, $x, $y) = $pl->savebed; |
113 | |
112 | |
114 | $ob->message ( |
113 | $ob->message ( |
115 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
114 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
… | |
… | |
128 | cf::NDI_RED |
127 | cf::NDI_RED |
129 | ); |
128 | ); |
130 | } |
129 | } |
131 | } |
130 | } |
132 | |
131 | |
|
|
132 | warn $pl->ob->name, ": enter map 5\n";#d# |
133 | $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 | |
134 | } |
142 | } |
135 | |
143 | |
136 | # delete a player directory, be non-blocking AND synchronous... |
144 | # delete a player directory, be non-blocking AND synchronous... |
137 | # (thats hard, so we crap out and fork). |
145 | # (thats hard, so we crap out and fork). |
138 | sub nuke_playerdir { |
146 | sub nuke_playerdir { |
… | |
… | |
211 | cf::NDI_BLUE |
219 | cf::NDI_BLUE |
212 | ); |
220 | ); |
213 | |
221 | |
214 | # read username |
222 | # read username |
215 | while () { |
223 | while () { |
216 | $user = query $ns, 0, "What is your name?\n:"; |
224 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
217 | |
225 | |
218 | if ($cf::LOGIN_LOCK{$user}) { |
226 | if ($cf::LOGIN_LOCK{$user}) { |
219 | $ns->send_drawinfo ( |
227 | $ns->send_drawinfo ( |
220 | "That username is currently used in another login session. " |
228 | "That username is currently used in another login session. " |
221 | . "Chose another, or wait till the other session has ended.", |
229 | . "Chose another, or wait till the other session has ended.", |
222 | cf::NDI_RED |
230 | cf::NDI_RED |
223 | ); |
231 | ); |
224 | } 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/) { |
225 | last; |
233 | last; |
226 | } else { |
234 | } else { |
227 | $ns->send_drawinfo ( |
235 | $ns->send_drawinfo ( |
228 | "Your username contains illegal characters " |
236 | "Your username contains illegal characters " |
229 | . "(only a-z, A-Z and 0-9 are allowed), " |
237 | . "(only a-z, A-Z and 0-9 are allowed), " |
230 | . "or is not between 3 and 18 characters in length.", |
238 | . "or is not between 3 and 20 characters in length.", |
231 | cf::NDI_RED |
239 | cf::NDI_RED |
232 | ); |
240 | ); |
233 | } |
241 | } |
234 | Coro::Timer::sleep 0.4; |
242 | Coro::Timer::sleep 0.4; |
235 | } |
243 | } |
… | |
… | |
281 | 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) { |
282 | nuke_str $pass; |
290 | nuke_str $pass; |
283 | # password matches, wonderful |
291 | # password matches, wonderful |
284 | my $pl = cf::player::find $user or next; |
292 | my $pl = cf::player::find $user or next; |
285 | $pl->connect ($ns); |
293 | $pl->connect ($ns); |
|
|
294 | $pl->ob->flag (cf::FLAG_DEBUG, 1);#d# temp |
286 | enter_map $pl; |
295 | enter_map $pl; |
287 | last; |
296 | last; |
288 | } elsif (can_cleanup $pl, $mtime) { |
297 | } elsif (can_cleanup $pl, $mtime) { |
289 | Coro::Timer::sleep 1; |
298 | Coro::Timer::sleep 1; |
290 | |
299 | |
… | |
… | |
379 | } |
388 | } |
380 | } else { |
389 | } else { |
381 | $ob->roll_stats; |
390 | $ob->roll_stats; |
382 | } |
391 | } |
383 | |
392 | |
384 | Coro::Timer::sleep 0.2; |
393 | Coro::Timer::sleep 0.05; |
385 | } |
394 | } |
386 | |
395 | |
387 | $ob->set_animation (2); |
396 | $ob->set_animation (2); |
388 | $ob->add_statbonus; |
397 | $ob->add_statbonus; |
389 | |
398 | |
… | |
… | |
400 | |
409 | |
401 | $pl->chargen_race_next; |
410 | $pl->chargen_race_next; |
402 | Coro::Timer::sleep 0.2; |
411 | Coro::Timer::sleep 0.2; |
403 | } |
412 | } |
404 | |
413 | |
|
|
414 | # create the playerdir, if necessary, as chargen_race_done did it before |
|
|
415 | # presumably because of unique maps |
|
|
416 | aio_mkdir playerdir $pl, 0770; |
405 | $pl->chargen_race_done; |
417 | $pl->chargen_race_done; |
406 | |
418 | |
407 | while () { |
419 | while () { |
408 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
420 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
409 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
421 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
… | |
… | |
418 | Coro::Timer::sleep 0.2; |
430 | Coro::Timer::sleep 0.2; |
419 | } |
431 | } |
420 | |
432 | |
421 | $ob->reply (undef, "Welcome to Deliantra!"); |
433 | $ob->reply (undef, "Welcome to Deliantra!"); |
422 | |
434 | |
|
|
435 | # XXX: Workaround for delayed client ext protocol handshake |
|
|
436 | $pl->esrv_new_player; |
|
|
437 | |
423 | delete $pl->{deny_save}; |
438 | delete $pl->{deny_save}; |
424 | |
439 | |
425 | last; |
440 | last; |
426 | } |
441 | } |
427 | }); |
442 | }); |
428 | }); |
443 | }); |
429 | |
444 | |
430 | cf::register_command quit => sub { |
445 | cf::register_command quit => sub { |
431 | my ($ob, $arg) = @_; |
446 | my ($ob, $arg) = @_; |
432 | |
447 | |
433 | $ob->reply (undef, |
448 | $ob->send_msg (undef, |
434 | "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. " |
435 | . "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.", |
436 | cf::NDI_UNIQUE | cf::NDI_RED); |
451 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
437 | }; |
452 | }; |
438 | |
453 | |
439 | cf::register_command quit_character => sub { |
454 | cf::register_command quit_character => sub { |
440 | my ($ob, $arg) = @_; |
455 | my ($ob, $arg) = @_; |
441 | |
456 | |
442 | my $pl = $ob->contr; |
457 | my $pl = $ob->contr; |
443 | |
458 | |
444 | $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 { |
445 | if ($_[0] !~ /^[yY]/) { |
460 | if ($_[0] !~ /^[yY]/) { |
446 | $ob->reply (undef, |
461 | $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
447 | "Ok, not not quitting then.", |
|
|
448 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
449 | } else { |
462 | } else { |
450 | $ob->reply (undef, |
463 | $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
451 | "Ok, quitting, hope to see you again.", |
|
|
452 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
453 | $pl->ns->flush; |
464 | $pl->ns->flush; |
454 | cf::async { $pl->quit_character }; |
465 | cf::async { $pl->quit_character }; |
455 | } |
466 | } |
456 | }); |
467 | }); |
457 | }; |
468 | }; |
… | |
… | |
467 | |
478 | |
468 | # update respawn position |
479 | # update respawn position |
469 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
480 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
470 | cf::async { $pl->save }; |
481 | cf::async { $pl->save }; |
471 | |
482 | |
472 | my $killer = cf::arch::get "killer_logout"; |
483 | my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; |
473 | $pl->killer ($killer); |
|
|
474 | $killer->destroy; |
|
|
475 | $ob->check_score; |
484 | $ob->check_score; |
476 | |
485 | |
477 | $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); |
478 | |
487 | |
479 | $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 { |
480 | if ($_[0] !~ /^[yY]/) { |
489 | if ($_[0] !~ /^[yY]/) { |
481 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
490 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
482 | $pl->deactivate; |
491 | $pl->deactivate; |
… | |
… | |
507 | unless safe_spot $pl; |
516 | unless safe_spot $pl; |
508 | } |
517 | } |
509 | }, |
518 | }, |
510 | ); |
519 | ); |
511 | |
520 | |
512 | ############################################################################# |
|
|
513 | |
521 | |
514 | our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs |
|
|
515 | our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds |
|
|
516 | |
|
|
517 | our $SCHEDULER = cf::async_ext { |
|
|
518 | $Coro::current->{desc} = "player scheduler"; |
|
|
519 | |
|
|
520 | while () { |
|
|
521 | Coro::EV::timer_once $SCHEDULE_INTERVAL; |
|
|
522 | |
|
|
523 | # this weird form of iteration over values is used because |
|
|
524 | # the hash changes underneath us frequently, and for |
|
|
525 | # keeps a direct reference to the value without (in 5.8 perls) |
|
|
526 | # keeping a reference, so this is prone to crashes or worse. |
|
|
527 | my @players = keys %cf::PLAYER; |
|
|
528 | for (@players) { |
|
|
529 | my $pl = $cf::PLAYER{$_} |
|
|
530 | or next; |
|
|
531 | $pl->valid or next; |
|
|
532 | |
|
|
533 | eval { |
|
|
534 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
535 | $pl->save; |
|
|
536 | |
|
|
537 | unless ($pl->active || $pl->ns) { |
|
|
538 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
539 | my $ob = $pl->ob; |
|
|
540 | |
|
|
541 | my $pl_ref = $pl->refcnt_cnt; |
|
|
542 | my $ob_ref = $ob->refcnt_cnt; |
|
|
543 | |
|
|
544 | ## pl_ref == $pl + ob->contr + %cf::PLAYER |
|
|
545 | ## ob_ref == $ob + pl->observe + simply being an object |
|
|
546 | if ($pl_ref == 3 && $ob_ref == 3) { |
|
|
547 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
548 | |
|
|
549 | # remove from sight and get fresh "copies" |
|
|
550 | $pl = delete $cf::PLAYER{$ob->name}; |
|
|
551 | $ob = $pl->ob; |
|
|
552 | |
|
|
553 | $pl->destroy; # destroys $ob |
|
|
554 | } else { |
|
|
555 | my $a_ = $pl->refcnt;#d# |
|
|
556 | my $b_ = $ob->refcnt;#d# |
|
|
557 | |
|
|
558 | warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref/3 ob $ob_ref/3 (C pl $a_/1 ob $b_/2)\n";#d# |
|
|
559 | } |
|
|
560 | } |
|
|
561 | } |
|
|
562 | }; |
|
|
563 | warn $@ if $@; |
|
|
564 | cf::cede_to_tick; |
|
|
565 | }; |
|
|
566 | } |
|
|
567 | }; |
|
|
568 | |
|
|
569 | $SCHEDULER->prio (1); |
|
|
570 | |
|
|