| 1 | package Langertha::Holmgang::Sim::Roost; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The answer world roost — an invented dragon roost with perches and a larder, for the example suite demo-roost | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 38 | with 'Langertha::Holmgang::Role::AnswerWorld'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 38 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 38 | our %TOOLS = ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | survey_roost => [], | ||||||||||||||||||||||||||||||||||||||||||
| 14 | feed_dragon => [qw( dragon food )], | ||||||||||||||||||||||||||||||||||||||||||
| 15 | move_dragon => [qw( dragon perch )], | ||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | #### Plugin contract | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 80 | ▶ | sub device_type { 'roost' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
device_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 17 | ▶ | sub tools { return \%TOOLS } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tools: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 89 | ▶ | sub tool { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tool: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 89 | my ( $self, $roost, $name, $args, $ctx ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 89 | ▶ | $name //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$name //= ""
MC/DC0%0/1
$name //= ""
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 89 | ▶ | my $code = $TOOLS{$name} && $self->can( '_t_'.$name ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$Langertha::Holmgang::Sim::Roost::TOOLS{$name} && $self->can('_t_' . $name)
MC/DC50%1/2
$Langertha::Holmgang::Sim::Roost::TOOLS{$name} && $self->can('_t_' . $name)
$Langertha::Holmgang::Sim::Roost::TOOLS{$name}
$self->can('_t_' . $name)
Truth table66%2/3
A && B
A= $Langertha::Holmgang::Sim::Roost::TOOLS{$name}B= $self->can('_t_' . $name)
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | or return ( "Error: unknown tool '".$name."'.", 0 ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 88 | ▶ | return $self->$code( $roost, $args || {} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args || {}
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | #### State | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # the key of a hash entry, matched without case and surrounding white space | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 153 | ▶ | sub _key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_key: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 153 | my ( $self, $h, $want ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 153 | ▶ | return undef unless ref $h eq 'HASH' && defined $want && !ref $want; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $h eq "HASH" and defined $want and not ref $want
ref $h eq "HASH" and defined $want
MC/DC33%1/3
ref $h eq "HASH" and defined $want and not ref $want
ref $h eq "HASH"
defined $want
not ref $want
Truth table50%2/4
A and B and not(C)
A= ref $h eq "HASH"B= defined $wantC= not ref $want
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 152 | ( my $w = lc $want ) =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 152 | my ($k) = grep { lc $_ eq $w } sort keys %$h; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 152 | return $k; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | sub _given { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_given: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | my ( $self, $v ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | return defined $v && !ref $v ? $v : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $v and not ref $v
MC/DC50%1/2
defined $v and not ref $v
defined $v
not ref $v
Truth table66%2/3
A and not(B)
A= defined $vB= not ref $v
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 55 | ▶ | sub _baskets { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_baskets: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 55 | my ( $self, $n ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 55 | ▶ | return $n.' basket'.( $n == 1 ? '' : 's' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 46 | ▶ | sub _list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_list: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 46 | my ( $self, $l ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 46 | ▶ | my @l = ref $l eq 'ARRAY' ? @$l : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 46 | ▶ | return @l ? join( ', ', @l ) : 'none'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | #### Tools | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | sub _t_survey_roost { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_t_survey_roost: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 10 | my ( $self, $r ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 10 | ▶ | my $d = ref $r->{dragons} eq 'HASH' ? $r->{dragons} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | my $p = ref $r->{perches} eq 'HASH' ? $r->{perches} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 10 | ▶ | my $l = ref $r->{larder} eq 'HASH' ? $r->{larder} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | my %on = map { defined $d->{$_}{perch} ? ( $d->{$_}{perch} => $_ ) : () } keys %$d; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | my @out = ('Dragons:'); | |||||||||||||||||||||||||||||||||||||||||
| 71 | push @out, map { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | my $x = $d->{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | '- '.$_.': wingspan '.( $x->{wingspan} // '?' ).', perch '.( $x->{perch} // 'none' ) | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 23 | ▶ | .', eats '.$self->_list( $x->{eats} ).', meals '.$self->_list( $x->{meals} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$$x{"wingspan"} // "?"
$$x{"perch"} // "none"
MC/DC0%0/1
$$x{"wingspan"} // "?"
$$x{"wingspan"}
MC/DC100%1/1
$$x{"perch"} // "none"
$$x{"perch"}
Truth table50%1/2
A
A= $$x{"wingspan"}
Truth table100%2/2
A
A= $$x{"perch"}
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } sort keys %$d; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | ▶ | push @out, '- none' unless %$d; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | push @out, 'Perches:'; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | ▶ | push @out, map { '- '.$_.': span '.( $p->{$_}{span} // '?' ).', '.( $on{$_} // 'free' ) } sort keys %$p; | ||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$$p{$_}{"span"} // "?"
$on{$_} // "free"
MC/DC0%0/1
$$p{$_}{"span"} // "?"
$$p{$_}{"span"}
MC/DC100%1/1
$on{$_} // "free"
$on{$_}
Truth table50%1/2
A
A= $$p{$_}{"span"}
Truth table100%2/2
A
A= $on{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | ▶ | push @out, '- none' unless %$p; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | push @out, 'Larder:'; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | push @out, map { '- '.$_.': '.$self->_baskets( $l->{$_} ) } sort keys %$l; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 10 | ▶ | push @out, '- empty' unless %$l; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 10 | return ( join( "\n", @out ), 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 42 | ▶ | sub _t_feed_dragon { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_t_feed_dragon: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 42 | my ( $self, $r, $a ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | my $dk = $self->_key( $r->{dragons}, $a->{dragon} ) | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 42 | ▶ | or return ( "Error: there is no dragon called '".$self->_given( $a->{dragon} )."'.", 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | my $fk = $self->_key( $r->{larder}, $a->{food} ) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 40 | ▶ | or return ( "Error: the larder has no '".$self->_given( $a->{food} )."'.", 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 39 | ▶ | return ( 'Error: no '.$fk.' left in the larder.', 0 ) unless $r->{larder}{$fk} > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 37 | $r->{larder}{$fk}--; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 37 | my $dragon = $r->{dragons}{$dk}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 37 | ▶ | $dragon->{meals} = [] unless ref $dragon->{meals} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 37 | push @{ $dragon->{meals} }, $fk; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 37 | return ( $dk.' ate a basket of '.$fk.'. Left in the larder: '.$self->_baskets( $r->{larder}{$fk} ).'.', 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 36 | ▶ | sub _t_move_dragon { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_t_move_dragon: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 36 | my ( $self, $r, $a ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | my $dk = $self->_key( $r->{dragons}, $a->{dragon} ) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 36 | ▶ | or return ( "Error: there is no dragon called '".$self->_given( $a->{dragon} )."'.", 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | my $pk = $self->_key( $r->{perches}, $a->{perch} ) | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 35 | ▶ | or return ( "Error: there is no perch called '".$self->_given( $a->{perch} )."'.", 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 34 | my $dragon = $r->{dragons}{$dk}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 34 | ▶ | return ( $dk.' is already on '.$pk.'.', 1 ) if ( $dragon->{perch} // '' ) eq $pk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$$dragon{"perch"} // ""
MC/DC100%1/1
$$dragon{"perch"} // ""
$$dragon{"perch"}
Truth table100%2/2
A
A= $$dragon{"perch"}
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 33 | ▶ | my ($other) = grep { $_ ne $dk && ( $r->{dragons}{$_}{perch} // '' ) eq $pk } sort keys %{ $r->{dragons} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
$_ ne $dk && ($$r{'dragons'}{$_}{'perch'} // '') eq $pk
$$r{"dragons"}{$_}{"perch"} // ""
MC/DC100%2/2
$_ ne $dk && ($$r{'dragons'}{$_}{'perch'} // '') eq $pk
$_ ne $dk
$$r{"dragons"}{$_}{"perch"}
Truth table100%3/3
A && B
A= $_ ne $dkB= $$r{"dragons"}{$_}{"perch"}
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 33 | ▶ | return ( 'Error: '.$pk.' is taken by '.$other.'.', 0 ) if defined $other; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 32 | $dragon->{perch} = $pk; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 32 | return ( $dk.' flew to '.$pk.'.', 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 38 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||||||