blib/lib/Game/Brandubh/Error.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Game/Brandubh.pm ↑ Summary blib/lib/Game/Brandubh/Result.pm »
1 package Game::Brandubh::Error;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 our $VERSION = '0.03';
11  
12 our @FLAGS;
13 BEGIN {
14 @FLAGS = qw(
15 bad_move
16 game_over
17 not_a_seat
18 not_your_turn
19 no_piece
20 not_your_piece
21 no_move
22 not_a_line
23 path_blocked
24 throne_closed
25 corner_closed
26 no_offer
27 own_offer
28 offer_standing
29 );
30 }
31  
32 our %MESSAGE;
33 BEGIN {
34 %MESSAGE = (
35 bad_move => 'that is not a move',
36 game_over => 'the game is already over',
37 not_a_seat => 'that is not one of the two seats',
38 not_your_turn => 'it is not your turn',
39 no_piece => 'there is no piece there',
40 not_your_piece => 'that piece is not yours',
41 no_move => 'that piece did not move',
42 not_a_line => 'a piece moves in a straight line along a row or a column',
43 path_blocked => 'another piece is in the way',
44 throne_closed => 'no piece may stop on the throne',
45 corner_closed => 'only the king may stand on a corner',
46 no_offer => 'no draw has been offered',
47 own_offer => 'you cannot answer your own offer',
48 offer_standing => 'a draw has already been offered',
49 );
50 }
51  
52 has [@FLAGS] => (is => 'ro');
53  
54 has message => (is => 'ro', default => '');
55  
56 has move => (is => 'ro');
57  
58 sub throw {
59 my ($class, $flag, %extra) = @_;
60 Carp::croak("Game::Brandubh::Error: no such refusal as '" . ($flag // 'undef') . "'")
61 unless defined $flag && exists $MESSAGE{$flag};
62 return $class->new(
63 $flag => 1,
64 message => $MESSAGE{$flag},
65 (defined $extra{move} ? (move => "$extra{move}") : ()),
66 );
67 }
68  
69 sub BUILD {
70 my ($self) = @_;
71 my @set = grep { $self->$_ } @FLAGS;
72 Carp::croak('Game::Brandubh::Error: a refusal is made by throw, and names exactly one thing')
73 unless @set == 1;
74 return;
75 }
76  
77 sub code {
78 my ($self) = @_;
79 for my $flag (@FLAGS) {
80 return $flag if $self->$flag;
81 }
82 return undef;
83 }
84  
85 sub flags { @FLAGS }
86  
87 sub message_for { $MESSAGE{ $_[1] // '' } }
88  
89 sub known { defined $_[1] && exists $MESSAGE{ $_[1] } ? 1 : 0 }
90  
91 1;
92  
93 __END__
« blib/lib/Game/Brandubh.pm ↑ Summary blib/lib/Game/Brandubh/Result.pm »