… | |
… | |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
18 | use Coro::AIO; |
18 | use Coro::AIO; |
19 | |
19 | |
|
|
20 | use Data::Dumper; |
20 | use Digest::MD5; |
21 | use Digest::MD5; |
21 | use Fcntl; |
22 | use Fcntl; |
22 | use IO::AIO 2.32 (); |
23 | use IO::AIO 2.32 (); |
23 | use YAML::Syck (); |
24 | use YAML::Syck (); |
24 | use Time::HiRes; |
25 | use Time::HiRes; |
… | |
… | |
154 | |
155 | |
155 | =head2 UTILITY FUNCTIONS |
156 | =head2 UTILITY FUNCTIONS |
156 | |
157 | |
157 | =over 4 |
158 | =over 4 |
158 | |
159 | |
|
|
160 | =item dumpval $ref |
|
|
161 | |
159 | =cut |
162 | =cut |
|
|
163 | |
|
|
164 | sub dumpval { |
|
|
165 | eval { |
|
|
166 | local $SIG{__DIE__}; |
|
|
167 | my $d; |
|
|
168 | if (1) { |
|
|
169 | $d = new Data::Dumper([$_[0]], ["*var"]); |
|
|
170 | $d->Terse(1); |
|
|
171 | $d->Indent(2); |
|
|
172 | $d->Quotekeys(0); |
|
|
173 | $d->Useqq(1); |
|
|
174 | #$d->Bless(...); |
|
|
175 | $d->Seen($_[1]) if @_ > 1; |
|
|
176 | $d = $d->Dump(); |
|
|
177 | } |
|
|
178 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
|
|
179 | $d |
|
|
180 | } || "[unable to dump $_[0]: '$@']"; |
|
|
181 | } |
160 | |
182 | |
161 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
183 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
162 | |
184 | |
163 | =item $ref = cf::from_json $json |
185 | =item $ref = cf::from_json $json |
164 | |
186 | |
… | |
… | |
334 | =cut |
356 | =cut |
335 | |
357 | |
336 | ############################################################################# |
358 | ############################################################################# |
337 | |
359 | |
338 | package cf::path; |
360 | package cf::path; |
|
|
361 | |
|
|
362 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
|
|
363 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
339 | |
364 | |
340 | sub new { |
365 | sub new { |
341 | my ($class, $path, $base) = @_; |
366 | my ($class, $path, $base) = @_; |
342 | |
367 | |
343 | $path = $path->as_string if ref $path; |
368 | $path = $path->as_string if ref $path; |
… | |
… | |
407 | # } |
432 | # } |
408 | } |
433 | } |
409 | |
434 | |
410 | # escape the /'s in the path |
435 | # escape the /'s in the path |
411 | sub _escaped_path { |
436 | sub _escaped_path { |
412 | # ∕ is U+2215 |
|
|
413 | (my $path = $_[0]{path}) =~ s/\//∕/g; |
437 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
414 | $path |
438 | $path |
415 | } |
439 | } |
416 | |
440 | |
417 | # the original (read-only) location |
441 | # the original (read-only) location |
418 | sub load_path { |
442 | sub load_path { |
… | |
… | |
1076 | |
1100 | |
1077 | =cut |
1101 | =cut |
1078 | |
1102 | |
1079 | package cf::player; |
1103 | package cf::player; |
1080 | |
1104 | |
|
|
1105 | use Coro::AIO; |
|
|
1106 | |
1081 | =head3 cf::player |
1107 | =head3 cf::player |
1082 | |
1108 | |
1083 | =over 4 |
1109 | =over 4 |
1084 | |
1110 | |
1085 | =item cf::player::find $login |
1111 | =item cf::player::find $login |
… | |
… | |
1137 | my $path = path $pl; |
1163 | my $path = path $pl; |
1138 | my $guard = cf::lock_acquire "user_save:$path"; |
1164 | my $guard = cf::lock_acquire "user_save:$path"; |
1139 | |
1165 | |
1140 | return if $pl->{deny_save}; |
1166 | return if $pl->{deny_save}; |
1141 | |
1167 | |
1142 | Coro::AIO::aio_mkdir playerdir $pl, 0770; |
1168 | aio_mkdir playerdir $pl, 0770; |
1143 | $pl->{last_save} = $cf::RUNTIME; |
1169 | $pl->{last_save} = $cf::RUNTIME; |
1144 | |
1170 | |
1145 | $pl->save_pl ($path); |
1171 | $pl->save_pl ($path); |
1146 | Coro::cede; |
1172 | Coro::cede; |
1147 | } |
1173 | } |
… | |
… | |
1156 | |
1182 | |
1157 | $cf::PLAYER{$login} = $self; |
1183 | $cf::PLAYER{$login} = $self; |
1158 | |
1184 | |
1159 | $self |
1185 | $self |
1160 | } |
1186 | } |
|
|
1187 | |
|
|
1188 | =item $pl->quit_character |
|
|
1189 | |
|
|
1190 | Nukes the player without looking back. If logged in, the connection will |
|
|
1191 | be destroyed. May block for a long time. |
|
|
1192 | |
|
|
1193 | =cut |
1161 | |
1194 | |
1162 | sub quit_character { |
1195 | sub quit_character { |
1163 | my ($pl) = @_; |
1196 | my ($pl) = @_; |
1164 | |
1197 | |
1165 | $pl->{deny_save} = 1; |
1198 | $pl->{deny_save} = 1; |
… | |
… | |
1170 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1203 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1171 | $pl->ns->destroy if $pl->ns; |
1204 | $pl->ns->destroy if $pl->ns; |
1172 | |
1205 | |
1173 | my $path = playerdir $pl; |
1206 | my $path = playerdir $pl; |
1174 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1207 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1175 | Coro::AIO::aio_rename $path, $temp; |
1208 | aio_rename $path, $temp; |
1176 | delete $cf::PLAYER{$pl->ob->name}; |
1209 | delete $cf::PLAYER{$pl->ob->name}; |
1177 | $pl->destroy; |
1210 | $pl->destroy; |
1178 | IO::AIO::aio_rmtree $temp; |
1211 | IO::AIO::aio_rmtree $temp; |
|
|
1212 | } |
|
|
1213 | |
|
|
1214 | =item cf::player::list_logins |
|
|
1215 | |
|
|
1216 | Returns am arrayref of all valid playernames in the system, can take a |
|
|
1217 | while and may block, so not sync_job-capable, ever. |
|
|
1218 | |
|
|
1219 | =cut |
|
|
1220 | |
|
|
1221 | sub list_logins { |
|
|
1222 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
|
|
1223 | or return []; |
|
|
1224 | |
|
|
1225 | my @logins; |
|
|
1226 | |
|
|
1227 | for my $login (@$dirs) { |
|
|
1228 | my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; |
|
|
1229 | aio_read $fh, 0, 512, my $buf, 0 or next; |
|
|
1230 | $buf !~ /^password -------------$/ or next; # official not-valid tag |
|
|
1231 | |
|
|
1232 | utf8::decode $login; |
|
|
1233 | push @logins, $login; |
|
|
1234 | } |
|
|
1235 | |
|
|
1236 | \@logins |
|
|
1237 | } |
|
|
1238 | |
|
|
1239 | =item $player->maps |
|
|
1240 | |
|
|
1241 | Returns an arrayref of cf::path's of all maps that are private for this |
|
|
1242 | player. May block. |
|
|
1243 | |
|
|
1244 | =cut |
|
|
1245 | |
|
|
1246 | sub maps($) { |
|
|
1247 | my ($pl) = @_; |
|
|
1248 | |
|
|
1249 | my $files = aio_readdir playerdir $pl |
|
|
1250 | or return; |
|
|
1251 | |
|
|
1252 | my @paths; |
|
|
1253 | |
|
|
1254 | for (@$files) { |
|
|
1255 | utf8::decode $_; |
|
|
1256 | next if /\.(?:pl|pst)$/; |
|
|
1257 | next unless /^$PATH_SEP/; |
|
|
1258 | |
|
|
1259 | s/$PATH_SEP/\//g; |
|
|
1260 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
|
|
1261 | } |
|
|
1262 | |
|
|
1263 | \@paths |
1179 | } |
1264 | } |
1180 | |
1265 | |
1181 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1266 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1182 | |
1267 | |
1183 | Sends an ext reply to the player. |
1268 | Sends an ext reply to the player. |
… | |
… | |
1671 | =cut |
1756 | =cut |
1672 | |
1757 | |
1673 | sub cf::object::player::goto { |
1758 | sub cf::object::player::goto { |
1674 | my ($self, $path, $x, $y) = @_; |
1759 | my ($self, $path, $x, $y) = @_; |
1675 | |
1760 | |
|
|
1761 | $path = new cf::path $path; |
|
|
1762 | $path ne "/" or Carp::cluck ("oy");#d# |
|
|
1763 | |
1676 | $self->enter_link; |
1764 | $self->enter_link; |
1677 | |
1765 | |
1678 | (async { |
1766 | (async { |
1679 | $path = new cf::path $path; |
|
|
1680 | |
|
|
1681 | my $map = cf::map::find $path->as_string; |
1767 | my $map = cf::map::find $path->as_string; |
1682 | $map = $map->customise_for ($self) if $map; |
1768 | $map = $map->customise_for ($self) if $map; |
1683 | |
1769 | |
1684 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1770 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1685 | # if $map; |
1771 | # if $map; |
1686 | |
1772 | |
1687 | $map or $map->cluck ("oy");#d# |
|
|
1688 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1773 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1689 | |
1774 | |
1690 | $self->leave_link ($map, $x, $y); |
1775 | $self->leave_link ($map, $x, $y); |
1691 | })->prio (1); |
1776 | })->prio (1); |
1692 | } |
1777 | } |