1 |
#! perl |
2 |
|
3 |
# login handling |
4 |
|
5 |
use Fcntl; |
6 |
use Coro::AIO; |
7 |
|
8 |
my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
9 |
|
10 |
# paranoia function to overwrite a string-in-place |
11 |
sub nuke_str { |
12 |
substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
13 |
} |
14 |
|
15 |
sub query { |
16 |
my ($ns, $flags, $text) = @_; |
17 |
|
18 |
my $current = $Coro::current; |
19 |
$ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); |
20 |
Coro::schedule while ref $current; |
21 |
|
22 |
$current |
23 |
} |
24 |
|
25 |
sub can_cleanup { |
26 |
my ($pl, $mtime) = @_; |
27 |
|
28 |
my $age = time - $mtime; |
29 |
my $level = $pl->ob->level; |
30 |
|
31 |
($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
32 |
|| ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
33 |
|| ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
34 |
|| $age > 700 * 86400 # 2 years for everybody else |
35 |
} |
36 |
|
37 |
sub check_playing { |
38 |
my ($ns, $user) = @_; |
39 |
|
40 |
return unless cf::player::find_active $user; |
41 |
|
42 |
$ns->send_drawinfo ( |
43 |
"That player is already logged in on this server. " |
44 |
. "If you want to create a new player, choose another name. " |
45 |
. "If you are already a registered player, make sure nobody " |
46 |
. "else is using your account at this time. If you lost your conenction " |
47 |
. "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 " |
49 |
. "you do not have another client running. If you use windows, reboot, " |
50 |
. "this will fix anything.", |
51 |
cf::NDI_RED |
52 |
); |
53 |
|
54 |
1 |
55 |
} |
56 |
|
57 |
sub check_clean_save { |
58 |
my ($pl) = @_; |
59 |
|
60 |
unless (delete $pl->{clean_save}) { |
61 |
#d#TODO |
62 |
} |
63 |
} |
64 |
|
65 |
# delete a player directory, be non-blocking AND synchronous... |
66 |
# (thats hard, so we crap out and fork). |
67 |
sub nuke_playerdir { |
68 |
my ($user) = @_; |
69 |
|
70 |
aio_stat "$PLAYERDIR/$user"; |
71 |
system "cd \Q$PLAYERDIR\E " |
72 |
. "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
73 |
. "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
74 |
} |
75 |
|
76 |
sub addme { |
77 |
my ($ns) = @_; |
78 |
|
79 |
$ns->destroy if $ns->pl; |
80 |
|
81 |
$ns->async (sub { |
82 |
my ($user, $pass); |
83 |
|
84 |
$ns->send_packet ("addme_success"); |
85 |
|
86 |
for (;;) { |
87 |
$ns->send_drawinfo ( |
88 |
"Please enter your username now. If you are a new user, " |
89 |
. "make one up that describes your character best. " |
90 |
. "Only letters and digits are allowed, though.", |
91 |
cf::NDI_BLUE |
92 |
); |
93 |
|
94 |
# read username |
95 |
while () { |
96 |
$user = query $ns, 0, "What is your name?\n:"; |
97 |
|
98 |
if ($cf::LOGIN_LOCK{$user}) { |
99 |
$ns->send_drawinfo ( |
100 |
"That username is currently used in another login session. " |
101 |
. "Chose another, or wait till the other session has ended.", |
102 |
cf::NDI_RED |
103 |
); |
104 |
} elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { |
105 |
last; |
106 |
} else { |
107 |
$ns->send_drawinfo ( |
108 |
"Your username contains illegal characters " |
109 |
. "(only a-z, A-Z and 0-9 are allowed), " |
110 |
. "or is not between 3 and 18 characters in length.", |
111 |
cf::NDI_RED |
112 |
); |
113 |
} |
114 |
} |
115 |
|
116 |
check_playing $ns, $user and next; |
117 |
|
118 |
$ns->send_drawinfo ( |
119 |
"Welcome $user, please enter your password now. " |
120 |
. "New users should now choose a password. " |
121 |
. "Anything your client lets you enter is fine.", |
122 |
cf::NDI_BLUE |
123 |
); |
124 |
|
125 |
# read password |
126 |
while () { |
127 |
$pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
128 |
last if $pass =~ /.../; |
129 |
$ns->send_drawinfo ( |
130 |
"Try to use at least three characters as your password please, " |
131 |
. "that cannot be too much to ask for :)", |
132 |
cf::NDI_RED |
133 |
); |
134 |
} |
135 |
|
136 |
# lock this username for the remainder of this login session |
137 |
if ($cf::LOGIN_LOCK{$user}) { |
138 |
$ns->send_drawinfo ( |
139 |
"That username is currently used in another login session. " |
140 |
. "Chose another, or wait till the other session has ended.", |
141 |
cf::NDI_RED |
142 |
); |
143 |
next; |
144 |
} |
145 |
local $cf::LOGIN_LOCK{$user} = 1; |
146 |
|
147 |
check_playing $ns, $user and next; |
148 |
|
149 |
# try to read the user file and check the password |
150 |
if (my $pl = cf::player::find $user) { |
151 |
aio_stat $pl->path and next; |
152 |
my $mtime = (stat _)[9]; |
153 |
my $hash = $pl->password; |
154 |
|
155 |
if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
156 |
nuke_str $pass; |
157 |
# password matches, wonderful |
158 |
my $pl = cf::player::find $user or next; |
159 |
$pl->connect ($ns); |
160 |
check_clean_save $pl; |
161 |
$pl->{clean_save} = 1; |
162 |
last; |
163 |
} elsif (can_cleanup $pl, $mtime) { |
164 |
Coro::Timer::sleep 1; |
165 |
|
166 |
$ns->send_drawinfo ( |
167 |
"Player exists, but password does not match. If this is your account, " |
168 |
. "please try again. If not, you can now decide to take over this account " |
169 |
. "because it has not been in-use for some time.", |
170 |
cf::NDI_RED |
171 |
); |
172 |
|
173 |
#TODO: nuke_str |
174 |
(query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
175 |
or next; |
176 |
|
177 |
# check if the file hasn't changed |
178 |
aio_stat cf::player::path $user and next; |
179 |
$mtime == (stat _)[9] or next; |
180 |
|
181 |
$pl->quit_character; |
182 |
|
183 |
# fall through to creation |
184 |
} else { |
185 |
nuke_str $pass; |
186 |
|
187 |
Coro::Timer::sleep 1; |
188 |
|
189 |
$ns->send_drawinfo ( |
190 |
"Wrong username or password. Please try again " |
191 |
. "(check for Numlock and other semi-obvious error sources).", |
192 |
cf::NDI_RED |
193 |
); |
194 |
next; |
195 |
} |
196 |
} |
197 |
|
198 |
# the rest of this function is character creation |
199 |
|
200 |
# just to make sure nothing is left over |
201 |
nuke_playerdir $user; |
202 |
|
203 |
my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
204 |
|
205 |
if ($pass2 ne $pass) { |
206 |
nuke_str $pass; |
207 |
nuke_str $pass2; |
208 |
$ns->send_drawinfo ( |
209 |
"The passwords do not match, please try again.", |
210 |
cf::NDI_RED |
211 |
); |
212 |
next; |
213 |
} |
214 |
|
215 |
nuke_str $pass2; |
216 |
|
217 |
my $pl = cf::player::new $user; |
218 |
$pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
219 |
nuke_str $pass; |
220 |
$pl->connect ($ns); |
221 |
|
222 |
my $ob = $pl->ob; |
223 |
|
224 |
while () { |
225 |
$ob->update_stats; |
226 |
$pl->save_stats; |
227 |
|
228 |
my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
229 |
"[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
230 |
|
231 |
if ($res =~ /^[Nn]/) { |
232 |
last; |
233 |
} elsif ($res > 0 && $res <= 7) { |
234 |
my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
235 |
|
236 |
if ($swap > 0 && $swap <= 7) { |
237 |
$ob->swap_stats ($res - 1, $swap - 1); |
238 |
} |
239 |
} else { |
240 |
$ob->roll_stats; |
241 |
} |
242 |
} |
243 |
|
244 |
$ob->set_animation (2); |
245 |
$ob->add_statbonus; |
246 |
|
247 |
$ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
248 |
$ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
249 |
"Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
250 |
|
251 |
$ns->state (cf::ST_CHANGE_CLASS); |
252 |
delete $pl->{deny_save};#d# too early |
253 |
|
254 |
last; |
255 |
} |
256 |
}); |
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 |
}; |
287 |
|
288 |
cf::object->attach ( |
289 |
type => cf::SAVEBED, |
290 |
on_apply => sub { |
291 |
my ($bed, $ob) = @_; |
292 |
|
293 |
return cf::override 0 unless $ob->type == cf::PLAYER; |
294 |
|
295 |
my $pl = $ob->contr; |
296 |
|
297 |
# update respawn position |
298 |
$pl->savebed ($bed->map->path, $bed->x, $bed->y); |
299 |
cf::async { $pl->save }; |
300 |
|
301 |
$pl->killer ("left"); |
302 |
$ob->check_score; |
303 |
|
304 |
$ob->reply (undef, "In the future, you will wake up here when you die."); |
305 |
|
306 |
$pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
307 |
if ($_[0] !~ /^[yY]/) { |
308 |
$pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
309 |
$pl->deactivate; |
310 |
$pl->ns->destroy; |
311 |
} else { |
312 |
cf::async { $pl->save }; |
313 |
} |
314 |
}); |
315 |
}, |
316 |
); |
317 |
|
318 |
cf::player->attach ( |
319 |
on_login => sub { |
320 |
my ($pl) = @_; |
321 |
my $name = $pl->ob->name; |
322 |
|
323 |
$_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
324 |
}, |
325 |
on_logout => sub { |
326 |
my ($pl, $cleanly) = @_; |
327 |
my $name = $pl->ob->name; |
328 |
|
329 |
if ($cleanly) { |
330 |
$_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
331 |
} else { |
332 |
$_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
333 |
delete $pl->{clean_save}; |
334 |
} |
335 |
}, |
336 |
); |
337 |
|
338 |
cf::client->attach ( |
339 |
on_addme => \&addme, |
340 |
); |
341 |
|
342 |
############################################################################# |
343 |
|
344 |
our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
345 |
our $SAVE_TIMEOUT = 20; # save players every n seconds |
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 |
$cf::WAIT_FOR_TICK_ONE->wait; |
364 |
$pl->save; |
365 |
|
366 |
unless ($pl->active) { |
367 |
# check refcounts, this is tricky and needs to be adjusted to fit server internals |
368 |
my $ob = $pl->ob; |
369 |
Scalar::Util::weaken $pl; |
370 |
Scalar::Util::weaken $ob; |
371 |
my $a_ = $pl->refcnt;#d# |
372 |
my $b_ = $ob->refcnt;#d# |
373 |
my $pl_ref = $pl->refcnt_cnt; |
374 |
my $ob_ref = $ob->refcnt_cnt; |
375 |
|
376 |
## pl_ref == one from object + one from cf::PLAYER |
377 |
## ob_ref == one from simply being an object |
378 |
if ($pl_ref == 2 && $ob_ref == 1) { |
379 |
warn "player-scheduler destroy ", $ob->name;#d# |
380 |
|
381 |
# remove from sight and get fresh "copies" |
382 |
$pl = delete $cf::PLAYER{$ob->name}; |
383 |
$ob = $pl->ob; |
384 |
|
385 |
$ob->destroy; |
386 |
$pl->destroy; |
387 |
} else { |
388 |
warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
389 |
} |
390 |
} |
391 |
} |
392 |
}; |
393 |
warn $@ if $@; |
394 |
Coro::cede; |
395 |
}; |
396 |
} |
397 |
}; |
398 |
|
399 |
$SCHEDULER->prio (1); |
400 |
|