Kit/Replay.pm

filter: Statementstmt 100% 138 / 138 Branchbran 93.1% 82 / 88 Conditioncond 86.0% 43 / 50 MC/DCmcdc 74.0% 20 / 27 Subroutinesub 100% 17 / 17 Podpod 90.9% 10 / 11 totaltotal 93.6% 310 / 331 CC 63 SCAR 24.1
CC
63
Cov
95%
step
22.1
BUILD
14.3
play
8.0
CRAP 11.1
« Kit/Pixels.pm ↑ Summary Kit/Steer.pm »
1 package Game::Terminal::Kit::Replay;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Game::Terminal::Kit::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.06';
12  
13 has steps => (
14 is => 'rw',
15 isa => ArrayRef
16 3 );
17  
18 has [qw/load draw/] => (
19 is => 'rw',
20 isa => CodeRef
21 3 );
22  
23 has [qw/head legend/] => (
24 is => 'rw',
25 isa => ArrayRef
26 3 );
27  
28 has at => (
29 is => 'rw',
30 isa => Int,
31 default => 0
32 3 );
33  
34 has mark => (
35 is => 'rw',
36 isa => Int
37 3 );
38  
39 has pace => (
40 is => 'rw',
41 default => 1
42 3 );
43  
44 has playing => (
45 is => 'rw',
46 isa => Bool,
47 default => 0
48 3 );
49  
50 has [qw/save screen log/] => (
51 is => 'rw'
52 3 );
53  
54 has _games => (
55 is => 'rw',
56 isa => HashRef,
57 private => 1
58 3 );
59  
60 20 ▶sub BUILD {
Subroutine BUILD: called CC=14 SCAR=26.6
Pod undocumented
61 20 my ($self) = @_;
62 20 ▶ if (my $save = $self->save) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $save = $self->save) 1 19 20
63 1 ▶ $self->steps([ $save->steps ]) unless $self->steps;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->steps 1 0 1
64 1 ▶ $self->mark($save->at) unless defined $self->mark;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->mark 1 0 1
65 }
66 Carp::croak('Game::Terminal::Kit::Replay: steps, or a save, are needed')
67 20 ▶ unless ref $self->steps eq 'ARRAY' && @{ $self->steps };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->steps eq "ARRAY" and @{$self->steps;} 2 18 20
Condition100%3/3
ref $self->steps eq "ARRAY" and @{$self->steps;}
!ll&&!rl&&r
1118
MC/DC100%2/2
ref $self->steps eq "ARRAY" and @{$self->steps;}
ref $self->steps eq "ARRAY" @{$self->steps;}
Truth table100%3/3
A and B
A= ref $self->steps eq "ARRAY"B= @{$self->steps;}
ABresult
0X0
100
111
68 18 ▶ Carp::croak('Game::Terminal::Kit::Replay: load and draw are both needed')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load and $self->draw 1 17 18
Condition66%2/3
$self->load and $self->draw
!ll&&!rl&&r
1017
MC/DC50%1/2
$self->load and $self->draw
$self->load $self->draw
Truth table66%2/3
A and B
A= $self->loadB= $self->draw
ABresult
0X0
100
111
69 unless $self->load && $self->draw;
70 17 ▶ Carp::croak('Game::Terminal::Kit::Replay: the pace is not a number of seconds')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pace =~ /\A(?:\d+\.?\d*|\.\d+)\z/u 1 16 17
71 unless $self->pace =~ m/\A(?:\d+\.?\d*|\.\d+)\z/;
72 16 ▶ $self->mark($#{ $self->steps }) unless defined $self->mark;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->mark 12 4 16
73 16 ▶ $self->head([]) unless $self->head;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head 14 2 16
74 16 ▶ $self->legend([ 'left and right step, home and end go to either end, space plays,',
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legend 16 0 16
75 'enter plays on from here, q stops' ]) unless $self->legend;
76 16 $self->_games({});
77 16 ▶ $self->at(0) unless $self->at >= 0 && $self->at <= $#{ $self->steps };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->at >= 0 and $self->at <= $#{$self->steps;} 1 15 16
Condition66%2/3
$self->at >= 0 and $self->at <= $#{$self->steps;}
!ll&&!rl&&r
0115
MC/DC50%1/2
$self->at >= 0 and $self->at <= $#{$self->steps;}
$self->at >= 0 $self->at <= $#{$self->steps;}
Truth table66%2/3
A and B
A= $self->at >= 0B= $self->at <= $#{$self->steps;}
ABresult
0X0
100
111
78 16 return $self;
79 }
80  
81 52 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
82 52 my ($self) = @_;
83 52 return scalar @{ $self->steps };
84 }
85  
86 53 ▶sub game {
Subroutine game: called CC=5 SCAR=16.1
Pod documented
87 53 my ($self, $index) = @_;
88 53 ▶ $index = $self->at unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 52 1 53
89 53 my $games = $self->_games;
90 53 ▶ return $games->{$index} if exists $games->{$index};
Branch: 1 branch
BranchTrueFalseTotal
if exists $games->{$index} 23 30 53
91 30 my $text = $self->steps->[$index];
92 30 ▶ Carp::croak("Game::Terminal::Kit::Replay: there is no step $index") unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 30 30
93 30 my $game = $self->load->($text);
94 30 ▶ Carp::croak("Game::Terminal::Kit::Replay: load gave nothing back for step " . ($index + 1))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game 1 29 30
95 unless defined $game;
96 29 return $games->{$index} = $game;
97 }
98  
99 52 ▶sub status {
Subroutine status: called CC=4 SCAR=13.9
Pod documented
100 52 my ($self) = @_;
101 52 my ($at, $mark) = ($self->at, $self->mark);
102 52 ▶ return sprintf('step %d of %d', $at + 1, $self->count)
Branch: 3 branches
BranchTrueFalseTotal
$at == $mark ? : 16 36 52
$at > $mark ? : 8 28 36
$self->playing ? : 4 48 52
103 . ($at == $mark ? ', where it was saved' : $at > $mark ? ', undone before it was saved' : '')
104 . ($self->playing ? ', playing' : '');
105 }
106  
107 52 ▶sub frame {
Subroutine frame: called CC=3 SCAR=11.0
Pod documented
108 52 my ($self) = @_;
109 52 my @board = eval { $self->draw->($self->game, $self) };
110 52 ▶ unless (@board) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@board) 2 50 52
111 2 ▶ my $why = $@ || 'draw gave back nothing';
Condition50%1/2
$@ || 'draw gave back nothing'
l!l
20
MC/DC0%0/1
$@ || 'draw gave back nothing'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
112 2 $why =~ s/ at \S+ line \d+\.?\n?\z//;
113 2 $why =~ s/\AGame::Terminal::Kit::\w+: //;
114 2 chomp $why;
115 2 @board = ("step " . ($self->at + 1) . " cannot be shown: $why");
116 }
117 52 return (@board, '', $self->status);
118 }
119  
120 48 ▶sub lines {
Subroutine lines: called CC=3 SCAR=11.0
Pod documented
121 48 my ($self) = @_;
122 48 ▶ my @said = $self->log ? $self->log->lines : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->log ? : 23 25 48
123 48 ▶ return (@{ $self->head }, $self->frame, (@said ? ('', @said) : ()),
Branch: 1 branch
BranchTrueFalseTotal
@said ? : 11 37 48
124 48 map { Game::Terminal::Kit::Widget::paint($self, $_, '2') } @{ $self->legend });
125 }
126  
127 34 ▶sub go {
Subroutine go: called CC=4 SCAR=13.9
Pod documented
128 34 my ($self, $index) = @_;
129 34 my $last = $#{ $self->steps };
130 34 ▶ $index = 0 if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 2 32 34
131 34 ▶ $index = $last if $index > $last;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $last 1 33 34
132 34 ▶ return ('none') if $index == $self->at;
Branch: 1 branch
BranchTrueFalseTotal
if $index == $self->at 3 31 34
133 31 $self->at($index);
134 31 return ('moved');
135 }
136  
137 33 ▶sub tick {
Subroutine tick: called CC=3 SCAR=11.0
Pod documented
138 33 my ($self) = @_;
139 33 ▶ return ('none') unless $self->playing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->playing 26 7 33
140 7 my @event = $self->go($self->at + 1);
141 7 ▶ $self->playing(0) if $self->at == $#{ $self->steps };
Branch: 1 branch
BranchTrueFalseTotal
if $self->at == $#{$self->steps;} 3 4 7
142 7 return @event;
143 }
144  
145 45 ▶sub step {
Subroutine step: called CC=22 SCAR=31.0
Pod documented
146 45 my ($self, $key) = @_;
147 45 ▶ return ('quit') if !defined $key || $key eq 'eof' || $key eq 'interrupt' || $key eq 'q' || $key eq 'escape';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape" 8 37 45
Condition91%11/12
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape"
l!l&&r!l&&!r
6237
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
l!l&&r!l&&!r
2439
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
2043
not defined $key or $key eq "eof"
l!l&&r!l&&!r
1143
MC/DC80%4/5
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape"
not defined $key $key eq "eof" $key eq "interrupt" $key eq "q" $key eq "escape"
Truth table83%5/6
not(A) or B or C or D or E
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"D= $key eq "q"E= $key eq "escape"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
148 37 ▶ return $self->go($self->at - 1) if $key eq 'left' || $key eq 'up' || $key eq 'back_tab' || $key eq 'backspace';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" or $key eq "up" or $key eq "back_tab" or $key eq "backspace" 6 31 37
Condition100%9/9
$key eq "left" or $key eq "up" or $key eq "back_tab" or $key eq "backspace"
l!l&&r!l&&!r
5131
$key eq "left" or $key eq "up" or $key eq "back_tab"
l!l&&r!l&&!r
4132
$key eq "left" or $key eq "up"
l!l&&r!l&&!r
2233
MC/DC100%4/4
$key eq "left" or $key eq "up" or $key eq "back_tab" or $key eq "backspace"
$key eq "left" $key eq "up" $key eq "back_tab" $key eq "backspace"
Truth table100%5/5
A or B or C or D
A= $key eq "left"B= $key eq "up"C= $key eq "back_tab"D= $key eq "backspace"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
149 31 ▶ return $self->go($self->at + 1) if $key eq 'right' || $key eq 'down' || $key eq 'tab';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq "down" or $key eq "tab" 10 21 31
Condition100%6/6
$key eq "right" or $key eq "down" or $key eq "tab"
l!l&&r!l&&!r
9121
$key eq "right" or $key eq "down"
l!l&&r!l&&!r
7222
MC/DC100%3/3
$key eq "right" or $key eq "down" or $key eq "tab"
$key eq "right" $key eq "down" $key eq "tab"
Truth table100%4/4
A or B or C
A= $key eq "right"B= $key eq "down"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
150 21 ▶ return $self->go(0) if $key eq 'home' || $key eq 'page_up';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" or $key eq "page_up" 2 19 21
Condition66%2/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
2019
MC/DC50%1/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table66%2/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
151 19 ▶ return $self->go($#{ $self->steps }) if $key eq 'end' || $key eq 'page_down';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" or $key eq "page_down" 2 17 19
Condition66%2/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
2017
MC/DC50%1/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table66%2/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
152 17 ▶ return ('chosen', $self->at) if $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" 3 14 17
153 14 ▶ if ($key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq " ") 5 9 14
154 5 ▶ if ($self->playing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->playing) { } 1 4 5
155 1 $self->playing(0);
156 } else {
157 4 ▶ $self->at(0) if $self->at == $#{ $self->steps };
Branch: 1 branch
BranchTrueFalseTotal
if $self->at == $#{$self->steps;} 1 3 4
158 4 $self->playing(1);
159 }
160 5 return ('changed');
161 }
162 9 ▶ $self->log->note('That key does nothing here.') if $self->log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 9 0 9
163 9 return ('none');
164 }
165  
166 4 ▶sub run {
Subroutine run: called CC=6 SCAR=17.9
Pod documented
167 4 my ($self, $keys, $screen) = @_;
168 4 while (1) {
169 12 Game::Terminal::Kit::Widget::draw($self, $screen);
170 12 ▶ my $key = $keys->read_key($self->playing ? $self->pace : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->playing ? : 3 9 12
171 12 ▶ if (!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 4 8 12
172 4 ▶ return ('quit') unless $self->playing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->playing 1 3 4
173 3 $self->tick;
174 3 next;
175 }
176 8 my ($event, $value) = $self->step($key);
177 8 ▶ next if Game::Terminal::Kit::Widget::goes_on($event);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Terminal::Kit::Widget::goes_on($event) 5 3 8
178 3 ▶ return $event eq 'chosen' ? ($event, $value) : ($event);
Branch: 1 branch
BranchTrueFalseTotal
$event eq 'chosen' ? : 1 2 3
179 }
180 }
181  
182 3 ▶sub play {
Subroutine play: called CC=8 SCAR=20.8
Pod documented
183 3 my ($self, $keys, $screen) = @_;
184 3 my $was_raw = $keys->raw;
185 3 ▶ if ($was_raw || $keys->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_raw or $keys->enter_raw) 2 1 3
Condition100%3/3
$was_raw or $keys->enter_raw
l!l&&r!l&&!r
111
MC/DC100%2/2
$was_raw or $keys->enter_raw
$was_raw $keys->enter_raw
Truth table100%3/3
A or B
A= $was_rawB= $keys->enter_raw
ABresult
1X1
011
000
186 2 $screen->hide_cursor;
187 2 my @result = eval { $self->run($keys, $screen) };
188 2 my $died = $@;
189 2 $screen->show_cursor;
190 2 ▶ $keys->leave_raw unless $was_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_raw 1 1 2
191 2 ▶ die $died unless @result;
Branch: 1 branch
BranchTrueFalseTotal
unless @result 0 2 2
192 2 return @result;
193 }
194 1 my $was = $self->at;
195 1 for my $index (0 .. $#{ $self->steps }) {
196 4 $self->at($index);
197 4 ▶ $screen->say(@{ $self->head }) if $index == 0 && @{ $self->head };
Branch: 1 branch
BranchTrueFalseTotal
if $index == 0 and @{$self->head;} 1 3 4
Condition66%2/3
$index == 0 and @{$self->head;}
!ll&&!rl&&r
301
MC/DC50%1/2
$index == 0 and @{$self->head;}
$index == 0 @{$self->head;}
Truth table66%2/3
A and B
A= $index == 0B= @{$self->head;}
ABresult
0X0
100
111
198 4 $screen->say($self->frame, '');
199 }
200 1 $self->at($was);
201 1 return ('quit');
202 }
203  
204 3 1;
205  
206 __END__
« Kit/Pixels.pm ↑ Summary Kit/Steer.pm »