lib/Game/CharacterSheetGenerator/Command/random.pm

filter: Statementstmt 82.4% 47 / 57 Branchbran 50.0% 4 / 8 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 50.0% 1 / 2 totaltotal 75.9% 63 / 83 CC 10 SCAR 20.3
CC
10
Cov
77%
run
14.7
init
2.0
__ANON__
1.3
CRAP 7.6
« blib/lib/Game/CharacterSheetGenerator/ElfNames.pm ↑ Summary blib/lib/Game/CharacterSheetGenerator/Command/multiple.pm »
1 # Copyright (C) 2022 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::CharacterSheetGenerator::Command::random
19  
20 =head1 SYNOPSIS
21  
22 character-sheet-generator random [language] [format] [key=value ...]
23 character-sheet-generator random help
24  
25 =head1 DESCRIPTION
26  
27 This prints a random character SVG document to STDOUT.
28  
29 character-sheet-generator random > test.svg
30  
31 =head1 OPTIONS
32  
33 The supported languages are C<en> and C<de>. English is the default.
34  
35 The supported formats are C<SVG> and C<text>. SVG is the default.
36  
37 You can provide additional key-value pairs. These can be combined. Class names
38 are case sensitive.
39  
40 ```
41 character-sheet-generator random de text name=Alex
42 character-sheet-generator random en text class=elf
43 character-sheet-generator random en text class=elf name=Alex
44 ```
45  
46 C<help> prints the man page.
47  
48 =cut
49  
50 package Game::CharacterSheetGenerator::Command::random;
51  
52 1 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Game::CharacterSheetGenerator;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Pod::Simple::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use Encode::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 binmode(STDOUT, ':utf8');
61  
62 1 has description => 'Print a random character sheet to STDOUT';
63  
64 0 ▶has usage => sub { my $self = shift; $self->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=2.6
65  
66 sub init {
67 1 ▶ my %char = ();
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
68 1 my @provided;
69 1 for my $arg (@_) {
70 1 my ($key, $value) = split(/=/, $arg, 2);
71 1 push(@provided, $key);
72 1 $char{$key} = decode(locale => $value);
73 }
74 1 $char{provided} = \@provided;
75 1 return \%char;
76 }
77  
78 sub run {
79 1 ▶ my ($self, $lang, $format, @args) = @_;
Subroutine run: called CC=9 SCAR=26.8
Pod documented
80 1 ▶ $lang ||= 'en';
Condition50%1/2
$lang ||= "en"
l!l
10
MC/DC0%0/1
$lang ||= "en"
$lang
Truth table50%1/2
A
A= $lang
Aresult
11
00
81 1 ▶ $format ||= 'SVG';
Condition50%1/2
$format ||= "SVG"
l!l
10
MC/DC0%0/1
$format ||= "SVG"
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
82 1 ▶ if ($lang eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq "help") 0 1 1
83 0 seek(DATA, 0, 0); # read from this file
84 0 my $parser = Pod::Simple::Text->new();
85 0 $parser->output_fh(*STDOUT);
86 0 $parser->parse_lines(<DATA>);
87 0 return 1;
88 }
89 1 my $char = init(@args);
90 1 Game::CharacterSheetGenerator::random_parameters($char, $lang, "portrait");
91 1 Game::CharacterSheetGenerator::compute_data($char, $lang);
92 1 ▶ if ($format eq 'SVG') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'SVG') { } 0 1 1
elsif ($format eq 'text') { } 1 0 1
93 0 my $svg = Game::CharacterSheetGenerator::svg_transform(undef, Game::CharacterSheetGenerator::svg_read($char));
94 0 print $svg->toString();
95 } elsif ($format eq 'text') {
96 1 for my $key (@{$char->{provided}}) {
97 27 ▶ next unless defined $char->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char->{$key} 0 27 27
98 27 for my $value (split(/\\\\/, $char->{$key})) {
99 25 say "$key: $value";
100 }
101 }
102 } else {
103 0 warn "Unknown format '$format'\n";
104 }
105 1 return 1;
106 }
107  
108 1 1;
109  
110 __DATA__
« blib/lib/Game/CharacterSheetGenerator/ElfNames.pm ↑ Summary blib/lib/Game/CharacterSheetGenerator/Command/multiple.pm »