ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
(Generate patch)

Comparing deliantra/server/ext/login.ext (file contents):
Revision 1.3 by root, Sat Dec 23 06:21:02 2006 UTC vs.
Revision 1.26 by root, Thu Jan 18 21:27:19 2007 UTC

1#! perl 1#! perl # MANDATORY
2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7 7
8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; 8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9 9
10# testbed for coroutines in crossfire : 10# paranoia function to overwrite a string-in-place
11sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13}
11 14
12sub query { 15sub query {
13 my ($ns, $flags, $text) = @_; 16 my ($ns, $flags, $text) = @_;
14 17
15 my $current = $Coro::current; 18 my $current = $Coro::current;
18 21
19 $current 22 $current
20} 23}
21 24
22sub can_cleanup { 25sub can_cleanup {
23 my ($playerfile, $mtime) = @_; 26 my ($pl, $mtime) = @_;
24 27
25 my $age = time - $mtime; 28 my $age = time - $mtime;
26 my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; 29 my $level = $pl->ob->level;
27 30
28 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 31 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3
29 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 32 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
30 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 33 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
31 || $age > 700 * 86400 # 2 years for everybody else 34 || $age > 700 * 86400 # 2 years for everybody else
32} 35}
33 36
34sub check_playing { 37sub check_playing {
35 my ($ns, $user) = @_; 38 my ($ns, $user) = @_;
36 39
37 return unless cf::player::find $user; 40 return unless cf::player::find_active $user;
38 41
39 $ns->send_drawinfo ( 42 $ns->send_drawinfo (
40 "That player is already logged in on this server. " 43 "That player is already logged in on this server. "
41 . "If you want to create a new player, choose another name. " 44 . "If you want to create a new player, choose another name. "
42 . "If you are already a registered player, make sure nobody " 45 . "If you are already a registered player, make sure nobody "
49 ); 52 );
50 53
51 1 54 1
52} 55}
53 56
57sub check_clean_save {
58 my ($pl) = @_;
59
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 );
67 #d#TODO
68 }
69}
70
54# delete a player directory, be non-blocking AND synchronous... 71# delete a player directory, be non-blocking AND synchronous...
55# (thats hard, so we crap out and fork). 72# (thats hard, so we crap out and fork).
56sub nuke_playerdir { 73sub nuke_playerdir {
57 my ($user) = @_; 74 my ($user) = @_;
58 75
60 system "cd \Q$PLAYERDIR\E " 77 system "cd \Q$PLAYERDIR\E "
61 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 78 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
62 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 79 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
63} 80}
64 81
65sub on_addme { 82sub addme {
66 my ($ns) = @_; 83 my ($ns) = @_;
67 84
68 $ns->destroy if $ns->pl; 85 $ns->destroy if $ns->pl;
69 86
70 $ns->coro (sub { 87 $ns->async (sub {
71 my ($user, $pass); 88 my ($user, $pass);
72 89
73 $ns->send_packet ("addme_success"); 90 $ns->send_packet ("addme_success");
74 91
75 for (;;) { 92 for (;;) {
120 . "that cannot be too much to ask for :)", 137 . "that cannot be too much to ask for :)",
121 cf::NDI_RED 138 cf::NDI_RED
122 ); 139 );
123 } 140 }
124 141
125 my $dir = "$PLAYERDIR/$user";
126 my $plfile = "$dir/$user.pl";
127
128 # lock this username for the remainder of this login session 142 # lock this username for the remainder of this login session
129 if ($cf::LOGIN_LOCK{$user}) { 143 if ($cf::LOGIN_LOCK{$user}) {
130 $ns->send_drawinfo ( 144 $ns->send_drawinfo (
131 "That username is currently used in another login session. " 145 "That username is currently used in another login session. "
132 . "Chose another, or wait till the other session has ended.", 146 . "Chose another, or wait till the other session has ended.",
137 local $cf::LOGIN_LOCK{$user} = 1; 151 local $cf::LOGIN_LOCK{$user} = 1;
138 152
139 check_playing $ns, $user and next; 153 check_playing $ns, $user and next;
140 154
141 # try to read the user file and check the password 155 # try to read the user file and check the password
142 if (my $fh = aio_open $plfile, O_RDONLY, 0) { 156 if (my $pl = cf::player::find $user) {
157 aio_stat $pl->path and next;
143 my $mtime = (stat $fh)[9]; 158 my $mtime = (stat _)[9];
144
145 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
146 $buf =~ /^password (\S+)$/m or next;
147 my $hash = $1; 159 my $hash = $pl->password;
148 160
149 if ($hash eq crypt $pass, $hash) { 161 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
162 nuke_str $pass;
150 # password matches, wonderful 163 # password matches, wonderful
151 my $pl = cf::player::load $plfile or next; 164 my $pl = cf::player::find $user or next;
152 $pl->enable_save (1);
153 $pl->connect ($ns); 165 $pl->connect ($ns);
166 check_clean_save $pl;
154 last; 167 last;
155 } elsif (can_cleanup $buf, $mtime) { 168 } elsif (can_cleanup $pl, $mtime) {
156 Coro::Timer::sleep 1; 169 Coro::Timer::sleep 1;
157 170
158 $ns->send_drawinfo ( 171 $ns->send_drawinfo (
159 "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, "
160 . "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 "
161 . "because it has not been in-use for some time.", 174 . "because it has not been in-use for some time.",
162 cf::NDI_RED 175 cf::NDI_RED
163 ); 176 );
164 177
178 #TODO: nuke_str
165 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 179 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
166 or next; 180 or next;
167 181
168 # check if the file hasn't changed 182 # check if the file hasn't changed
169 aio_stat $plfile and next; 183 aio_stat cf::player::path $user and next;
170 $mtime == (stat _)[9] or next; 184 $mtime == (stat _)[9] or next;
171 185
172 nuke_playerdir $user; 186 $pl->quit_character;
173 187
174 # fall through to creation 188 # fall through to creation
175 } else { 189 } else {
190 nuke_str $pass;
191
176 Coro::Timer::sleep 1; 192 Coro::Timer::sleep 1;
177 193
178 $ns->send_drawinfo ( 194 $ns->send_drawinfo (
179 "Wrong username or password. Please try again " 195 "Wrong username or password. Please try again "
180 . "(check for Numlock and other semi-obvious error sources).", 196 . "(check for Numlock and other semi-obvious error sources).",
190 nuke_playerdir $user; 206 nuke_playerdir $user;
191 207
192 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 208 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
193 209
194 if ($pass2 ne $pass) { 210 if ($pass2 ne $pass) {
211 nuke_str $pass;
212 nuke_str $pass2;
195 $ns->send_drawinfo ( 213 $ns->send_drawinfo (
196 "The passwords do not match, please try again.", 214 "The passwords do not match, please try again.",
197 cf::NDI_RED 215 cf::NDI_RED
198 ); 216 );
199 next; 217 next;
200 } 218 }
201 219
220 nuke_str $pass2;
221
202 my $pl = cf::player::create; 222 my $pl = cf::player::new $user;
203 $pl->ob->name ($user);
204 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); 223 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
224 nuke_str $pass;
205 $pl->connect ($ns); 225 $pl->connect ($ns);
206 226
207 my $ob = $pl->ob; 227 my $ob = $pl->ob;
208 228
209 while () { 229 while () {
232 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 252 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
233 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 253 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR,
234 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); 254 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
235 255
236 $ns->state (cf::ST_CHANGE_CLASS); 256 $ns->state (cf::ST_CHANGE_CLASS);
237 $pl->enable_save (1);#d# too early 257 delete $pl->{deny_save};#d# too early
238 258
239 last; 259 last;
240 } 260 }
241 }); 261 });
242} 262}
263
264cf::register_command quit => sub {
265 my ($ob, $arg) = @_;
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
273cf::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};
243 292
244cf::object->attach ( 293cf::object->attach (
245 type => cf::SAVEBED, 294 type => cf::SAVEBED,
246 on_apply => sub { 295 on_apply => sub {
247 my ($bed, $ob) = @_; 296 my ($bed, $ob) = @_;
248 297
249 return cf::override 0 unless $ob->type == cf::PLAYER; 298 return cf::override 0 unless $ob->type == cf::PLAYER;
250 299
300 my $pl = $ob->contr;
301
251 # update respawn position 302 # update respawn position
252 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 303 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
304 cf::async { $pl->save };
253 305
254 #TODO? 306 $pl->killer ("left");
255 #strcpy (pl->contr->killer, "left"); 307 $ob->check_score;
256 #check_score (pl); /* Always check score */
257 308
258 $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.");
259 $ob->contr->save ();
260 310
261 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 311 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
262 if ($_[0] !~ /^[yY]/) { 312 if ($_[0] !~ /^[yY]/) {
263 $ob->contr->save (1); 313 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
314 $pl->deactivate;
264 $ob->contr->ns->destroy (); 315 $pl->ns->destroy;
316 } else {
317 cf::async { $pl->save };
265 } 318 }
266 }); 319 });
267 }, 320 },
268); 321);
269 322
270cf::client->attach (package => __PACKAGE__); 323cf::player->attach (
324 on_login => sub {
325 my ($pl) = @_;
326 my $name = $pl->ob->name;
271 327
328 $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
329 },
330 on_logout => sub {
331 my ($pl, $cleanly) = @_;
332 my $name = $pl->ob->name;
333
334 if ($cleanly) {
335 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
336 } else {
337 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
338 $pl->{unclean_save} = $cf::RUNTIME;
339 }
340 },
341);
342
343cf::client->attach (
344 on_addme => \&addme,
345);
346
347#############################################################################
348
349our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
350our $SAVE_TIMEOUT = 20; # save players every n seconds
351
352our $SCHEDULER = cf::async_ext {
353 while () {
354 Coro::Timer::sleep $SCHEDULE_INTERVAL;
355
356 # this weird form of iteration over values is used because
357 # the hash changes underneath us frequently, and for
358 # keeps a direct reference to the value without (in 5.8 perls)
359 # keeping a reference, so this is prone to crashes or worse.
360 my @players = keys %cf::PLAYER;
361 for (@players) {
362 my $pl = $cf::PLAYER{$_}
363 or next;
364 $pl->valid or next;
365
366 eval {
367 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
368 $cf::WAIT_FOR_TICK_ONE->wait;
369 $pl->save;
370
371 unless ($pl->active) {
372 # check refcounts, this is tricky and needs to be adjusted to fit server internals
373 my $ob = $pl->ob;
374 Scalar::Util::weaken $pl;
375 Scalar::Util::weaken $ob;
376 my $a_ = $pl->refcnt;#d#
377 my $b_ = $ob->refcnt;#d#
378 my $pl_ref = $pl->refcnt_cnt;
379 my $ob_ref = $ob->refcnt_cnt;
380
381 ## pl_ref == one from object + one from cf::PLAYER
382 ## ob_ref == one from simply being an object
383 if ($pl_ref == 2 && $ob_ref == 1) {
384 warn "player-scheduler destroy ", $ob->name;#d#
385
386 # remove from sight and get fresh "copies"
387 $pl = delete $cf::PLAYER{$ob->name};
388 $ob = $pl->ob;
389
390 $ob->destroy;
391 $pl->destroy;
392 } else {
393 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
394 }
395 }
396 }
397 };
398 warn $@ if $@;
399 Coro::cede;
400 };
401 }
402};
403
404$SCHEDULER->prio (1);
405

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines