… | |
… | |
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 |
35 | } |
35 | } |
36 | |
36 | |
37 | sub check_playing { |
37 | sub check_playing { |
38 | my ($ns, $user) = @_; |
38 | my ($ns, $user) = @_; |
39 | |
39 | |
40 | return unless cf::player::find $user; |
40 | return unless cf::player::find_active $user; |
41 | |
41 | |
42 | $ns->send_drawinfo ( |
42 | $ns->send_drawinfo ( |
43 | "That player is already logged in on this server. " |
43 | "That player is already logged in on this server. " |
44 | . "If you want to create a new player, choose another name. " |
44 | . "If you want to create a new player, choose another name. " |
45 | . "If you are already a registered player, make sure nobody " |
45 | . "If you are already a registered player, make sure nobody " |
… | |
… | |
52 | ); |
52 | ); |
53 | |
53 | |
54 | 1 |
54 | 1 |
55 | } |
55 | } |
56 | |
56 | |
|
|
57 | sub check_clean_save { |
|
|
58 | my ($pl) = @_; |
|
|
59 | |
|
|
60 | unless (delete $pl->{clean_save}) { |
|
|
61 | #d#TODO |
|
|
62 | } |
|
|
63 | } |
|
|
64 | |
57 | # delete a player directory, be non-blocking AND synchronous... |
65 | # delete a player directory, be non-blocking AND synchronous... |
58 | # (thats hard, so we crap out and fork). |
66 | # (thats hard, so we crap out and fork). |
59 | sub nuke_playerdir { |
67 | sub nuke_playerdir { |
60 | my ($user) = @_; |
68 | my ($user) = @_; |
61 | |
69 | |
… | |
… | |
68 | sub addme { |
76 | sub addme { |
69 | my ($ns) = @_; |
77 | my ($ns) = @_; |
70 | |
78 | |
71 | $ns->destroy if $ns->pl; |
79 | $ns->destroy if $ns->pl; |
72 | |
80 | |
73 | $ns->coro (sub { |
81 | $ns->async (sub { |
74 | my ($user, $pass); |
82 | my ($user, $pass); |
75 | |
83 | |
76 | $ns->send_packet ("addme_success"); |
84 | $ns->send_packet ("addme_success"); |
77 | |
85 | |
78 | for (;;) { |
86 | for (;;) { |
… | |
… | |
123 | . "that cannot be too much to ask for :)", |
131 | . "that cannot be too much to ask for :)", |
124 | cf::NDI_RED |
132 | cf::NDI_RED |
125 | ); |
133 | ); |
126 | } |
134 | } |
127 | |
135 | |
128 | my $dir = "$PLAYERDIR/$user"; |
|
|
129 | my $plfile = "$dir/$user.pl"; |
|
|
130 | |
|
|
131 | # lock this username for the remainder of this login session |
136 | # lock this username for the remainder of this login session |
132 | if ($cf::LOGIN_LOCK{$user}) { |
137 | if ($cf::LOGIN_LOCK{$user}) { |
133 | $ns->send_drawinfo ( |
138 | $ns->send_drawinfo ( |
134 | "That username is currently used in another login session. " |
139 | "That username is currently used in another login session. " |
135 | . "Chose another, or wait till the other session has ended.", |
140 | . "Chose another, or wait till the other session has ended.", |
… | |
… | |
140 | local $cf::LOGIN_LOCK{$user} = 1; |
145 | local $cf::LOGIN_LOCK{$user} = 1; |
141 | |
146 | |
142 | check_playing $ns, $user and next; |
147 | check_playing $ns, $user and next; |
143 | |
148 | |
144 | # try to read the user file and check the password |
149 | # try to read the user file and check the password |
145 | if (my $fh = aio_open $plfile, O_RDONLY, 0) { |
150 | if (my $pl = cf::player::find $user) { |
|
|
151 | aio_stat $pl->path and next; |
146 | my $mtime = (stat $fh)[9]; |
152 | my $mtime = (stat _)[9]; |
147 | |
|
|
148 | 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
|
|
149 | $buf =~ /^password (\S+)$/m or next; |
|
|
150 | my $hash = $1; |
153 | my $hash = $pl->password; |
151 | |
154 | |
152 | if ($hash eq crypt $pass, $hash) { |
155 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
153 | nuke_str $pass; |
156 | nuke_str $pass; |
154 | # password matches, wonderful |
157 | # password matches, wonderful |
155 | my $pl = cf::player::load $plfile or next; |
158 | my $pl = cf::player::find $user or next; |
156 | $pl->enable_save (1); |
|
|
157 | $pl->connect ($ns); |
159 | $pl->connect ($ns); |
|
|
160 | check_clean_save $pl; |
|
|
161 | $pl->{clean_save} = 1; |
158 | last; |
162 | last; |
159 | } elsif (can_cleanup $buf, $mtime) { |
163 | } elsif (can_cleanup $pl, $mtime) { |
160 | Coro::Timer::sleep 1; |
164 | Coro::Timer::sleep 1; |
161 | |
165 | |
162 | $ns->send_drawinfo ( |
166 | $ns->send_drawinfo ( |
163 | "Player exists, but password does not match. If this is your account, " |
167 | "Player exists, but password does not match. If this is your account, " |
164 | . "please try again. If not, you can now decide to take over this account " |
168 | . "please try again. If not, you can now decide to take over this account " |
… | |
… | |
169 | #TODO: nuke_str |
173 | #TODO: nuke_str |
170 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
174 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
171 | or next; |
175 | or next; |
172 | |
176 | |
173 | # check if the file hasn't changed |
177 | # check if the file hasn't changed |
174 | aio_stat $plfile and next; |
178 | aio_stat cf::player::path $user and next; |
175 | $mtime == (stat _)[9] or next; |
179 | $mtime == (stat _)[9] or next; |
176 | |
180 | |
177 | nuke_playerdir $user; |
181 | $pl->quit_character; |
178 | |
182 | |
179 | # fall through to creation |
183 | # fall through to creation |
180 | } else { |
184 | } else { |
181 | nuke_str $pass; |
185 | nuke_str $pass; |
182 | |
186 | |
… | |
… | |
208 | next; |
212 | next; |
209 | } |
213 | } |
210 | |
214 | |
211 | nuke_str $pass2; |
215 | nuke_str $pass2; |
212 | |
216 | |
213 | my $pl = cf::player::create; |
217 | my $pl = cf::player::new $user; |
214 | $pl->ob->name ($user); |
|
|
215 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
218 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
216 | nuke_str $pass; |
219 | nuke_str $pass; |
217 | $pl->connect ($ns); |
220 | $pl->connect ($ns); |
218 | |
221 | |
219 | my $ob = $pl->ob; |
222 | my $ob = $pl->ob; |
… | |
… | |
244 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
247 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
245 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
248 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
246 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
249 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
247 | |
250 | |
248 | $ns->state (cf::ST_CHANGE_CLASS); |
251 | $ns->state (cf::ST_CHANGE_CLASS); |
249 | $pl->enable_save (1);#d# too early |
252 | delete $pl->{deny_save};#d# too early |
250 | |
253 | |
251 | last; |
254 | last; |
252 | } |
255 | } |
253 | }); |
256 | }); |
254 | } |
257 | } |
|
|
258 | |
|
|
259 | cf::register_command quit => sub { |
|
|
260 | my ($ob, $arg) = @_; |
|
|
261 | |
|
|
262 | $ob->reply (undef, |
|
|
263 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
|
|
264 | . "If you are sure you want to do this, then use the quit_character command instead of quit.", |
|
|
265 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
266 | }; |
|
|
267 | |
|
|
268 | cf::register_command quit_character => sub { |
|
|
269 | my ($ob, $arg) = @_; |
|
|
270 | |
|
|
271 | my $pl = $ob->contr; |
|
|
272 | |
|
|
273 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
|
|
274 | if ($_[0] !~ /^[yY]/) { |
|
|
275 | $ob->reply (undef, |
|
|
276 | "Ok, not not quitting then.", |
|
|
277 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
278 | } else { |
|
|
279 | $ob->reply (undef, |
|
|
280 | "Ok, quitting, hope to see you again.", |
|
|
281 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
282 | $pl->ns->flush; |
|
|
283 | $pl->quit_character; |
|
|
284 | } |
|
|
285 | }); |
|
|
286 | }; |
255 | |
287 | |
256 | cf::object->attach ( |
288 | cf::object->attach ( |
257 | type => cf::SAVEBED, |
289 | type => cf::SAVEBED, |
258 | on_apply => sub { |
290 | on_apply => sub { |
259 | my ($bed, $ob) = @_; |
291 | my ($bed, $ob) = @_; |
260 | |
292 | |
261 | return cf::override 0 unless $ob->type == cf::PLAYER; |
293 | return cf::override 0 unless $ob->type == cf::PLAYER; |
262 | |
294 | |
|
|
295 | my $pl = $ob->contr; |
|
|
296 | |
263 | # update respawn position |
297 | # update respawn position |
264 | $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); |
298 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
265 | |
299 | |
266 | $ob->contr->killer ("left"); |
300 | $pl->killer ("left"); |
267 | $ob->check_score; |
301 | $ob->check_score; |
268 | |
302 | |
269 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
303 | $ob->reply (undef, "In the future, you will wake up here when you die."); |
270 | $ob->contr->save (1); |
|
|
271 | |
304 | |
272 | $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
305 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
273 | if ($_[0] !~ /^[yY]/) { |
306 | if ($_[0] !~ /^[yY]/) { |
274 | $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
307 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
|
|
308 | $pl->deactivate; |
275 | $ob->contr->ns->destroy; |
309 | $pl->ns->destroy; |
276 | } else { |
310 | } else { |
277 | $ob->contr->enable_save (1); |
311 | cf::async { $pl->save }; |
278 | } |
312 | } |
279 | }); |
313 | }); |
280 | }, |
314 | }, |
281 | ); |
315 | ); |
282 | |
316 | |
… | |
… | |
293 | |
327 | |
294 | if ($cleanly) { |
328 | if ($cleanly) { |
295 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
329 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
296 | } else { |
330 | } else { |
297 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
331 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
|
|
332 | delete $pl->{clean_save}; |
298 | } |
333 | } |
299 | }, |
334 | }, |
300 | ); |
335 | ); |
301 | |
336 | |
302 | cf::client->attach ( |
337 | cf::client->attach ( |
303 | on_addme => \&addme, |
338 | on_addme => \&addme, |
304 | ); |
339 | ); |
305 | |
340 | |
|
|
341 | ############################################################################# |
|
|
342 | |
|
|
343 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
|
|
344 | our $SAVE_TIMEOUT = 200; # save players every n seconds |
|
|
345 | our $SAVE_INTERVAL = 1.1; # save at max. one player every $SAVE_INTERVAL |
|
|
346 | |
|
|
347 | our $SCHEDULER = cf::async_ext { |
|
|
348 | while () { |
|
|
349 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
|
|
350 | |
|
|
351 | # this weird form of iteration over values is used because |
|
|
352 | # the hash changes underneath us frequently, and for |
|
|
353 | # keeps a direct reference to the value without (in 5.8 perls) |
|
|
354 | # keeping a reference, so this is prone to crashes or worse. |
|
|
355 | my @players = keys %cf::PLAYER; |
|
|
356 | for (@players) { |
|
|
357 | my $pl = $cf::PLAYER{$_} |
|
|
358 | or next; |
|
|
359 | $pl->valid or next; |
|
|
360 | |
|
|
361 | eval { |
|
|
362 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
|
|
363 | $pl->save; |
|
|
364 | Coro::Timer::sleep $SAVE_INTERVAL; |
|
|
365 | } |
|
|
366 | |
|
|
367 | unless ($pl->active) { |
|
|
368 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
|
|
369 | my $ob = $pl->ob; |
|
|
370 | Scalar::Util::weaken $pl; |
|
|
371 | Scalar::Util::weaken $ob; |
|
|
372 | my $a_ = $pl->refcnt; |
|
|
373 | my $b_ = $ob->refcnt; |
|
|
374 | my $pl_ref = $pl->refcnt_cnt; |
|
|
375 | my $ob_ref = $ob->refcnt_cnt; |
|
|
376 | |
|
|
377 | if ($pl_ref == 2 && $ob_ref == 1) { |
|
|
378 | warn "player-scheduler destroy ", $ob->name;#d# |
|
|
379 | #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; |
|
|
383 | #$pl->destroy; |
|
|
384 | } else { |
|
|
385 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
|
|
386 | } |
|
|
387 | } |
|
|
388 | }; |
|
|
389 | warn $@ if $@; |
|
|
390 | Coro::cede; |
|
|
391 | }; |
|
|
392 | } |
|
|
393 | }; |
|
|
394 | |
|
|
395 | $SCHEDULER->prio (1); |
|
|
396 | |