ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.1
Committed: Fri Dec 22 16:34:00 2006 UTC (17 years, 5 months ago) by root
Branch: MAIN
Log Message:
- preliminary check in, stuff is rudimentarily working
- moved most of the player creation process into a perl coroutine
- changed internal design of player management to not reuse
  and morph the object in funny ways. should be safer and much
  nicer to handle.
- got rid of some annoying hacks, such as clear()
  (TODO: get rid of player_pod and other stuff now unnecessary?)

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     # login handling
4    
5     use Fcntl;
6     use Coro::AIO;
7    
8     my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9    
10     # testbed for coroutines in crossfire :
11    
12     sub query {
13     my ($ns, $flags, $text) = @_;
14    
15     my $current = $Coro::current;
16     $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; });
17     Coro::schedule while ref $current;
18    
19     $current
20     }
21    
22     sub can_cleanup {
23     my ($playerfile, $mtime) = @_;
24    
25     my $age = time - $mtime;
26     my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return;
27    
28     ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3
29     || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
30     || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
31     || $age > 700 * 86400 # 2 years for everybody else
32     }
33    
34     sub check_playing {
35     my ($ns, $user) = @_;
36    
37     return unless cf::player::find $user;
38    
39     $ns->send_drawinfo (
40     "That player is already logged in on this server. "
41     . "If you want to create a new player, choose another name. "
42     . "If you are already a registered player, make sure nobody "
43     . "else is using your account at this time. If you lost your conenction "
44     . "then the server will likely timeout within a minute. If you still "
45     . "cannot log-in after a minute, you are still logged in. Make sure "
46     . "you do not have another client running. If you use windows, reboot, "
47     . "this will fix anything.",
48     cf::NDI_RED
49     );
50    
51     1
52     }
53    
54     # delete a player directory, be non-blocking AND synchronous...
55     # (thats hard, so we crap out and fork).
56     sub nuke_playerdir {
57     my ($user) = @_;
58    
59     aio_stat "$PLAYERDIR/$user";
60     system "cd \Q$PLAYERDIR\E "
61     . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
62     . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
63     }
64    
65     sub on_addme {
66     my ($ns) = @_;
67    
68     $ns->destroy if $ns->pl;
69    
70     $ns->coro (sub {
71     my ($user, $pass);
72    
73     $ns->send_packet ("addme_success");
74    
75     for (;;) {
76     $ns->send_drawinfo (
77     "Please enter your username now. If you are a new user, "
78     . "make one up that describes your character best. "
79     . "Only letters and digits are allowed, though.",
80     cf::NDI_BLUE
81     );
82    
83     # read username
84     while () {
85     $user = query $ns, 0, "What is your name?\n:";
86     last if $user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/;
87     $ns->send_drawinfo (
88     "Your username contains illegal characters "
89     . "(only a-z, A-Z and 0-9 are allowed), "
90     . "or is not between 3 and 18 characters in length.",
91     cf::NDI_RED
92     );
93     }
94    
95     check_playing $ns, $user and next;
96    
97     $ns->send_drawinfo (
98     "Welcome $user, please enter your password now. "
99     . "New users should now choose a password. "
100     . "Anything your client lets you enter is fine.",
101     cf::NDI_BLUE
102     );
103    
104     # read password
105     while () {
106     $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:";
107     last if $pass =~ /.../;
108     $ns->send_drawinfo (
109     "Try to use at least three characters as your password please, "
110     . "that cannot be too much to ask for :)",
111     cf::NDI_RED
112     );
113     }
114    
115     check_playing $ns, $user and next;
116    
117     my $dir = "$PLAYERDIR/$user";
118     my $plfile = "$dir/$user.pl";
119    
120     # try to read the user file and check the password
121     if (my $fh = aio_open $plfile, O_RDONLY, 0) {
122     my $mtime = (stat $fh)[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;
127    
128     check_playing $ns, $user and next;
129    
130     if ($hash eq crypt $pass, $hash) {
131     # password matches, wonderful
132     my $pl = cf::player::load $plfile or next;
133     $pl->enable_save (1);
134     $pl->connect ($ns);
135     last;
136     }
137    
138     if (can_cleanup $buf, $mtime) {
139     Coro::Timer::sleep 1;
140    
141     $ns->send_drawinfo (
142     "Player exists, but password does not match. If this is you account, "
143     . "please try again. If not, you can now opt to take over this account "
144     . "because it has not been in-use for some time.",
145     cf::NDI_RED
146     );
147    
148     (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
149     or next;
150    
151     # check if the file hasn't changed
152     aio_stat $plfile or next;
153     $mtime == (stat _)[9] or next;
154    
155     # nuke playerdir, this might block, but it needs to be atomic
156     nuke_playerdir $user;
157    
158     # fall through to creation
159     } else {
160     Coro::Timer::sleep 1;
161    
162     $ns->send_drawinfo (
163     "Wrong username or password. Please try again "
164     . "(check for Numlock and other semi-obvious error sources).",
165     cf::NDI_RED
166     );
167     next;
168     }
169     }
170    
171     # the rest of this function is character creation
172     check_playing $ns, $user and next;
173    
174     nuke_playerdir $user;
175    
176     my $pl = cf::player::create;
177     $pl->ob->name ($user);
178     $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
179     $pl->connect ($ns);
180     my $ob = $pl->ob;
181    
182     while () {
183     $ob->update_stats;
184     $pl->save_stats;
185    
186     my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
187     "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
188    
189     if ($res =~ /^[Nn]/) {
190     last;
191     } elsif ($res > 0 && $res <= 7) {
192     my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
193    
194     if ($swap > 0 && $swap <= 7) {
195     $ob->swap_stats ($res - 1, $swap - 1);
196     }
197     } else {
198     $ob->roll_stats;
199     }
200     }
201    
202     $ob->set_animation (2);
203     $ob->add_statbonus;
204    
205     $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
206     $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");
208    
209     $ns->state (cf::ST_CHANGE_CLASS);
210    
211     last;
212     }
213     });
214     }
215    
216     cf::object->attach (
217     type => cf::SAVEBED,
218     on_apply => sub {
219     my ($bed, $ob) = @_;
220    
221     return cf::override 0 unless $ob->type == cf::PLAYER;
222    
223     my $ns = $ob->contr->ns;
224    
225     # update respawn position
226     $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y);
227    
228     #TODO?
229     #strcpy (pl->contr->killer, "left");
230     #check_score (pl); /* Always check score */
231    
232     $ob->reply (undef, "In the future, you will wake up here when you die.");
233     $ob->contr->save ();
234    
235     $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
236     if ($_[0] !~ /^[yY]/) {
237     $ob->contr->save (1);
238     $ns->destroy ();
239     }
240     });
241     },
242     );
243    
244     cf::client->attach (package => __PACKAGE__);
245