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.19 by root, Mon Jan 8 19:25:53 2007 UTC vs.
Revision 1.43 by root, Mon Apr 23 18:09:57 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;
7 7use List::Util qw(min max);
8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9 8
10# paranoia function to overwrite a string-in-place 9# paranoia function to overwrite a string-in-place
11sub nuke_str { 10sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0] 11 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13} 12}
14 13
15sub query { 14sub query {
16 my ($ns, $flags, $text) = @_; 15 my ($ns, $flags, $text) = @_;
17 16
18 my $current = $Coro::current; 17 my $current = $Coro::current;
19 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); 18 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] });
20 Coro::schedule while ref $current; 19 Coro::schedule while ref $current;
21 20
22 $current 21 $current
23} 22}
24 23
55} 54}
56 55
57sub check_clean_save { 56sub check_clean_save {
58 my ($pl) = @_; 57 my ($pl) = @_;
59 58
60 unless (delete $pl->{clean_save}) { 59 if (my $time = delete $pl->{unclean_save}) {
60 $pl->ns->send_drawinfo (
61 "You didn't use a savebed to leave this realm. This is very dangerous, "
62 . "as lots of things could happen when you leave by other means, such as cave-ins, "
63 . "or monsters suddenly snapping your body. Better use a savebed next time.",
64 cf::NDI_RED
65 );
61 #d#TODO 66 #d#TODO
62 } 67 }
63} 68}
64 69
65# delete a player directory, be non-blocking AND synchronous... 70# delete a player directory, be non-blocking AND synchronous...
71 system "cd \Q$PLAYERDIR\E " 76 system "cd \Q$PLAYERDIR\E "
72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 77 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 78 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
74} 79}
75 80
81sub send_capabilities {
82 my ($ns) = @_;
83
84 return unless $ns->extcmd;
85
86 $ns->ext_event (capabilities =>
87 # id, name, flags (1 == 2d), edge length
88 tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32]],
89 );
90}
91
92sub setup {
93 my ($ns, $args) = @_;
94
95 # run through the cmds of setup
96 # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ...
97 #
98 # we send the status of the cmd back, or a FALSE is the cmd is the server unknown
99 # The client then must sort this out
100
101 my %setup = split / +/, $args;
102 while (my ($k, $v) = each %setup) {
103 if ($k eq "sound") {
104 $ns->sound ($v);
105
106 } elsif ($k eq "exp64") {
107 $setup{$k} = 1;
108
109 } elsif ($k eq "spellmon") {
110 $ns->monitor_spells ($v);
111
112 } elsif ($k eq "darkness") {
113 $ns->darkness ($v);
114
115 } elsif ($k eq "map1cmd") {
116 $ns->mapmode (cf::Map1Cmd) if $v > 0;
117
118 } elsif ($k eq "map1acmd") {
119 $ns->mapmode (cf::Map1aCmd) if $v > 0;
120
121 } elsif ($k eq "map2cmd") {
122 # gcfclient bug, map1acmd is sent too late
123 $ns->mapmode (cf::Map1aCmd);
124 $setup{$k} = "FALSE";
125
126 } elsif ($k eq "newmapcmd") {
127 $ns->newmapcmd ($v);
128
129 } elsif ($k eq "mapinfocmd") {
130 $ns->mapinfocmd ($v);
131
132 } elsif ($k eq "extcmd") {
133 $ns->extcmd ($v > 0);
134 send_capabilities $ns;
135
136 } elsif ($k eq "extmap") {
137 $ns->extmap ($v);
138
139 } elsif ($k eq "facecache") {
140 if (!$v) {
141 $v = 1;
142 $setup{$k} = $v;
143 $ns->send_drawinfo ("(trying to forcefully enable facecaching)", cf::NDI_RED);
144 }
145
146 $ns->facecache ($v);
147
148 } elsif ($k eq "faceset") {
149 $ns->faceset (0);
150 $setup{$k} = 0;
151 # $ns->image2 (1)
152
153 } elsif ($k eq "tileset") {
154 $setup{$k} = $ns->faceset ($v & 1);
155
156 } elsif ($k eq "itemcmd") {
157 # Version of the item protocol command to use. Currently,
158 # only supported versions are 1 and 2. Using a numeric
159 # value will make it very easy to extend this in the future.
160 $ns->itemcmd ($v) if $v >= 1 && $v <= 2;
161
162 $setup{$k} = $ns->itemcmd;
163
164 } elsif ($k eq "mapsize") {
165 my ($x, $y) = split /x/, $v;
166
167 $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, ($x - 1) | 1);
168 $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, ($y - 1) | 1);
169
170 $setup{$k} = "${x}x${y}";
171
172 } elsif ($k eq "extendedMapInfos") {
173 $ns->ext_mapinfos ($v);
174
175 } elsif ($k eq "extendedTextInfos") {
176 $ns->has_readable_type ($v);
177
178 } elsif ($k eq "smoothing") { # cfplus-style smoothing
179 $ns->smoothing ($v);
180
181 } elsif ($k eq "fxix") {
182 $ns->fxix ($v);
183
184 } elsif ($k eq "msg") {
185 $ns->{setup}{msg} = $v;
186
187 } else {
188 # other commands:
189 # sexp: no idea, probably for oudated servers
190 # tick: more stupidity, server should sned a tick per tick
191
192 $setup{$k} = "FALSE";
193 }
194 }
195
196 $ns->send_packet (join " ", setup => %setup);
197
198 cf::datalog setup =>
199 request => $args,
200 reply => \%setup,
201 client => $ns->version,
202 ;
203}
204
76sub addme { 205sub addme {
77 my ($ns) = @_; 206 my ($ns) = @_;
78 207
79 $ns->destroy if $ns->pl; 208 if (!$ns->facecache)
209 {
210 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
211
212
213***
214*** WARNING:
215*** Your client does not support face/image caching,
216*** or it has been disabled. Face caching is mandatory
217*** so please enable it or use a newer client.
218***
219*** Look at your client preferences:
220***
221*** CFPlus: all known versions automatically enable the facecache.
222*** cfclient: use the -cache commandline option.
223*** cfclient: map will not redraw automatically (bug).
224*** gcfclient: use -cache commandline option, or enable
225*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.
226*** jcrossclient: your client is broken, use CFPlus or gcfclient.
227***
228***
229EOF
230 if ($ns->version =~ /jcrossclient/) {
231 # let them, for now
232 } else {
233 $ns->flush;
234 return $ns->destroy;
235 }
236
237 # $ns->facecache = true;
238 }
239
240 if ($ns->mapmode < cf::Map1aCmd) {
241 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
242
243
244***
245*** WARNING:
246*** Your client is too old. Please upgrade to a newer version.
247EOF
248
249 $ns->flush;
250 return $ns->destroy;
251 }
252
253 $ns->pl and return $ns->destroy;
80 254
81 $ns->async (sub { 255 $ns->async (sub {
82 my ($user, $pass); 256 my ($user, $pass);
83 257
84 $ns->send_packet ("addme_success"); 258 $ns->send_packet ("addme_success");
99 $ns->send_drawinfo ( 273 $ns->send_drawinfo (
100 "That username is currently used in another login session. " 274 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.", 275 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED 276 cf::NDI_RED
103 ); 277 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { 278 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) {
105 last; 279 last;
106 } else { 280 } else {
107 $ns->send_drawinfo ( 281 $ns->send_drawinfo (
108 "Your username contains illegal characters " 282 "Your username contains illegal characters "
109 . "(only a-z, A-Z and 0-9 are allowed), " 283 . "(only a-z, A-Z and 0-9 are allowed), "
156 nuke_str $pass; 330 nuke_str $pass;
157 # password matches, wonderful 331 # password matches, wonderful
158 my $pl = cf::player::find $user or next; 332 my $pl = cf::player::find $user or next;
159 $pl->connect ($ns); 333 $pl->connect ($ns);
160 check_clean_save $pl; 334 check_clean_save $pl;
161 $pl->{clean_save} = 1;
162 last; 335 last;
163 } elsif (can_cleanup $pl, $mtime) { 336 } elsif (can_cleanup $pl, $mtime) {
164 Coro::Timer::sleep 1; 337 Coro::Timer::sleep 1;
165 338
166 $ns->send_drawinfo ( 339 $ns->send_drawinfo (
187 Coro::Timer::sleep 1; 360 Coro::Timer::sleep 1;
188 361
189 $ns->send_drawinfo ( 362 $ns->send_drawinfo (
190 "Wrong username or password. Please try again " 363 "Wrong username or password. Please try again "
191 . "(check for Numlock and other semi-obvious error sources).", 364 . "(check for Numlock and other semi-obvious error sources).",
365 cf::NDI_RED
366 );
367 next;
368 }
369 } else {
370 # unable to load the playerfile:
371 # check wether the player dir exists, which means the file is corrupted or
372 # something very similar.
373 if (!aio_stat cf::player::playerdir $user) {
374 $ns->send_drawinfo (
375 "Unable to retrieve this player. It might be a locked or broken account. "
376 . "If this is your account, ask a dungeon master for assistance. "
377 . "Otherwise choose a different login name.",
192 cf::NDI_RED 378 cf::NDI_RED
193 ); 379 );
194 next; 380 next;
195 } 381 }
196 } 382 }
278 } else { 464 } else {
279 $ob->reply (undef, 465 $ob->reply (undef,
280 "Ok, quitting, hope to see you again.", 466 "Ok, quitting, hope to see you again.",
281 cf::NDI_UNIQUE | cf::NDI_RED); 467 cf::NDI_UNIQUE | cf::NDI_RED);
282 $pl->ns->flush; 468 $pl->ns->flush;
283 $pl->quit_character; 469 cf::async { $pl->quit_character };
284 } 470 }
285 }); 471 });
286}; 472};
287 473
288cf::object->attach ( 474cf::object->attach (
294 480
295 my $pl = $ob->contr; 481 my $pl = $ob->contr;
296 482
297 # update respawn position 483 # update respawn position
298 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 484 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
485 cf::async { $pl->save };
299 486
300 $pl->killer ("left"); 487 $pl->killer ("left");
301 $ob->check_score; 488 $ob->check_score;
302 489
303 $ob->reply (undef, "In the future, you will wake up here when you die."); 490 $ob->reply (undef, "In the future, you will wake up here when you die.");
327 514
328 if ($cleanly) { 515 if ($cleanly) {
329 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 516 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
330 } else { 517 } else {
331 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 518 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
332 delete $pl->{clean_save}; 519 $pl->{unclean_save} = $cf::RUNTIME;
333 } 520 }
334 }, 521 },
335); 522);
336 523
337cf::client->attach ( 524cf::client->attach (
338 on_addme => \&addme, 525 on_addme => \&addme,
526 on_setup => \&setup,
339); 527);
340 528
341############################################################################# 529#############################################################################
342 530
343our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs 531our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
344our $SAVE_TIMEOUT = 200; # save players every n seconds 532our $SAVE_TIMEOUT = 20; # save players every n seconds
345our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
346 533
347our $SCHEDULER = cf::async_ext { 534our $SCHEDULER = cf::async_ext {
535 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
348 while () { 536 while () {
349 Coro::Timer::sleep $SCHEDULE_INTERVAL; 537 $schedule_interval->next;
350 538
351 # this weird form of iteration over values is used because 539 # this weird form of iteration over values is used because
352 # the hash changes underneath us frequently, and for 540 # the hash changes underneath us frequently, and for
353 # keeps a direct reference to the value without (in 5.8 perls) 541 # keeps a direct reference to the value without (in 5.8 perls)
354 # keeping a reference, so this is prone to crashes or worse. 542 # keeping a reference, so this is prone to crashes or worse.
358 or next; 546 or next;
359 $pl->valid or next; 547 $pl->valid or next;
360 548
361 eval { 549 eval {
362 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { 550 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
551 cf::wait_for_tick_begin;
363 $pl->save; 552 $pl->save;
364 Coro::Timer::sleep $SAVE_INTERVAL;
365 }
366 553
367 unless ($pl->active) { 554 unless ($pl->active) {
368 # check refcounts, this is tricky and needs to be adjusted to fit server internals 555 # check refcounts, this is tricky and needs to be adjusted to fit server internals
369 my $ob = $pl->ob; 556 my $ob = $pl->ob;
370 Scalar::Util::weaken $pl; 557 Scalar::Util::weaken $pl;
371 Scalar::Util::weaken $ob; 558 Scalar::Util::weaken $ob;
372 my $a_ = $pl->refcnt; 559 my $a_ = $pl->refcnt;#d#
373 my $b_ = $ob->refcnt; 560 my $b_ = $ob->refcnt;#d#
374 my $pl_ref = $pl->refcnt_cnt; 561 my $pl_ref = $pl->refcnt_cnt;
375 my $ob_ref = $ob->refcnt_cnt; 562 my $ob_ref = $ob->refcnt_cnt;
376 563
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 564 ## pl_ref == one from object + one from cf::PLAYER
381 # ob_ref == one from simply being an object 565 ## ob_ref == one from simply being an object
566 if ($pl_ref == 2 && $ob_ref == 1) {
567 warn "player-scheduler destroy ", $ob->name;#d#
568
569 # remove from sight and get fresh "copies"
570 $pl = delete $cf::PLAYER{$ob->name};
571 $ob = $pl->ob;
572
382 $ob->destroy; 573 $ob->destroy;
383 $pl->destroy; 574 $pl->destroy;
384 } else { 575 } else {
385 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# 576 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
577 }
386 } 578 }
387 } 579 }
388 }; 580 };
389 warn $@ if $@; 581 warn $@ if $@;
390 Coro::cede; 582 Coro::cede;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines