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.10 by root, Fri Jan 5 20:04:02 2007 UTC vs.
Revision 1.27 by root, Mon Jan 22 03:44:41 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;
21 21
22 $current 22 $current
23} 23}
24 24
25sub can_cleanup { 25sub 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
37sub check_playing { 37sub 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 "
50 . "this will fix anything.", 50 . "this will fix anything.",
51 cf::NDI_RED 51 cf::NDI_RED
52 ); 52 );
53 53
54 1 54 1
55}
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 }
55} 69}
56 70
57# delete a player directory, be non-blocking AND synchronous... 71# delete a player directory, be non-blocking AND synchronous...
58# (thats hard, so we crap out and fork). 72# (thats hard, so we crap out and fork).
59sub nuke_playerdir { 73sub nuke_playerdir {
123 . "that cannot be too much to ask for :)", 137 . "that cannot be too much to ask for :)",
124 cf::NDI_RED 138 cf::NDI_RED
125 ); 139 );
126 } 140 }
127 141
128 my $dir = "$PLAYERDIR/$user";
129 my $plfile = "$dir/$user.pl";
130
131 # lock this username for the remainder of this login session 142 # lock this username for the remainder of this login session
132 if ($cf::LOGIN_LOCK{$user}) { 143 if ($cf::LOGIN_LOCK{$user}) {
133 $ns->send_drawinfo ( 144 $ns->send_drawinfo (
134 "That username is currently used in another login session. " 145 "That username is currently used in another login session. "
135 . "Chose another, or wait till the other session has ended.", 146 . "Chose another, or wait till the other session has ended.",
140 local $cf::LOGIN_LOCK{$user} = 1; 151 local $cf::LOGIN_LOCK{$user} = 1;
141 152
142 check_playing $ns, $user and next; 153 check_playing $ns, $user and next;
143 154
144 # try to read the user file and check the password 155 # try to read the user file and check the password
145 if (my $fh = aio_open $plfile, O_RDONLY, 0) { 156 if (my $pl = cf::player::find $user) {
157 aio_stat $pl->path and next;
146 my $mtime = (stat $fh)[9]; 158 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; 159 my $hash = $pl->password;
151 160
152 if ($hash eq crypt $pass, $hash) { 161 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
153 nuke_str $pass; 162 nuke_str $pass;
154 # password matches, wonderful 163 # password matches, wonderful
155 my $pl = cf::player::load $plfile or next; 164 my $pl = cf::player::find $user or next;
156 $pl->enable_save (1);
157 $pl->connect ($ns); 165 $pl->connect ($ns);
166 check_clean_save $pl;
158 last; 167 last;
159 } elsif (can_cleanup $buf, $mtime) { 168 } elsif (can_cleanup $pl, $mtime) {
160 Coro::Timer::sleep 1; 169 Coro::Timer::sleep 1;
161 170
162 $ns->send_drawinfo ( 171 $ns->send_drawinfo (
163 "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, "
164 . "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 "
169 #TODO: nuke_str 178 #TODO: nuke_str
170 (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]/
171 or next; 180 or next;
172 181
173 # check if the file hasn't changed 182 # check if the file hasn't changed
174 aio_stat $plfile and next; 183 aio_stat cf::player::path $user and next;
175 $mtime == (stat _)[9] or next; 184 $mtime == (stat _)[9] or next;
176 185
177 nuke_playerdir $user; 186 $pl->quit_character;
178 187
179 # fall through to creation 188 # fall through to creation
180 } else { 189 } else {
181 nuke_str $pass; 190 nuke_str $pass;
182 191
208 next; 217 next;
209 } 218 }
210 219
211 nuke_str $pass2; 220 nuke_str $pass2;
212 221
213 my $pl = cf::player::create; 222 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]); 223 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
216 nuke_str $pass; 224 nuke_str $pass;
217 $pl->connect ($ns); 225 $pl->connect ($ns);
218 226
219 my $ob = $pl->ob; 227 my $ob = $pl->ob;
244 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 252 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
245 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 253 $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"); 254 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
247 255
248 $ns->state (cf::ST_CHANGE_CLASS); 256 $ns->state (cf::ST_CHANGE_CLASS);
249 $pl->enable_save (1);#d# too early 257 delete $pl->{deny_save};#d# too early
250 258
251 last; 259 last;
252 } 260 }
253 }); 261 });
254} 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};
255 292
256cf::object->attach ( 293cf::object->attach (
257 type => cf::SAVEBED, 294 type => cf::SAVEBED,
258 on_apply => sub { 295 on_apply => sub {
259 my ($bed, $ob) = @_; 296 my ($bed, $ob) = @_;
260 297
261 return cf::override 0 unless $ob->type == cf::PLAYER; 298 return cf::override 0 unless $ob->type == cf::PLAYER;
262 299
300 my $pl = $ob->contr;
301
263 # update respawn position 302 # update respawn position
264 $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 };
265 305
266 $ob->contr->killer ("left"); 306 $pl->killer ("left");
267 $ob->check_score; 307 $ob->check_score;
268 308
269 $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.");
270 $ob->contr->save (1);
271 310
272 $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 {
273 if ($_[0] !~ /^[yY]/) { 312 if ($_[0] !~ /^[yY]/) {
274 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 313 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
314 $pl->deactivate;
275 $ob->contr->ns->destroy; 315 $pl->ns->destroy;
276 } else { 316 } else {
277 $ob->contr->enable_save (1); 317 cf::async { $pl->save };
278 } 318 }
279 }); 319 });
280 }, 320 },
281); 321);
282 322
293 333
294 if ($cleanly) { 334 if ($cleanly) {
295 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 335 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
296 } else { 336 } else {
297 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 337 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
338 $pl->{unclean_save} = $cf::RUNTIME;
298 } 339 }
299 }, 340 },
300); 341);
301 342
302cf::client->attach ( 343cf::client->attach (
303 on_addme => \&addme, 344 on_addme => \&addme,
304); 345);
305 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 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
354 while () {
355 $schedule_interval->next;
356
357 # this weird form of iteration over values is used because
358 # the hash changes underneath us frequently, and for
359 # keeps a direct reference to the value without (in 5.8 perls)
360 # keeping a reference, so this is prone to crashes or worse.
361 my @players = keys %cf::PLAYER;
362 for (@players) {
363 my $pl = $cf::PLAYER{$_}
364 or next;
365 $pl->valid or next;
366
367 eval {
368 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
369 $cf::WAIT_FOR_TICK_ONE->wait;
370 $pl->save;
371
372 unless ($pl->active) {
373 # check refcounts, this is tricky and needs to be adjusted to fit server internals
374 my $ob = $pl->ob;
375 Scalar::Util::weaken $pl;
376 Scalar::Util::weaken $ob;
377 my $a_ = $pl->refcnt;#d#
378 my $b_ = $ob->refcnt;#d#
379 my $pl_ref = $pl->refcnt_cnt;
380 my $ob_ref = $ob->refcnt_cnt;
381
382 ## pl_ref == one from object + one from cf::PLAYER
383 ## ob_ref == one from simply being an object
384 if ($pl_ref == 2 && $ob_ref == 1) {
385 warn "player-scheduler destroy ", $ob->name;#d#
386
387 # remove from sight and get fresh "copies"
388 $pl = delete $cf::PLAYER{$ob->name};
389 $ob = $pl->ob;
390
391 $ob->destroy;
392 $pl->destroy;
393 } else {
394 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
395 }
396 }
397 }
398 };
399 warn $@ if $@;
400 Coro::cede;
401 };
402 }
403};
404
405$SCHEDULER->prio (1);
406

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines