… | |
… | |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | use List::Util qw(min max); |
7 | use List::Util qw(min max); |
8 | |
8 | |
9 | my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
|
|
10 | |
|
|
11 | # paranoia function to overwrite a string-in-place |
9 | # paranoia function to overwrite a string-in-place |
12 | sub nuke_str { |
10 | sub nuke_str { |
13 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
11 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
14 | } |
12 | } |
15 | |
13 | |
16 | sub query { |
14 | sub query { |
17 | my ($ns, $flags, $text) = @_; |
15 | my ($ns, $flags, $text) = @_; |
18 | |
16 | |
19 | my $current = $Coro::current; |
17 | my $current = $Coro::current; |
20 | $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); |
18 | $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); |
21 | Coro::schedule while ref $current; |
19 | Coro::schedule while ref $current; |
22 | |
20 | |
23 | $current |
21 | $current |
24 | } |
22 | } |
25 | |
23 | |
… | |
… | |
53 | ); |
51 | ); |
54 | |
52 | |
55 | 1 |
53 | 1 |
56 | } |
54 | } |
57 | |
55 | |
58 | sub check_clean_save { |
56 | sub enter_map { |
59 | my ($pl) = @_; |
57 | my ($pl) = @_; |
60 | |
58 | |
|
|
59 | my ($map, $x, $y) |
|
|
60 | = $pl->ob->{_link_pos} |
|
|
61 | ? @{delete $pl->ob->{_link_pos}} |
|
|
62 | : ($pl->maplevel, $pl->ob->x, $pl->ob->y); |
|
|
63 | |
|
|
64 | $pl->ob->enter_link; |
|
|
65 | |
61 | if (my $time = delete $pl->{unclean_save}) { |
66 | if (my $time = delete $pl->{unclean_save}) { |
|
|
67 | if (my $m = cf::map::find $map) { |
|
|
68 | if ($time < $m->{instantiate_time}) { |
|
|
69 | # the map was reset in the meantime |
|
|
70 | my $age = $cf::RUNTIME - $time; |
|
|
71 | warn $pl->ob->name, " map reset after logout, logout age $age\n";#d# |
|
|
72 | |
|
|
73 | # for now, just go back to savebed |
|
|
74 | ($map, $x, $y) = $pl->savebed; |
|
|
75 | |
62 | $pl->ns->send_drawinfo ( |
76 | $pl->ns->send_drawinfo ( |
|
|
77 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
|
|
78 | . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " |
|
|
79 | . "Better use a savebed next time, much worse things could have happened...", |
|
|
80 | cf::NDI_RED |
|
|
81 | ); |
|
|
82 | } else { |
|
|
83 | $pl->ns->send_drawinfo ( |
63 | "You didn't use a savebed to leave this realm. This is very dangerous, " |
84 | "You didn't use a bed to reality to leave this realm. This is very dangerous, " |
64 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
85 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
65 | . "or monsters suddenly snapping your body. Better use a savebed next time.", |
86 | . "or monsters suddenly snapping your body. Better use a savebed next time.", |
66 | cf::NDI_RED |
87 | cf::NDI_RED |
|
|
88 | ); |
|
|
89 | } |
67 | ); |
90 | } |
68 | #d#TODO |
|
|
69 | } |
91 | } |
|
|
92 | |
|
|
93 | $pl->ob->goto ($map, $x, $y); |
70 | } |
94 | } |
71 | |
95 | |
72 | # delete a player directory, be non-blocking AND synchronous... |
96 | # delete a player directory, be non-blocking AND synchronous... |
73 | # (thats hard, so we crap out and fork). |
97 | # (thats hard, so we crap out and fork). |
74 | sub nuke_playerdir { |
98 | sub nuke_playerdir { |
… | |
… | |
76 | |
100 | |
77 | aio_stat "$PLAYERDIR/$user"; |
101 | aio_stat "$PLAYERDIR/$user"; |
78 | system "cd \Q$PLAYERDIR\E " |
102 | system "cd \Q$PLAYERDIR\E " |
79 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
103 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
80 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
104 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
|
|
105 | } |
|
|
106 | |
|
|
107 | sub send_capabilities { |
|
|
108 | my ($ns) = @_; |
|
|
109 | |
|
|
110 | return unless $ns->extcmd; |
|
|
111 | |
|
|
112 | $ns->ext_event (capabilities => |
|
|
113 | # id, name, flags (1 == 2d), edge length |
|
|
114 | tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32]], |
|
|
115 | ); |
|
|
116 | } |
|
|
117 | |
|
|
118 | sub setup { |
|
|
119 | my ($ns, $args) = @_; |
|
|
120 | |
|
|
121 | # run through the cmds of setup |
|
|
122 | # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ... |
|
|
123 | # |
|
|
124 | # we send the status of the cmd back, or a FALSE is the cmd is the server unknown |
|
|
125 | # The client then must sort this out |
|
|
126 | |
|
|
127 | my %setup = split / +/, $args; |
|
|
128 | while (my ($k, $v) = each %setup) { |
|
|
129 | if ($k eq "sound") { |
|
|
130 | $ns->sound ($v); |
|
|
131 | |
|
|
132 | } elsif ($k eq "exp64") { |
|
|
133 | $setup{$k} = 1; |
|
|
134 | |
|
|
135 | } elsif ($k eq "spellmon") { |
|
|
136 | $ns->monitor_spells ($v); |
|
|
137 | |
|
|
138 | } elsif ($k eq "darkness") { |
|
|
139 | $ns->darkness ($v); |
|
|
140 | |
|
|
141 | } elsif ($k eq "map1cmd") { |
|
|
142 | $ns->mapmode (cf::Map1Cmd) if $v > 0; |
|
|
143 | |
|
|
144 | } elsif ($k eq "map1acmd") { |
|
|
145 | $ns->mapmode (cf::Map1aCmd) if $v > 0; |
|
|
146 | |
|
|
147 | } elsif ($k eq "map2cmd") { |
|
|
148 | # gcfclient bug, map1acmd is sent too late |
|
|
149 | $ns->mapmode (cf::Map1aCmd); |
|
|
150 | $setup{$k} = "FALSE"; |
|
|
151 | |
|
|
152 | } elsif ($k eq "newmapcmd") { |
|
|
153 | $ns->newmapcmd ($v); |
|
|
154 | |
|
|
155 | } elsif ($k eq "mapinfocmd") { |
|
|
156 | $ns->mapinfocmd ($v); |
|
|
157 | |
|
|
158 | } elsif ($k eq "extcmd") { |
|
|
159 | $ns->extcmd ($v > 0); |
|
|
160 | send_capabilities $ns; |
|
|
161 | |
|
|
162 | } elsif ($k eq "extmap") { |
|
|
163 | $ns->extmap ($v); |
|
|
164 | |
|
|
165 | } elsif ($k eq "facecache") { |
|
|
166 | if (!$v) { |
|
|
167 | $v = 1; |
|
|
168 | $setup{$k} = $v; |
|
|
169 | $ns->send_drawinfo ("(trying to forcefully enable facecaching)", cf::NDI_RED); |
|
|
170 | } |
|
|
171 | |
|
|
172 | $ns->facecache ($v); |
|
|
173 | |
|
|
174 | } elsif ($k eq "faceset") { |
|
|
175 | $ns->faceset (0); |
|
|
176 | $setup{$k} = 0; |
|
|
177 | # $ns->image2 (1) |
|
|
178 | |
|
|
179 | } elsif ($k eq "tileset") { |
|
|
180 | $setup{$k} = $ns->faceset ($v & 1); |
|
|
181 | |
|
|
182 | } elsif ($k eq "itemcmd") { |
|
|
183 | # Version of the item protocol command to use. Currently, |
|
|
184 | # only supported versions are 1 and 2. Using a numeric |
|
|
185 | # value will make it very easy to extend this in the future. |
|
|
186 | $ns->itemcmd ($v) if $v >= 1 && $v <= 2; |
|
|
187 | |
|
|
188 | $setup{$k} = $ns->itemcmd; |
|
|
189 | |
|
|
190 | } elsif ($k eq "mapsize") { |
|
|
191 | my ($x, $y) = split /x/, $v; |
|
|
192 | |
|
|
193 | $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, ($x - 1) | 1); |
|
|
194 | $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, ($y - 1) | 1); |
|
|
195 | |
|
|
196 | $setup{$k} = "${x}x${y}"; |
|
|
197 | |
|
|
198 | } elsif ($k eq "extendedMapInfos") { |
|
|
199 | $ns->ext_mapinfos ($v); |
|
|
200 | |
|
|
201 | } elsif ($k eq "extendedTextInfos") { |
|
|
202 | $ns->has_readable_type ($v); |
|
|
203 | |
|
|
204 | } elsif ($k eq "smoothing") { # cfplus-style smoothing |
|
|
205 | $ns->smoothing ($v); |
|
|
206 | |
|
|
207 | } elsif ($k eq "fxix") { |
|
|
208 | $ns->fxix ($v); |
|
|
209 | |
|
|
210 | } elsif ($k eq "msg") { |
|
|
211 | $ns->can_msg ($v); |
|
|
212 | |
|
|
213 | } elsif ($k eq "excmd") { |
|
|
214 | # we support it |
|
|
215 | |
|
|
216 | } else { |
|
|
217 | # other commands: |
|
|
218 | # sexp: no idea, probably for oudated servers |
|
|
219 | # tick: more stupidity, server should send a tick per tick |
|
|
220 | |
|
|
221 | $setup{$k} = "FALSE"; |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
|
|
225 | $ns->send_packet (join " ", setup => %setup); |
|
|
226 | |
|
|
227 | cf::datalog setup => |
|
|
228 | request => $args, |
|
|
229 | reply => \%setup, |
|
|
230 | client => $ns->version, |
|
|
231 | ; |
81 | } |
232 | } |
82 | |
233 | |
83 | sub addme { |
234 | sub addme { |
84 | my ($ns) = @_; |
235 | my ($ns) = @_; |
85 | |
236 | |
… | |
… | |
111 | $ns->flush; |
262 | $ns->flush; |
112 | return $ns->destroy; |
263 | return $ns->destroy; |
113 | } |
264 | } |
114 | |
265 | |
115 | # $ns->facecache = true; |
266 | # $ns->facecache = true; |
116 | } |
267 | } |
|
|
268 | |
|
|
269 | if ($ns->mapmode < cf::Map1aCmd) { |
|
|
270 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
|
|
271 | |
|
|
272 | |
|
|
273 | *** |
|
|
274 | *** WARNING: |
|
|
275 | *** Your client is too old. Please upgrade to a newer version. |
|
|
276 | EOF |
|
|
277 | |
|
|
278 | $ns->flush; |
|
|
279 | return $ns->destroy; |
|
|
280 | } |
117 | |
281 | |
118 | $ns->pl and return $ns->destroy; |
282 | $ns->pl and return $ns->destroy; |
119 | |
283 | |
120 | $ns->async (sub { |
284 | $ns->async (sub { |
121 | my ($user, $pass); |
285 | my ($user, $pass); |
… | |
… | |
194 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
358 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
195 | nuke_str $pass; |
359 | nuke_str $pass; |
196 | # password matches, wonderful |
360 | # password matches, wonderful |
197 | my $pl = cf::player::find $user or next; |
361 | my $pl = cf::player::find $user or next; |
198 | $pl->connect ($ns); |
362 | $pl->connect ($ns); |
199 | check_clean_save $pl; |
363 | enter_map $pl; |
200 | last; |
364 | last; |
201 | } elsif (can_cleanup $pl, $mtime) { |
365 | } elsif (can_cleanup $pl, $mtime) { |
202 | Coro::Timer::sleep 1; |
366 | Coro::Timer::sleep 1; |
203 | |
367 | |
204 | $ns->send_drawinfo ( |
368 | $ns->send_drawinfo ( |
… | |
… | |
225 | Coro::Timer::sleep 1; |
389 | Coro::Timer::sleep 1; |
226 | |
390 | |
227 | $ns->send_drawinfo ( |
391 | $ns->send_drawinfo ( |
228 | "Wrong username or password. Please try again " |
392 | "Wrong username or password. Please try again " |
229 | . "(check for Numlock and other semi-obvious error sources).", |
393 | . "(check for Numlock and other semi-obvious error sources).", |
|
|
394 | cf::NDI_RED |
|
|
395 | ); |
|
|
396 | next; |
|
|
397 | } |
|
|
398 | } else { |
|
|
399 | # unable to load the playerfile: |
|
|
400 | # check wether the player dir exists, which means the file is corrupted or |
|
|
401 | # something very similar. |
|
|
402 | if (!aio_stat cf::player::playerdir $user) { |
|
|
403 | $ns->send_drawinfo ( |
|
|
404 | "Unable to retrieve this player. It might be a locked or broken account. " |
|
|
405 | . "If this is your account, ask a dungeon master for assistance. " |
|
|
406 | . "Otherwise choose a different login name.", |
230 | cf::NDI_RED |
407 | cf::NDI_RED |
231 | ); |
408 | ); |
232 | next; |
409 | next; |
233 | } |
410 | } |
234 | } |
411 | } |
… | |
… | |
254 | |
431 | |
255 | my $pl = cf::player::new $user; |
432 | my $pl = cf::player::new $user; |
256 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
433 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
257 | nuke_str $pass; |
434 | nuke_str $pass; |
258 | $pl->connect ($ns); |
435 | $pl->connect ($ns); |
259 | |
|
|
260 | my $ob = $pl->ob; |
436 | my $ob = $pl->ob; |
|
|
437 | |
|
|
438 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
261 | |
439 | |
262 | while () { |
440 | while () { |
263 | $ob->update_stats; |
441 | $ob->update_stats; |
264 | $pl->save_stats; |
442 | $pl->save_stats; |
265 | |
443 | |
… | |
… | |
280 | } |
458 | } |
281 | |
459 | |
282 | $ob->set_animation (2); |
460 | $ob->set_animation (2); |
283 | $ob->add_statbonus; |
461 | $ob->add_statbonus; |
284 | |
462 | |
285 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
463 | while () { |
286 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
464 | $ns->send_msg (-1, "chargen-race-title", ucfirst $pl->title); |
|
|
465 | my $msg = $ob->msg; |
|
|
466 | $msg =~ s/(?<=\S)\n(?=\S)/ /g; |
|
|
467 | $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg); |
|
|
468 | |
|
|
469 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
287 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
470 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
288 | |
471 | |
289 | $ns->state (cf::ST_CHANGE_CLASS); |
472 | last if $res =~ /[dD]/; |
|
|
473 | |
|
|
474 | $pl->chargen_race_next; |
|
|
475 | } |
|
|
476 | |
|
|
477 | $pl->chargen_race_done; |
|
|
478 | |
290 | delete $pl->{deny_save};#d# too early |
479 | delete $pl->{deny_save}; |
291 | |
480 | |
292 | last; |
481 | last; |
293 | } |
482 | } |
294 | }); |
483 | }); |
295 | } |
484 | } |
… | |
… | |
371 | $pl->{unclean_save} = $cf::RUNTIME; |
560 | $pl->{unclean_save} = $cf::RUNTIME; |
372 | } |
561 | } |
373 | }, |
562 | }, |
374 | ); |
563 | ); |
375 | |
564 | |
376 | sub setup { |
|
|
377 | my ($ns, $args) = @_; |
|
|
378 | |
|
|
379 | # run through the cmds of setup |
|
|
380 | # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ... |
|
|
381 | # |
|
|
382 | # we send the status of the cmd back, or a FALSE is the cmd is the server unknown |
|
|
383 | # The client then must sort this out |
|
|
384 | |
|
|
385 | my %setup = split / +/, $args; |
|
|
386 | while (my ($k, $v) = each %setup) { |
|
|
387 | |
|
|
388 | if ($k eq "sound") { |
|
|
389 | $ns->sound ($v); |
|
|
390 | |
|
|
391 | } elsif ($k eq "exp64") { |
|
|
392 | $setup{$k} = 1; |
|
|
393 | |
|
|
394 | } elsif ($k eq "spellmon") { |
|
|
395 | $ns->monitor_spells ($v); |
|
|
396 | |
|
|
397 | } elsif ($k eq "darkness") { |
|
|
398 | $ns->darkness ($v); |
|
|
399 | |
|
|
400 | } elsif ($k eq "map1cmd") { |
|
|
401 | $ns->mapmode (cf::Map1Cmd) if $v > 0; |
|
|
402 | |
|
|
403 | } elsif ($k eq "map1acmd") { |
|
|
404 | $ns->mapmode (cf::Map1aCmd) if $v > 0; |
|
|
405 | |
|
|
406 | } elsif ($k eq "newmapcmd") { |
|
|
407 | $ns->newmapcmd ($v); |
|
|
408 | |
|
|
409 | } elsif ($k eq "mapinfocmd") { |
|
|
410 | $ns->mapinfocmd ($v); |
|
|
411 | |
|
|
412 | } elsif ($k eq "extcmd") { |
|
|
413 | $ns->extcmd ($v > 0); |
|
|
414 | |
|
|
415 | } elsif ($k eq "extmap") { |
|
|
416 | $ns->extmap ($v); |
|
|
417 | |
|
|
418 | } elsif ($k eq "facecache") { |
|
|
419 | $ns->facecache ($v); |
|
|
420 | |
|
|
421 | } elsif ($k eq "faceset") { |
|
|
422 | $ns->faceset (0); |
|
|
423 | $setup{$k} = 0; |
|
|
424 | # $ns->image2 (1) |
|
|
425 | |
|
|
426 | } elsif ($k eq "itemcmd") { |
|
|
427 | # Version of the item protocol command to use. Currently, |
|
|
428 | # only supported versions are 1 and 2. Using a numeric |
|
|
429 | # value will make it very easy to extend this in the future. |
|
|
430 | $ns->itemcmd ($v) if $v >= 1 && $v <= 2; |
|
|
431 | |
|
|
432 | $setup{$k} = $ns->itemcmd; |
|
|
433 | |
|
|
434 | } elsif ($k eq "mapsize") { |
|
|
435 | my ($x, $y) = split /x/, $v; |
|
|
436 | |
|
|
437 | $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, $x); |
|
|
438 | $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, $y); |
|
|
439 | |
|
|
440 | $setup{$k} = "${x}x${y}"; |
|
|
441 | |
|
|
442 | } elsif ($k eq "extendedMapInfos") { |
|
|
443 | $ns->ext_mapinfos ($v); |
|
|
444 | |
|
|
445 | } elsif ($k eq "extendedTextInfos") { |
|
|
446 | $ns->has_readable_type ($v); |
|
|
447 | |
|
|
448 | } else { |
|
|
449 | # other commands: |
|
|
450 | # sexp: no idea, probably for oudated servers |
|
|
451 | # tick: more stupidity, server should sned a tick per tick |
|
|
452 | |
|
|
453 | $setup{$k} = "FALSE"; |
|
|
454 | } |
|
|
455 | } |
|
|
456 | |
|
|
457 | $ns->send_packet (join " ", setup => %setup); |
|
|
458 | |
|
|
459 | cf::datalog setup => |
|
|
460 | request => $args, |
|
|
461 | reply => \%setup, |
|
|
462 | client => $ns->version, |
|
|
463 | ; |
|
|
464 | } |
|
|
465 | |
|
|
466 | cf::client->attach ( |
565 | cf::client->attach ( |
467 | on_addme => \&addme, |
566 | on_addme => \&addme, |
468 | on_setup => \&setup, |
567 | on_setup => \&setup, |
469 | ); |
568 | ); |
470 | |
569 | |
… | |
… | |
488 | or next; |
587 | or next; |
489 | $pl->valid or next; |
588 | $pl->valid or next; |
490 | |
589 | |
491 | eval { |
590 | eval { |
492 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
591 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
493 | $cf::WAIT_FOR_TICK_ONE->wait; |
592 | cf::wait_for_tick_begin; |
494 | $pl->save; |
593 | $pl->save; |
495 | |
594 | |
496 | unless ($pl->active) { |
595 | unless ($pl->active) { |
497 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
596 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
498 | my $ob = $pl->ob; |
597 | my $ob = $pl->ob; |
… | |
… | |
513 | $ob = $pl->ob; |
612 | $ob = $pl->ob; |
514 | |
613 | |
515 | $ob->destroy; |
614 | $ob->destroy; |
516 | $pl->destroy; |
615 | $pl->destroy; |
517 | } else { |
616 | } else { |
518 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
617 | warn "player-scheduler refcnt ", $ob->name, " pp$pl_ref,pc$a_ op$ob_ref,oc$b_\n";#d# |
519 | } |
618 | } |
520 | } |
619 | } |
521 | } |
620 | } |
522 | }; |
621 | }; |
523 | warn $@ if $@; |
622 | warn $@ if $@; |