| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Phoebe::Ijirait - a Gemini-based MUSH running on Phoebe | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | The ijirait are red-eyed shape shifters, and a game one plays via the Gemini | ||
| 24 | protocol, and Ijiraq is also one of the moons of Saturn. | ||
| 25 | |||
| 26 | The Ijirait game is modelled along traditional MUSH games ("multi-user shared | ||
| 27 | hallucination"), that is: players have a character in the game world; the game | ||
| 28 | world consists of rooms; these rooms are connected to each other; if two | ||
| 29 | characters are in the same room, they see each other; if one of them says | ||
| 30 | something, the other hears it. | ||
| 31 | |||
| 32 | When you visit the URL using your Gemini browser, you're asked for a client | ||
| 33 | certificate. The common name of the certificate is the name of your character in | ||
| 34 | the game. | ||
| 35 | |||
| 36 | As the server doesn't know whether you're still active or not, it assumes a | ||
| 37 | 10min timout. If you were active in the last 10min, other people in the same | ||
| 38 | "room". Similarly, if you "say" something, whatever you said hangs on the room | ||
| 39 | description for up to 10min as long as your character is still in the room. | ||
| 40 | |||
| 41 | There is no configuration. Simply add it to your F<config> file: | ||
| 42 | |||
| 43 | use App::Phoebe::Ijirait; | ||
| 44 | |||
| 45 | By default, /play/ijirait on all hosts is the same game. | ||
| 46 | |||
| 47 | In a virtual host setup, this extension serves all the hosts. Here's how to | ||
| 48 | serve just one of them: | ||
| 49 | |||
| 50 | package App::Phoebe::Ijirait; | ||
| 51 | our $host = "campaignwiki.org"; | ||
| 52 | use App::Phoebe::Ijirait; | ||
| 53 | |||
| 54 | The help file, if you have one, is F<ijirait-help.gmi> in your wiki data | ||
| 55 | directory. Feel free to get a copy of | ||
| 56 | L<gemini://transjovian.org/ijiraq/page/Help>. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | package App::Phoebe::Ijirait; | ||
| 61 | use App::Phoebe qw(@extensions $log $server @request_handlers success result); | ||
| 62 | use Modern::Perl; | ||
| 63 | use Archive::Tar; | ||
| 64 | use Encode qw(encode_utf8 decode_utf8); | ||
| 65 | use File::Slurper qw(read_binary write_binary read_text); | ||
| 66 | use Mojo::JSON qw(decode_json encode_json); | ||
| 67 | use Mojo::Util qw(gzip); | ||
| 68 | use List::Util qw(first none any); | ||
| 69 | use URI::Escape; | ||
| 70 | use utf8; | ||
| 71 | |||
| 72 | # See "load world on startup" for the small world generated if no save file is | ||
| 73 | # available. | ||
| 74 | my $data; | ||
| 75 | |||
| 76 | # By default, /play/ijirait on all hosts is the same game. | ||
| 77 | our $host = App::Phoebe::host_regex(); | ||
| 78 | |||
| 79 | # Streamers are people connecting to /stream/ijirait. | ||
| 80 | my @streamers; | ||
| 81 | |||
| 82 | Mojo::IOLoop->next_tick(sub { | ||
| 83 | $log->info("Serving Ijirait on $host") }); | ||
| 84 | |||
| 85 | # global commands | ||
| 86 | our $commands = { | ||
| 87 | help => \&help, | ||
| 88 | look => \&look, | ||
| 89 | type => \&type, | ||
| 90 | save => \&save, | ||
| 91 | backup => \&backup, | ||
| 92 | export => \&export, | ||
| 93 | say => \&speak, # can't use say! | ||
| 94 | who => \&who, | ||
| 95 | go => \&go, | ||
| 96 | examine => \&examine, | ||
| 97 | describe => \&describe, | ||
| 98 | name => \&name, | ||
| 99 | create => \&create, | ||
| 100 | delete => \&delete, | ||
| 101 | rooms => \&rooms, | ||
| 102 | connect => \&connect, | ||
| 103 | emote => \&emote, | ||
| 104 | hide => \&hide, | ||
| 105 | reveal => \&reveal, | ||
| 106 | secrets => \&secrets, | ||
| 107 | home => \&home, | ||
| 108 | find => \&find, | ||
| 109 | id => \&id, | ||
| 110 | forget => \&forget, | ||
| 111 | }; | ||
| 112 | |||
| 113 | our $ijrait_commands_without_cert = { | ||
| 114 | who => \&who, | ||
| 115 | }; | ||
| 116 | |||
| 117 | # load world on startup | ||
| 118 | Mojo::IOLoop->next_tick(sub { | ||
| 119 | my $dir = $server->{wiki_dir}; | ||
| 120 | if (-f "$dir/ijirait.json") { | ||
| 121 | my $bytes = read_binary("$dir/ijirait.json"); | ||
| 122 | $data = decode_json $bytes; | ||
| 123 | } else { | ||
| 124 | init(); | ||
| 125 | } } ); | ||
| 126 | |||
| 127 | sub init { | ||
| 128 | my $next = 1; | ||
| 129 | $data = { | ||
| 130 | people => [ | ||
| 131 | { | ||
| 132 | id => $next++, # 1 | ||
| 133 | name => "Ijiraq", | ||
| 134 | description => "A shape-shifter with red eyes.", | ||
| 135 | fingerprint => "", | ||
| 136 | location => $next, # 2 | ||
| 137 | seen => [], | ||
| 138 | ts => time, | ||
| 139 | } ], | ||
| 140 | rooms => [ | ||
| 141 | { | ||
| 142 | id => $next++, # 2 | ||
| 143 | name => "The Tent", | ||
| 144 | description => "This is a large tent, illuminated by candles.", | ||
| 145 | exits => [ | ||
| 146 | { | ||
| 147 | id => $next++, # 3 | ||
| 148 | name => "An exit leads outside.", | ||
| 149 | direction => "out", | ||
| 150 | destination => $next, | ||
| 151 | } ], | ||
| 152 | things => [], | ||
| 153 | words => [ | ||
| 154 | { | ||
| 155 | text => "Welcome!", | ||
| 156 | by => 1, # Ijirait | ||
| 157 | ts => time, | ||
| 158 | } ], | ||
| 159 | }, | ||
| 160 | { | ||
| 161 | id => $next++, # 4 | ||
| 162 | name => "Outside The Tent", | ||
| 163 | description => "You’re standing in a rocky hollow, somewhat protected from the wind. There’s a large tent, here.", | ||
| 164 | exits => [ | ||
| 165 | { | ||
| 166 | id => $next++, # 5 | ||
| 167 | name => "A tent flap leads inside.", | ||
| 168 | direction => "tent", | ||
| 169 | destination => 2, # The Tent | ||
| 170 | } ], | ||
| 171 | things => [], | ||
| 172 | words => [], | ||
| 173 | } ] }; | ||
| 174 | $data->{next} = $next; | ||
| 175 | }; | ||
| 176 | |||
| 177 | # Save the world every half hour. | ||
| 178 | Mojo::IOLoop->recurring(1800 => \&save_world); | ||
| 179 | |||
| 180 | # Streaming needs a special handler because the stream never closes. | ||
| 181 | unshift(@request_handlers, "^gemini://(?:$host)(?:\\d+)?/play/ijirait/stream" => \&add_streamer); | ||
| 182 | |||
| 183 | sub add_streamer { | ||
| 184 | my $stream = shift; | ||
| 185 | my $data = shift; | ||
| 186 | $log->debug("Handle streaming request"); | ||
| 187 | $log->debug("Discarding " . length($data->{buffer}) . " bytes") | ||
| 188 | if $data->{buffer}; | ||
| 189 | my $url = $data->{request}; | ||
| 190 | my $port = App::Phoebe::port($stream); | ||
| 191 | if ($url =~ m!^(?:gemini:)?//($host)(?::$port)?/play/ijirait/stream$!) { | ||
| 192 | my $p = login($stream); | ||
| 193 | if ($p) { | ||
| 194 | # 1h timeout | ||
| 195 | $stream->timeout(3600); | ||
| 196 | # remove from channel members if an error happens | ||
| 197 | $stream->on(close => sub { my $stream = shift; logout($stream, $p, "Connection closed") }); | ||
| 198 | $stream->on(error => sub { my ($stream, $err) = @_; logout($stream, $p, $err) }); | ||
| 199 | push(@streamers, { stream => $stream, person => $p }); | ||
| 200 | success($stream); | ||
| 201 | $stream->write(encode_utf8 "# Streaming $p->{name}\n"); | ||
| 202 | $stream->write(encode_utf8 "Make sure you connect to game using a different client " | ||
| 203 | . "(with the same client certificate!) in order to play $p->{name}.\n"); | ||
| 204 | $stream->write(encode_utf8 "=> /play/ijirait Play $p->{name}."); | ||
| 205 | # don't close the stream! | ||
| 206 | } else { | ||
| 207 | $stream->close_gracefully(); | ||
| 208 | } | ||
| 209 | } else { | ||
| 210 | result($stream, "59", "Don't know how to handle $url"); | ||
| 211 | $stream->close_gracefully(); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | sub logout { | ||
| 216 | my ($stream, $p, $msg) = @_; | ||
| 217 | $log->debug("Disconnected $p->{name}: $msg"); | ||
| 218 | @streamers = grep { $_->{stream} ne $stream and $_->{person} ne $p } @streamers; | ||
| 219 | } | ||
| 220 | |||
| 221 | # run every minute and print a timestamp every 5 minutes | ||
| 222 | Mojo::IOLoop->recurring(60 => sub { | ||
| 223 | my $loop = shift; | ||
| 224 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); | ||
| 225 | return unless $min % 5 == 0; | ||
| 226 | return unless @streamers > 0; | ||
| 227 | $log->debug("Ijirait streamer ping"); | ||
| 228 | my $ts = sprintf("%02d:%02d UTC\n", $hour, $min); | ||
| 229 | for (@streamers) { | ||
| 230 | $_->{stream}->write($ts); | ||
| 231 | }}); | ||
| 232 | |||
| 233 | # notify every streamer in the same room | ||
| 234 | sub notify { | ||
| 235 | my ($p, $msg) = @_; | ||
| 236 | eval { | ||
| 237 | for my $s (grep { $_->{person}->{location} == $p->{location} } @streamers) { | ||
| 238 | my $stream = $s->{stream}; | ||
| 239 | next unless $stream; | ||
| 240 | $stream->write(encode_utf8 $msg); | ||
| 241 | $stream->write("\n"); | ||
| 242 | } | ||
| 243 | }; | ||
| 244 | $log->error("Error notifying people of '$msg': $@") if $@; | ||
| 245 | } | ||
| 246 | |||
| 247 | # main loop | ||
| 248 | push(@extensions, \&main); | ||
| 249 | |||
| 250 | sub main { | ||
| 251 | my $stream = shift; | ||
| 252 | my $url = shift; | ||
| 253 | my $port = App::Phoebe::port($stream); | ||
| 254 | if ($url =~ m!^gemini://(?:$host)(?::$port)?/play/ijirait\.tar\.gz$!) { | ||
| 255 | export_archive($stream); | ||
| 256 | } | ||
| 257 | elsif ($url =~ m!^gemini://(?:$host)(?::$port)?/play/ijirait(?:/([a-z]+))?(?:\?(.*))?!) { | ||
| 258 | my $command = ($1 || "look") . ($2 ? " " . decode_utf8 uri_unescape($2) : ""); | ||
| 259 | $log->debug("Handling $url - $command"); | ||
| 260 | # some commands require no client certificate (and no person argument!) | ||
| 261 | my $routine = $ijrait_commands_without_cert->{$command}; | ||
| 262 | if ($routine) { | ||
| 263 | $log->debug("Running $command"); | ||
| 264 | $routine->($stream); | ||
| 265 | return 1; | ||
| 266 | } | ||
| 267 | # regular commands | ||
| 268 | my $p = login($stream); | ||
| 269 | if ($p) { | ||
| 270 | type($stream, $p, $command); | ||
| 271 | } | ||
| 272 | return 1; | ||
| 273 | } | ||
| 274 | return 0; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub login { | ||
| 278 | my ($stream) = @_; | ||
| 279 | # you need a client certificate | ||
| 280 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 281 | if (!$fingerprint) { | ||
| 282 | $log->info("Requested client certificate"); | ||
| 283 | result($stream, "60", "You need a client certificate to play"); | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | # find the right person | ||
| 287 | my $p = first { $_->{fingerprint} eq $fingerprint} @{$data->{people}}; | ||
| 288 | if (!$p) { | ||
| 289 | # create a new person if we can't find one | ||
| 290 | $log->info("New client certificate $fingerprint"); | ||
| 291 | $p = new_person($fingerprint); | ||
| 292 | } else { | ||
| 293 | $log->info("Successfully identified client certificate: " . $p->{name}); | ||
| 294 | } | ||
| 295 | return $p; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub new_person { | ||
| 299 | my $fingerprint = shift; | ||
| 300 | my $p = { | ||
| 301 | id => $data->{next}++, | ||
| 302 | name => person_name(), | ||
| 303 | description => "A shape-shifter with red eyes.", | ||
| 304 | fingerprint => $fingerprint, | ||
| 305 | location => 2, # The Tent | ||
| 306 | seen => [], | ||
| 307 | ts => time, | ||
| 308 | }; | ||
| 309 | push(@{$data->{people}}, $p); | ||
| 310 | return $p; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub person_name { | ||
| 314 | my $digraphs = "..lexegezacebisousesarmaindire.aeratenberalavetiedorquanteisrion"; | ||
| 315 | my $max = length($digraphs); | ||
| 316 | my $length = 4 + rand(7); # 4-8 | ||
| 317 | my $name = ''; | ||
| 318 | while (length($name) < $length) { | ||
| 319 | $name .= substr($digraphs, 2*int(rand($max/2)), 2); | ||
| 320 | } | ||
| 321 | $name =~ s/\.//g; | ||
| 322 | return ucfirst $name; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub look { | ||
| 326 | my ($stream, $p) = @_; | ||
| 327 | success($stream); | ||
| 328 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 329 | $stream->write(encode_utf8 "# " . $room->{name} . "\n"); | ||
| 330 | $stream->write(encode_utf8 $room->{description} . "\n") if $room->{description}; | ||
| 331 | my @things = grep { my $thing = $_; | ||
| 332 | not $thing->{hidden} | ||
| 333 | or $thing->{seen} | ||
| 334 | and any { $_ eq $thing->{seen} } @{$p->{seen}}} @{$room->{things}}; | ||
| 335 | $stream->write("## Things\n") if @things > 0; | ||
| 336 | for my $thing (@things) { | ||
| 337 | my $name = uri_escape_utf8 $thing->{short}; | ||
| 338 | $stream->write(encode_utf8 "=> /play/ijirait/examine?$name $thing->{name} ($thing->{short})\n"); | ||
| 339 | } | ||
| 340 | my @exits = grep { my $exit = $_; | ||
| 341 | not $exit->{hidden} | ||
| 342 | or $exit->{seen} | ||
| 343 | and any { $_ eq $exit->{seen} } @{$p->{seen}} } @{$room->{exits}}; | ||
| 344 | $stream->write("## Exits\n") if @exits > 0; | ||
| 345 | for my $exit (@exits) { | ||
| 346 | my $direction = uri_escape_utf8 $exit->{direction}; | ||
| 347 | $stream->write(encode_utf8 "=> /play/ijirait/go?$direction $exit->{name} ($exit->{direction})\n"); | ||
| 348 | } | ||
| 349 | $stream->write("## People\n"); # there is always at least the observer! | ||
| 350 | my $n = 0; | ||
| 351 | my $now = time; | ||
| 352 | for my $o (@{$data->{people}}) { | ||
| 353 | next unless $o->{location} == $p->{location}; | ||
| 354 | next if $now - $o->{ts} > 600; # don't show people inactive for 10min or more | ||
| 355 | $n++; | ||
| 356 | my $name = uri_escape_utf8 $o->{name}; | ||
| 357 | if ($o->{id} == $p->{id}) { | ||
| 358 | $stream->write(encode_utf8 "=> /play/ijirait/examine?$name $o->{name} (you)\n"); | ||
| 359 | } else { | ||
| 360 | $stream->write(encode_utf8 "=> /play/ijirait/examine?$name $o->{name}\n"); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | my $title = 0; | ||
| 364 | for my $word (@{$room->{words}}) { | ||
| 365 | next if $now - $word->{ts} > 600; # don't show messages older than 10min | ||
| 366 | $stream->write("## Words\n") unless $title++; | ||
| 367 | if ($word->{by}) { | ||
| 368 | my $o = first { $_->{id} == $word->{by} } @{$data->{people}}; | ||
| 369 | $stream->write(encode_utf8 ucfirst timespan($now - $word->{ts}) | ||
| 370 | . ", " . $o->{name} . " said “" . $word->{text} . "”\n"); | ||
| 371 | } elsif ($word->{text}) { | ||
| 372 | # emotes | ||
| 373 | $stream->write(encode_utf8 $word->{text} . "\n"); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | menu($stream); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub export { | ||
| 380 | my $stream = shift; | ||
| 381 | result($stream, "31", "/play/ijirait.tar.gz"); # redirect in order to get a better filename | ||
| 382 | } | ||
| 383 | |||
| 384 | sub export_archive { | ||
| 385 | my $stream = shift; | ||
| 386 | success($stream, "application/gzip"); | ||
| 387 | my $tar = Archive::Tar->new; | ||
| 388 | my $bytes = "# Rooms\n"; | ||
| 389 | $bytes .= encode_utf8 "=> rooms/$_->{id}.gmi $_->{name}\n" for @{$data->{rooms}}; | ||
| 390 | $tar->add_data("ijirait/index.gmi", $bytes); | ||
| 391 | for my $room (@{$data->{rooms}}) { | ||
| 392 | $bytes = encode_utf8 "# " . $room->{name} . "\n"; | ||
| 393 | $bytes .= encode_utf8 $room->{description} . "\n" if $room->{description}; | ||
| 394 | my @things = @{$room->{things}}; | ||
| 395 | $bytes .= "## Things\n" if @things > 0; | ||
| 396 | for my $thing (@things) { | ||
| 397 | my $name = uri_escape_utf8 $thing->{short}; | ||
| 398 | $bytes .= encode_utf8 "=> ../things/$room->{id}/$name.gmi $thing->{name}\n"; | ||
| 399 | my $bytes2 = encode_utf8 "# $thing->{name}\n"; | ||
| 400 | $bytes2 .= encode_utf8 "$thing->{description}\n"; | ||
| 401 | $bytes2 .= "=> ../../rooms/$room->{id}.gmi Back\n"; | ||
| 402 | $tar->add_data("ijirait/things/$room->{id}/$name.gmi", $bytes2); | ||
| 403 | } | ||
| 404 | my @exits = @{$room->{exits}}; | ||
| 405 | $bytes .= "## Exits\n" if @exits > 0; | ||
| 406 | for my $exit (@exits) { | ||
| 407 | my $direction = uri_escape_utf8 $exit->{direction}; | ||
| 408 | my $destination = first { $_->{id} == $exit->{destination} } @{$data->{rooms}}; | ||
| 409 | $bytes .= encode_utf8 "=> $destination->{id}.gmi $exit->{name}\n"; | ||
| 410 | } | ||
| 411 | # We also print rooms without people! We also print inactive people. | ||
| 412 | my @people = grep { $_->{location} == $room->{id} } @{$data->{people}}; | ||
| 413 | $bytes .= "## People\n" if @people > 0; | ||
| 414 | for my $o (@people) { | ||
| 415 | $bytes .= encode_utf8 "=> ../people/$o->{id}.gmi $o->{name}\n"; | ||
| 416 | my $bytes2 = encode_utf8 "# $o->{name}\n"; | ||
| 417 | $bytes2 .= encode_utf8 "$o->{description}\n"; | ||
| 418 | $bytes2 .= encode_utf8 "=> ../rooms/$room->{id}.gmi $room->{name}\n"; | ||
| 419 | $tar->add_data("ijirait/people/$o->{id}.gmi", $bytes2); | ||
| 420 | } | ||
| 421 | $tar->add_data("ijirait/rooms/$room->{id}.gmi", $bytes); | ||
| 422 | } | ||
| 423 | my $io; | ||
| 424 | open(my $fh, ">", \$io) or $log->error("Error preparing string for tarball: $!"); | ||
| 425 | $tar->write($fh) or $log->error("Error writing tarball: " . $tar->error); | ||
| 426 | close($fh); | ||
| 427 | $stream->write(gzip $io); | ||
| 428 | } | ||
| 429 | |||
| 430 | sub timespan { | ||
| 431 | my $seconds = shift; | ||
| 432 | return "some time ago" if not defined $seconds; | ||
| 433 | return "just now" if $seconds == 0; | ||
| 434 | return sprintf("%d days ago", int($seconds/86400)) if abs($seconds) > 172800; # 2d | ||
| 435 | return sprintf("%d hours ago", int($seconds/3600)) if abs($seconds) > 7200; # 2h | ||
| 436 | return sprintf("%d minutes ago", int($seconds/60)) if abs($seconds) > 120; # 2min | ||
| 437 | return sprintf("%d seconds ago", $seconds); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub menu { | ||
| 441 | my $stream = shift; | ||
| 442 | $stream->write("## Commands\n"); | ||
| 443 | $stream->write("=> /play/ijirait/look look\n"); | ||
| 444 | $stream->write("=> /play/ijirait/say say\n"); | ||
| 445 | $stream->write("=> /play/ijirait/emote emote\n"); | ||
| 446 | $stream->write("=> /play/ijirait/help help\n"); | ||
| 447 | $stream->write("=> /play/ijirait/type type\n"); | ||
| 448 | } | ||
| 449 | |||
| 450 | sub help { | ||
| 451 | my ($stream, $p) = @_; | ||
| 452 | success($stream); | ||
| 453 | $stream->write("## Help\n"); | ||
| 454 | my $dir = $server->{wiki_dir}; | ||
| 455 | my $file = "$dir/ijirait-help.gmi"; | ||
| 456 | if (-f $file) { | ||
| 457 | $stream->write(encode_utf8 read_text($file)); | ||
| 458 | } else { | ||
| 459 | $stream->write("The help file does not exist.\n"); | ||
| 460 | } | ||
| 461 | $stream->write("## Automatically Generated Command List\n"); | ||
| 462 | for my $command (sort keys %$commands) { | ||
| 463 | $stream->write("* $command\n"); | ||
| 464 | } | ||
| 465 | $stream->write("=> /play/ijirait Back\n"); | ||
| 466 | } | ||
| 467 | |||
| 468 | sub type { | ||
| 469 | my ($stream, $p, $str) = @_; | ||
| 470 | if (!$str) { | ||
| 471 | result($stream, "10", "Type your command"); | ||
| 472 | return; | ||
| 473 | } | ||
| 474 | # mark activity | ||
| 475 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 476 | $p->{ts} = $room->{ts} = time; | ||
| 477 | # parse commands | ||
| 478 | my ($command, $arg) = split(/\s+/, $str, 2); | ||
| 479 | $arg =~ s/\s+$// if defined $arg; # trim | ||
| 480 | my $routine = $commands->{$command}; | ||
| 481 | if ($routine) { | ||
| 482 | $log->debug("Running $command"); | ||
| 483 | $routine->($stream, $p, $arg); | ||
| 484 | return; | ||
| 485 | } | ||
| 486 | # using exits instead of go | ||
| 487 | if (first { $_->{direction} eq $str } @{$room->{exits}}) { | ||
| 488 | go($stream, $p, $str); | ||
| 489 | return; | ||
| 490 | } | ||
| 491 | # using the name of a person or thing instead of examine | ||
| 492 | if (first { $_->{location} eq $p->{location} and $_->{name} eq $str } @{$data->{people}} | ||
| 493 | or first { $_->{short} eq $str } @{$room->{things}}) { | ||
| 494 | examine($stream, $p, $str); | ||
| 495 | return; | ||
| 496 | } | ||
| 497 | $log->debug("Unknown command '$command'"); | ||
| 498 | success($stream); | ||
| 499 | $stream->write("# Unknown command\n"); | ||
| 500 | $stream->write(encode_utf8 "“$command” is an unknown command.\n"); | ||
| 501 | menu($stream); | ||
| 502 | } | ||
| 503 | |||
| 504 | sub home { | ||
| 505 | my ($stream, $p) = @_; | ||
| 506 | $log->debug("Going home"); | ||
| 507 | notify($p, "$p->{name} turns to yellow mist and disappears."); | ||
| 508 | $p->{location} = 2; # The Tent | ||
| 509 | notify($p, "$p->{name} arrives."); | ||
| 510 | result($stream, "30", "/play/ijirait/look"); | ||
| 511 | } | ||
| 512 | |||
| 513 | sub go { | ||
| 514 | my ($stream, $p, $direction) = @_; | ||
| 515 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 516 | my $exit = first { $_->{direction} eq $direction } @{$room->{exits}}; | ||
| 517 | if ($exit) { | ||
| 518 | $log->debug("Taking the exit $direction"); | ||
| 519 | notify($p, "$p->{name} leaves ($direction)."); | ||
| 520 | $exit->{ts} = time; | ||
| 521 | $p->{location} = $exit->{destination}; | ||
| 522 | push(@{$p->{seen}}, $p->{location}) if none { $_ == $p->{location} } @{$p->{seen}}; | ||
| 523 | notify($p, "$p->{name} arrives."); | ||
| 524 | result($stream, "30", "/play/ijirait/look"); | ||
| 525 | } else { | ||
| 526 | success($stream); | ||
| 527 | $log->debug("Unknown exit '$direction'"); | ||
| 528 | $stream->write(encode_utf8 "# Unknown exit “$direction”\n"); | ||
| 529 | $stream->write("The exit does not exist.\n"); | ||
| 530 | $stream->write("=> /play/ijirait Back\n"); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | sub examine { | ||
| 535 | my ($stream, $p, $name) = @_; | ||
| 536 | success($stream); | ||
| 537 | my $o = first { $_->{location} eq $p->{location} and $_->{name} eq $name } @{$data->{people}}; | ||
| 538 | if ($o) { | ||
| 539 | $log->debug("Looking at $name"); | ||
| 540 | notify($p, "$p->{name} examines $o->{name}.") unless $p->{id} == $o->{id}; | ||
| 541 | $stream->write(encode_utf8 "# $o->{name}\n"); | ||
| 542 | $stream->write(encode_utf8 "$o->{description}\n"); | ||
| 543 | $stream->write("=> /play/ijirait Back\n"); | ||
| 544 | return; | ||
| 545 | } | ||
| 546 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 547 | my $thing = first { $_->{short} eq $name } @{$room->{things}}; | ||
| 548 | if ($thing) { | ||
| 549 | $log->debug("Looking at $name"); | ||
| 550 | notify($p, "$p->{name} examines $thing->{name}."); | ||
| 551 | $thing->{ts} = time; | ||
| 552 | push(@{$p->{seen}}, $thing->{id}) if none { $_ == $thing->{id} } @{$p->{seen}}; | ||
| 553 | $stream->write(encode_utf8 "# $thing->{name}\n"); | ||
| 554 | $stream->write(encode_utf8 "$thing->{description}\n"); | ||
| 555 | $stream->write("=> /play/ijirait Back\n"); | ||
| 556 | return; | ||
| 557 | } | ||
| 558 | $log->debug("Unknown target '$name'"); | ||
| 559 | $stream->write(encode_utf8 "# Unknown target “$name”\n"); | ||
| 560 | $stream->write("No such person or object is visible.\n"); | ||
| 561 | $stream->write("=> /play/ijirait Back\n"); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub speak { | ||
| 565 | my ($stream, $p, $text) = @_; | ||
| 566 | if ($text) { | ||
| 567 | $text =~ s/^["“„«]//; | ||
| 568 | $text =~ s/["”“»]$//; | ||
| 569 | $text =~ s/^\s+//; | ||
| 570 | $text =~ s/\s+$//; | ||
| 571 | } | ||
| 572 | if (not $text) { | ||
| 573 | result($stream, "10", "You say"); | ||
| 574 | return; | ||
| 575 | } | ||
| 576 | my $w = { | ||
| 577 | text => $text, | ||
| 578 | by => $p->{id}, | ||
| 579 | ts => time, | ||
| 580 | }; | ||
| 581 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 582 | push(@{$room->{words}}, $w); | ||
| 583 | notify($p, "$p->{name} says: “$text”"); | ||
| 584 | look($stream, $p); | ||
| 585 | } | ||
| 586 | |||
| 587 | sub save { | ||
| 588 | my ($stream, $p) = @_; | ||
| 589 | save_world(); | ||
| 590 | success($stream); | ||
| 591 | $stream->write("# World Save\n"); | ||
| 592 | $stream->write("Data was saved.\n"); | ||
| 593 | $stream->write("=> /play/ijirait Back\n"); | ||
| 594 | } | ||
| 595 | |||
| 596 | sub save_world { | ||
| 597 | cleanup(); | ||
| 598 | my $bytes = encode_json $data; | ||
| 599 | my $dir = $server->{wiki_dir}; | ||
| 600 | write_binary("$dir/ijirait.json", $bytes); | ||
| 601 | } | ||
| 602 | |||
| 603 | sub cleanup() { | ||
| 604 | my $now = time; | ||
| 605 | my %people = map { $_->{location} => 1 } @{$data->{people}}; | ||
| 606 | for my $room (@{$data->{rooms}}) { | ||
| 607 | my @words; | ||
| 608 | for my $word (@{$room->{words}}) { | ||
| 609 | next if $now - $word->{ts} > 600; # don't show messages older than 10min | ||
| 610 | push(@words, $word); | ||
| 611 | } | ||
| 612 | $room->{words} = \@words; | ||
| 613 | } | ||
| 614 | } | ||
| 615 | |||
| 616 | sub backup() { | ||
| 617 | my $stream = shift; | ||
| 618 | my $bytes = encode_json $data; | ||
| 619 | $bytes =~ s/"fingerprint":"[^"]+"/"fingerprint":""/g; | ||
| 620 | success($stream, "application/json+gzip"); | ||
| 621 | $stream->write(gzip $bytes); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub who { | ||
| 625 | my ($stream) = @_; | ||
| 626 | my $now = time; | ||
| 627 | success($stream); | ||
| 628 | $stream->write("# Who are the shape shifters?\n"); | ||
| 629 | for my $o (sort { $b->{ts} <=> $a->{ts} } @{$data->{people}}) { | ||
| 630 | $o->{name} //= ""; | ||
| 631 | $stream->write(encode_utf8 "* $o->{name}, active " . timespan($now - $o->{ts}) . "\n"); | ||
| 632 | } | ||
| 633 | $stream->write("=> /play/ijirait Back\n"); | ||
| 634 | } | ||
| 635 | |||
| 636 | sub describe { | ||
| 637 | my ($stream, $p, $text) = @_; | ||
| 638 | if ($text) { | ||
| 639 | my ($obj, $description) = split(/\s+/, $text, 2); | ||
| 640 | if ($obj eq "me") { | ||
| 641 | $log->debug("Describing $p->{name}"); | ||
| 642 | notify($p, "$p->{name} changes appearance."); | ||
| 643 | $p->{description} = $description; | ||
| 644 | my $name = uri_escape_utf8 $p->{name}; | ||
| 645 | result($stream, "30", "/play/ijirait/examine?$name"); | ||
| 646 | return; | ||
| 647 | } | ||
| 648 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 649 | if ($obj eq "room") { | ||
| 650 | $log->debug("Describing $room->{name}"); | ||
| 651 | notify($p, "$p->{name} changes the room’s description."); | ||
| 652 | $room->{description} = $description; | ||
| 653 | result($stream, "30", "/play/ijirait/look"); | ||
| 654 | return; | ||
| 655 | } | ||
| 656 | my $thing = first { $_->{short} eq $obj } @{$room->{things}}; | ||
| 657 | if ($thing) { | ||
| 658 | $log->debug("Describe $thing->{name}"); | ||
| 659 | notify($p, "$p->{name} changes the description of $thing->{name}."); | ||
| 660 | $thing->{description} = $description; | ||
| 661 | my $name = uri_escape_utf8 $thing->{short}; | ||
| 662 | result($stream, "30", "/play/ijirait/examine?$name"); | ||
| 663 | return; | ||
| 664 | } | ||
| 665 | # No description of exits. | ||
| 666 | } | ||
| 667 | success($stream); | ||
| 668 | $log->debug("Describing unknown object"); | ||
| 669 | $stream->write(encode_utf8 "# I don’t know what to describe\n"); | ||
| 670 | $stream->write(encode_utf8 "The description needs to start with what to describe, e.g. “describe me A shape-shifter with red eyes.”\n"); | ||
| 671 | $stream->write(encode_utf8 "You can describe yourself (“me”), the room you are in (“room”), or a thing (using its shortcut). You cannot describe exits.\n"); | ||
| 672 | $stream->write("=> /play/ijirait Back\n"); | ||
| 673 | } | ||
| 674 | |||
| 675 | sub name { | ||
| 676 | my ($stream, $p, $text) = @_; | ||
| 677 | if ($text) { | ||
| 678 | my ($obj, $name) = split(/\s+/, $text, 2); | ||
| 679 | if ($obj eq "me" and $name !~ /\s/) { | ||
| 680 | $log->debug("Name $p->{name}"); | ||
| 681 | notify($p, "$p->{name} changes their name to $name."); | ||
| 682 | $p->{name} = $name; | ||
| 683 | my $nm = uri_escape_utf8 $p->{name}; | ||
| 684 | result($stream, "30", "/play/ijirait/examine?$nm"); | ||
| 685 | return; | ||
| 686 | } elsif ($obj eq "room") { | ||
| 687 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 688 | $log->debug("Name $room->{name}"); | ||
| 689 | notify($p, "$p->{name} changes the room’s name to $name."); | ||
| 690 | $room->{name} = $name; | ||
| 691 | result($stream, "30", "/play/ijirait/look"); | ||
| 692 | return; | ||
| 693 | } else { | ||
| 694 | my $short; | ||
| 695 | if ($name =~ /(^.*) \((\w+)\)$/) { | ||
| 696 | $name = $1; | ||
| 697 | $short = $2; | ||
| 698 | } | ||
| 699 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 700 | my $exit = first { $_->{direction} eq $obj } @{$room->{exits}}; | ||
| 701 | if ($exit) { | ||
| 702 | $log->debug("Name $exit->{name}"); | ||
| 703 | notify($p, "$p->{name} renames $exit->{direction} to $name ($short)."); | ||
| 704 | $exit->{name} = $name; | ||
| 705 | $exit->{direction} = $short if $short; | ||
| 706 | result($stream, "30", "/play/ijirait/look"); | ||
| 707 | return; | ||
| 708 | } | ||
| 709 | my $thing = first { $_->{short} eq $obj } @{$room->{things}}; | ||
| 710 | if ($thing) { | ||
| 711 | $log->debug("Name $thing->{short}"); | ||
| 712 | notify($p, "$p->{name} renames $thing->{name} to $name ($short)."); | ||
| 713 | $thing->{name} = $name; | ||
| 714 | $thing->{short} = $short if $short; | ||
| 715 | result($stream, "30", "/play/ijirait/look"); | ||
| 716 | return; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | } | ||
| 720 | success($stream); | ||
| 721 | $log->debug("Naming unknown object"); | ||
| 722 | $stream->write(encode_utf8 "# I don’t know what to name\n"); | ||
| 723 | $stream->write(encode_utf8 "The command needs to start with what to name, e.g. “name me Sogeeran.”\n"); | ||
| 724 | $stream->write("=> /play/ijirait Back\n"); | ||
| 725 | } | ||
| 726 | |||
| 727 | sub create { | ||
| 728 | my ($stream, $p, $obj) = @_; | ||
| 729 | if ($obj eq "room") { | ||
| 730 | $log->debug("Create room"); | ||
| 731 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 732 | my $dest = new_room(); | ||
| 733 | my $exit = new_exit($room, $dest, $p); | ||
| 734 | new_exit($dest, $room, $p); | ||
| 735 | notify($p, "$p->{name} creates a new room."); | ||
| 736 | result($stream, "30", "/play/ijirait"); | ||
| 737 | } elsif ($obj eq "thing") { | ||
| 738 | $log->debug("Create thing"); | ||
| 739 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 740 | new_thing($room, $p); | ||
| 741 | notify($p, "$p->{name} creates a new thing."); | ||
| 742 | result($stream, "30", "/play/ijirait"); | ||
| 743 | } else { | ||
| 744 | success($stream); | ||
| 745 | $log->debug("Cannot create '$obj'"); | ||
| 746 | $stream->write(encode_utf8 "# Cannot create new “$obj”\n"); | ||
| 747 | $stream->write(encode_utf8 "Currently, all you can create is a room, or a thing: “create room” or “create thing”.\n"); | ||
| 748 | $stream->write(encode_utf8 "Use the “name” and “describe” commands to customize it.\n"); | ||
| 749 | $stream->write("=> /play/ijirait Back\n"); | ||
| 750 | } | ||
| 751 | } | ||
| 752 | |||
| 753 | sub new_room { | ||
| 754 | my $r = { | ||
| 755 | id => $data->{next}++, | ||
| 756 | name => "Lost in fog", | ||
| 757 | description => "Dense fog surrounds you. Nothing can be discerned in this gloom.", | ||
| 758 | things => [], | ||
| 759 | exits => [], | ||
| 760 | ts => time, | ||
| 761 | }; | ||
| 762 | push(@{$data->{rooms}}, $r); | ||
| 763 | return $r; | ||
| 764 | } | ||
| 765 | |||
| 766 | sub new_exit { | ||
| 767 | # $from and $to are rooms | ||
| 768 | my ($from, $to, $owner) = @_; | ||
| 769 | my $e = { | ||
| 770 | id => $data->{next}++, | ||
| 771 | name => "A tunnel", | ||
| 772 | direction => "tunnel", | ||
| 773 | destination => $to->{id}, | ||
| 774 | owner => $owner->{id}, | ||
| 775 | ts => time, | ||
| 776 | }; | ||
| 777 | push(@{$from->{exits}}, $e); | ||
| 778 | return $e; | ||
| 779 | } | ||
| 780 | |||
| 781 | sub new_thing { | ||
| 782 | my ($room, $owner) = @_; | ||
| 783 | my $t = { | ||
| 784 | id => $data->{next}++, | ||
| 785 | short => "stone", | ||
| 786 | name => "A small stone", | ||
| 787 | description => "It’s round.", | ||
| 788 | owner => $owner->{id}, | ||
| 789 | ts => time, | ||
| 790 | }; | ||
| 791 | push(@{$room->{things}}, $t); | ||
| 792 | return $t; | ||
| 793 | } | ||
| 794 | |||
| 795 | sub delete { | ||
| 796 | my ($stream, $p, $str) = @_; | ||
| 797 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 798 | # try to delete an exit | ||
| 799 | my $exit = first { $_->{direction} eq $str } @{$room->{exits}}; | ||
| 800 | if ($exit) { | ||
| 801 | $log->debug("Delete '$str'"); | ||
| 802 | @{$room->{exits}} = grep { $_->{direction} ne $str } @{$room->{exits}}; | ||
| 803 | notify($p, "$p->{name} deletes $exit->{name}"); | ||
| 804 | result($stream, "30", "/play/ijirait"); | ||
| 805 | return; | ||
| 806 | } | ||
| 807 | # try to delete a thing | ||
| 808 | my $thing = first { $_->{short} eq $str } @{$room->{things}}; | ||
| 809 | if ($thing) { | ||
| 810 | $log->debug("Delete '$str'"); | ||
| 811 | @{$room->{things}} = grep { $_->{short} ne $str } @{$room->{things}}; | ||
| 812 | notify($p, "$p->{name} deletes $thing->{name}"); | ||
| 813 | result($stream, "30", "/play/ijirait"); | ||
| 814 | return; | ||
| 815 | } | ||
| 816 | $log->debug("Cannot delete '$str'"); | ||
| 817 | success($stream); | ||
| 818 | $stream->write(encode_utf8 "# Cannot delete “$str”\n"); | ||
| 819 | $stream->write(encode_utf8 "Only things and exits can be deleted.\n"); | ||
| 820 | menu($stream); | ||
| 821 | } | ||
| 822 | |||
| 823 | sub rooms { | ||
| 824 | my ($stream, $p, $option) = @_; | ||
| 825 | $option //= ""; | ||
| 826 | $log->debug("Listing all rooms"); | ||
| 827 | success($stream); | ||
| 828 | $stream->write("# Rooms\n"); | ||
| 829 | $stream->write("=> /play/ijirait/rooms?ghosts Rooms with ghosts (rooms ghosts)\n") unless $option eq "ghosts"; | ||
| 830 | my %location; | ||
| 831 | my $now = time; | ||
| 832 | foreach my $o (@{$data->{people}}) { | ||
| 833 | # mark active people | ||
| 834 | push(@{$location{$o->{location}}}, $o->{name} . ($now - $o->{ts} < 600 ? "*" : "")) | ||
| 835 | } | ||
| 836 | for my $room (sort { ($b->{ts}||0) <=> ($a->{ts}||0) } @{$data->{rooms}}) { | ||
| 837 | $stream->write(encode_utf8 "* $room->{name}"); | ||
| 838 | $stream->write(", last activity " . timespan($now - $room->{ts})) if $room->{ts}; | ||
| 839 | $stream->write(" (" . join(", ", @{$location{$room->{id}}}) . ")") if $option eq "ghosts" and $location{$room->{id}}; | ||
| 840 | $stream->write("\n"); | ||
| 841 | } | ||
| 842 | $stream->write("=> /play/ijirait Back\n"); | ||
| 843 | } | ||
| 844 | |||
| 845 | sub connect { | ||
| 846 | my ($stream, $p, $name) = @_; | ||
| 847 | if ($name) { | ||
| 848 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 849 | my $dest = first { $_->{name} eq $name } @{$data->{rooms}}; | ||
| 850 | if ($dest) { | ||
| 851 | $log->debug("Connecting $name"); | ||
| 852 | new_exit($room, $dest, $p); | ||
| 853 | new_exit($dest, $room, $p); | ||
| 854 | notify($p, "$p->{name} creates an exit to $dest->{name}."); | ||
| 855 | result($stream, "30", "/play/ijirait"); | ||
| 856 | return; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | success($stream); | ||
| 860 | $log->debug("Cannot connect '$name'"); | ||
| 861 | $stream->write(encode_utf8 "# Cannot connect “$name”\n"); | ||
| 862 | $stream->write(encode_utf8 "You need to provide the name of an existing room: “connect <room>”.\n"); | ||
| 863 | $stream->write(encode_utf8 "You can get a list of all existing rooms using “rooms”.\n"); | ||
| 864 | $stream->write("=> /play/ijirait Back\n"); | ||
| 865 | } | ||
| 866 | |||
| 867 | sub emote { | ||
| 868 | my ($stream, $p, $text) = @_; | ||
| 869 | if (not $text) { | ||
| 870 | result($stream, "10", "What happens"); | ||
| 871 | return; | ||
| 872 | } | ||
| 873 | my $w = { | ||
| 874 | text => $text, | ||
| 875 | author => $p->{id}, | ||
| 876 | ts => time, | ||
| 877 | }; | ||
| 878 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 879 | push(@{$room->{words}}, $w); | ||
| 880 | notify($p, $text); | ||
| 881 | look($stream, $p); | ||
| 882 | } | ||
| 883 | |||
| 884 | sub hide { | ||
| 885 | my ($stream, $p, $obj) = @_; | ||
| 886 | if ($obj) { | ||
| 887 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 888 | my $thing = first { $_->{short} eq $obj } @{$room->{things}}; | ||
| 889 | if ($thing) { | ||
| 890 | $log->debug("Hide '$obj'"); | ||
| 891 | notify($p, "$p->{name} hides $thing->{name}."); | ||
| 892 | $thing->{hidden} = 1; | ||
| 893 | result($stream, "30", "/play/ijirait/look"); | ||
| 894 | return; | ||
| 895 | } | ||
| 896 | my $exit = first { $_->{direction} eq $obj } @{$room->{exits}}; | ||
| 897 | if ($exit) { | ||
| 898 | $log->debug("Hide '$obj'"); | ||
| 899 | notify($p, "$p->{name} hides $exit->{name}."); | ||
| 900 | $exit->{hidden} = 1; | ||
| 901 | result($stream, "30", "/play/ijirait"); | ||
| 902 | return; | ||
| 903 | } | ||
| 904 | } | ||
| 905 | success($stream); | ||
| 906 | $log->debug("Hiding unknown object"); | ||
| 907 | $stream->write(encode_utf8 "# I don’t know what to hide\n"); | ||
| 908 | $stream->write(encode_utf8 "The command needs to use the shortcut of a thing, e.g. “hide stone”\n"); | ||
| 909 | $stream->write("=> /play/ijirait Back\n"); | ||
| 910 | } | ||
| 911 | |||
| 912 | sub reveal { | ||
| 913 | my ($stream, $p, $str) = @_; | ||
| 914 | my ($obj, $cond, $id) = split(/\s+/, $str); | ||
| 915 | if ($cond and $cond ne "for") { | ||
| 916 | success($stream); | ||
| 917 | $log->debug("Revealing object with unknown condition"); | ||
| 918 | $stream->write(encode_utf8 "# I don’t know how to reveal “$cond” something\n"); | ||
| 919 | $stream->write(encode_utf8 "The command needs to use the shortcut of a hidden thing, e.g. “reveal bird”.\n"); | ||
| 920 | $stream->write(encode_utf8 "You can add an option to that, by using “for” and a number." | ||
| 921 | . " The number must be the id of a thing or a room, e.g. “reveal bird for 123”." | ||
| 922 | . " Then the thing or exit you’re naming is revelead only if the character has" | ||
| 923 | . " examined the thing or been to the room with that id." | ||
| 924 | . " Use the “id” to show the id of things and of the room you’re in.\n"); | ||
| 925 | $stream->write("=> /play/ijirait Back\n"); | ||
| 926 | return; | ||
| 927 | } | ||
| 928 | if ($id and none { $_->{id} == $id } map { $_, @{$_->{things}} } @{$data->{rooms}}) { | ||
| 929 | success($stream); | ||
| 930 | $log->debug("Revealing object with impossible condition"); | ||
| 931 | $stream->write(encode_utf8 "# I don’t know how to reveal that\n"); | ||
| 932 | $stream->write(encode_utf8 "$id does not refer to a known room or thing.\n"); | ||
| 933 | $stream->write("=> /play/ijirait Back\n"); | ||
| 934 | return; | ||
| 935 | } | ||
| 936 | if ($obj) { | ||
| 937 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 938 | my $thing = first { $_->{short} eq $obj } @{$room->{things}}; | ||
| 939 | if ($thing) { | ||
| 940 | if ($thing->{hidden}) { | ||
| 941 | $log->debug("Reveal '$obj'" . ($id ? " for $id" : "")); | ||
| 942 | notify($p, "$p->{name} reveals $thing->{name}." . ($id ? " Maybe." : "")); | ||
| 943 | if ($id) { | ||
| 944 | $thing->{seen} = $id; | ||
| 945 | } else { | ||
| 946 | delete $thing->{hidden}; | ||
| 947 | delete $thing->{seen}; | ||
| 948 | } | ||
| 949 | } | ||
| 950 | result($stream, "30", "/play/ijirait/look"); | ||
| 951 | return; | ||
| 952 | } | ||
| 953 | my $exit = first { $_->{direction} eq $obj } @{$room->{exits}}; | ||
| 954 | if ($exit) { | ||
| 955 | if ($exit->{hidden}) { | ||
| 956 | $log->debug("Reveal '$obj'" . ($id ? " for $id" : "")); | ||
| 957 | notify($p, "$p->{name} reveals $exit->{name}." . ($id ? " Maybe." : "")); | ||
| 958 | if ($id) { | ||
| 959 | $exit->{seen} = $id; | ||
| 960 | } else { | ||
| 961 | delete $exit->{hidden}; | ||
| 962 | delete $exit->{seen}; | ||
| 963 | } | ||
| 964 | } | ||
| 965 | result($stream, "30", "/play/ijirait"); | ||
| 966 | return; | ||
| 967 | } | ||
| 968 | } | ||
| 969 | success($stream); | ||
| 970 | $log->debug("Revealing unknown object"); | ||
| 971 | $stream->write(encode_utf8 "# I don’t know what to reveal\n"); | ||
| 972 | $stream->write(encode_utf8 "The command needs to use the shortcut of a hidden thing, e.g. “reveal bird”.\n"); | ||
| 973 | $stream->write(encode_utf8 "To list all the hidden things: “secrets”.\n"); | ||
| 974 | $stream->write("=> /play/ijirait Back\n"); | ||
| 975 | } | ||
| 976 | |||
| 977 | sub secrets { | ||
| 978 | my ($stream, $p, $phrase) = @_; | ||
| 979 | if ($phrase and $phrase eq "are something I do not care for!") { | ||
| 980 | $log->debug("Secrets"); | ||
| 981 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 982 | my @things = grep { $_->{hidden} } @{$room->{things}}; | ||
| 983 | my @exits = grep { $_->{hidden} } @{$room->{exits}}; | ||
| 984 | if (@things > 0) { | ||
| 985 | $stream->write("## Hidden Things\n"); | ||
| 986 | for my $thing (@things) { | ||
| 987 | my $name = uri_escape_utf8 $thing->{short}; | ||
| 988 | $stream->write(encode_utf8 "=> /play/ijirait/examine?$name $thing->{name} ($thing->{short})\n"); | ||
| 989 | } | ||
| 990 | } | ||
| 991 | if (@exits > 0) { | ||
| 992 | $stream->write("## Hidden Exits\n"); | ||
| 993 | for my $exit (@exits) { | ||
| 994 | my $direction = uri_escape_utf8 $exit->{direction}; | ||
| 995 | $stream->write(encode_utf8 "=> /play/ijirait/go?$direction $exit->{name} ($exit->{direction})\n"); | ||
| 996 | } | ||
| 997 | } | ||
| 998 | if (@things + @exits == 0) { | ||
| 999 | $stream->write("## No secrets\n"); | ||
| 1000 | $stream->write("There are neither hidden things nor hidden exists, here\n"); | ||
| 1001 | } | ||
| 1002 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1003 | return; | ||
| 1004 | } | ||
| 1005 | success($stream); | ||
| 1006 | $log->debug("Secrets without a passphrase"); | ||
| 1007 | $stream->write(encode_utf8 "# Secrets\n"); | ||
| 1008 | $stream->write(encode_utf8 "Are you sure you want all the secrets to be revealed? If you are, please use the full command: “secrets are something I do not care for!”\n"); | ||
| 1009 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | sub find { | ||
| 1013 | my ($stream, $p, $name) = @_; | ||
| 1014 | if (not $name) { | ||
| 1015 | success($stream); | ||
| 1016 | $log->debug("Missing a name in route finding"); | ||
| 1017 | $stream->write(encode_utf8 "# Missing a name\n"); | ||
| 1018 | $stream->write(encode_utf8 "You need to provide the name of an existing person: “find <name>”.\n"); | ||
| 1019 | $stream->write(encode_utf8 "You can get a list of all existing persons using “who”.\n"); | ||
| 1020 | $stream->write("=> /play/ijirait/who Who\n"); | ||
| 1021 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1022 | return; | ||
| 1023 | } | ||
| 1024 | my $to; | ||
| 1025 | my $o = first { $_->{name} eq $name } @{$data->{people}}; | ||
| 1026 | if ($o) { | ||
| 1027 | $to = $o->{location}; | ||
| 1028 | } else { | ||
| 1029 | my $room = first { $_->{name} eq $name } @{$data->{rooms}}; | ||
| 1030 | $to = $room->{id} if $room; | ||
| 1031 | } | ||
| 1032 | if (not $to) { | ||
| 1033 | success($stream); | ||
| 1034 | $log->debug("Cannot find '$name'"); | ||
| 1035 | $stream->write(encode_utf8 "# Cannot find “$name”\n"); | ||
| 1036 | $stream->write(encode_utf8 "You need to provide the name of an existing room or person: “find <name>”.\n"); | ||
| 1037 | $stream->write(encode_utf8 "You can get a list of all existing rooms using “rooms”.\n"); | ||
| 1038 | $stream->write(encode_utf8 "You can get a list of all existing persons using “who”.\n"); | ||
| 1039 | $stream->write("=> /play/ijirait/rooms Rooms\n"); | ||
| 1040 | $stream->write("=> /play/ijirait/who Who\n"); | ||
| 1041 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1042 | return; | ||
| 1043 | } | ||
| 1044 | my $route = find_route($p->{location}, $to); | ||
| 1045 | if (not @$route) { | ||
| 1046 | success($stream); | ||
| 1047 | $log->debug("Cannot find a route to '$name'"); | ||
| 1048 | $stream->write(encode_utf8 "# Cannot find a way\n"); | ||
| 1049 | $stream->write(encode_utf8 "There seems to be no way to get from here to $name.\n"); | ||
| 1050 | $stream->write(encode_utf8 "One of you must use the “connect” command to connect back to the rest of the game.\n"); | ||
| 1051 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1052 | return; | ||
| 1053 | } | ||
| 1054 | success($stream); | ||
| 1055 | $log->debug("Find '$name'"); | ||
| 1056 | $stream->write(encode_utf8 "# How to find $name\n"); | ||
| 1057 | my $room = uri_escape_utf8 $route->[0]->{direction}; | ||
| 1058 | $stream->write(encode_utf8 "=> /play/ijirait/$room $route->[0]->{name} ($route->[0]->{direction})\n"); | ||
| 1059 | for (1 .. $#$route) { | ||
| 1060 | $stream->write(encode_utf8 "* $route->[$_]->{name} ($route->[$_]->{direction})\n"); | ||
| 1061 | } | ||
| 1062 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | sub find_route { | ||
| 1066 | my ($from, $to) = @_; | ||
| 1067 | my %rooms = map { $_->{id} => $_ } @{$data->{rooms}}; | ||
| 1068 | # breadth first! | ||
| 1069 | my @routes = map { [ $_ ] } @{$rooms{$from}->{exits}}; | ||
| 1070 | my $route; | ||
| 1071 | while ($route = shift(@routes)) { | ||
| 1072 | my $last = $route->[$#$route]; | ||
| 1073 | return $route if $last->{destination} == $to; | ||
| 1074 | for my $exit (@{$rooms{$last->{destination}}->{exits}}) { | ||
| 1075 | push(@routes, [@$route, $exit]) if none { $exit == $_ } @$route; | ||
| 1076 | } | ||
| 1077 | } | ||
| 1078 | return []; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | sub id { | ||
| 1082 | my ($stream, $p, $obj) = @_; | ||
| 1083 | success($stream); | ||
| 1084 | if ($obj) { | ||
| 1085 | my $room = first { $_->{id} == $p->{location} } @{$data->{rooms}}; | ||
| 1086 | if ($obj eq "room") { | ||
| 1087 | $log->debug("Id $obj"); | ||
| 1088 | $stream->write($room->{id} . "\n"); | ||
| 1089 | return; | ||
| 1090 | } | ||
| 1091 | my $thing = first { $_->{short} eq $obj } @{$room->{things}}; | ||
| 1092 | if ($thing) { | ||
| 1093 | $log->debug("Id '$obj'"); | ||
| 1094 | $stream->write($thing->{id} . "\n"); | ||
| 1095 | return; | ||
| 1096 | } | ||
| 1097 | } | ||
| 1098 | $log->debug("Id unknown thing"); | ||
| 1099 | $stream->write(encode_utf8 "# I don’t know what to id\n"); | ||
| 1100 | $stream->write(encode_utf8 "The command needs to use the shortcut of a thing, e.g. “id bird”, or just “id room”.\n"); | ||
| 1101 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | sub forget { | ||
| 1105 | my ($stream, $p, $id) = @_; | ||
| 1106 | $log->debug("Forget"); | ||
| 1107 | success($stream); | ||
| 1108 | my %seen = map { $_->{id} => $_->{name}, map { $_->{id} => $_->{name} } @{$_->{things}} } @{$data->{rooms}}; | ||
| 1109 | if ($id and $seen{$id}) { | ||
| 1110 | $stream->write("# Forgetting\n"); | ||
| 1111 | $stream->write(encode_utf8 "You forget ever having seen: $seen{$id} ($id)\n"); | ||
| 1112 | $p->{seen} = [grep { $_ != $id } @{$p->{seen}}]; | ||
| 1113 | $stream->write("=> /play/ijirait/forget List all the other things you could forget\n"); | ||
| 1114 | } elsif ($id) { | ||
| 1115 | $stream->write("# Forgetting the impossible\n"); | ||
| 1116 | $stream->write(encode_utf8 "You can’t forget what you haven’t seen ($id).\n"); | ||
| 1117 | } else { | ||
| 1118 | $stream->write(encode_utf8 "# Things you might want to forget:\n"); | ||
| 1119 | for my $id (@{$p->{seen}}) { | ||
| 1120 | $stream->write(encode_utf8 "=> /play/ijirait/forget?$id $seen{$id} ($id)\n"); | ||
| 1121 | } | ||
| 1122 | } | ||
| 1123 | $stream->write("=> /play/ijirait Back\n"); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | 1; |