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.2 by root, Sat Dec 23 03:38:43 2006 UTC vs.
Revision 1.20 by root, Mon Jan 8 22:32:10 2007 UTC

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 unless (delete $pl->{clean_save}) {
61 #d#TODO
62 }
63}
64
54# delete a player directory, be non-blocking AND synchronous... 65# delete a player directory, be non-blocking AND synchronous...
55# (thats hard, so we crap out and fork). 66# (thats hard, so we crap out and fork).
56sub nuke_playerdir { 67sub nuke_playerdir {
57 my ($user) = @_; 68 my ($user) = @_;
58 69
60 system "cd \Q$PLAYERDIR\E " 71 system "cd \Q$PLAYERDIR\E "
61 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
62 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
63} 74}
64 75
65sub on_addme { 76sub addme {
66 my ($ns) = @_; 77 my ($ns) = @_;
67 78
68 $ns->destroy if $ns->pl; 79 $ns->destroy if $ns->pl;
69 80
70 $ns->coro (sub { 81 $ns->async (sub {
71 my ($user, $pass); 82 my ($user, $pass);
72 83
73 $ns->send_packet ("addme_success"); 84 $ns->send_packet ("addme_success");
74 85
75 for (;;) { 86 for (;;) {
81 ); 92 );
82 93
83 # read username 94 # read username
84 while () { 95 while () {
85 $user = query $ns, 0, "What is your name?\n:"; 96 $user = query $ns, 0, "What is your name?\n:";
86 last if $user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/; 97
98 if ($cf::LOGIN_LOCK{$user}) {
87 $ns->send_drawinfo ( 99 $ns->send_drawinfo (
100 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED
103 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) {
105 last;
106 } else {
107 $ns->send_drawinfo (
88 "Your username contains illegal characters " 108 "Your username contains illegal characters "
89 . "(only a-z, A-Z and 0-9 are allowed), " 109 . "(only a-z, A-Z and 0-9 are allowed), "
90 . "or is not between 3 and 18 characters in length.", 110 . "or is not between 3 and 18 characters in length.",
91 cf::NDI_RED 111 cf::NDI_RED
92 ); 112 );
113 }
93 } 114 }
94 115
95 check_playing $ns, $user and next; 116 check_playing $ns, $user and next;
96 117
97 $ns->send_drawinfo ( 118 $ns->send_drawinfo (
110 . "that cannot be too much to ask for :)", 131 . "that cannot be too much to ask for :)",
111 cf::NDI_RED 132 cf::NDI_RED
112 ); 133 );
113 } 134 }
114 135
136 # lock this username for the remainder of this login session
137 if ($cf::LOGIN_LOCK{$user}) {
138 $ns->send_drawinfo (
139 "That username is currently used in another login session. "
140 . "Chose another, or wait till the other session has ended.",
141 cf::NDI_RED
142 );
143 next;
144 }
145 local $cf::LOGIN_LOCK{$user} = 1;
146
115 check_playing $ns, $user and next; 147 check_playing $ns, $user and next;
116 148
117 my $dir = "$PLAYERDIR/$user";
118 my $plfile = "$dir/$user.pl";
119
120 # try to read the user file and check the password 149 # try to read the user file and check the password
121 if (my $fh = aio_open $plfile, O_RDONLY, 0) { 150 if (my $pl = cf::player::find $user) {
151 aio_stat $pl->path and next;
122 my $mtime = (stat $fh)[9]; 152 my $mtime = (stat _)[9];
123
124 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
125 $buf =~ /^password (\S+)$/m or next;
126 my $hash = $1; 153 my $hash = $pl->password;
127 154
128 check_playing $ns, $user and next; 155 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
129 156 nuke_str $pass;
130 if ($hash eq crypt $pass, $hash) {
131 # password matches, wonderful 157 # password matches, wonderful
132 my $pl = cf::player::load $plfile or next; 158 my $pl = cf::player::find $user or next;
133 $pl->enable_save (1);
134 $pl->connect ($ns); 159 $pl->connect ($ns);
160 check_clean_save $pl;
161 $pl->{clean_save} = 1;
135 last; 162 last;
136 }
137
138 if (can_cleanup $buf, $mtime) { 163 } elsif (can_cleanup $pl, $mtime) {
139 Coro::Timer::sleep 1; 164 Coro::Timer::sleep 1;
140 165
141 $ns->send_drawinfo ( 166 $ns->send_drawinfo (
142 "Player exists, but password does not match. If this is you account, " 167 "Player exists, but password does not match. If this is your account, "
143 . "please try again. If not, you can now opt to take over this account " 168 . "please try again. If not, you can now decide to take over this account "
144 . "because it has not been in-use for some time.", 169 . "because it has not been in-use for some time.",
145 cf::NDI_RED 170 cf::NDI_RED
146 ); 171 );
147 172
173 #TODO: nuke_str
148 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 174 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
149 or next; 175 or next;
150 176
151 # check if the file hasn't changed 177 # check if the file hasn't changed
152 aio_stat $plfile or next; 178 aio_stat cf::player::path $user and next;
153 $mtime == (stat _)[9] or next; 179 $mtime == (stat _)[9] or next;
154 180
155 # nuke playerdir, this might block, but it needs to be atomic 181 $pl->quit_character;
156 nuke_playerdir $user;
157 182
158 # fall through to creation 183 # fall through to creation
159 } else { 184 } else {
185 nuke_str $pass;
186
160 Coro::Timer::sleep 1; 187 Coro::Timer::sleep 1;
161 188
162 $ns->send_drawinfo ( 189 $ns->send_drawinfo (
163 "Wrong username or password. Please try again " 190 "Wrong username or password. Please try again "
164 . "(check for Numlock and other semi-obvious error sources).", 191 . "(check for Numlock and other semi-obvious error sources).",
167 next; 194 next;
168 } 195 }
169 } 196 }
170 197
171 # the rest of this function is character creation 198 # the rest of this function is character creation
172 check_playing $ns, $user and next;
173 199
200 # just to make sure nothing is left over
174 nuke_playerdir $user; 201 nuke_playerdir $user;
175 202
203 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
204
205 if ($pass2 ne $pass) {
206 nuke_str $pass;
207 nuke_str $pass2;
208 $ns->send_drawinfo (
209 "The passwords do not match, please try again.",
210 cf::NDI_RED
211 );
212 next;
213 }
214
215 nuke_str $pass2;
216
176 my $pl = cf::player::create; 217 my $pl = cf::player::new $user;
177 $pl->ob->name ($user);
178 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); 218 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
219 nuke_str $pass;
179 $pl->connect ($ns); 220 $pl->connect ($ns);
221
180 my $ob = $pl->ob; 222 my $ob = $pl->ob;
181 223
182 while () { 224 while () {
183 $ob->update_stats; 225 $ob->update_stats;
184 $pl->save_stats; 226 $pl->save_stats;
205 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 247 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
206 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 248 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR,
207 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); 249 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
208 250
209 $ns->state (cf::ST_CHANGE_CLASS); 251 $ns->state (cf::ST_CHANGE_CLASS);
210 $pl->enable_save (1);#d# too early 252 delete $pl->{deny_save};#d# too early
211 253
212 last; 254 last;
213 } 255 }
214 }); 256 });
215} 257}
258
259cf::register_command quit => sub {
260 my ($ob, $arg) = @_;
261
262 $ob->reply (undef,
263 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
264 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
265 cf::NDI_UNIQUE | cf::NDI_RED);
266};
267
268cf::register_command quit_character => sub {
269 my ($ob, $arg) = @_;
270
271 my $pl = $ob->contr;
272
273 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
274 if ($_[0] !~ /^[yY]/) {
275 $ob->reply (undef,
276 "Ok, not not quitting then.",
277 cf::NDI_UNIQUE | cf::NDI_RED);
278 } else {
279 $ob->reply (undef,
280 "Ok, quitting, hope to see you again.",
281 cf::NDI_UNIQUE | cf::NDI_RED);
282 $pl->ns->flush;
283 $pl->quit_character;
284 }
285 });
286};
216 287
217cf::object->attach ( 288cf::object->attach (
218 type => cf::SAVEBED, 289 type => cf::SAVEBED,
219 on_apply => sub { 290 on_apply => sub {
220 my ($bed, $ob) = @_; 291 my ($bed, $ob) = @_;
221 292
222 return cf::override 0 unless $ob->type == cf::PLAYER; 293 return cf::override 0 unless $ob->type == cf::PLAYER;
223 294
295 my $pl = $ob->contr;
296
224 # update respawn position 297 # update respawn position
225 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 298 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
226 299
227 #TODO? 300 $pl->killer ("left");
228 #strcpy (pl->contr->killer, "left"); 301 $ob->check_score;
229 #check_score (pl); /* Always check score */
230 302
231 $ob->reply (undef, "In the future, you will wake up here when you die."); 303 $ob->reply (undef, "In the future, you will wake up here when you die.");
232 $ob->contr->save ();
233 304
234 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 305 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
235 if ($_[0] !~ /^[yY]/) { 306 if ($_[0] !~ /^[yY]/) {
236 $ob->contr->save (1); 307 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
308 $pl->deactivate;
237 $ob->contr->ns->destroy (); 309 $pl->ns->destroy;
310 } else {
311 cf::async { $pl->save };
238 } 312 }
239 }); 313 });
240 }, 314 },
241); 315);
242 316
243cf::client->attach (package => __PACKAGE__); 317cf::player->attach (
318 on_login => sub {
319 my ($pl) = @_;
320 my $name = $pl->ob->name;
244 321
322 $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
323 },
324 on_logout => sub {
325 my ($pl, $cleanly) = @_;
326 my $name = $pl->ob->name;
327
328 if ($cleanly) {
329 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
330 } else {
331 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
332 delete $pl->{clean_save};
333 }
334 },
335);
336
337cf::client->attach (
338 on_addme => \&addme,
339);
340
341#############################################################################
342
343our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
344our $SAVE_TIMEOUT = 200; # save players every n seconds
345our $SAVE_INTERVAL = 1.1; # save at max. one player every $SAVE_INTERVAL
346
347our $SCHEDULER = cf::async_ext {
348 while () {
349 Coro::Timer::sleep $SCHEDULE_INTERVAL;
350
351 # this weird form of iteration over values is used because
352 # the hash changes underneath us frequently, and for
353 # keeps a direct reference to the value without (in 5.8 perls)
354 # keeping a reference, so this is prone to crashes or worse.
355 my @players = keys %cf::PLAYER;
356 for (@players) {
357 my $pl = $cf::PLAYER{$_}
358 or next;
359 $pl->valid or next;
360
361 eval {
362 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
363 $pl->save;
364 Coro::Timer::sleep $SAVE_INTERVAL;
365 }
366
367 unless ($pl->active) {
368 # check refcounts, this is tricky and needs to be adjusted to fit server internals
369 my $ob = $pl->ob;
370 Scalar::Util::weaken $pl;
371 Scalar::Util::weaken $ob;
372 my $a_ = $pl->refcnt;
373 my $b_ = $ob->refcnt;
374 my $pl_ref = $pl->refcnt_cnt;
375 my $ob_ref = $ob->refcnt_cnt;
376
377 if ($pl_ref == 2 && $ob_ref == 1) {
378 warn "player-scheduler destroy ", $ob->name;#d#
379 #delete $cf::PLAYER{$ob->name};
380 ## pl_ref == one from object + one from cf::PLAYER
381 ## ob_ref == one from simply being an object
382 #$ob->destroy;
383 #$pl->destroy;
384 } else {
385 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
386 }
387 }
388 };
389 warn $@ if $@;
390 Coro::cede;
391 };
392 }
393};
394
395$SCHEDULER->prio (1);
396

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines