blib/lib/Game/HexDescribe.pm

filter: Statementstmt 39.4% 135 / 342 Branchbran 19.4% 14 / 72 Conditioncond 17.6% 9 / 51 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 36.1% 17 / 47 Podpod 80.0% 4 / 5 totaltotal 32.7% 179 / 546 CC 58 SCAR 25.5
CC
58
Cov
34%
__ANON__
56.0
__ANON__
30.0
__ANON__
20.0
CRAP 12.8
« blib/lib/Game/HexDescribe/Utils.pm ↑ Summary 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.04;
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 $Game::HexDescribe::Util::text_mapper_url = $text_mapper_url;
115 6 $log->debug("Text Mapper URL: $text_mapper_url");
116  
117 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
118 6 $log->debug("Face Generator URL: $Game::HexDescribe::Util::face_generator_url");
119  
120 =head2 Entry Points
121  
122 As this is a web app, the URLs you can call are basically the API it exposes.
123 Each URL can accept either I<get> or I<post> requests, or I<any>.
124  
125 The config file key C<under> can be used to host the entire application on a
126 specific path. Example C<hex-describe.conf> file:
127  
128 {
129 loglevel => 'debug',
130 logfile => undef,
131 contrib => 'share',
132 text_mapper_url => 'http://localhost:3010',
133 face_generator_url => 'http://localhost:3020',
134 under => 'hex-describe',
135 };
136  
137 Command line to start it:
138  
139 morbo --mode development --listen http://*:3000 script/hex-describe
140  
141 The result is that Hex Describe is now available via
142 C<http://localhost:3000/hex-describe>.
143  
144 =cut
145  
146 6 my $under = app->config('under');
147 6 ▶under $under if $under;
Branch: 1 branch
BranchTrueFalseTotal
if $under 0 6 6
148  
149 =over 4
150  
151 =item get /
152  
153 The default entry point is where you I<edit> your map and table. B<map> is the
154 map, B<url> is the URL to an external table, B<table> is the text of the table
155 if you want to paste it. See C</describe> below if you want to display the
156 result instead of allow the user to edit the form.
157  
158 =cut
159  
160 get '/' => sub {
161 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
162 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
163 1 my $url = $c->param('url');
164 1 my $table = $c->param('table');
165 1 $c->render(template => 'edit', map => $map, url => $url, table => $table);
166 6 } => 'edit';
167  
168 =item get /load/random/smale
169  
170 This shows you the I<edit> page again, with a new random map generated by Text
171 Mapper using the Smale algorithm.
172  
173 =cut
174  
175 get '/load/random/smale' => sub {
176 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 my $url = "$text_mapper_url/smale/random/text";
178 0 my $map = get_data($url);
179 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
180 6 };
181  
182 =item get /load/random/apocalypse
183  
184 This shows you the I<edit> page again, with a new random map generated by Text
185 Mapper using the Apocalypse algorithm.
186  
187 =cut
188  
189 get '/load/random/apocalypse' => sub {
190 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 my $url = "$text_mapper_url/apocalypse/random/text";
192 0 my $map = get_data($url);
193 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
194 6 };
195  
196 =item get /load/random/traveller
197  
198 This shows you the I<edit> page again, with a new random map generated by Text
199 Mapper using the Traveller algorithm.
200  
201 =cut
202  
203 get '/load/random/traveller' => sub {
204 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 0 my $url = "$text_mapper_url/traveller/random/text";
206 0 my $map = get_data($url);
207 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
208 6 };
209  
210 =item get /load/random/alpine
211  
212 This shows you the I<edit> page again, with a new random map generated by Text
213 Mapper using the Alpine algorithm.
214  
215 =cut
216  
217 get '/load/random/alpine' => sub {
218 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 my $url = "$text_mapper_url/alpine/random/text";
220 0 my $map = get_data($url);
221 0 $c->render(template => 'edit', map => $map, url=>'', table => '');
222 6 };
223  
224 =item get /stats/random/alpine
225  
226 This uses a random map and the Alpine algorithm, and describes the map, and then
227 it presents you with some stats.
228  
229 =cut
230  
231 get '/stats/random/alpine' => sub {
232 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=7 SCAR=40.3
233 0 my $url = "$text_mapper_url/alpine/random/text";
234 0 my $map_data = parse_map(get_data($url));
235 0 my %reverse_map;
236 0 for my $coords (keys %$map_data) {
237 0 for my $type (@{$map_data->{$coords}}) {
238 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
239 0 $reverse_map{$type}++;
240 }
241 }
242 0 my $table = load_table('schroeder', app->config('contrib'));
243 0 my $descriptions = describe_map($map_data, parse_table($table), 0);
244 0 my %reverse_creatures;
245 0 for my $coords (keys %$descriptions) {
246 0 ▶ while ($descriptions->{$coords} =~ m!<strong>(.+?)</strong>!g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coords} =~ m[<strong>(.+?)</strong>]gu) 0 0 0
247 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
248 0 $reverse_creatures{$1}++;
249 }
250 }
251 0 $c->render(template => 'stats',
252 map_stats => \%reverse_map,
253 creature_stats => \%reverse_creatures);
254 6 };
255  
256 =item any /describe
257  
258 This is where the actual map is described.
259  
260 B<map> is the map, B<url> is the URL to an external table. B<table> is the text
261 of the table. B<load> determines the table to load. Current valid values are
262 I<seckler>, I<strom>, I<schroeder>, I<johnston>, and I<traveller>. B<markdown>
263 returns Markdown and no map. B<faces> determines whether images are kept in the
264 HTML output. B<show> determines whether the map is kept in the HTML output.
265  
266 If we want to call this from the command line, we will need to request a map
267 from Text Mapper, too.
268  
269 text-mapper get /alpine.txt > map.txt
270 hex-describe get /describe --form map=@map.txt --form load=schroeder
271  
272 Pipe through C<lynx -stdin -dump -nolist> to get text instead of HTML.
273  
274 =cut
275  
276 any '/describe' => sub {
277 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=8 SCAR=24.9
278 1 my $map = $c->param('map');
279 1 my $labels = $c->param('labels');
280 1 my $markdown = $c->param('markdown');
281 1 my $faces = $c->param('faces');
282 1 my $show = $c->param('show');
283 1 my $table = get_table($c);
284 1 init();
285 1 my $descriptions = describe_map(parse_map($map), parse_table($table), $faces);
286 1 ▶ if ($markdown) {
Branch: 2 branches
BranchTrueFalseTotal
if ($markdown) { } 1 0 1
elsif ($show) { } 0 0 0
287 1 my $texts = [];
288 1 my $top = delete $descriptions->{TOP};
289 1 my $end = delete $descriptions->{END};
290 1 ▶ push(@$texts, $top) if $top;
Branch: 1 branch
BranchTrueFalseTotal
if $top 1 0 1
291 1 for my $hex (sort keys %$descriptions) {
292 1 push(@$texts, "**$hex**: $descriptions->{$hex}");
293 }
294 1 ▶ push(@$texts, $end) if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 1 0 1
295 1 $c->render(text => markdown($texts), format => 'txt');
296 } elsif ($show) {
297 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
298 0 my $svg = get_post_data($text_mapper_url . '/render', map => $map);
299 0 $c->render(template => 'description',
300 svg => add_links($svg),
301 descriptions => $descriptions);
302 } else {
303 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
304 0 $c->render(template => 'description',
305 svg => '',
306 descriptions => $descriptions);
307 }
308 6 };
309  
310 =item get /describe/random/smale
311  
312 This variant is for when you want to just keep reloading and getting different
313 maps with different descriptions. Note that you may pass a C<url> parameter,
314 which determines the map retrieved by Text Mapper. This allows you to refer to
315 an existing, random map, if you use the seed parameter in that URL. If you don't
316 provide a URL, a random map using the Smale algorithm will get used. The
317 description will be generated using the Seckler tables.
318  
319 =cut
320  
321 get '/describe/random/smale' => sub {
322 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
323 0 my $labels = $c->param('labels');
324 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
325 0 my $map = get_data($url);
326 0 my $table = load_table('seckler', app->config('contrib'));
327 0 init();
328 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
329 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
330 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
331 0 $c->render(template => 'description',
332 svg => add_links($svg),
333 url => $url,
334 descriptions => $descriptions);
335 6 };
336  
337 =item get /describe/random/alpine
338  
339 Same thing for a map using the Alpine algorithm and the Schroeder random tables.
340  
341 =cut
342  
343 get '/describe/random/alpine' => sub {
344 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
345 0 my $labels = $c->param('labels');
346 0 my $seed = $c->param('seed');
347 0 my $url = $c->param('url');
348 0 ▶ if (not $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
349 0 $url = "$text_mapper_url/alpine/random/text";
350 0 ▶ $url .= "?seed=$seed" if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
351 }
352 0 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
353 0 my $map = get_data($url);
354 0 my $table = load_table('schroeder', 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/strom
366  
367 Same thing for a map using the Smale algorithm and the Strom random tables.
368  
369 =cut
370  
371 get '/describe/random/strom' => 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/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
375 0 my $map = get_data($url);
376 0 my $table = load_table('strom', 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/johnston
388  
389 Same thing for a map using the Apocalypse algorithm and the Johnston random tables.
390  
391 =cut
392  
393 get '/describe/random/johnston' => 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/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
397 0 my $map = get_data($url);
398 0 my $table = load_table('johnston', 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 /describe/random/traveller
410  
411 Same thing for a map using the Traveller algorithm and the Traveller random tables.
412  
413 =cut
414  
415 get '/describe/random/traveller' => sub {
416 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
417 0 my $labels = $c->param('labels');
418 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
419 0 my $map = get_data($url);
420 0 my $table = load_table('traveller', app->config('contrib'));
421 0 init();
422 0 my $descriptions = describe_map(parse_map($map), parse_table($table), 1); # with faces
423 0 ▶ $map = add_labels($map) if $labels;
Branch: 1 branch
BranchTrueFalseTotal
if $labels 0 0 0
424 0 my $svg = get_post_data("$text_mapper_url/render", map => $map);
425 0 $c->render(template => 'description',
426 svg => add_links($svg),
427 url => $url,
428 descriptions => $descriptions);
429 6 };
430  
431 =item get /nomap
432  
433 This shows you the I<edit> page for use cases without a map. Now you're using
434 Hex Describe like many of the existing random table driven text generators. This
435 is where you can test your tables. If you've changed the code for the I<village>
436 table, for example, generate a few villages to see some examples:
437  
438 [village]
439 [village]
440 [village]
441 [village]
442 [village]
443  
444 B<input> is your source text. This is no longer a map. B<url> is the URL to an
445 external table, B<table> is the text of the table if you want to paste it. See
446 C</describe/text> below if you want to display the result instead of allow the
447 user to edit the form.
448  
449 =cut
450  
451 get '/nomap' => sub {
452 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
453 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
454 0 my $url = $c->param('url');
455 0 my $table = $c->param('table');
456 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
457 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
458 0 $c->render(template => 'nomap', input => $input, url => $url, table => $table, seed => $seed);
459 6 };
460  
461 any '/nomap/markdown' => sub {
462 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
463 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
464 0 my $table = get_table($c);
465 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
466 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
467 0 my $descriptions = describe_text($input, parse_table($table));
468 0 $c->render(text => markdown($descriptions), format => 'txt', seed => $seed);
469 6 } => 'nomap_markdown';
470  
471 =item /rules
472  
473 This lists all the rules we have and allows you to pick one.
474  
475 =cut
476  
477 get '/rules' => sub {
478 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
479 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
480 1 my $url = $c->param('url');
481 1 my $table = $c->param('table');
482 1 $c->render(template => 'rules', input => $input, url => $url, table => $table);
483 6 };
484  
485 any '/rules/list' => sub {
486 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=4 SCAR=16.1
487 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
488 1 my ($url, $table) = get_table($c);
489 # we cannot test for 'load' because a radiobutton is always selected
490 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
491 $c->render(template => 'ruleslist_post', input => $input,
492 url => $url, table => $table,
493 log => $c->param('log'),
494 0 rules => [keys %{parse_table($table)}]);
495 } else {
496 $c->render(template => 'ruleslist_get',
497 load => $c->param('load'),
498 log => $c->param('log'),
499 1 rules => [keys %{parse_table($table)}]);
500 }
501 6 } => 'ruleslist';
502  
503 sub to_id {
504 1 ▶ $_ = shift;
Subroutine to_id: called CC=2 SCAR=7.0
Pod undocumented
505 1 ▶ return "" unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 1 1
506 1 s/ /_/g;
507 1 s/[^0-9a-z_]//gi;
508 1 s/^(\d)/x$1/;
509 1 $_;
510 }
511  
512 any '/rule' => sub {
513 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=5 SCAR=17.1
514 1 my $rule = $c->param('rule');
515 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
516 1 my $input = "[$rule]\n" x $n;
517 1 my $table = get_table($c);
518 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
519 1 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 1 0 1
520 1 my $descriptions = describe_text($input, parse_table($table), 1); # with redirects
521 1 ▶ $c->render(template => 'text', input => $input, load => $c->param('load'), seed => $seed,
Branch: 1 branch
BranchTrueFalseTotal
$c->param('log') ? : 0 1 1
522 n => $n, url => $c->param('url'), table => $c->param('table'),
523 rule => $rule, id => to_id($rule),
524 log => $c->param('log') ? $log->history : undef,
525 descriptions => $descriptions);
526 6 } => 'rule';
527  
528 any '/rule/markdown' => sub {
529 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
530 0 my $rule = $c->param('rule');
531 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
532 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
533 0 my $table = get_table($c);
534 0 ▶ srand($c->param('seed')) if $c->param('seed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("seed") 0 0 0
535 0 my $descriptions = describe_text($input, parse_table($table), 1); # with redirects
536 0 $c->render(text => markdown($descriptions), format => 'txt');
537 6 } => 'rule_markdown';
538  
539 any '/rule/show' => sub {
540 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
541 0 my $rule = $c->param('rule');
542 0 my $load = $c->param('load');
543 0 my $table = get_table($c);
544 0 $table =~ s!\r!!g;
545 0 $table =~ s!&!&amp;!gm;
546 0 $table =~ s!<!&lt;!gm;
547 0 $table =~ s!>!&gt;!gm;
548 0 $table =~ s!\[([^][\n]+)\]!"[<a href=\"#" . to_id($1) . "\">$1</a>]"!gme;
549 0 my $jump = 0;
550 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
551 0 $jump = 1;
552 0 $table =~ s!^;(.+)!";<strong><a id=\"" . to_id($1) . "\">$1</a></strong>"!gme;
553 } else {
554 0 $table =~ s!^;(.+)!";<strong><a id=\"" . to_id($1)
555 . "\" href=\"" . $c->url_for('rule')->query(load => $load, rule => $1)
556 . "\">$1</a></strong>"!gme;
557 }
558 0 $c->render(template => 'show',
559 id => to_id($rule),
560 rule => $rule,
561 jump => $jump,
562 load => $load,
563 table => $table);
564 6 } => 'rule_show';
565  
566 =item any /describe/text
567  
568 This is where the text input is rendered. B<input> is the text, B<url> is the
569 URL to an external table. If not provided, B<table> is the text of the table. If
570 neither is provided, the default table is used.
571  
572 To call this from the command line:
573  
574 hex-describe get /describe/text --form input=[village] --form load=schroeder
575  
576 Pipe through C<lynx -stdin -dump -nolist> to get text instead of HTML.
577  
578 =cut
579  
580 any '/describe/text' => sub {
581 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
582 0 my $rule = $c->param('rule');
583 0 my $load = $c->param('load');
584 0 my $n = $c->param('n');
585 0 my $input = $c->param('input');
586 0 my $url = $c->param('url');
587 0 my $table = $c->param('table');
588 0 my $seed = $c->param('seed');
589 0 ▶ srand($seed) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
590 0 my $data = get_table($c); # must be scalar context
591 0 ▶ $c->render(template => 'text', input => $input, load => $load, seed => $seed,
Branch: 1 branch
BranchTrueFalseTotal
$c->param('log') ? : 0 0 0
592 n => $n, url => $url, table => $table,
593 rule => $rule, id => to_id($rule),
594 log => $c->param('log') ? $log->history : undef,
595 descriptions => describe_text($input, parse_table($data)));
596 6 };
597  
598 =item get /default/map
599  
600 This shows you the default map.
601  
602 =cut
603  
604 get '/default/map' => sub {
605 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 0 $c->render(text => load_map('default', app->config('contrib')), format => 'txt');
607 6 };
608  
609 =item get /schroeder/table
610  
611 This shows you the table by Alex Schroeder.
612  
613 =cut
614  
615 get '/schroeder/table' => sub {
616 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 $c->render(text => load_table('schroeder', app->config('contrib')), format => 'txt');
618 6 };
619  
620 =item get /seckler/table
621  
622 This shows you the table by Peter Seckler.
623  
624 =cut
625  
626 get '/seckler/table' => sub {
627 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
628 0 $c->render(text => load_table('seckler', app->config('contrib')), format => 'txt');
629 6 };
630  
631 =item get /strom/table
632  
633 This shows you the table by Matt Strom.
634  
635 =cut
636  
637 get '/strom/table' => sub {
638 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
639 0 $c->render(text => load_table('strom', app->config('contrib')), format => 'txt');
640 6 };
641  
642 =item get /johnston/table
643  
644 This shows you the table by Josh Johnston.
645  
646 =cut
647  
648 get '/johnston/table' => sub {
649 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
650 0 $c->render(text => load_table('johnston', app->config('contrib')), format => 'txt');
651 6 };
652  
653 =item get /traveller/table
654  
655 This shows you the Traveller table by Vicky Radcliffe and Alex Schroeder.
656  
657 =cut
658  
659 get '/traveller/table' => sub {
660 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
661 0 $c->render(text => load_table('traveller', app->config('contrib')), format => 'txt');
662 6 };
663  
664 =item get /rorschachhamster/table
665  
666 Für die deutschen Tabellen von Rorschachhamster Alex Schroeder.
667  
668 =cut
669  
670 get '/rorschachhamster/table' => sub {
671 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
672 0 $c->render(text => load_table('rorschachhamster', app->config('contrib')), format => 'txt');
673 6 };
674  
675 =item get /source
676  
677 This gets you the source code of Hex Describe in case the source repository is
678 no longer available.
679  
680 =cut
681  
682 get '/source' => sub {
683 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
684 0 seek(DATA,0,0);
685 0 local $/ = undef;
686 0 $c->render(text => <DATA>, format => 'txt');
687 6 };
688  
689 =item get /authors
690  
691 This lists the contributors to Hex Describe.
692  
693 =cut
694  
695 get '/authors' => sub {
696 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
697 0 $c->render(template => 'authors');
698 6 };
699  
700 =item get /help
701  
702 This shows you a little tutorial. Unlike this documentation, which is for
703 programmers, the tutorial is for the users of the app.
704  
705 =cut
706  
707 get '/help' => sub {
708 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
709 0 $c->render(template => 'help');
710 6 };
711  
712 =back
713  
714 =head2 Code
715  
716 This chapter is used to document the code.
717  
718 =over 4
719  
720 =item get_data
721  
722 This is is the basic work horse to get data from a URL. It is used to download
723 the table from a URL, if provided. This uses a simple GET request.
724  
725 =cut
726  
727 sub get_data {
728 0 ▶ my $url = shift;
Subroutine get_data: not called CC=2 SCAR=17.9
Pod documented
729 0 $log->debug("get_data: $url");
730 0 my $ua = Mojo::UserAgent->new;
731 0 my $res = $ua->get($url)->result;
732 0 ▶ return decode_utf8($res->body) if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
733 0 $log->error("get_data: " . $res->code . " " . $res->message);
734 }
735  
736 =item get_post_data
737  
738 This is is used to get data from a URL when we need a POST request instead of a
739 GET request. We need this for Text Mapper when rendering the map since we send
740 the entire map to Text Mapper in order to render it. A simple GET request will
741 not do.
742  
743 =cut
744  
745 sub get_post_data {
746 0 ▶ my $url = shift;
Subroutine get_post_data: not called CC=4 SCAR=30.0
Pod documented
747 0 my %data = @_;
748 0 $log->debug("get_post_data: $url");
749 0 my $ua = Mojo::UserAgent->new;
750 0 my $tx = $ua->post($url => form => \%data);
751 0 my $error;
752 0 ▶ if (my $err = $tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) { } 0 0 0
753 0 ▶ if ($err->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err->{'code'}) { } 0 0 0
754 0 $error = $err->{code} . " " . $err->{message};
755 } else {
756 0 $error = $err->{message};
757 }
758 } else {
759 0 my $res = $ua->post($url => form => \%data)->result;
760 0 ▶ return decode_utf8($res->body) if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
761 0 $error = $res->code . " " . $res->message;
762 }
763 0 $log->error("get_post_data: $error");
764 0 return "<p>There was an error when attempting to load the map ($error).</p>";
765 }
766  
767 =item get_table
768  
769 This function gets a Mojolicious Controller object and looks for C<map>,
770 C<load>, C<url> and C<table> parameters in order to determine the table data to
771 use.
772  
773 =cut
774  
775 sub get_table {
776 3 ▶ my $c = shift;
Subroutine get_table: called CC=5 SCAR=16.4
Pod documented
777 3 my $load = $c->param('load');
778 3 my $url = $c->param('url');
779 3 my $table = '';
780 3 ▶ $table .= get_data($url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 3 3
781 3 ▶ $table .= load_table($load, app->config('contrib')) if $load;
Branch: 1 branch
BranchTrueFalseTotal
if $load 3 0 3
782 # the table in the text area comes at the end and overrides the defaults
783 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
784 3 ▶ return $url, $table if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 2 3
785 2 return $table;
786 }
787  
788 =item add_links
789  
790 After we get the SVG map from Text Mapper, we need to add links to the hex
791 descriptions. Text Mapper already allows us to define an URL such that I<labels>
792 get linked to that URL. This feature is of no use to us because we're not using
793 labels. Basically, we want to add links to the I<coordinates>. This function
794 does that: it goes through the SVG and adds appropriate anchor elements.
795  
796 =cut
797  
798 sub add_links {
799 0 ▶ my $svg = shift;
Subroutine add_links: not called CC=1 SCAR=6.9
Pod documented
800 0 $svg =~ s/<\?xml[^>]*>\s*//g; # remove processing instruction
801 0 my $dom = Mojo::DOM->new($svg);
802 $dom->find('g#coordinates text')
803 ->each(sub {
804 0 ▶ my $text = $_->text;
Subroutine __ANON__: not called CC=1 SCAR=6.9
805 0 $text =~ s/\.//; # strip dot
806 0 $_->wrap(qq{<a xlink:href="#desc$text"></a>})});
807 0 return "$dom";
808 }
809  
810 =item helper example
811  
812 This Mojolicious helper is used on the help page to make all the examples
813 clickable.
814  
815 =cut
816  
817 helper example => sub {
818 0 ▶ my ($c, $block) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
819 0 my $result = $block->();
820 0 my $url;
821 0 ▶ if ($result =~ /^\d\d\d\d/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /^\d\d\d\d/mu) { } 0 0 0
822 0 my $map = join("\n", grep(/^\d\d\d\d|^include/, split(/\n/, $result)));
823 0 my $table = join("\n", grep(!/^\d\d\d\d|^include/, split(/\n/, $result)));
824 0 $url = $c->url_for('edit')->query(map => $map,
825 load => 'none',
826 table=> html_unescape($table));
827 } else {
828 0 my ($key) = $result =~ /^;(.*)/m;
829 0 $url = $c->url_for('nomap')->query(input => "[$key]\n" x 10,
830 load => 'none',
831 table=> html_unescape($result));
832 }
833 0 return Mojo::ByteStream->new(qq(<pre>$result</pre><p><a href='$url'>Try it</a>.</p>));
834 6 };
835  
836 =back
837  
838 =head2 Finally
839  
840 Start the app at the very end. The rest is templates for the various web pages.
841  
842 =cut
843  
844 6 ▶app->start || 1;
Branch: 1 branch
BranchTrueFalseTotal
app()->start or 1 0 6 6
845  
846 __DATA__
« blib/lib/Game/HexDescribe/Utils.pm ↑ Summary lib/Game/HexDescribe/Command/rule.pm »