blib/lib/Game/HexDescribe.pm

filter: Statementstmt 39.3% 134 / 341 Branchbran 18.5% 13 / 70 Conditioncond 18.8% 10 / 53 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 36.1% 17 / 47 Podpod 80.0% 4 / 5 totaltotal 32.6% 178 / 546 SCAR 69.4
CC
58
Cov
34%
__ANON__
56.0
__ANON__
30.0
__ANON__
20.0
CRAP 1032.4
« blib/lib/Game/HexDescribe/Utils.pm ↑ Summary blib/lib/Game/HexDescribe/Command/rule.pm »
1 #!/usr/bin/env perl
2 # Copyright (C) 2018–2022 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 <http://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Game::HexDescribe - a web app to add random table driven data to map data
22  
23 =head1 DESCRIPTION
24  
25 Hex Describe is a web application which uses recursive random tables to create
26 the description of a map. A map in this context is a hex map. This is different
27 from other such tools like Tracery because a collection of locations on a maps
28 differ from a list of unrelated items. Neighbouring locations can share features
29 and thus a river can flow through many locations, a forest can cover many
30 locations, and so on.
31  
32 On a technical level, Hex Describe is a web app based on the Mojolicious
33 framework. This class in particular uses L<Mojolicious::Lite>.
34  
35 See L<Mojolicious::Guides> for more information.
36  
37 =cut
38  
39 package Game::HexDescribe;
40  
41 6 our $VERSION = 1.03;
42  
43 6 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use Mojo::Util qw(html_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 use Game::HexDescribe::Utils qw(init describe_text parse_table load_table
49 6 ▶ describe_map parse_map load_map markdown);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 6 ▶use Game::HexDescribe::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 6 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 6 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 6 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # Commands for the command line!
56 6 push @{app->commands->namespaces}, 'Game::HexDescribe::Command';
57  
58 =head2 Configuration
59  
60 As a Mojolicious application, it will read a config file called
61 F<hex-describe.conf> in the same directory, if it exists. As the default log
62 level is 'warn', one use of the config file is to change the log level using
63 the C<loglevel> key, and if you're not running the server in a terminal, using
64 the C<logfile> key to set a file.
65  
66 The default map and table are stored in the F<contrib> directory. You can change
67 this directory using the C<contrib> key. By default, the directory included with
68 the distribution is used. Thus, if you're a developer, you probably want to use
69 something like the following to use the files from the source directory.
70  
71 {
72 loglevel => 'debug',
73 logfile => undef,
74 contrib => 'share',
75 };
76  
77 The default map was created using Text Mapper's Alpine algorithm at one point in
78 time and the code has changed in the mean time, so it cannot be recreated
79 anymore.
80  
81 =cut
82  
83 6 plugin Config => {
84 default => {
85 loglevel => 'warn',
86 logfile => undef,
87 contrib => dist_dir('Game-HexDescribe'),
88 },
89 file => getcwd() . '/hex-describe.conf',
90 };
91  
92 6 my $log = Game::HexDescribe::Log->get;
93 6 $log->level(app->config('loglevel'));
94 6 $log->path(app->config('logfile'));
95 6 ▶$log->info($log->path ? "Logfile is " . $log->path : "Logging to stderr");
Branch: 1 branch
BranchTrueFalseTotal
$log->path ? : 0 6 6
96  
97 =head2 URLs
98  
99 The code needs to know where Text Mapper and the Face Generator can be found.
100 You can add these to the same config file we mentioned above. This is what you
101 probably want as a developer:
102  
103 {
104 text_mapper_url => 'http://localhost:3010',
105 face_generator_url => 'http://localhost:3020',
106 };
107  
108 This assumes you are running the two locally. See L<Game::TextMapper> for Text
109 Mapper.
110  
111 =cut
112  
113 6 ▶my $text_mapper_url = app->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper';
Condition50%1/2
app()->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper'
l!l
06
MC/DC0%0/1
app()->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper'
app()->config('text_mapper_url')
Truth table50%1/2
A
A= app()->config('text_mapper_url')
Aresult
11
00
114 6 $log->debug("Text Mapper URL: $text_mapper_url");
115  
116 6 ▶$Game::HexDescribe::Util::face_generator_url = app->config('face_generator_url') || 'https://campaignwiki.org/face';
Condition50%1/2
app()->config('face_generator_url') || 'https://campaignwiki.org/face'
l!l
06
MC/DC0%0/1
app()->config('face_generator_url') || 'https://campaignwiki.org/face'
app()->config('face_generator_url')
Truth table50%1/2
A
A= app()->config('face_generator_url')
Aresult
11
00
117 6 $log->debug("Face Generator URL: $Game::HexDescribe::Util::face_generator_url");
118  
119 6 ▶$Game::HexDescribe::Util::text_mapper_url = app->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper';
Condition50%1/2
app()->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper'
l!l
06
MC/DC0%0/1
app()->config('text_mapper_url') || 'https://campaignwiki.org/text-mapper'
app()->config('text_mapper_url')
Truth table50%1/2
A
A= app()->config('text_mapper_url')
Aresult
11
00
120 6 $log->debug("Text Mapper URL: $Game::HexDescribe::Util::text_mapper_url");
121  
122 =head2 Entry Points
123  
124 As this is a web app, the URLs you can call are basically the API it exposes.
125 Each URL can accept either I<get> or I<post> requests, or I<any>.
126  
127 =over 4
128  
129 =item get /
130  
131 The default entry point is where you I<edit> your map and table. B<map> is the
132 map, B<url> is the URL to an external table, B<table> is the text of the table
133 if you want to paste it. See C</describe> below if you want to display the
134 result instead of allow the user to edit the form.
135  
136 =cut
137  
138 get '/' => sub {
139 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
140 1 ▶ my $map = $c->param('map') || load_map('default', app->config('contrib'));
Condition33%1/3
$c->param('map') || load_map('default', app()->config('contrib'))
l!l&&r!l&&!r
010
MC/DC0%0/2
$c->param('map') || load_map('default', app()->config('contrib'))
$c->param('map') load_map('default', app()->config('contrib'))
Truth table33%1/3
A || B
A= $c->param('map')B= load_map('default', app()->config('contrib'))
ABresult
1X1
011
000
141 1 my $url = $c->param('url');
142 1 my $table = $c->param('table');
143 1 $c->render(template => 'edit', map => $map, url => $url, table => $table);
144 6 } => 'edit';
145  
146 =item get /load/random/smale
147  
148 This shows you the I<edit> page again, with a new random map generated by Text
149 Mapper using the Smale algorithm.
150  
151 =cut
152  
153 get '/load/random/smale' => sub {
154 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 my $url = "$text_mapper_url/smale/random/text";
156 0 my $map = get_data($url);
157 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
158 6 };
159  
160 =item get /load/random/apocalypse
161  
162 This shows you the I<edit> page again, with a new random map generated by Text
163 Mapper using the Apocalypse algorithm.
164  
165 =cut
166  
167 get '/load/random/apocalypse' => sub {
168 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 my $url = "$text_mapper_url/apocalypse/random/text";
170 0 my $map = get_data($url);
171 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
172 6 };
173  
174 =item get /load/random/traveller
175  
176 This shows you the I<edit> page again, with a new random map generated by Text
177 Mapper using the Traveller algorithm.
178  
179 =cut
180  
181 get '/load/random/traveller' => sub {
182 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 my $url = "$text_mapper_url/traveller/random/text";
184 0 my $map = get_data($url);
185 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
186 6 };
187  
188 =item get /load/random/alpine
189  
190 This shows you the I<edit> page again, with a new random map generated by Text
191 Mapper using the Alpine algorithm.
192  
193 =cut
194  
195 get '/load/random/alpine' => sub {
196 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 my $url = "$text_mapper_url/alpine/random/text";
198 0 my $map = get_data($url);
199 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
200 6 };
201  
202 =item get /stats/random/alpine
203  
204 This uses a random map and the Alpine algorithm, and describes the map, and then
205 it presents you with some stats.
206  
207 =cut
208  
209 get '/stats/random/alpine' => sub {
210 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=7 SCAR=40.3
211 0 my $url = "$text_mapper_url/alpine/random/text";
212 0 my $map_data = parse_map(get_data($url));
213 0 my %reverse_map;
214 0 for my $coords (keys %$map_data) {
215 0 for my $type (@{$map_data->{$coords}}) {
216 0 ▶ $reverse_map{$type} ||= 0;
Condition0%0/2
$reverse_map{$type} ||= 0
l!l
00
MC/DC0%0/1
$reverse_map{$type} ||= 0
$reverse_map{$type}
Truth table0%0/2
A
A= $reverse_map{$type}
Aresult
11
00
217 0 $reverse_map{$type}++;
218 }
219 }
220 0 my $table = load_table('schroeder', app->config('contrib'));
221 0 my $descriptions = describe_map($map_data, parse_table($table), 0);
222 0 my %reverse_creatures;
223 0 for my $coords (keys %$descriptions) {
224 0 ▶ while ($descriptions->{$coords} =~ m!<strong>(.+?)</strong>!g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coords} =~ m[<strong>(.+?)</strong>]gu) 0 0 0
225 0 ▶ $reverse_creatures{$1} ||= 0;
Condition0%0/2
$reverse_creatures{$1} ||= 0
l!l
00
MC/DC0%0/1
$reverse_creatures{$1} ||= 0
$reverse_creatures{$1}
Truth table0%0/2
A
A= $reverse_creatures{$1}
Aresult
11
00
226 0 $reverse_creatures{$1}++;
227 }
228 }
229 0 $c->render(template => 'stats',
230 map_stats => \%reverse_map,
231 creature_stats => \%reverse_creatures);
232 6 };
233  
234 =item any /describe
235  
236 This is where the actual map is described.
237  
238 B<map> is the map, B<url> is the URL to an external table. B<table> is the text
239 of the table. B<load> determines the table to load. Current valid values are
240 I<seckler>, I<strom>, I<schroeder>, I<johnston>, and I<traveller>. B<markdown>
241 returns Markdown and no map. B<faces> determines whether images are kept in the
242 HTML output. B<show> determines whether the map is kept in the HTML output.
243  
244 If we want to call this from the command line, we will need to request a map
245 from Text Mapper, too.
246  
247 text-mapper get /alpine.txt > map.txt
248 hex-describe get /describe --form map=@map.txt --form load=schroeder
249  
250 Pipe through C<lynx -stdin -dump -nolist> to get text instead of HTML.
251  
252 =cut
253  
254 any '/describe' => sub {
255 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=8 SCAR=24.9
256 1 my $map = $c->param('map');
257 1 my $labels = $c->param('labels');
258 1 my $markdown = $c->param('markdown');
259 1 my $faces = $c->param('faces');
260 1 my $show = $c->param('show');
261 1 my $table = get_table($c);
262 1 init();
263 1 my $descriptions = describe_map(parse_map($map), parse_table($table), $faces);
264 1 ▶ if ($markdown) {
Branch: 2 branches
BranchTrueFalseTotal
if ($markdown) { } 1 0 1
elsif ($show) { } 0 0 0
265 1 my $texts = [];
266 1 my $top = delete $descriptions->{TOP};
267 1 my $end = delete $descriptions->{END};
268 1 ▶ push(@$texts, $top) if $top;
Branch: 1 branch
BranchTrueFalseTotal
if $top 1 0 1
269 1 for my $hex (sort keys %$descriptions) {
270 1 push(@$texts, "**$hex**: $descriptions->{$hex}");
271 }
272 1 ▶ push(@$texts, $end) if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 1 0 1
273 1 $c->render(text => markdown($texts), format => 'txt');
274 } elsif ($show) {
275 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
276 0 my $svg = get_post_data($text_mapper_url . '/render', map => $map);
277 0 $c->render(template => 'description',
278 svg => add_links($svg),
279 descriptions => $descriptions);
280 } else {
281 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
282 0 $c->render(template => 'description',
283 svg => '',
284 descriptions => $descriptions);
285 }
286 6 };
287  
288 =item get /describe/random/smale
289  
290 This variant is for when you want to just keep reloading and getting different
291 maps with different descriptions. Note that you may pass a C<url> parameter,
292 which determines the map retrieved by Text Mapper. This allows you to refer to
293 an existing, random map, if you use the seed parameter in that URL. If you don't
294 provide a URL, a random map using the Smale algorithm will get used. The
295 description will be generated using the Seckler tables.
296  
297 =cut
298  
299 get '/describe/random/smale' => sub {
300 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
301 0 my $labels = $c->param('labels');
302 0 ▶ my $url = $c->param('url') || "$text_mapper_url/smale/random/text";
Condition0%0/2
$c->param('url') || "$text_mapper_url/smale/random/text"
l!l
00
MC/DC0%0/1
$c->param('url') || "$text_mapper_url/smale/random/text"
$c->param('url')
Truth table0%0/2
A
A= $c->param('url')
Aresult
11
00
303 0 my $map = get_data($url);
304 0 my $table = load_table('seckler', app->config('contrib'));
305 0 init();
306 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
307 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
308 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
309 0 $c->render(template => 'description',
310 svg => add_links($svg),
311 url => $url,
312 descriptions => $descriptions);
313 6 };
314  
315 =item get /describe/random/alpine
316  
317 Same thing for a map using the Alpine algorithm and the Schroeder random tables.
318  
319 =cut
320  
321 get '/describe/random/alpine' => sub {
322 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
323 0 my $labels = $c->param('labels');
324 0 my $seed = $c->param('seed');
325 0 my $url = $c->param('url');
326 0 ▶ if (not $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
327 0 $url = "$text_mapper_url/alpine/random/text";
328 0 ▶ $url .= "?seed=$seed" if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
329 }
330 0 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
331 0 my $map = get_data($url);
332 0 my $table = load_table('schroeder', app->config('contrib'));
333 0 init();
334 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
335 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
336 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
337 0 $c->render(template => 'description',
338 svg => add_links($svg),
339 url => $url,
340 descriptions => $descriptions);
341 6 };
342  
343 =item get /describe/random/strom
344  
345 Same thing for a map using the Smale algorithm and the Strom random tables.
346  
347 =cut
348  
349 get '/describe/random/strom' => sub {
350 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
351 0 my $labels = $c->param('labels');
352 0 ▶ my $url = $c->param('url') || "$text_mapper_url/smale/random/text";
Condition0%0/2
$c->param('url') || "$text_mapper_url/smale/random/text"
l!l
00
MC/DC0%0/1
$c->param('url') || "$text_mapper_url/smale/random/text"
$c->param('url')
Truth table0%0/2
A
A= $c->param('url')
Aresult
11
00
353 0 my $map = get_data($url);
354 0 my $table = load_table('strom', app->config('contrib'));
355 0 init();
356 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
357 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
358 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
359 0 $c->render(template => 'description',
360 svg => add_links($svg),
361 url => $url,
362 descriptions => $descriptions);
363 6 };
364  
365 =item get /describe/random/johnston
366  
367 Same thing for a map using the Apocalypse algorithm and the Johnston random tables.
368  
369 =cut
370  
371 get '/describe/random/johnston' => sub {
372 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
373 0 my $labels = $c->param('labels');
374 0 ▶ my $url = $c->param('url') || "$text_mapper_url/apocalypse/random/text";
Condition0%0/2
$c->param('url') || "$text_mapper_url/apocalypse/random/text"
l!l
00
MC/DC0%0/1
$c->param('url') || "$text_mapper_url/apocalypse/random/text"
$c->param('url')
Truth table0%0/2
A
A= $c->param('url')
Aresult
11
00
375 0 my $map = get_data($url);
376 0 my $table = load_table('johnston', app->config('contrib'));
377 0 init();
378 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
379 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
380 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
381 0 $c->render(template => 'description',
382 svg => add_links($svg),
383 url => $url,
384 descriptions => $descriptions);
385 6 };
386  
387 =item get /describe/random/traveller
388  
389 Same thing for a map using the Traveller algorithm and the Traveller random tables.
390  
391 =cut
392  
393 get '/describe/random/traveller' => sub {
394 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
395 0 my $labels = $c->param('labels');
396 0 ▶ my $url = $c->param('url') || "$text_mapper_url/traveller/random/text";
Condition0%0/2
$c->param('url') || "$text_mapper_url/traveller/random/text"
l!l
00
MC/DC0%0/1
$c->param('url') || "$text_mapper_url/traveller/random/text"
$c->param('url')
Truth table0%0/2
A
A= $c->param('url')
Aresult
11
00
397 0 my $map = get_data($url);
398 0 my $table = load_table('traveller', app->config('contrib'));
399 0 init();
400 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
401 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
402 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
403 0 $c->render(template => 'description',
404 svg => add_links($svg),
405 url => $url,
406 descriptions => $descriptions);
407 6 };
408  
409 =item get /nomap
410  
411 This shows you the I<edit> page for use cases without a map. Now you're using
412 Hex Describe like many of the existing random table driven text generators. This
413 is where you can test your tables. If you've changed the code for the I<village>
414 table, for example, generate a few villages to see some examples:
415  
416 [village]
417 [village]
418 [village]
419 [village]
420 [village]
421  
422 B<input> is your source text. This is no longer a map. B<url> is the URL to an
423 external table, B<table> is the text of the table if you want to paste it. See
424 C</describe/text> below if you want to display the result instead of allow the
425 user to edit the form.
426  
427 =cut
428  
429 get '/nomap' => sub {
430 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
431 0 ▶ my $input = $c->param('input') || '';
Condition0%0/2
$c->param('input') || ''
l!l
00
MC/DC0%0/1
$c->param('input') || ''
$c->param('input')
Truth table0%0/2
A
A= $c->param('input')
Aresult
11
00
432 0 my $url = $c->param('url');
433 0 my $table = $c->param('table');
434 0 ▶ my $seed = $c->param('seed') || time;
Condition0%0/3
$c->param('seed') || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->param('seed') || time
$c->param('seed') time
Truth table0%0/3
A || B
A= $c->param('seed')B= time
ABresult
1X1
011
000
435 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
436 0 $c->render(template => 'nomap', input => $input, url => $url, table => $table, seed => $seed);
437 6 };
438  
439 any '/nomap/markdown' => sub {
440 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
441 0 ▶ my $input = $c->param('input') || '';
Condition0%0/2
$c->param('input') || ''
l!l
00
MC/DC0%0/1
$c->param('input') || ''
$c->param('input')
Truth table0%0/2
A
A= $c->param('input')
Aresult
11
00
442 0 my $table = get_table($c);
443 0 ▶ my $seed = $c->param('seed') || time;
Condition0%0/3
$c->param('seed') || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->param('seed') || time
$c->param('seed') time
Truth table0%0/3
A || B
A= $c->param('seed')B= time
ABresult
1X1
011
000
444 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
445 0 my $descriptions = describe_text($input, parse_table($table));
446 0 $c->render(text => markdown($descriptions), format => 'txt', seed => $seed);
447 6 } => 'nomap_markdown';
448  
449 =item /rules
450  
451 This lists all the rules we have and allows you to pick one.
452  
453 =cut
454  
455 get '/rules' => sub {
456 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
457 1 ▶ my $input = $c->param('input') || '';
Condition50%1/2
$c->param('input') || ''
l!l
01
MC/DC0%0/1
$c->param('input') || ''
$c->param('input')
Truth table50%1/2
A
A= $c->param('input')
Aresult
11
00
458 1 my $url = $c->param('url');
459 1 my $table = $c->param('table');
460 1 $c->render(template => 'rules', input => $input, url => $url, table => $table);
461 6 };
462  
463 any '/rules/list' => sub {
464 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=4 SCAR=16.1
465 1 ▶ my $input = $c->param('input') || '';
Condition50%1/2
$c->param('input') || ''
l!l
01
MC/DC0%0/1
$c->param('input') || ''
$c->param('input')
Truth table50%1/2
A
A= $c->param('input')
Aresult
11
00
466 1 my ($url, $table) = get_table($c);
467 # we cannot test for 'load' because a radiobutton is always selected
468 1 ▶ if ($c->param('url') or $c->param('table')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->param('url') or $c->param('table')) { } 0 1 1
Condition33%1/3
$c->param("url") or $c->param("table")
l!l&&r!l&&!r
001
MC/DC0%0/2
$c->param("url") or $c->param("table")
$c->param("url") $c->param("table")
Truth table33%1/3
A or B
A= $c->param("url")B= $c->param("table")
ABresult
1X1
011
000
469 $c->render(template => 'ruleslist_post', input => $input,
470 url => $url, table => $table,
471 log => $c->param('log'),
472 0 rules => [keys %{parse_table($table)}]);
473 } else {
474 $c->render(template => 'ruleslist_get',
475 load => $c->param('load'),
476 log => $c->param('log'),
477 1 rules => [keys %{parse_table($table)}]);
478 }
479 6 } => 'ruleslist';
480  
481 sub to_id {
482 1 ▶ $_ = shift;
Subroutine to_id: called CC=2 SCAR=7.0
Pod undocumented
483 1 ▶ return "" unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 1 1
484 1 s/ /_/g;
485 1 s/[^0-9a-z_]//gi;
486 1 s/^(\d)/x$1/;
487 1 $_;
488 }
489  
490 any '/rule' => sub {
491 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=5 SCAR=17.1
492 1 my $rule = $c->param('rule');
493 1 ▶ my $n = $c->param('n') || 10;
Condition50%1/2
$c->param('n') || 10
l!l
01
MC/DC0%0/1
$c->param('n') || 10
$c->param('n')
Truth table50%1/2
A
A= $c->param('n')
Aresult
11
00
494 1 my $input = "[$rule]\n" x $n;
495 1 my $table = get_table($c);
496 1 ▶ my $seed = $c->param('seed') || time;
Condition33%1/3
$c->param('seed') || time
l!l&&r!l&&!r
010
MC/DC0%0/2
$c->param('seed') || time
$c->param('seed') time
Truth table33%1/3
A || B
A= $c->param('seed')B= time
ABresult
1X1
011
000
497 1 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 1 0 1
498 1 my $descriptions = describe_text($input, parse_table($table), 1); # with redirects
499 1 ▶ $c->render(template => 'text', input => $input, load => $c->param('load'), seed => $seed,
Branch: 1 branch
BranchTrueFalseTotal
$c->param('log') ? : 0 1 1
500 n => $n, url => $c->param('url'), table => $c->param('table'),
501 rule => $rule, id => to_id($rule),
502 log => $c->param('log') ? $log->history : undef,
503 descriptions => $descriptions);
504 6 } => 'rule';
505  
506 any '/rule/markdown' => sub {
507 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
508 0 my $rule = $c->param('rule');
509 0 ▶ my $n = $c->param('n') || 10;
Condition0%0/2
$c->param('n') || 10
l!l
00
MC/DC0%0/1
$c->param('n') || 10
$c->param('n')
Truth table0%0/2
A
A= $c->param('n')
Aresult
11
00
510 0 ▶ my $input = $c->param('input') || "[$rule]\n" x $n;
Condition0%0/3
$c->param('input') || ("[$rule]\n") x $n
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->param('input') || ("[$rule]\n") x $n
$c->param('input') ("[$rule]\n") x $n
Truth table0%0/3
A || B
A= $c->param('input')B= ("[$rule]\n") x $n
ABresult
1X1
011
000
511 0 my $table = get_table($c);
512 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
513 0 my $descriptions = describe_text($input, parse_table($table), 1); # with redirects
514 0 $c->render(text => markdown($descriptions), format => 'txt');
515 6 } => 'rule_markdown';
516  
517 any '/rule/show' => sub {
518 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
519 0 my $rule = $c->param('rule');
520 0 my $load = $c->param('load');
521 0 my $table = get_table($c);
522 0 $table =~ s!\r!!g;
523 0 $table =~ s!&!&amp;!gm;
524 0 $table =~ s!<!&lt;!gm;
525 0 $table =~ s!>!&gt;!gm;
526 0 $table =~ s!\[([^][\n]+)\]!"[<a href=\"#" . to_id($1) . "\">$1</a>]"!gme;
527 0 my $jump = 0;
528 0 ▶ if ($c->param('url') or $c->param('table')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->param('url') or $c->param('table')) { } 0 0 0
Condition0%0/3
$c->param("url") or $c->param("table")
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->param("url") or $c->param("table")
$c->param("url") $c->param("table")
Truth table0%0/3
A or B
A= $c->param("url")B= $c->param("table")
ABresult
1X1
011
000
529 0 $jump = 1;
530 0 $table =~ s!^;(.+)!";<strong><a id=\"" . to_id($1) . "\">$1</a></strong>"!gme;
531 } else {
532 0 $table =~ s!^;(.+)!";<strong><a id=\"" . to_id($1)
533 . "\" href=\"" . $c->url_for('rule')->query(load => $load, rule => $1)
534 . "\">$1</a></strong>"!gme;
535 }
536 0 $c->render(template => 'show',
537 id => to_id($rule),
538 rule => $rule,
539 jump => $jump,
540 load => $load,
541 table => $table);
542 6 } => 'rule_show';
543  
544 =item any /describe/text
545  
546 This is where the text input is rendered. B<input> is the text, B<url> is the
547 URL to an external table. If not provided, B<table> is the text of the table. If
548 neither is provided, the default table is used.
549  
550 To call this from the command line:
551  
552 hex-describe get /describe/text --form input=[village] --form load=schroeder
553  
554 Pipe through C<lynx -stdin -dump -nolist> to get text instead of HTML.
555  
556 =cut
557  
558 any '/describe/text' => sub {
559 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
560 0 my $rule = $c->param('rule');
561 0 my $load = $c->param('load');
562 0 my $n = $c->param('n');
563 0 my $input = $c->param('input');
564 0 my $url = $c->param('url');
565 0 my $table = $c->param('table');
566 0 my $seed = $c->param('seed');
567 0 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
568 0 my $data = get_table($c); # must be scalar context
569 0 ▶ $c->render(template => 'text', input => $input, load => $load, seed => $seed,
Branch: 1 branch
BranchTrueFalseTotal
$c->param('log') ? : 0 0 0
570 n => $n, url => $url, table => $table,
571 rule => $rule, id => to_id($rule),
572 log => $c->param('log') ? $log->history : undef,
573 descriptions => describe_text($input, parse_table($data)));
574 6 };
575  
576 =item get /default/map
577  
578 This shows you the default map.
579  
580 =cut
581  
582 get '/default/map' => sub {
583 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
584 0 $c->render(text => load_map('default', app->config('contrib')), format => 'txt');
585 6 };
586  
587 =item get /schroeder/table
588  
589 This shows you the table by Alex Schroeder.
590  
591 =cut
592  
593 get '/schroeder/table' => sub {
594 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
595 0 $c->render(text => load_table('schroeder', app->config('contrib')), format => 'txt');
596 6 };
597  
598 =item get /seckler/table
599  
600 This shows you the table by Peter Seckler.
601  
602 =cut
603  
604 get '/seckler/table' => sub {
605 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 0 $c->render(text => load_table('seckler', app->config('contrib')), format => 'txt');
607 6 };
608  
609 =item get /strom/table
610  
611 This shows you the table by Matt Strom.
612  
613 =cut
614  
615 get '/strom/table' => sub {
616 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 $c->render(text => load_table('strom', app->config('contrib')), format => 'txt');
618 6 };
619  
620 =item get /johnston/table
621  
622 This shows you the table by Josh Johnston.
623  
624 =cut
625  
626 get '/johnston/table' => sub {
627 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
628 0 $c->render(text => load_table('johnston', app->config('contrib')), format => 'txt');
629 6 };
630  
631 =item get /traveller/table
632  
633 This shows you the Traveller table by Vicky Radcliffe and Alex Schroeder.
634  
635 =cut
636  
637 get '/traveller/table' => sub {
638 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
639 0 $c->render(text => load_table('traveller', app->config('contrib')), format => 'txt');
640 6 };
641  
642 =item get /rorschachhamster/table
643  
644 Für die deutschen Tabellen von Rorschachhamster Alex Schroeder.
645  
646 =cut
647  
648 get '/rorschachhamster/table' => sub {
649 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
650 0 $c->render(text => load_table('rorschachhamster', app->config('contrib')), format => 'txt');
651 6 };
652  
653 =item get /source
654  
655 This gets you the source code of Hex Describe in case the source repository is
656 no longer available.
657  
658 =cut
659  
660 get '/source' => sub {
661 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
662 0 seek(DATA,0,0);
663 0 local $/ = undef;
664 0 $c->render(text => <DATA>, format => 'txt');
665 6 };
666  
667 =item get /authors
668  
669 This lists the contributors to Hex Describe.
670  
671 =cut
672  
673 get '/authors' => sub {
674 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
675 0 $c->render(template => 'authors');
676 6 };
677  
678 =item get /help
679  
680 This shows you a little tutorial. Unlike this documentation, which is for
681 programmers, the tutorial is for the users of the app.
682  
683 =cut
684  
685 get '/help' => sub {
686 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
687 0 $c->render(template => 'help');
688 6 };
689  
690 =back
691  
692 =head2 Code
693  
694 This chapter is used to document the code.
695  
696 =over 4
697  
698 =item get_data
699  
700 This is is the basic work horse to get data from a URL. It is used to download
701 the table from a URL, if provided. This uses a simple GET request.
702  
703 =cut
704  
705 sub get_data {
706 0 ▶ my $url = shift;
Subroutine get_data: not called CC=2 SCAR=17.9
Pod documented
707 0 $log->debug("get_data: $url");
708 0 my $ua = Mojo::UserAgent->new;
709 0 my $res = $ua->get($url)->result;
710 0 ▶ return decode_utf8($res->body) if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
711 0 $log->error("get_data: " . $res->code . " " . $res->message);
712 }
713  
714 =item get_post_data
715  
716 This is is used to get data from a URL when we need a POST request instead of a
717 GET request. We need this for Text Mapper when rendering the map since we send
718 the entire map to Text Mapper in order to render it. A simple GET request will
719 not do.
720  
721 =cut
722  
723 sub get_post_data {
724 0 ▶ my $url = shift;
Subroutine get_post_data: not called CC=4 SCAR=30.0
Pod documented
725 0 my %data = @_;
726 0 $log->debug("get_post_data: $url");
727 0 my $ua = Mojo::UserAgent->new;
728 0 my $tx = $ua->post($url => form => \%data);
729 0 my $error;
730 0 ▶ if (my $err = $tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) { } 0 0 0
731 0 ▶ if ($err->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err->{'code'}) { } 0 0 0
732 0 $error = $err->{code} . " " . $err->{message};
733 } else {
734 0 $error = $err->{message};
735 }
736 } else {
737 0 my $res = $ua->post($url => form => \%data)->result;
738 0 ▶ return decode_utf8($res->body) if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
739 0 $error = $res->code . " " . $res->message;
740 }
741 0 $log->error("get_post_data: $error");
742 0 return "<p>There was an error when attempting to load the map ($error).</p>";
743 }
744  
745 =item get_table
746  
747 This function gets a Mojolicious Controller object and looks for C<map>,
748 C<load>, C<url> and C<table> parameters in order to determine the table data to
749 use.
750  
751 =cut
752  
753 sub get_table {
754 3 ▶ my $c = shift;
Subroutine get_table: called CC=5 SCAR=16.4
Pod documented
755 3 my $load = $c->param('load');
756 3 my $url = $c->param('url');
757 3 my $table = '';
758 3 ▶ $table .= get_data($url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 3 3
759 3 ▶ $table .= load_table($load, app->config('contrib')) if $load;
Branch: 1 branch
BranchTrueFalseTotal
if $load 3 0 3
760 # the table in the text area comes at the end and overrides the defaults
761 3 ▶ $table .= $c->param('table') || '';
Condition50%1/2
$c->param('table') || ''
l!l
03
MC/DC0%0/1
$c->param('table') || ''
$c->param('table')
Truth table50%1/2
A
A= $c->param('table')
Aresult
11
00
762 3 ▶ return $url, $table if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 2 3
763 2 return $table;
764 }
765  
766 =item add_links
767  
768 After we get the SVG map from Text Mapper, we need to add links to the hex
769 descriptions. Text Mapper already allows us to define an URL such that I<labels>
770 get linked to that URL. This feature is of no use to us because we're not using
771 labels. Basically, we want to add links to the I<coordinates>. This function
772 does that: it goes through the SVG and adds appropriate anchor elements.
773  
774 =cut
775  
776 sub add_links {
777 0 ▶ my $svg = shift;
Subroutine add_links: not called CC=1 SCAR=6.9
Pod documented
778 0 $svg =~ s/<\?xml[^>]*>\s*//g; # remove processing instruction
779 0 my $dom = Mojo::DOM->new($svg);
780 $dom->find('g#coordinates text')
781 ->each(sub {
782 0 ▶ my $text = $_->text;
Subroutine __ANON__: not called CC=1 SCAR=6.9
783 0 $text =~ s/\.//; # strip dot
784 0 $_->wrap(qq{<a xlink:href="#desc$text"></a>})});
785 0 return "$dom";
786 }
787  
788 =item helper example
789  
790 This Mojolicious helper is used on the help page to make all the examples
791 clickable.
792  
793 =cut
794  
795 helper example => sub {
796 0 ▶ my ($c, $block) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
797 0 my $result = $block->();
798 0 my $url;
799 0 ▶ if ($result =~ /^\d\d\d\d/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /^\d\d\d\d/mu) { } 0 0 0
800 0 my $map = join("\n", grep(/^\d\d\d\d|^include/, split(/\n/, $result)));
801 0 my $table = join("\n", grep(!/^\d\d\d\d|^include/, split(/\n/, $result)));
802 0 $url = $c->url_for('edit')->query(map => $map,
803 load => 'none',
804 table=> html_unescape($table));
805 } else {
806 0 my ($key) = $result =~ /^;(.*)/m;
807 0 $url = $c->url_for('nomap')->query(input => "[$key]\n" x 10,
808 load => 'none',
809 table=> html_unescape($result));
810 }
811 0 return Mojo::ByteStream->new(qq(<pre>$result</pre><p><a href='$url'>Try it</a>.</p>));
812 6 };
813  
814 =back
815  
816 =head2 Finally
817  
818 Start the app at the very end. The rest is templates for the various web pages.
819  
820 =cut
821  
822 6 ▶app->start || 1;
Branch: 1 branch
BranchTrueFalseTotal
app()->start or 1 0 6 6
823  
824 __DATA__
« blib/lib/Game/HexDescribe/Utils.pm ↑ Summary blib/lib/Game/HexDescribe/Command/rule.pm »