1 |
root |
1.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 |
root |
1.9 |
# paranoia function to overwrite a string-in-place |
11 |
|
|
sub nuke_str { |
12 |
|
|
substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
13 |
|
|
} |
14 |
root |
1.1 |
|
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 ($playerfile, $mtime) = @_; |
27 |
|
|
|
28 |
|
|
my $age = time - $mtime; |
29 |
|
|
my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; |
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 |
root |
1.11 |
return unless cf::player::find_active $user; |
41 |
root |
1.1 |
|
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 |
root |
1.11 |
sub check_clean_save { |
58 |
|
|
my ($pl) = @_; |
59 |
|
|
|
60 |
|
|
unless (delete $pl->{clean_save}) { |
61 |
|
|
#d#TODO |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
root |
1.1 |
# 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 |
root |
1.8 |
sub addme { |
77 |
root |
1.1 |
my ($ns) = @_; |
78 |
|
|
|
79 |
|
|
$ns->destroy if $ns->pl; |
80 |
|
|
|
81 |
root |
1.10 |
$ns->async (sub { |
82 |
root |
1.1 |
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 |
root |
1.3 |
|
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 |
root |
1.1 |
} |
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 |
root |
1.3 |
# 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 |
root |
1.1 |
# try to read the user file and check the password |
150 |
root |
1.11 |
if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { |
151 |
root |
1.1 |
my $mtime = (stat $fh)[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; |
156 |
|
|
|
157 |
|
|
if ($hash eq crypt $pass, $hash) { |
158 |
root |
1.9 |
nuke_str $pass; |
159 |
root |
1.1 |
# password matches, wonderful |
160 |
root |
1.11 |
my $pl = cf::player::find $user or next; |
161 |
root |
1.1 |
$pl->connect ($ns); |
162 |
root |
1.11 |
check_clean_save $pl; |
163 |
root |
1.13 |
$pl->{clean_save} = 1; |
164 |
root |
1.1 |
last; |
165 |
root |
1.3 |
} elsif (can_cleanup $buf, $mtime) { |
166 |
root |
1.1 |
Coro::Timer::sleep 1; |
167 |
|
|
|
168 |
|
|
$ns->send_drawinfo ( |
169 |
root |
1.3 |
"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 " |
171 |
root |
1.1 |
. "because it has not been in-use for some time.", |
172 |
|
|
cf::NDI_RED |
173 |
|
|
); |
174 |
|
|
|
175 |
root |
1.9 |
#TODO: nuke_str |
176 |
root |
1.1 |
(query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
177 |
|
|
or next; |
178 |
|
|
|
179 |
|
|
# check if the file hasn't changed |
180 |
root |
1.11 |
aio_stat cf::player::path $user and next; |
181 |
root |
1.1 |
$mtime == (stat _)[9] or next; |
182 |
|
|
|
183 |
|
|
nuke_playerdir $user; |
184 |
|
|
|
185 |
|
|
# fall through to creation |
186 |
|
|
} else { |
187 |
root |
1.9 |
nuke_str $pass; |
188 |
|
|
|
189 |
root |
1.1 |
Coro::Timer::sleep 1; |
190 |
|
|
|
191 |
|
|
$ns->send_drawinfo ( |
192 |
|
|
"Wrong username or password. Please try again " |
193 |
|
|
. "(check for Numlock and other semi-obvious error sources).", |
194 |
|
|
cf::NDI_RED |
195 |
|
|
); |
196 |
|
|
next; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
# the rest of this function is character creation |
201 |
|
|
|
202 |
root |
1.3 |
# just to make sure nothing is left over |
203 |
root |
1.1 |
nuke_playerdir $user; |
204 |
|
|
|
205 |
root |
1.3 |
my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
206 |
|
|
|
207 |
|
|
if ($pass2 ne $pass) { |
208 |
root |
1.9 |
nuke_str $pass; |
209 |
|
|
nuke_str $pass2; |
210 |
root |
1.3 |
$ns->send_drawinfo ( |
211 |
|
|
"The passwords do not match, please try again.", |
212 |
|
|
cf::NDI_RED |
213 |
|
|
); |
214 |
|
|
next; |
215 |
|
|
} |
216 |
|
|
|
217 |
root |
1.9 |
nuke_str $pass2; |
218 |
|
|
|
219 |
root |
1.11 |
my $pl = cf::player::new $user; |
220 |
root |
1.1 |
$pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
221 |
root |
1.9 |
nuke_str $pass; |
222 |
root |
1.1 |
$pl->connect ($ns); |
223 |
root |
1.3 |
|
224 |
root |
1.1 |
my $ob = $pl->ob; |
225 |
|
|
|
226 |
|
|
while () { |
227 |
|
|
$ob->update_stats; |
228 |
|
|
$pl->save_stats; |
229 |
|
|
|
230 |
|
|
my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
231 |
|
|
"[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
232 |
|
|
|
233 |
|
|
if ($res =~ /^[Nn]/) { |
234 |
|
|
last; |
235 |
|
|
} elsif ($res > 0 && $res <= 7) { |
236 |
|
|
my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
237 |
|
|
|
238 |
|
|
if ($swap > 0 && $swap <= 7) { |
239 |
|
|
$ob->swap_stats ($res - 1, $swap - 1); |
240 |
|
|
} |
241 |
|
|
} else { |
242 |
|
|
$ob->roll_stats; |
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
$ob->set_animation (2); |
247 |
|
|
$ob->add_statbonus; |
248 |
|
|
|
249 |
|
|
$ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
250 |
|
|
$ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
251 |
|
|
"Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
252 |
|
|
|
253 |
|
|
$ns->state (cf::ST_CHANGE_CLASS); |
254 |
root |
1.11 |
delete $pl->{deny_save};#d# too early |
255 |
root |
1.1 |
|
256 |
|
|
last; |
257 |
|
|
} |
258 |
|
|
}); |
259 |
|
|
} |
260 |
|
|
|
261 |
root |
1.12 |
cf::register_command quit => sub { |
262 |
|
|
my ($ob, $arg) = @_; |
263 |
|
|
|
264 |
|
|
$ob->reply (undef, |
265 |
|
|
"Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
266 |
|
|
. "If you are sure you want to do this, then use the quit_character command instead of quit.", |
267 |
|
|
cf::NDI_UNIQUE | cf::NDI_RED); |
268 |
|
|
}; |
269 |
|
|
|
270 |
|
|
cf::register_command quit_character => sub { |
271 |
|
|
my ($ob, $arg) = @_; |
272 |
|
|
|
273 |
|
|
my $pl = $ob->contr; |
274 |
|
|
|
275 |
|
|
$pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
276 |
|
|
if ($_[0] !~ /^[yY]/) { |
277 |
|
|
$ob->reply (undef, |
278 |
|
|
"Ok, not not quitting then.", |
279 |
|
|
cf::NDI_UNIQUE | cf::NDI_RED); |
280 |
|
|
} else { |
281 |
|
|
$ob->reply (undef, |
282 |
|
|
"Ok, quitting, hope to see you again.", |
283 |
|
|
cf::NDI_UNIQUE | cf::NDI_RED); |
284 |
|
|
$pl->ns->flush; |
285 |
|
|
$pl->quit_character; |
286 |
|
|
} |
287 |
|
|
}); |
288 |
|
|
}; |
289 |
root |
1.11 |
|
290 |
root |
1.1 |
cf::object->attach ( |
291 |
|
|
type => cf::SAVEBED, |
292 |
|
|
on_apply => sub { |
293 |
|
|
my ($bed, $ob) = @_; |
294 |
|
|
|
295 |
|
|
return cf::override 0 unless $ob->type == cf::PLAYER; |
296 |
|
|
|
297 |
root |
1.11 |
my $pl = $ob->pl; |
298 |
|
|
|
299 |
root |
1.1 |
# update respawn position |
300 |
root |
1.11 |
$pl->savebed ($bed->map->path, $bed->x, $bed->y); |
301 |
root |
1.1 |
|
302 |
root |
1.11 |
$pl->killer ("left"); |
303 |
root |
1.5 |
$ob->check_score; |
304 |
root |
1.1 |
|
305 |
|
|
$ob->reply (undef, "In the future, you will wake up here when you die."); |
306 |
|
|
|
307 |
root |
1.11 |
$pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
308 |
root |
1.6 |
if ($_[0] !~ /^[yY]/) { |
309 |
root |
1.11 |
$pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
310 |
|
|
$pl->deactivate; |
311 |
|
|
$pl->ns->destroy; |
312 |
root |
1.7 |
} else { |
313 |
root |
1.13 |
cf::async { $pl->save }; |
314 |
root |
1.6 |
} |
315 |
root |
1.1 |
}); |
316 |
|
|
}, |
317 |
|
|
); |
318 |
|
|
|
319 |
root |
1.8 |
cf::player->attach ( |
320 |
|
|
on_login => sub { |
321 |
|
|
my ($pl) = @_; |
322 |
|
|
my $name = $pl->ob->name; |
323 |
|
|
|
324 |
|
|
$_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
325 |
|
|
}, |
326 |
|
|
on_logout => sub { |
327 |
|
|
my ($pl, $cleanly) = @_; |
328 |
|
|
my $name = $pl->ob->name; |
329 |
|
|
|
330 |
|
|
if ($cleanly) { |
331 |
|
|
$_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
332 |
|
|
} else { |
333 |
|
|
$_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
334 |
|
|
} |
335 |
|
|
}, |
336 |
|
|
); |
337 |
|
|
|
338 |
|
|
cf::client->attach ( |
339 |
|
|
on_addme => \&addme, |
340 |
|
|
); |
341 |
root |
1.1 |
|
342 |
root |
1.11 |
############################################################################# |
343 |
|
|
|
344 |
|
|
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 |
347 |
|
|
our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL |
348 |
|
|
|
349 |
|
|
our $SCHEDULER = cf::async_ext { |
350 |
|
|
while () { |
351 |
|
|
Coro::Timer::sleep $SCHEDULE_INTERVAL; |
352 |
|
|
|
353 |
|
|
# this weird form of iteration over values is used because |
354 |
|
|
# the hash changes underneath us frequently, and for |
355 |
|
|
# keeps a direct reference to the value without (in 5.8 perls) |
356 |
|
|
# keeping a reference, so this is prone to crashes or worse. |
357 |
|
|
my @players = keys %cf::PLAYER; |
358 |
|
|
for (@players) { |
359 |
|
|
my $pl = $cf::PLAYER{$_} |
360 |
|
|
or next; |
361 |
|
|
$pl->valid or next; |
362 |
|
|
|
363 |
|
|
eval { |
364 |
|
|
if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
365 |
|
|
$pl->save; |
366 |
|
|
Coro::Timer::sleep $SAVE_INTERVAL; |
367 |
|
|
} |
368 |
root |
1.14 |
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 |
root |
1.11 |
}; |
377 |
|
|
warn $@ if $@; |
378 |
|
|
Coro::cede; |
379 |
|
|
}; |
380 |
|
|
} |
381 |
|
|
}; |
382 |
|
|
|
383 |
root |
1.14 |
cf::player::find "schmorp";#d# |
384 |
|
|
|
385 |
root |
1.11 |
$SCHEDULER->prio (1); |
386 |
|
|
|