Game-HexDescribe
view release on metacpan or search on metacpan
lib/Game/HexDescribe/Utils.pm view on Meta::CPAN
#!/usr/bin/env perl
# Copyright (C) 2018â2022 Alex Schroeder <alex@gnu.org>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
Game::HexDescribe::Utils - utilities to use the Hex Describe data
=head1 DESCRIPTION
L<Hex::Describe> is a web application which uses recursive random tables to
create the description of a map. This package contains the functions used to
access the information outside the web application framework.
=cut
package Game::HexDescribe::Utils;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(init markdown describe_text list_tables parse_table load_table
describe_map parse_map load_map);
use Text::Autoformat;
use Game::HexDescribe::Log;
use Modern::Perl;
use Mojo::URL;
use Mojo::File;
use List::Util qw(shuffle sum);
use Array::Utils qw(intersect);
use Encode qw(decode_utf8);
use utf8;
my $log = Game::HexDescribe::Log->get;
our $face_generator_url;
our $text_mapper_url;
=item list_tables($dir)
This function returns the table names in $dir. These are based on the following
filename convention: "$dir/hex-describe-$name-table.txt".
=cut
sub list_tables {
my $dir = shift;
$log->debug("Looking for maps in the contrib directory: $dir");
my @names = map { $_->basename('.txt') } Mojo::File->new($dir)->list->each;
return grep { $_ } map { $1 if /^hex-describe-(.*)-table$/ } @names;
}
=item load_table($name, $dir)
This function returns the unparsed table from the filename
"$dir/hex-describe-$name-table.txt".
=cut
sub load_table {
my ($name, $dir) = @_;
$log->debug("Looking for table '$name' in the contrib directory '$dir'");
my $file = Mojo::File->new("$dir/hex-describe-$name-table.txt");
return decode_utf8($file->slurp) if -e $file;
return '';
}
=item load_map($name, $dir)
This function returns the unparsed map from the filename
"$dir/hex-describe-$name-map.txt".
=cut
sub load_map {
my ($name, $dir) = @_;
$log->debug("Looking for map in the contrib directory: $dir");
my $file = Mojo::File->new("$dir/hex-describe-$name-map.txt");
return decode_utf8($file->slurp) if -e $file;
}
=item parse_table
This parses the random tables. This is also where *bold* gets translated to
HTML. We also do some very basic checking of references. If we refer to another
table in square brackets we check whether we've seen such a table.
( run in 1.178 second using v1.01-cache-2.11-cpan-fe3c2283af0 )