« lib/Game/CharacterSheetGenerator/Command/random.pm
↑ Summary
blib/lib/Game/CharacterSheetGenerator/Command/stats.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::multiple | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | character-sheet-generator multiple [language] [key=value ...] | ||
| 23 | character-sheet-generator multiple help | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This prints multiple characters to STDOUT. | ||
| 28 | |||
| 29 | character-sheet-generator multiple > npc.txt | ||
| 30 | |||
| 31 | =head1 OPTIONS | ||
| 32 | |||
| 33 | The supported languages are C<en> and C<de>. English is the default. | ||
| 34 | |||
| 35 | You can provide additional key-value pairs. These can be combined. Class names | ||
| 36 | are case sensitive. | ||
| 37 | |||
| 38 | ``` | ||
| 39 | character-sheet-generator multiple en class=elf | ||
| 40 | ``` | ||
| 41 | |||
| 42 | C<help> prints the man page. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | package Game::CharacterSheetGenerator::Command::multiple; | ||
| 47 | |||
| 48 | use Modern::Perl '2018'; | ||
| 49 | use Mojo::Base 'Mojolicious::Command'; | ||
| 50 | use Game::CharacterSheetGenerator; | ||
| 51 | use Pod::Simple::Text; | ||
| 52 | use Role::Tiny; | ||
| 53 | use Encode::Locale; | ||
| 54 | use Encode; | ||
| 55 | |||
| 56 | binmode(STDOUT, ':utf8'); | ||
| 57 | |||
| 58 | has description => 'Print a bunch of characters to STDOUT'; | ||
| 59 | |||
| 60 | has usage => sub { my $self = shift; $self->extract_usage }; | ||
| 61 | |||
| 62 | sub init { | ||
| 63 | my %char = (); | ||
| 64 | my @provided; | ||
| 65 | for my $arg (@_) { | ||
| 66 | my ($key, $value) = split(/=/, $arg, 2); | ||
| 67 | push(@provided, $key); | ||
| 68 | $char{$key} = decode(locale => $value); | ||
| 69 | } | ||
| 70 | $char{provided} = \@provided; | ||
| 71 | return \%char; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub wrap { | ||
| 75 | my $line = ""; | ||
| 76 | for (@_) { | ||
| 77 | if (length($line) + length > 72) { | ||
| 78 | print $line . ",\n"; | ||
| 79 | $line = ""; | ||
| 80 | } | ||
| 81 | $line .= ", " if $line; | ||
| 82 | $line .= $_; | ||
| 83 | } | ||
| 84 | print $line . "\n" if $line; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub print_char { | ||
| 88 | my ($char, $lang) = @_; | ||
| 89 | say $char->{traits}; | ||
| 90 | if ($lang eq 'de') { | ||
| 91 | say "Kraft Gesc. Gesu. Bild. Weis. Auft. TP RK Klasse"; | ||
| 92 | say join(" ", map { sprintf("%4d", $_) } | ||
| 93 | $char->{str}, $char->{dex}, $char->{con}, $char->{int}, | ||
| 94 | $char->{wis}, $char->{cha}) | ||
| 95 | . " " . join(" ", map { sprintf("%2d", $_) } | ||
| 96 | $char->{hp}, $char->{ac}) | ||
| 97 | . " " . $char->{class}; | ||
| 98 | } else { | ||
| 99 | say "Str Dex Con Int Wis Cha HP AC Class"; | ||
| 100 | say join(" ", map { sprintf("%3d", $_) } | ||
| 101 | $char->{str}, $char->{dex}, $char->{con}, $char->{int}, | ||
| 102 | $char->{wis}, $char->{cha}) | ||
| 103 | . " " . join(" ", map { sprintf("%2d", $_) } | ||
| 104 | $char->{hp}, $char->{ac}) | ||
| 105 | . " " . $char->{class}; | ||
| 106 | } | ||
| 107 | wrap(split(/\\\\/, $char->{property})); | ||
| 108 | say ""; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub run { | ||
| 112 | my ($self, $lang, @args) = @_; | ||
| 113 | $lang ||= 'en'; | ||
| 114 | if ($lang eq 'help') { | ||
| 115 | seek(DATA, 0, 0); # read from this file | ||
| 116 | my $parser = Pod::Simple::Text->new(); | ||
| 117 | $parser->output_fh(*STDOUT); | ||
| 118 | $parser->parse_lines(<DATA>); | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | my $char = init(@args); | ||
| 122 | my $characters = Game::CharacterSheetGenerator::characters($char, $lang); | ||
| 123 | print_char($_, $lang) for @$characters; | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | __DATA__ |