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; |
… | |
… | |
21 | |
21 | |
22 | $current |
22 | $current |
23 | } |
23 | } |
24 | |
24 | |
25 | sub can_cleanup { |
25 | sub can_cleanup { |
26 | my ($playerfile, $mtime) = @_; |
26 | my ($pl, $mtime) = @_; |
27 | |
27 | |
28 | my $age = time - $mtime; |
28 | my $age = time - $mtime; |
29 | my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; |
29 | my $level = $pl->ob->level; |
30 | |
30 | |
31 | ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
31 | ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
32 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
32 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
33 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
33 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
34 | || $age > 700 * 86400 # 2 years for everybody else |
34 | || $age > 700 * 86400 # 2 years for everybody else |
… | |
… | |
55 | } |
55 | } |
56 | |
56 | |
57 | sub check_clean_save { |
57 | sub check_clean_save { |
58 | my ($pl) = @_; |
58 | my ($pl) = @_; |
59 | |
59 | |
60 | unless (delete $pl->{clean_save}) { |
60 | if (my $time = delete $pl->{unclean_save}) { |
|
|
61 | $pl->ns->send_drawinfo ( |
|
|
62 | "You didn't use a savebed to leave this realm. This is very dangerous, " |
|
|
63 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
|
|
64 | . "or monsters suddenly snapping your body. Better use a savebed next time.", |
|
|
65 | cf::NDI_RED |
|
|
66 | ); |
61 | #d#TODO |
67 | #d#TODO |
62 | } |
68 | } |
63 | } |
69 | } |
64 | |
70 | |
65 | # delete a player directory, be non-blocking AND synchronous... |
71 | # delete a player directory, be non-blocking AND synchronous... |
… | |
… | |
145 | local $cf::LOGIN_LOCK{$user} = 1; |
151 | local $cf::LOGIN_LOCK{$user} = 1; |
146 | |
152 | |
147 | check_playing $ns, $user and next; |
153 | check_playing $ns, $user and next; |
148 | |
154 | |
149 | # try to read the user file and check the password |
155 | # try to read the user file and check the password |
150 | if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { |
156 | if (my $pl = cf::player::find $user) { |
|
|
157 | aio_stat $pl->path and next; |
151 | my $mtime = (stat $fh)[9]; |
158 | my $mtime = (stat _)[9]; |
152 | |
|
|
153 | 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
|
|
154 | $buf =~ /^password (\S+)$/m or next; |
|
|
155 | my $hash = $1; |
159 | my $hash = $pl->password; |
156 | |
160 | |
157 | if ($hash eq crypt $pass, $hash) { |
161 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
158 | nuke_str $pass; |
162 | nuke_str $pass; |
159 | # password matches, wonderful |
163 | # password matches, wonderful |
160 | my $pl = cf::player::find $user or next; |
164 | my $pl = cf::player::find $user or next; |
161 | $pl->connect ($ns); |
165 | $pl->connect ($ns); |
162 | check_clean_save $pl; |
166 | check_clean_save $pl; |
163 | last; |
167 | last; |
164 | } elsif (can_cleanup $buf, $mtime) { |
168 | } elsif (can_cleanup $pl, $mtime) { |
165 | Coro::Timer::sleep 1; |
169 | Coro::Timer::sleep 1; |
166 | |
170 | |
167 | $ns->send_drawinfo ( |
171 | $ns->send_drawinfo ( |
168 | "Player exists, but password does not match. If this is your account, " |
172 | "Player exists, but password does not match. If this is your account, " |
169 | . "please try again. If not, you can now decide to take over this account " |
173 | . "please try again. If not, you can now decide to take over this account " |
… | |
… | |
177 | |
181 | |
178 | # check if the file hasn't changed |
182 | # check if the file hasn't changed |
179 | aio_stat cf::player::path $user and next; |
183 | aio_stat cf::player::path $user and next; |
180 | $mtime == (stat _)[9] or next; |
184 | $mtime == (stat _)[9] or next; |
181 | |
185 | |
182 | nuke_playerdir $user; |
186 | $pl->quit_character; |
183 | |
187 | |
184 | # fall through to creation |
188 | # fall through to creation |
185 | } else { |
189 | } else { |
186 | nuke_str $pass; |
190 | nuke_str $pass; |
187 | |
191 | |
… | |
… | |
255 | last; |
259 | last; |
256 | } |
260 | } |
257 | }); |
261 | }); |
258 | } |
262 | } |
259 | |
263 | |
260 | # "Quitting will delete your character PERMANENTLY. If you are sure you want to do this, then use the quit_character command instead of quit." |
264 | cf::register_command quit => sub { |
261 | # "Quitting will delete your character.\nAre you sure you want to quit (y/n):" |
265 | my ($ob, $arg) = @_; |
262 | # quit, quit_character, save |
266 | |
|
|
267 | $ob->reply (undef, |
|
|
268 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
|
|
269 | . "If you are sure you want to do this, then use the quit_character command instead of quit.", |
|
|
270 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
271 | }; |
|
|
272 | |
|
|
273 | cf::register_command quit_character => sub { |
|
|
274 | my ($ob, $arg) = @_; |
|
|
275 | |
|
|
276 | my $pl = $ob->contr; |
|
|
277 | |
|
|
278 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
|
|
279 | if ($_[0] !~ /^[yY]/) { |
|
|
280 | $ob->reply (undef, |
|
|
281 | "Ok, not not quitting then.", |
|
|
282 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
283 | } else { |
|
|
284 | $ob->reply (undef, |
|
|
285 | "Ok, quitting, hope to see you again.", |
|
|
286 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
287 | $pl->ns->flush; |
|
|
288 | $pl->quit_character; |
|
|
289 | } |
|
|
290 | }); |
|
|
291 | }; |
263 | |
292 | |
264 | cf::object->attach ( |
293 | cf::object->attach ( |
265 | type => cf::SAVEBED, |
294 | type => cf::SAVEBED, |
266 | on_apply => sub { |
295 | on_apply => sub { |
267 | my ($bed, $ob) = @_; |
296 | my ($bed, $ob) = @_; |
268 | |
297 | |
269 | return cf::override 0 unless $ob->type == cf::PLAYER; |
298 | return cf::override 0 unless $ob->type == cf::PLAYER; |
270 | |
299 | |
271 | my $pl = $ob->pl; |
300 | my $pl = $ob->contr; |
272 | |
301 | |
273 | # update respawn position |
302 | # update respawn position |
274 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
303 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
|
|
304 | cf::async { $pl->save }; |
275 | |
305 | |
276 | $pl->killer ("left"); |
306 | $pl->killer ("left"); |
277 | $ob->check_score; |
307 | $ob->check_score; |
278 | |
308 | |
279 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
309 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
… | |
… | |
282 | if ($_[0] !~ /^[yY]/) { |
312 | if ($_[0] !~ /^[yY]/) { |
283 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
313 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
284 | $pl->deactivate; |
314 | $pl->deactivate; |
285 | $pl->ns->destroy; |
315 | $pl->ns->destroy; |
286 | } else { |
316 | } else { |
287 | cf::async { |
317 | cf::async { $pl->save }; |
288 | $pl->{clean_save} = 1; |
|
|
289 | $pl->save; |
|
|
290 | }; |
|
|
291 | } |
318 | } |
292 | }); |
319 | }); |
293 | }, |
320 | }, |
294 | ); |
321 | ); |
295 | |
322 | |
… | |
… | |
306 | |
333 | |
307 | if ($cleanly) { |
334 | if ($cleanly) { |
308 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
335 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
309 | } else { |
336 | } else { |
310 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
337 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
338 | $pl->{unclean_save} = $cf::RUNTIME; |
311 | } |
339 | } |
312 | }, |
340 | }, |
313 | ); |
341 | ); |
314 | |
342 | |
315 | cf::client->attach ( |
343 | cf::client->attach ( |
316 | on_addme => \&addme, |
344 | on_addme => \&addme, |
317 | ); |
345 | ); |
318 | |
346 | |
319 | ############################################################################# |
347 | ############################################################################# |
320 | |
348 | |
321 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
349 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
322 | our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory |
|
|
323 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
350 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
324 | our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
|
|
325 | |
351 | |
326 | our $SCHEDULER = cf::async_ext { |
352 | our $SCHEDULER = cf::async_ext { |
|
|
353 | my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL); |
327 | while () { |
354 | while () { |
328 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
355 | $schedule_interval->next; |
329 | |
356 | |
330 | # this weird form of iteration over values is used because |
357 | # this weird form of iteration over values is used because |
331 | # the hash changes underneath us frequently, and for |
358 | # the hash changes underneath us frequently, and for |
332 | # keeps a direct reference to the value without (in 5.8 perls) |
359 | # keeps a direct reference to the value without (in 5.8 perls) |
333 | # keeping a reference, so this is prone to crashes or worse. |
360 | # keeping a reference, so this is prone to crashes or worse. |
… | |
… | |
337 | or next; |
364 | or next; |
338 | $pl->valid or next; |
365 | $pl->valid or next; |
339 | |
366 | |
340 | eval { |
367 | eval { |
341 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
368 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
369 | $cf::WAIT_FOR_TICK_ONE->wait; |
342 | $pl->save; |
370 | $pl->save; |
343 | Coro::Timer::sleep $SAVE_INTERVAL; |
371 | |
|
|
372 | unless ($pl->active) { |
|
|
373 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
374 | my $ob = $pl->ob; |
|
|
375 | Scalar::Util::weaken $pl; |
|
|
376 | Scalar::Util::weaken $ob; |
|
|
377 | my $a_ = $pl->refcnt;#d# |
|
|
378 | my $b_ = $ob->refcnt;#d# |
|
|
379 | my $pl_ref = $pl->refcnt_cnt; |
|
|
380 | my $ob_ref = $ob->refcnt_cnt; |
|
|
381 | |
|
|
382 | ## pl_ref == one from object + one from cf::PLAYER |
|
|
383 | ## ob_ref == one from simply being an object |
|
|
384 | if ($pl_ref == 2 && $ob_ref == 1) { |
|
|
385 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
386 | |
|
|
387 | # remove from sight and get fresh "copies" |
|
|
388 | $pl = delete $cf::PLAYER{$ob->name}; |
|
|
389 | $ob = $pl->ob; |
|
|
390 | |
|
|
391 | $ob->destroy; |
|
|
392 | $pl->destroy; |
|
|
393 | } else { |
|
|
394 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
|
|
395 | } |
|
|
396 | } |
344 | } |
397 | } |
345 | }; |
398 | }; |
346 | warn $@ if $@; |
399 | warn $@ if $@; |
347 | Coro::cede; |
400 | Coro::cede; |
348 | }; |
401 | }; |