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