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