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.12 by root, Sun Jan 7 21:54:59 2007 UTC

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 unless (delete $pl->{clean_save}) {
61 #d#TODO
62 }
55} 63}
56 64
57# delete a player directory, be non-blocking AND synchronous... 65# delete a player directory, be non-blocking AND synchronous...
58# (thats hard, so we crap out and fork). 66# (thats hard, so we crap out and fork).
59sub nuke_playerdir { 67sub nuke_playerdir {
123 . "that cannot be too much to ask for :)", 131 . "that cannot be too much to ask for :)",
124 cf::NDI_RED 132 cf::NDI_RED
125 ); 133 );
126 } 134 }
127 135
128 my $dir = "$PLAYERDIR/$user";
129 my $plfile = "$dir/$user.pl";
130
131 # lock this username for the remainder of this login session 136 # lock this username for the remainder of this login session
132 if ($cf::LOGIN_LOCK{$user}) { 137 if ($cf::LOGIN_LOCK{$user}) {
133 $ns->send_drawinfo ( 138 $ns->send_drawinfo (
134 "That username is currently used in another login session. " 139 "That username is currently used in another login session. "
135 . "Chose another, or wait till the other session has ended.", 140 . "Chose another, or wait till the other session has ended.",
140 local $cf::LOGIN_LOCK{$user} = 1; 145 local $cf::LOGIN_LOCK{$user} = 1;
141 146
142 check_playing $ns, $user and next; 147 check_playing $ns, $user and next;
143 148
144 # try to read the user file and check the password 149 # try to read the user file and check the password
145 if (my $fh = aio_open $plfile, O_RDONLY, 0) { 150 if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) {
146 my $mtime = (stat $fh)[9]; 151 my $mtime = (stat $fh)[9];
147 152
148 0 < aio_read $fh, 0, 16384, my $buf, 0 or next; 153 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
149 $buf =~ /^password (\S+)$/m or next; 154 $buf =~ /^password (\S+)$/m or next;
150 my $hash = $1; 155 my $hash = $1;
151 156
152 if ($hash eq crypt $pass, $hash) { 157 if ($hash eq crypt $pass, $hash) {
153 nuke_str $pass; 158 nuke_str $pass;
154 # password matches, wonderful 159 # password matches, wonderful
155 my $pl = cf::player::load $plfile or next; 160 my $pl = cf::player::find $user or next;
156 $pl->enable_save (1);
157 $pl->connect ($ns); 161 $pl->connect ($ns);
162 check_clean_save $pl;
158 last; 163 last;
159 } elsif (can_cleanup $buf, $mtime) { 164 } elsif (can_cleanup $buf, $mtime) {
160 Coro::Timer::sleep 1; 165 Coro::Timer::sleep 1;
161 166
162 $ns->send_drawinfo ( 167 $ns->send_drawinfo (
169 #TODO: nuke_str 174 #TODO: nuke_str
170 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 175 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
171 or next; 176 or next;
172 177
173 # check if the file hasn't changed 178 # check if the file hasn't changed
174 aio_stat $plfile and next; 179 aio_stat cf::player::path $user and next;
175 $mtime == (stat _)[9] or next; 180 $mtime == (stat _)[9] or next;
176 181
177 nuke_playerdir $user; 182 nuke_playerdir $user;
178 183
179 # fall through to creation 184 # fall through to creation
208 next; 213 next;
209 } 214 }
210 215
211 nuke_str $pass2; 216 nuke_str $pass2;
212 217
213 my $pl = cf::player::create; 218 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]); 219 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
216 nuke_str $pass; 220 nuke_str $pass;
217 $pl->connect ($ns); 221 $pl->connect ($ns);
218 222
219 my $ob = $pl->ob; 223 my $ob = $pl->ob;
244 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 248 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
245 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 249 $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"); 250 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
247 251
248 $ns->state (cf::ST_CHANGE_CLASS); 252 $ns->state (cf::ST_CHANGE_CLASS);
249 $pl->enable_save (1);#d# too early 253 delete $pl->{deny_save};#d# too early
250 254
251 last; 255 last;
252 } 256 }
253 }); 257 });
254} 258}
259
260cf::register_command quit => sub {
261 my ($ob, $arg) = @_;
262
263 $ob->reply (undef,
264 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
265 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
266 cf::NDI_UNIQUE | cf::NDI_RED);
267};
268
269cf::register_command quit_character => sub {
270 my ($ob, $arg) = @_;
271
272 my $pl = $ob->contr;
273
274 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
275 if ($_[0] !~ /^[yY]/) {
276 $ob->reply (undef,
277 "Ok, not not quitting then.",
278 cf::NDI_UNIQUE | cf::NDI_RED);
279 } else {
280 $ob->reply (undef,
281 "Ok, quitting, hope to see you again.",
282 cf::NDI_UNIQUE | cf::NDI_RED);
283 $pl->ns->flush;
284 $pl->quit_character;
285 }
286 });
287};
255 288
256cf::object->attach ( 289cf::object->attach (
257 type => cf::SAVEBED, 290 type => cf::SAVEBED,
258 on_apply => sub { 291 on_apply => sub {
259 my ($bed, $ob) = @_; 292 my ($bed, $ob) = @_;
260 293
261 return cf::override 0 unless $ob->type == cf::PLAYER; 294 return cf::override 0 unless $ob->type == cf::PLAYER;
262 295
296 my $pl = $ob->pl;
297
263 # update respawn position 298 # update respawn position
264 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 299 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
265 300
266 $ob->contr->killer ("left"); 301 $pl->killer ("left");
267 $ob->check_score; 302 $ob->check_score;
268 303
269 $ob->reply (undef, "In the future, you will wake up here when you die."); 304 $ob->reply (undef, "In the future, you will wake up here when you die.");
270 $ob->contr->save (1);
271 305
272 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 306 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
273 if ($_[0] !~ /^[yY]/) { 307 if ($_[0] !~ /^[yY]/) {
274 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 308 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
309 $pl->deactivate;
275 $ob->contr->ns->destroy; 310 $pl->ns->destroy;
276 } else { 311 } else {
277 $ob->contr->enable_save (1); 312 cf::async {
313 $pl->{clean_save} = 1;
314 $pl->save;
315 };
278 } 316 }
279 }); 317 });
280 }, 318 },
281); 319);
282 320
301 339
302cf::client->attach ( 340cf::client->attach (
303 on_addme => \&addme, 341 on_addme => \&addme,
304); 342);
305 343
344#############################################################################
345
346our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
347our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory
348our $SAVE_TIMEOUT = 20; # save players every n seconds
349our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
350
351our $SCHEDULER = cf::async_ext {
352 while () {
353 Coro::Timer::sleep $SCHEDULE_INTERVAL;
354
355 # this weird form of iteration over values is used because
356 # the hash changes underneath us frequently, and for
357 # keeps a direct reference to the value without (in 5.8 perls)
358 # keeping a reference, so this is prone to crashes or worse.
359 my @players = keys %cf::PLAYER;
360 for (@players) {
361 my $pl = $cf::PLAYER{$_}
362 or next;
363 $pl->valid or next;
364
365 eval {
366 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
367 $pl->save;
368 Coro::Timer::sleep $SAVE_INTERVAL;
369 }
370 };
371 warn $@ if $@;
372 Coro::cede;
373 };
374 }
375};
376
377$SCHEDULER->prio (1);
378

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines