| 1 | package Game::Terminal::Kit::Save; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Game::Terminal::Kit::Line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | our $FORMAT = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | my $MAGIC = 'game-terminal-kit save'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | my %BLOCK = map { $_ => 1 } qw/past current future/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | my $NAME = qr/\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | has [qw/name version saved_at file/] => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | isa => Str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | has current => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | isa => Str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | has [qw/past future/] => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => ArrayRef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | has settings => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => HashRef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | has limit => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => Int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 117 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 117 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 117 | ▶ | $self->past([]) unless $self->past; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 117 | ▶ | $self->future([]) unless $self->future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 117 | ▶ | $self->settings({}) unless $self->settings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 117 | $self->check; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 104 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 62 | ▶ | sub _fail { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 62 | my ($why) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 62 | Carp::croak("Game::Terminal::Kit::Save: $why"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 156 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=23 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 156 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 156 | my $name = $self->name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 156 | ▶ | _fail('a save needs the name of the game') unless defined $name && !ref $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and not ref $name
MC/DC50%1/2
defined $name and not ref $name
defined $name
not ref $name
Truth table66%2/3
A and not(B)
A= defined $nameB= not ref $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 154 | ▶ | _fail("'$name' is not a name a save can carry") unless $name =~ $NAME; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 151 | for my $field (qw/version saved_at/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 301 | my $value = $self->$field; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 301 | ▶ | _fail("the $field is not one line of text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
defined $value and ref $value || $value =~ /[\r\n]/u || !($value =~ /\S/u)
ref $value or $value =~ /[\r\n]/u or not $value =~ /\S/u
ref $value or $value =~ /[\r\n]/u
MC/DC50%2/4
defined $value and ref $value || $value =~ /[\r\n]/u || !($value =~ /\S/u)
defined $value
ref $value
$value =~ /[\r\n]/u
not $value =~ /\S/u
Truth table60%3/5
A and B or C or not(D)
A= defined $valueB= ref $valueC= $value =~ /[\r\n]/uD= not $value =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | if defined $value && (ref $value || $value =~ m/[\r\n]/ || $value !~ m/\S/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 150 | ▶ | _fail('the limit is not a whole number') if defined $self->limit && $self->limit !~ m/\A\d+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->limit and not $self->limit =~ /\A\d+\z/u
MC/DC100%2/2
defined $self->limit and not $self->limit =~ /\A\d+\z/u
defined $self->limit
not $self->limit =~ /\A\d+\z/u
Truth table100%3/3
A and not(B)
A= defined $self->limitB= not $self->limit =~ /\A\d+\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 149 | ▶ | _fail('a save needs the game as it is now') unless defined $self->current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 148 | for my $which (qw/past future/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 295 | my $steps = $self->$which; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 295 | ▶ | _fail("the $which is not a list of steps") unless ref $steps eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 295 | ▶ | _fail("a step of the $which is not text") if grep { !defined || ref } @{$steps}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
!defined($_) || ref $_
MC/DC100%2/2
!defined($_) || ref $_
!defined($_)
ref $_
Truth table100%3/3
not(A) || B
A= !defined($_)B= ref $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 146 | ▶ | _fail('the game as it is now is not text') if ref $self->current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 144 | my $settings = $self->settings; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 144 | ▶ | _fail('the settings are not a hash') unless ref $settings eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 144 | for my $key (sort keys %{$settings}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 41 | ▶ | _fail("'$key' is not a name a setting can have") unless $key =~ $NAME; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 40 | my $value = $settings->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 40 | ▶ | _fail("the setting $key is not one line of text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $value and ref $value || $value =~ /[\r\n]/u
ref $value or $value =~ /[\r\n]/u
MC/DC66%2/3
defined $value and ref $value || $value =~ /[\r\n]/u
defined $value
ref $value
$value =~ /[\r\n]/u
Truth table75%3/4
A and B or C
A= defined $valueB= ref $valueC= $value =~ /[\r\n]/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | if defined $value && (ref $value || $value =~ m/[\r\n]/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 142 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 57 | ▶ | sub steps { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
steps: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 57 | ▶ | return $self->count unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 55 | return (@{ $self->past }, $self->current, @{ $self->future }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 23 | ▶ | sub at { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 23 | return scalar @{ $self->past }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub count { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | return @{ $self->past } + 1 + @{ $self->future }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | sub from_step { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_step: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 8 | my ($self, $index) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | my @steps = $self->steps; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | ▶ | _fail("there is no step $index") unless defined $index && $index =~ m/\A\d+\z/ && $index < @steps; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $index and $index =~ /\A\d+\z/u and $index < @steps
defined $index and $index =~ /\A\d+\z/u
MC/DC66%2/3
defined $index and $index =~ /\A\d+\z/u and $index < @steps
defined $index
$index =~ /\A\d+\z/u
$index < @steps
Truth table75%3/4
A and B and C
A= defined $indexB= $index =~ /\A\d+\z/uC= $index < @steps
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | return ref($self)->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 30 | ▶ | (map { defined $self->$_ ? ($_ => $self->$_) : () } qw/name version saved_at file limit/), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | settings => { %{ $self->settings } }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | past => [ @steps[0 .. $index - 1] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | current => $steps[$index], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | future => [ @steps[$index + 1 .. $#steps] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | ▶ | sub _now { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_now: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | my @t = gmtime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 11 | return sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', $t[5] + 1900, $t[4] + 1, @t[3, 2, 1, 0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 194 | ▶ | sub _block { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_block: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 194 | my ($keyword, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 194 | ▶ | return ($keyword, map { length $_ ? "| $_" : '|' } split /\n/, $text, -1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 39 | ▶ | sub as_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_text: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 39 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 39 | $self->check; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 38 | my $settings = $self->settings; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | my @lines = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | "$MAGIC $FORMAT", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 'game ' . $self->name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | (defined $self->version ? 'version ' . $self->version : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | (defined $self->saved_at ? 'saved ' . $self->saved_at : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | (defined $self->limit ? 'limit ' . $self->limit : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 11 | (map { "setting $_ $settings->{$_}" } grep { defined $settings->{$_} } sort keys %{$settings}), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 110 | (map { _block(past => $_) } @{ $self->past }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | _block(current => $self->current), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 38 | ▶ | (map { _block(future => $_) } @{ $self->future }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 'end', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 38 | return join '', map { "$_\n" } @lines; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 78 | ▶ | sub from_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_text: called CC=50 SCAR=39.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 78 | my ($class, $text, %expect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 78 | ▶ | my $source = defined $expect{source} ? $expect{source} : 'the text'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 39 | ▶ | my $refuse = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 39 | my ($number, $why) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 39 | ▶ | _fail("$source " . (defined $number ? "line $number: " : '') . $why); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 78 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 78 | ▶ | $refuse->(undef, 'is not text') unless defined $text && !ref $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and not ref $text
MC/DC50%1/2
defined $text and not ref $text
defined $text
not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 77 | my @lines = split /\n/, $text, -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 77 | ▶ | pop @lines if @lines && $lines[-1] eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@lines and $lines[-1] eq ""
MC/DC100%2/2
@lines and $lines[-1] eq ""
@lines
$lines[-1] eq ""
Truth table100%3/3
A and B
A= @linesB= $lines[-1] eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 77 | ▶ | my $crlf = @lines && $lines[0] =~ m/\r\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
@lines && $lines[0] =~ /\r\z/u
MC/DC100%2/2
@lines && $lines[0] =~ /\r\z/u
@lines
$lines[0] =~ /\r\z/u
Truth table100%3/3
A && B
A= @linesB= $lines[0] =~ /\r\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 77 | ▶ | s/\r\z// for $crlf ? @lines : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 77 | ▶ | my $first = @lines ? $lines[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 77 | ▶ | $refuse->(undef, 'is not a saved game') unless $first =~ m/\A\Q$MAGIC\E (\d+)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 73 | ▶ | $refuse->(undef, "was saved in format $1, newer than the format $FORMAT this Game::Terminal::Kit reads") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | if $1 > $FORMAT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 72 | my (%head, %settings, @past, @future, $current, $in, $ended); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 72 | my $number = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 72 | for my $line (@lines[1 .. $#lines]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 668 | $number++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 668 | ▶ | if ($ended) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | $refuse->($number, 'there is more after the end') if $line =~ m/\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 665 | ▶ | if ($line =~ m/\A\| ?(.*)\z/s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 268 | ▶ | $refuse->($number, 'a line of a step before any step began') unless $in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 267 | ▶ | push @{ $in->{lines} }, defined $1 ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 267 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 397 | ▶ | if ($BLOCK{$line}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 231 | ▶ | $refuse->($number, 'a second current step') if $line eq 'current' && defined $current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line eq "current" and defined $current
MC/DC100%2/2
$line eq "current" and defined $current
$line eq "current"
defined $current
Truth table100%3/3
A and B
A= $line eq "current"B= defined $current
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 230 | ▶ | $refuse->($number, 'a past step after the current one') if $line eq 'past' && defined $current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line eq "past" and defined $current
MC/DC100%2/2
$line eq "past" and defined $current
$line eq "past"
defined $current
Truth table100%3/3
A and B
A= $line eq "past"B= defined $current
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 229 | ▶ | $refuse->($number, 'a future step before the current one') if $line eq 'future' && !defined $current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line eq "future" and not defined $current
MC/DC100%2/2
$line eq "future" and not defined $current
$line eq "future"
not defined $current
Truth table100%3/3
A and not(B)
A= $line eq "future"B= not defined $current
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 228 | my $step = { lines => [] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 228 | ▶ | if ($line eq 'past') { push @past, $step } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 47 | elsif ($line eq 'future') { push @future, $step } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 56 | else { $current = $step } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 228 | $in = $step; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 228 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 166 | ▶ | if ($line eq 'end') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 48 | $ended = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 48 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 118 | ▶ | next if !$in && $line !~ m/\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in or $line =~ /\S/u
MC/DC100%2/2
$in or $line =~ /\S/u
$in
$line =~ /\S/u
Truth table100%3/3
A or B
A= $inB= $line =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 116 | ▶ | my ($key, $value) = $line =~ m/\A([a-z]+)(?: (.*))?\z/s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | or $refuse->($number, "'$line' is not a line a saved game has"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 115 | ▶ | $refuse->($number, "'$key' after the steps began") if $in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 114 | ▶ | $refuse->($number, "the $key line has no value") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $value and $key =~ /\A(?:game|version|saved|limit|setting)\z/u
MC/DC50%1/2
not defined $value and $key =~ /\A(?:game|version|saved|limit|setting)\z/u
not defined $value
$key =~ /\A(?:game|version|saved|limit|setting)\z/u
Truth table66%2/3
not(A) and B
A= not defined $valueB= $key =~ /\A(?:game|version|saved|limit|setting)\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | if !defined $value && $key =~ m/\A(?:game|version|saved|limit|setting)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 113 | ▶ | if ($key eq 'setting') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 23 | ▶ | my ($name, $setting) = defined $value ? $value =~ m/\A(\S+) (.*)\z/s : () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | or $refuse->($number, 'a setting needs a name and a value'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 22 | ▶ | $refuse->($number, "the setting $name is there twice") if exists $settings{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 21 | $settings{$name} = $setting; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 21 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 90 | ▶ | $refuse->($number, "the $key is there twice") if exists $head{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 89 | $head{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 61 | ▶ | $refuse->(undef, 'ends before its last line, and may have been cut short') unless $ended; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 47 | ▶ | $refuse->(undef, 'has no game line') unless defined $head{game}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 46 | ▶ | $refuse->(undef, 'has no current step') unless defined $current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | $refuse->(undef, "is a saved game of $head{game}, not of $expect{game}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 45 | ▶ | if defined $expect{game} && $head{game} ne $expect{game}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $expect{"game"} and $head{"game"} ne $expect{"game"}
MC/DC100%2/2
defined $expect{"game"} and $head{"game"} ne $expect{"game"}
defined $expect{"game"}
$head{"game"} ne $expect{"game"}
Truth table100%3/3
A and B
A= defined $expect{"game"}B= $head{"game"} ne $expect{"game"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | $refuse->(undef, 'has a limit that is not a whole number') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 41 | ▶ | if defined $head{limit} && $head{limit} !~ m/\A\d+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $head{"limit"} and not $head{"limit"} =~ /\A\d+\z/u
MC/DC100%2/2
defined $head{"limit"} and not $head{"limit"} =~ /\A\d+\z/u
defined $head{"limit"}
not $head{"limit"} =~ /\A\d+\z/u
Truth table100%3/3
A and not(B)
A= defined $head{"limit"}B= not $head{"limit"} =~ /\A\d+\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 40 | ▶ | my $text_of = sub { join "\n", @{ $_[0]{lines} } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 40 | my $save = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | name => $head{game}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | (defined $head{version} ? (version => $head{version}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | (defined $head{saved} ? (saved_at => $head{saved}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | (defined $head{limit} ? (limit => $head{limit} + 0) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | settings => \%settings, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 88 | past => [ map { $text_of->($_) } @past ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | current => $text_of->($current), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 40 | ▶ | future => [ map { $text_of->($_) } @future ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 40 | ▶ | unless ($save) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | (my $why = $@) =~ s/\AGame::Terminal::Kit::Save: //; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | $why =~ s/ at \S+ line \d+\.?\n?\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | $refuse->(undef, $why); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 39 | return $save; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | ▶ | sub write_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=11 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 12 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 12 | ▶ | _fail('write_file needs a file to write') unless defined $path && length $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and length $path
MC/DC50%1/2
defined $path and length $path
defined $path
length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 11 | my $keep = $self->saved_at; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | $self->saved_at(_now()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 11 | my $text = eval { $self->as_text }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 11 | ▶ | unless (defined $text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | my $why = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | $self->saved_at($keep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | die $why; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | my $temporary = "$path.$$.tmp"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 10 | my $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 10 | ▶ | unless (open $fh, '>:raw:encoding(UTF-8)', $temporary) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | my $why = $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | $self->saved_at($keep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | _fail("cannot write $path: $why"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 9 | my $wrote = print {$fh} $text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 9 | my $why = $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 9 | ▶ | $wrote = close($fh) && $wrote; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
close $fh && $wrote
MC/DC0%0/2
close $fh && $wrote
close $fh
$wrote
Truth table33%1/3
A && B
A= close $fhB= $wrote
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 9 | ▶ | $why = $! unless $wrote || $why; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$wrote or $why
MC/DC0%0/2
$wrote or $why
$wrote
$why
Truth table33%1/3
A or B
A= $wroteB= $why
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 9 | ▶ | $wrote &&= rename $temporary, $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$wrote &&= rename($temporary, $path)
MC/DC50%1/2
$wrote &&= rename($temporary, $path)
$wrote
rename($temporary, $path)
Truth table66%2/3
A &&= B
A= $wroteB= rename($temporary, $path)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 9 | ▶ | unless ($wrote) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | ▶ | $why = $! if !$why; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 1 | unlink $temporary; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 1 | $self->saved_at($keep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | _fail("cannot write $path: $why"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 8 | $self->file($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 8 | return $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 21 | ▶ | sub read_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 21 | my ($class, $path, %expect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 21 | ▶ | _fail('read_file needs a file to read') unless defined $path && length $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 21 | ▶ | _fail("there is no file $path") unless -e $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 19 | ▶ | _fail("$path is a directory, not a saved game") if -d $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 18 | ▶ | open my $fh, '<:raw', $path or _fail("cannot read $path: $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 18 | my $bytes = do { local $/; readline $fh }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 18 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 18 | ▶ | $bytes = '' unless defined $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 18 | ▶ | _fail("$path is not a saved game") unless utf8::decode($bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 17 | my $save = $class->from_text($bytes, %expect, source => $path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 12 | $save->file($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 12 | return $save; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 11 | ▶ | sub read_game { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_game: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 11 | my ($class, $path, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 11 | ▶ | my $save = eval { $class->read_file($path, (defined $name ? (game => $name) : ())) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 11 | ▶ | return $save if $save; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 5 | return (undef, why($@)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | ▶ | sub saves_in { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
saves_in: called CC=13 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | my ($dir, %option) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | $dir = '.' unless defined $dir && length $dir; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dir and length $dir
MC/DC0%0/2
defined $dir and length $dir
defined $dir
length $dir
Truth table33%1/3
A and B
A= defined $dirB= length $dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 3 | ▶ | opendir my $listing, $dir or return (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | ▶ | my @names = sort grep { m/\.save\z/ && !m/\A\./ } readdir $listing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
/\.save\z/u && !/\A\./u
MC/DC50%1/2
/\.save\z/u && !/\A\./u
/\.save\z/u
!/\A\./u
Truth table66%2/3
A && not(B)
A= /\.save\z/uB= !/\A\./u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 2 | closedir $listing; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 2 | my @found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | for my $name (@names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 10 | ▶ | my $path = $dir eq '.' ? $name : File::Spec->catfile($dir, $name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 10 | ▶ | next unless -f $path && -s _ <= 16 * 1024 * 1024; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $path and -s _ <= 16777216
MC/DC50%1/2
-f $path and -s _ <= 16777216
-f $path
-s _ <= 16777216
Truth table66%2/3
A and B
A= -f $pathB= -s _ <= 16777216
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 8 | my ($save) = __PACKAGE__->read_game($path, $option{game}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 8 | ▶ | push @found, $save if $save; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | return sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | ▶ | (defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') || $a->file cmp $b->file
MC/DC0%0/2
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') || $a->file cmp $b->file
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '')
$a->file cmp $b->file
Truth table66%2/3
A || B
A= (defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '')B= $a->file cmp $b->file
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | || $a->file cmp $b->file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } @found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 9 | ▶ | sub why { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
why: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 9 | my ($error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 9 | ▶ | $error = 'it failed, and said nothing of why' unless defined $error && length $error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $error and length $error
MC/DC50%1/2
defined $error and length $error
defined $error
length $error
Truth table66%2/3
A and B
A= defined $errorB= length $error
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 9 | $error =~ s/ at \S+ line \d+\.?\s*\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 9 | $error =~ s/\A(?:[A-Z]\w*(?:::\w+)*): //; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 9 | $error =~ s/\.?\s*\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 9 | return $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 2 | ▶ | sub default_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_file: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | ▶ | return (defined $name && length $name ? $name : 'game') . '.save'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and length $name
MC/DC0%0/2
defined $name and length $name
defined $name
length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 1 | ▶ | sub flags { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 1 | return ('save=s', 'resume=s', 'replay=s'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 7 | ▶ | sub check_flags { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_flags: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 7 | my ($given) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 7 | my @set = grep { defined $given->{$_} } qw/save resume replay/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 7 | for my $flag (@set) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 9 | ▶ | return "--$flag needs the name of a file" unless length $given->{$flag}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | return '--resume and --replay each start from a saved game: give one of them' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 6 | ▶ | if defined $given->{resume} && defined $given->{replay}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $given->{"resume"} and defined $given->{"replay"}
MC/DC100%2/2
defined $given->{"resume"} and defined $given->{"replay"}
defined $given->{"resume"}
defined $given->{"replay"}
Truth table100%3/3
A and B
A= defined $given->{"resume"}B= defined $given->{"replay"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | return '--save is where a game is written, and --replay writes nothing' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 5 | ▶ | if defined $given->{save} && defined $given->{replay}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $given->{"save"} and defined $given->{"replay"}
MC/DC100%2/2
defined $given->{"save"} and defined $given->{"replay"}
defined $given->{"save"}
defined $given->{"replay"}
Truth table100%3/3
A and B
A= defined $given->{"save"}B= defined $given->{"replay"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 4 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 2 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 2 | return <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | --save FILE where S, or "save", writes the game. Default: the file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | the game was resumed from, or NAME.save. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | --resume FILE carry on a game saved to FILE, undo and redo and all. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | --replay FILE watch a game saved to FILE, a step at a time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | USAGE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 6 | ▶ | sub command { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
command: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 6 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 6 | ▶ | return () unless defined $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 5 | ▶ | return () unless $line =~ m/\A\s*save(?:\s+(.*?))?\s*\z/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 3 | ▶ | return (1, defined $1 && length $1 ? $1 : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $1 and length $1
MC/DC50%1/2
defined $1 and length $1
defined $1
length $1
Truth table66%2/3
A and B
A= defined $1B= length $1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 1 | ▶ | sub game_keys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
game_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | return { S => 'save' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 6 | ▶ | sub ask_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ask_file: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 6 | my ($keys, $screen, %option) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 6 | ▶ | my $default = defined $option{default} ? $option{default} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 6 | ▶ | my $prompt = defined $option{prompt} ? $option{prompt} : 'save to: '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 6 | ▶ | unless ($keys->raw) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 3 | my $line = $keys->read_line("$prompt\[$default] ", $screen->out); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 3 | ▶ | return undef unless defined $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 2 | ▶ | return length $line ? $line : (length $default ? $default : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | my $line = Game::Terminal::Kit::Line->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | prompt => $prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | text => $default, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 3 | ▶ | head => [ @{ $option{head} || [] } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$option{'head'} || []
MC/DC0%0/1
$option{'head'} || []
$option{'head'}
Truth table50%1/2
A
A= $option{'head'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | screen => $screen, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 3 | my ($event, $value) = $line->run($keys, $screen); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 3 | ▶ | return undef unless $event eq 'chosen'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 2 | $value =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 2 | ▶ | return length $value ? $value : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||