blib/lib/Game/Terminal.pm

filter: Statementstmt 98.6% 149 / 151 Branchbran 90.9% 40 / 44 Conditioncond 70.0% 21 / 30 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 27 / 27 Podpod 100% 16 / 16 totaltotal 91.2% 260 / 285 CC 40 SCAR 16.3
CC
40
Cov
93%
launch
11.2
flags_for
7.3
distance
6.0
CRAP 5.1
« blib/lib/Game/Terminal/Menu.pm ↑ Summary
1 package Game::Terminal;
2  
3 19 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 19 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 19 ▶use Game::Terminal::Kit::Options ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 20 our $VERSION = '0.03';
11  
12 20 our @ROSTER;
13 20 my %BY_NAME;
14  
15 20 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
16 20 my %ALL = (ascii => '--ascii', pick => '--no-pick');
17  
18 20 @ROSTER = (
19 {
20 key => 'backgammon',
21 module => 'Game::Backgammon',
22 class => 'Game::Backgammon::Terminal',
23 blurb => 'backgammon',
24 family => 'board',
25 players => '2',
26 args => [],
27 aliases => [],
28 flags => { %ALL },
29 },
30 {
31 key => 'brandubh',
32 module => 'Game::Brandubh',
33 class => 'Game::Brandubh::Terminal',
34 blurb => 'the Irish 7 by 7 tafl game',
35 family => 'board',
36 players => '2',
37 args => [],
38 aliases => ['tafl'],
39 flags => { ascii => '--no-unicode', pick => '--no-pick' },
40 },
41 {
42 key => 'checkers',
43 module => 'Game::Checkers',
44 class => 'Game::Checkers::Terminal',
45 blurb => 'English draughts',
46 family => 'board',
47 players => '2',
48 args => [],
49 aliases => ['draughts'],
50 flags => { %ALL },
51 },
52 {
53 key => 'chess',
54 module => 'Term::Chess',
55 class => 'Term::Chess',
56 blurb => 'chess',
57 family => 'board',
58 players => '2',
59 args => [],
60 aliases => [],
61 flags => { %ALL },
62 },
63 {
64 key => 'cribbage',
65 module => 'Game::Cribbage',
66 class => 'Game::Cribbage::Terminal',
67 blurb => 'cribbage',
68 family => 'card',
69 players => '2',
70 args => [],
71 aliases => ['crib'],
72 flags => { pick => '--no-pick' },
73 },
74 {
75 key => 'dominoes',
76 module => 'Game::Dominoes',
77 class => 'Game::Dominoes::Terminal',
78 blurb => 'draw dominoes with All Fives scoring',
79 family => 'tile',
80 players => '2-4',
81 args => [],
82 aliases => [],
83 flags => { %ALL },
84 },
85 {
86 key => 'durak',
87 module => 'Game::Durak',
88 class => 'Game::Durak::Terminal',
89 blurb => 'Podkidnoy Durak, the Russian beating game',
90 family => 'card',
91 players => '2',
92 args => [],
93 aliases => [],
94 flags => { %ALL },
95 },
96 {
97 key => 'fanorona',
98 module => 'Game::Fanorona',
99 class => 'Game::Fanorona::Terminal',
100 blurb => 'the board game of Madagascar',
101 family => 'board',
102 players => '2',
103 args => [],
104 aliases => [],
105 flags => { ascii => '--no-unicode', pick => '--no-pick' },
106 },
107 {
108 key => 'floppy',
109 module => 'Game::Floppy',
110 class => 'Game::Floppy::Terminal',
111 blurb => 'a one-button bird through a field of tubes',
112 family => 'arcade',
113 players => '1',
114 args => [],
115 aliases => ['flappy'],
116 flags => { ascii => '--ascii' },
117 },
118 {
119 key => 'gin',
120 module => 'Game::Gin',
121 class => 'Game::Gin::Terminal',
122 blurb => 'gin rummy',
123 family => 'card',
124 players => '2',
125 args => [],
126 aliases => ['rummy'],
127 flags => { %ALL },
128 },
129 {
130 key => 'go',
131 module => 'Game::Go',
132 class => 'Game::Go::Terminal',
133 blurb => 'Go',
134 family => 'board',
135 players => '2',
136 args => [],
137 aliases => ['weiqi', 'baduk'],
138 flags => { %ALL },
139 },
140 {
141 key => 'mahjong',
142 module => 'Game::Mahjong',
143 class => 'Game::Mahjong::Terminal',
144 blurb => 'Mahjong, Chinese Official rules',
145 family => 'tile',
146 players => '4',
147 args => [],
148 aliases => [],
149 flags => { %ALL },
150 },
151 {
152 key => 'maze',
153 module => 'Term::Maze',
154 class => 'Term::Maze',
155 blurb => 'mazes in the terminal',
156 family => 'puzzle',
157 players => '1',
158 args => [],
159 aliases => [],
160 flags => {},
161 },
162 {
163 key => 'merrills',
164 module => 'Game::Merrills',
165 class => 'Game::Merrills::Terminal',
166 blurb => "Nine Men's Morris",
167 family => 'board',
168 players => '2',
169 args => [],
170 aliases => ['morris', 'mills', 'ninemensmorris'],
171 flags => { %ALL },
172 },
173 {
174 key => 'oware',
175 module => 'Game::Oware',
176 class => 'Game::Oware::Terminal',
177 blurb => 'the African sow and capture game, Abapa rules',
178 family => 'board',
179 players => '2',
180 args => [],
181 aliases => ['awale', 'awari'],
182 flags => { %ALL },
183 },
184 {
185 key => 'reversi',
186 module => 'Game::Reversi',
187 class => 'Game::Reversi::Terminal',
188 blurb => 'Reversi, with its 1883 opening',
189 family => 'board',
190 players => '2',
191 args => [],
192 aliases => ['othello'],
193 flags => { %ALL },
194 },
195 {
196 key => 'royalur',
197 module => 'Game::RoyalUr',
198 class => 'Game::RoyalUr::Terminal',
199 blurb => 'the Royal Game of Ur',
200 family => 'board',
201 players => '2',
202 args => [],
203 aliases => ['ur'],
204 flags => { %ALL },
205 },
206 {
207 key => 'schnapsen',
208 module => 'Game::Schnapsen',
209 class => 'Game::Schnapsen::Terminal',
210 blurb => 'Schnapsen',
211 family => 'card',
212 players => '2',
213 args => ['--variant', 'schnapsen'],
214 aliases => [],
215 flags => { %ALL },
216 },
217 {
218 key => 'shogi',
219 module => 'Game::Shogi',
220 class => 'Game::Shogi::Terminal',
221 blurb => 'shogi, Japanese chess',
222 family => 'board',
223 players => '2',
224 args => [],
225 aliases => ['japanesechess'],
226 flags => { %ALL },
227 },
228 {
229 key => 'sixtysix',
230 module => 'Game::Schnapsen',
231 class => 'Game::Schnapsen::Terminal',
232 blurb => 'Sixty-Six, the German parent of Schnapsen',
233 family => 'card',
234 players => '2',
235 args => ['--variant', 'sixtysix'],
236 aliases => ['66'],
237 flags => { %ALL },
238 },
239 {
240 key => 'snake',
241 module => 'Game::Snake',
242 class => 'Game::Snake::Terminal',
243 blurb => 'the classic snake game',
244 family => 'arcade',
245 players => '1',
246 args => [],
247 aliases => [],
248 flags => { ascii => '--ascii' },
249 },
250 {
251 key => 'sternhalma',
252 module => 'Game::Sternhalma',
253 class => 'Game::Sternhalma::Terminal',
254 blurb => 'sternhalma, Chinese checkers, for two to six',
255 family => 'board',
256 players => '2-6',
257 args => [],
258 aliases => ['chinesecheckers'],
259 flags => { ascii => '--no-unicode', pick => '--no-pick' },
260 },
261 {
262 key => 'xiangqi',
263 module => 'Game::Xiangqi',
264 class => 'Game::Xiangqi::Terminal',
265 blurb => 'xiangqi, Chinese chess',
266 family => 'board',
267 players => '2',
268 args => [],
269 aliases => ['chinesechess'],
270 flags => { %ALL },
271 },
272 );
273  
274  
275 20 for my $row (@ROSTER) {
276 460 $BY_NAME{$row->{key}} = $row;
277 460 $BY_NAME{$_} = $row for @{$row->{aliases}};
278 }
279 }
280  
281 13 ▶sub games { return @ROSTER }
Subroutine games: called CC=1 SCAR=0.0
Pod documented
282  
283 11 ▶sub names { return map { $_->{key} } @ROSTER }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
284  
285 73 ▶sub normalise {
Subroutine normalise: called CC=2 SCAR=6.9
Pod documented
286 73 my ($name) = @_;
287 73 ▶ return '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 71 73
288 71 my $clean = lc $name;
289 71 $clean =~ s/^game(?:::|-)//;
290 71 $clean =~ s/[^a-z0-9]//g;
291 71 return $clean;
292 }
293  
294 60 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
295 60 my ($name) = @_;
296 60 return $BY_NAME{normalise($name)};
297 }
298  
299 139 ▶sub distance {
Subroutine distance: called CC=6 SCAR=17.9
Pod documented
300 139 my ($a, $b) = @_;
301 139 my @a = split //, $a;
302 139 my @b = split //, $b;
303 139 my @previous = (0 .. @b);
304 139 for my $i (1 .. @a) {
305 972 my @current = ($i);
306 972 for my $j (1 .. @b) {
307 6468 ▶ my $cost = $a[$i - 1] eq $b[$j - 1] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$a[$i - 1] eq $b[$j - 1] ? : 343 6125 6468
308 6468 my $least = $previous[$j] + 1;
309 6468 ▶ $least = $current[$j - 1] + 1 if $current[$j - 1] + 1 < $least;
Branch: 1 branch
BranchTrueFalseTotal
if $current[$j - 1] + 1 < $least 2757 3711 6468
310 6468 ▶ $least = $previous[$j - 1] + $cost if $previous[$j - 1] + $cost < $least;
Branch: 1 branch
BranchTrueFalseTotal
if $previous[$j - 1] + $cost < $least 1046 5422 6468
311 6468 push @current, $least;
312 }
313 972 @previous = @current;
314 }
315 139 return $previous[-1];
316 }
317  
318 8 ▶sub nearest {
Subroutine nearest: called CC=4 SCAR=13.9
Pod documented
319 8 my ($name) = @_;
320 8 my $clean = normalise($name);
321 8 ▶ return () unless length $clean;
Branch: 1 branch
BranchTrueFalseTotal
unless length $clean 1 7 8
322 7 my @keys = names();
323 7 my @prefix = grep { index($_, $clean) == 0 } @keys;
324 7 ▶ return @prefix if @prefix;
Branch: 1 branch
BranchTrueFalseTotal
if @prefix 1 6 7
325 6 my %score = map { $_ => distance($clean, $_) } @keys;
326 6 ▶ return grep { $score{$_} <= 3 } sort { $score{$a} <=> $score{$b} || $a cmp $b } @keys;
Condition66%2/3
$score{$a} <=> $score{$b} || $a cmp $b
l!l&&r!l&&!r
2931830
MC/DC0%0/2
$score{$a} <=> $score{$b} || $a cmp $b
$score{$a} <=> $score{$b} $a cmp $b
Truth table66%2/3
A || B
A= $score{$a} <=> $score{$b}B= $a cmp $b
ABresult
1X1
011
000
327 }
328  
329 246 ▶sub module_file {
Subroutine module_file: called CC=1 SCAR=0.0
Pod documented
330 246 my ($row) = @_;
331 246 (my $file = $row->{class} . '.pm') =~ s{::}{/}g;
332 246 return $file;
333 }
334  
335 230 ▶sub installed {
Subroutine installed: called CC=4 SCAR=13.9
Pod documented
336 230 my ($row, @inc) = @_;
337 230 ▶ @inc = @INC unless @inc;
Branch: 1 branch
BranchTrueFalseTotal
unless @inc 27 203 230
338 230 my $file = module_file($row);
339 230 for my $dir (grep { !ref } @inc) {
340 1040 ▶ return "$dir/$file" if -f "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$dir/$file" 206 834 1040
341 }
342 24 return undef;
343 }
344  
345 107 ▶sub version_of {
Subroutine version_of: called CC=5 SCAR=16.5
Pod documented
346 107 my ($row, @inc) = @_;
347 107 my $path = installed($row, @inc);
348 107 ▶ return undef unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 106 107
349 106 ▶ open my $fh, '<', $path or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return undef 106 0 106
350 106 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2079 0 2079
351 2079 ▶ return $1 if $line =~ m/^\s*our\s+\$VERSION\s*=\s*['"]([^'"]+)['"]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*our\s+\$VERSION\s*=\s*['"]([^'"]+)['"]/u 106 1973 2079
352 }
353 0 return undef;
354 }
355  
356 14 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
357 14 my ($row) = @_;
358 14 require(module_file($row));
359 13 return $row->{class};
360 }
361  
362 31 ▶sub flags_for {
Subroutine flags_for: called CC=7 SCAR=19.9
Pod documented
363 31 my ($row, $common) = @_;
364 31 ▶ $common ||= {};
Condition50%1/2
$common ||= {}
l!l
310
MC/DC0%0/1
$common ||= {}
$common
Truth table50%1/2
A
A= $common
Aresult
11
00
365 31 my @flags;
366 push @flags, $row->{flags}{ascii}
367 31 ▶ if $common->{ascii} && $row->{flags}{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $common->{"ascii"} and $row->{"flags"}{"ascii"} 3 28 31
Condition66%2/3
$common->{"ascii"} and $row->{"flags"}{"ascii"}
!ll&&!rl&&r
2803
MC/DC50%1/2
$common->{"ascii"} and $row->{"flags"}{"ascii"}
$common->{"ascii"} $row->{"flags"}{"ascii"}
Truth table66%2/3
A and B
A= $common->{"ascii"}B= $row->{"flags"}{"ascii"}
ABresult
0X0
100
111
368 push @flags, $row->{flags}{pick}
369 31 ▶ if defined $common->{pick} && !$common->{pick} && $row->{flags}{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"} 3 28 31
Condition66%4/6
defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"}
!ll&&!rl&&r
2803
defined $common->{"pick"} and not $common->{"pick"}
!ll&&!rl&&r
2803
MC/DC33%1/3
defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"}
defined $common->{"pick"} not $common->{"pick"} $row->{"flags"}{"pick"}
Truth table50%2/4
A and not(B) and C
A= defined $common->{"pick"}B= not $common->{"pick"}C= $row->{"flags"}{"pick"}
ABCresult
0XX0
10X0
1100
1111
370 31 return @flags;
371 }
372  
373 36 ▶sub options_for {
Subroutine options_for: called CC=6 SCAR=17.9
Pod documented
374 36 my ($row, %seam) = @_;
375 36 ▶ return $row->{options} if ref $row->{options} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row->{"options"} eq "ARRAY" 17 19 36
376 19 ▶ my $loader = $seam{loader} || \&load;
Condition100%2/2
$seam{'loader'} || \&Game::Terminal::load
l!l
109
MC/DC100%1/1
$seam{'loader'} || \&Game::Terminal::load
$seam{'loader'}
Truth table100%2/2
A
A= $seam{'loader'}
Aresult
11
00
377 19 my $class = eval { $loader->($row) };
378 19 ▶ return [] unless $class && $class->can('options');
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class->can("options") 4 15 19
Condition100%3/3
$class and $class->can("options")
!ll&&!rl&&r
2215
MC/DC100%2/2
$class and $class->can("options")
$class $class->can("options")
Truth table100%3/3
A and B
A= $classB= $class->can("options")
ABresult
0X0
100
111
379 15 my $fields = eval { $class->options };
380 15 ▶ return ref $fields eq 'ARRAY' ? $fields : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 13 2 15
381 }
382  
383 1 ▶sub step_value {
Subroutine step_value: called CC=1 SCAR=0.0
Pod documented
384 1 return Game::Terminal::Kit::Options::step_value(@_);
385 }
386  
387 1 ▶sub check_value {
Subroutine check_value: called CC=1 SCAR=0.0
Pod documented
388 1 return Game::Terminal::Kit::Options::check_value(@_);
389 }
390  
391 5 ▶sub argv_for {
Subroutine argv_for: called CC=1 SCAR=0.0
Pod documented
392 5 my ($row, $values, %seam) = @_;
393 5 return Game::Terminal::Kit::Options::argv_for(options_for($row, %seam), $values);
394 }
395  
396 12 ▶sub launch {
Subroutine launch: called CC=11 SCAR=24.2
Pod documented
397 12 my ($row, $argv, %seam) = @_;
398 12 ▶ my $loader = $seam{loader} || \&load;
Condition100%2/2
$seam{'loader'} || \&Game::Terminal::load
l!l
75
MC/DC100%1/1
$seam{'loader'} || \&Game::Terminal::load
$seam{'loader'}
Truth table100%2/2
A
A= $seam{'loader'}
Aresult
11
00
399 12 ▶ my $out = $seam{out} || \*STDOUT;
Condition100%2/2
$seam{'out'} || \*STDOUT
l!l
111
MC/DC100%1/1
$seam{'out'} || \*STDOUT
$seam{'out'}
Truth table100%2/2
A
A= $seam{'out'}
Aresult
11
00
400 12 ▶ my @argv = (@{ $row->{args} || [] }, @{ $argv || [] });
Condition50%2/4
$row->{'args'} || []
l!l
120
$argv || []
l!l
120
MC/DC0%0/1
$row->{'args'} || []
$row->{'args'}
MC/DC0%0/1
$argv || []
$argv
Truth table50%1/2
A
A= $row->{'args'}
Aresult
11
00
Truth table50%1/2
A
A= $argv
Aresult
11
00
401  
402 12 my $getopt = Getopt::Long::Configure('default');
403 12 local @SIG{qw(INT TERM HUP)};
404 12 local $ENV{NO_COLOR} = $ENV{NO_COLOR};
405 12 ▶ $ENV{NO_COLOR} = 1 if $seam{no_colour};
Branch: 1 branch
BranchTrueFalseTotal
if $seam{"no_colour"} 1 11 12
406 12 my $depth = () = PerlIO::get_layers($out);
407 12 my $selected = select;
408 12 my $flush = do { select $out; my $was = $|; select $selected; $was };
409  
410 12 my ($status, $error);
411 12 my $ok = eval {
412 12 my $class = $loader->($row);
413 12 $status = $class->run(@argv);
414 9 1;
415 };
416 12 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 9 12
417 3 $error = $@;
418 3 ▶ $error = 'died without a message' unless defined $error && length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and length $error 0 3 3
Condition33%1/3
defined $error and length $error
!ll&&!rl&&r
003
MC/DC0%0/2
defined $error and length $error
defined $error length $error
Truth table33%1/3
A and B
A= defined $errorB= length $error
ABresult
0X0
100
111
419 3 $error =~ s/ at \S+ line \d+\.?\s*\z//;
420 3 $error =~ s/\s+\z//;
421 }
422  
423 12 select $selected;
424 12 { select $out; $| = $flush; select $selected }
425 12 ▶ binmode $out, ':pop' while (() = PerlIO::get_layers($out)) > $depth;
Branch: 1 branch
BranchTrueFalseTotal
(() = &PerlIO::get_layers($out)) > $depth and do { binmode $out, ":pop" } 1 12 13
426 12 Getopt::Long::Configure($getopt);
427 12 ▶ eval { Term::ReadKey::ReadMode(0) } if $INC{'Term/ReadKey.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Term/ReadKey.pm"} 0 12 12
428  
429 12 return ($status, $error);
430 }
431  
432 20 1;
433  
434 __END__
« blib/lib/Game/Terminal/Menu.pm ↑ Summary