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.11 by root, Sun Jan 7 02:39:14 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
260# "Quitting will delete your character PERMANENTLY. If you are sure you want to do this, then use the quit_character command instead of quit."
261# "Quitting will delete your character.\nAre you sure you want to quit (y/n):"
262# quit, quit_character, save
255 263
256cf::object->attach ( 264cf::object->attach (
257 type => cf::SAVEBED, 265 type => cf::SAVEBED,
258 on_apply => sub { 266 on_apply => sub {
259 my ($bed, $ob) = @_; 267 my ($bed, $ob) = @_;
260 268
261 return cf::override 0 unless $ob->type == cf::PLAYER; 269 return cf::override 0 unless $ob->type == cf::PLAYER;
262 270
271 my $pl = $ob->pl;
272
263 # update respawn position 273 # update respawn position
264 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 274 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
265 275
266 $ob->contr->killer ("left"); 276 $pl->killer ("left");
267 $ob->check_score; 277 $ob->check_score;
268 278
269 $ob->reply (undef, "In the future, you will wake up here when you die."); 279 $ob->reply (undef, "In the future, you will wake up here when you die.");
270 $ob->contr->save (1);
271 280
272 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 281 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
273 if ($_[0] !~ /^[yY]/) { 282 if ($_[0] !~ /^[yY]/) {
274 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 283 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
284 $pl->deactivate;
275 $ob->contr->ns->destroy; 285 $pl->ns->destroy;
276 } else { 286 } else {
277 $ob->contr->enable_save (1); 287 cf::async {
288 $pl->{clean_save} = 1;
289 $pl->save;
290 };
278 } 291 }
279 }); 292 });
280 }, 293 },
281); 294);
282 295
301 314
302cf::client->attach ( 315cf::client->attach (
303 on_addme => \&addme, 316 on_addme => \&addme,
304); 317);
305 318
319#############################################################################
320
321our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
322our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory
323our $SAVE_TIMEOUT = 20; # save players every n seconds
324our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
325
326our $SCHEDULER = cf::async_ext {
327 while () {
328 Coro::Timer::sleep $SCHEDULE_INTERVAL;
329
330 # this weird form of iteration over values is used because
331 # the hash changes underneath us frequently, and for
332 # keeps a direct reference to the value without (in 5.8 perls)
333 # keeping a reference, so this is prone to crashes or worse.
334 my @players = keys %cf::PLAYER;
335 for (@players) {
336 my $pl = $cf::PLAYER{$_}
337 or next;
338 $pl->valid or next;
339
340 eval {
341 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
342 $pl->save;
343 Coro::Timer::sleep $SAVE_INTERVAL;
344 }
345 };
346 warn $@ if $@;
347 Coro::cede;
348 };
349 }
350};
351
352$SCHEDULER->prio (1);
353

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines