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.9 by root, Thu Jan 4 16:19:31 2007 UTC vs.
Revision 1.17 by root, Mon Jan 8 14:11:05 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 "
52 ); 52 );
53 53
54 1 54 1
55} 55}
56 56
57sub check_clean_save {
58 my ($pl) = @_;
59
60 unless (delete $pl->{clean_save}) {
61 #d#TODO
62 }
63}
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 {
60 my ($user) = @_; 68 my ($user) = @_;
61 69
68sub addme { 76sub addme {
69 my ($ns) = @_; 77 my ($ns) = @_;
70 78
71 $ns->destroy if $ns->pl; 79 $ns->destroy if $ns->pl;
72 80
73 $ns->coro (sub { 81 $ns->async (sub {
74 my ($user, $pass); 82 my ($user, $pass);
75 83
76 $ns->send_packet ("addme_success"); 84 $ns->send_packet ("addme_success");
77 85
78 for (;;) { 86 for (;;) {
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;
163 $pl->{clean_save} = 1;
158 last; 164 last;
159 } elsif (can_cleanup $buf, $mtime) { 165 } elsif (can_cleanup $buf, $mtime) {
160 Coro::Timer::sleep 1; 166 Coro::Timer::sleep 1;
161 167
162 $ns->send_drawinfo ( 168 $ns->send_drawinfo (
169 #TODO: nuke_str 175 #TODO: nuke_str
170 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 176 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
171 or next; 177 or next;
172 178
173 # check if the file hasn't changed 179 # check if the file hasn't changed
174 aio_stat $plfile and next; 180 aio_stat cf::player::path $user and next;
175 $mtime == (stat _)[9] or next; 181 $mtime == (stat _)[9] or next;
176 182
177 nuke_playerdir $user; 183 nuke_playerdir $user;
178 184
179 # fall through to creation 185 # fall through to creation
208 next; 214 next;
209 } 215 }
210 216
211 nuke_str $pass2; 217 nuke_str $pass2;
212 218
213 my $pl = cf::player::create; 219 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]); 220 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
216 nuke_str $pass; 221 nuke_str $pass;
217 $pl->connect ($ns); 222 $pl->connect ($ns);
218 223
219 my $ob = $pl->ob; 224 my $ob = $pl->ob;
244 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 249 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
245 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 250 $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"); 251 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
247 252
248 $ns->state (cf::ST_CHANGE_CLASS); 253 $ns->state (cf::ST_CHANGE_CLASS);
249 $pl->enable_save (1);#d# too early 254 delete $pl->{deny_save};#d# too early
250 255
251 last; 256 last;
252 } 257 }
253 }); 258 });
254} 259}
260
261cf::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
270cf::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};
255 289
256cf::object->attach ( 290cf::object->attach (
257 type => cf::SAVEBED, 291 type => cf::SAVEBED,
258 on_apply => sub { 292 on_apply => sub {
259 my ($bed, $ob) = @_; 293 my ($bed, $ob) = @_;
260 294
261 return cf::override 0 unless $ob->type == cf::PLAYER; 295 return cf::override 0 unless $ob->type == cf::PLAYER;
262 296
297 my $pl = $ob->contr;
298
263 # update respawn position 299 # update respawn position
264 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 300 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
265 301
266 $ob->contr->killer ("left"); 302 $pl->killer ("left");
267 $ob->check_score; 303 $ob->check_score;
268 304
269 $ob->reply (undef, "In the future, you will wake up here when you die."); 305 $ob->reply (undef, "In the future, you will wake up here when you die.");
270 $ob->contr->save (1);
271 306
272 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 307 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
273 if ($_[0] !~ /^[yY]/) { 308 if ($_[0] !~ /^[yY]/) {
274 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 309 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
310 $pl->deactivate;
275 $ob->contr->ns->destroy; 311 $pl->ns->destroy;
276 } else { 312 } else {
277 $ob->contr->enable_save (1); 313 cf::async { $pl->save };
278 } 314 }
279 }); 315 });
280 }, 316 },
281); 317);
282 318
293 329
294 if ($cleanly) { 330 if ($cleanly) {
295 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 331 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
296 } else { 332 } else {
297 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 333 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
334 delete $pl->{clean_save};
298 } 335 }
299 }, 336 },
300); 337);
301 338
302cf::client->attach ( 339cf::client->attach (
303 on_addme => \&addme, 340 on_addme => \&addme,
304); 341);
305 342
343#############################################################################
344
345our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
346our $SAVE_TIMEOUT = 200; # save players every n seconds
347our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
348
349our $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
369 unless ($pl->active) {
370 # check refcounts, this is tricky and needs to be adjusted to fit server internals
371 my $ob = $pl->ob;
372 Scalar::Util::weaken $pl;
373 Scalar::Util::weaken $ob;
374 my $a_ = $pl->refcnt;
375 my $b_ = $ob->refcnt;
376 my $pl_ref = $pl->refcnt_cnt;
377 my $ob_ref = $ob->refcnt_cnt;
378
379 if ($pl_ref == 2 && $ob_ref == 1) {
380 warn "player-scheduler destroy ", $ob->name;#d#
381 delete $cf::PLAYER{$ob->name};
382 # pl_ref == one from object + one from cf::PLAYER
383 # ob_ref == one from simply being an object
384 $ob->destroy;
385 $pl->destroy;
386 } else {
387 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
388 }
389 }
390 };
391 warn $@ if $@;
392 Coro::cede;
393 };
394 }
395};
396
397$SCHEDULER->prio (1);
398

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines