blib/lib/Game/TextMapper/Command/random.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 60 CC 7 SCAR 35.1
CC
7
Cov
0%
run
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 33.5
1 # Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org>
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Affero General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Affero General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 =head1 NAME
17  
18 Game::TextMapper::Command::random
19  
20 =head1 SYNOPSIS
21  
22 text-mapper random [algorithm] [options]
23 text-mapper random help
24  
25 =head1 DESCRIPTION
26  
27 This prints a random map description to STDOUT.
28  
29 text-mapper random | text-mapper render > map.svg
30  
31 =head1 OPTIONS
32  
33 C<help> prints the man page.
34  
35 The algorithm can be any module that Perl can load using C<require>. By default,
36 these are the ones:
37  
38 =over
39  
40 =item * L<Game::TextMapper::Apocalypse>
41  
42 =item * L<Game::TextMapper::Gridmapper>
43  
44 =item * L<Game::TextMapper::Schroeder::Alpine> (needs role)
45  
46 =item * L<Game::TextMapper::Schroeder::Island> (needs role)
47  
48 =item * L<Game::TextMapper::Smale>
49  
50 =item * L<Game::TextMapper::Solo>
51  
52 =item * L<Game::TextMapper::Traveller>
53  
54 =back
55  
56 The default algorithm is L<Game::TextMapper::Smale>.
57  
58 Valid options depend on the algorithm. If an algorithm needs a role, you can
59 provide it using the C<--role> option.
60  
61 text-mapper random Game::TextMapper::Schroeder::Alpine \
62 --role Game::TextMapper::Schroeder::Hex
63  
64 If you don't do this, you'll get errors such as:
65  
66 Can't locate object method "random_neighbor" via package ...
67  
68 That's because C<random_neighbor> must differ depending on whether we are
69 looking at a hex map (6) or a square map (4).
70  
71 The two roles currently used:
72  
73 =over
74  
75 =item * L<Game::TextMapper::Schroeder::Hex>
76  
77 =item * L<Game::TextMapper::Schroeder::Square>
78  
79 =back
80  
81 =head1 DEVELOPING YOUR OWN
82  
83 The algorithm modules must be classes one instantiates using C<new> and they
84 must provide a method called C<generate_map> that returns a string.
85  
86 Assume you write your own, and put it in the F<./lib> directory, called
87 F<Arrakis.pm>. Here is a sample implementation. It uses L<Mojo::Base> to make it
88 a class.
89  
90 package Arrakis;
91 use Modern::Perl;
92 use Mojo::Base -base;
93 sub generate_map {
94 for my $x (0 .. 10) {
95 for my $y (0 .. 10) {
96 printf("%02d%02d dust desert\n", $x, $y);
97 }
98 }
99 say "include gnomeyland.txt";
100 }
101 1;
102  
103 Since the lib directory is in @INC when called via F<text-mapper>, you run it
104 like this:
105  
106 text-mapper random Arrakis | text-mapper render > map.svg
107  
108 Any extra arguments are passed along to the call to C<generate_map>.
109  
110 =cut
111  
112 package Game::TextMapper::Command::random;
113  
114 use Modern::Perl '2018';
115 use Mojo::Base 'Mojolicious::Command';
116 use Pod::Simple::Text;
117 use Getopt::Long qw(GetOptionsFromArray);
118 use Role::Tiny;
119 binmode(STDOUT, ':utf8');
120  
121 has description => 'Print a random map to STDOUT';
122  
123 has usage => sub { my $self = shift; $self->extract_usage };
124  
125 sub run {
126 my ($self, $module, @args) = @_;
127 $module ||= 'Game::TextMapper::Smale';
128 if ($module eq 'help') {
129 seek(DATA, 0, 0); # read from this file
130 my $parser = Pod::Simple::Text->new();
131 $parser->output_fh(*STDOUT);
132 $parser->parse_lines(<DATA>);
133 return 1;
134 }
135 my $res = eval "require $module"; # require needs bareword!
136 die "random: compilation of module '$module' failed: $!\n" unless defined $res;
137 die "$module did not return a true value\n" unless $res;
138 my $obj = eval "${module}->new";
139 die "random: module '$module->new' failed: $@" unless defined $obj;
140 die "random: module '$module->new' did not return a value\n" unless $obj;
141 my @roles;
142 GetOptionsFromArray (\@args, "role=s" => \@roles);
143 Role::Tiny->apply_roles_to_object($obj, @roles) if @roles;
144 print $obj->generate_map(@args);
145 }
146  
147 1;
148  
149 __DATA__