blib/lib/Game/RoyalUr/Variant.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Game/RoyalUr/Rules.pm ↑ Summary blib/lib/Game/RoyalUr/Terminal.pm »
1 package Game::RoyalUr::Variant;
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 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 our $VERSION = '0.03';
12  
13 my (@FIELDS, %NAMED, %ALLOWED);
14 BEGIN {
15 @FIELDS = qw(route dice zero_rolls safe_rosettes pieces);
16 %NAMED = (
17 finkel => { route => 'short', dice => 4, zero_rolls => 0, safe_rosettes => 1, pieces => 7 },
18 masters => { route => 'long', dice => 3, zero_rolls => 4, safe_rosettes => 0, pieces => 7 },
19 );
20 %ALLOWED = (
21 route => qr/\A(?:short|long)\z/,
22 dice => qr/\A[34]\z/,
23 zero_rolls => qr/\A[04]\z/,
24 safe_rosettes => qr/\A[01]\z/,
25 pieces => qr/\A[1-7]\z/,
26 );
27 }
28  
29 has route => (is => 'ro', default => 'short');
30  
31 has dice => (is => 'ro', default => 4);
32  
33 has zero_rolls => (is => 'ro', default => 0);
34  
35 has safe_rosettes => (is => 'ro', default => 1);
36  
37 has pieces => (is => 'ro', default => 7);
38  
39 sub BUILD {
40 my ($self) = @_;
41 for my $field (@FIELDS) {
42 my $value = $self->$field;
43 Carp::croak("Game::RoyalUr::Variant: $field may not be '" . ($value // 'undef') . "'")
44 unless defined $value && !ref $value && $value =~ $ALLOWED{$field};
45 }
46 return;
47 }
48  
49 sub fields { @FIELDS }
50  
51 sub names { sort keys %NAMED }
52  
53 sub named {
54 my ($class, $name) = @_;
55 Carp::croak("Game::RoyalUr::Variant: no rule set is called '" . ($name // 'undef')
56 . "'; the names are " . join(', ', $class->names))
57 unless defined $name && !ref $name && exists $NAMED{$name};
58 return $class->new(%{ $NAMED{$name} });
59 }
60  
61 sub custom {
62 my ($class, %given) = @_;
63 for my $key (sort keys %given) {
64 Carp::croak("Game::RoyalUr::Variant: no rule is called '$key'") unless exists $ALLOWED{$key};
65 }
66 return $class->new(%given);
67 }
68  
69 sub of {
70 my ($class, $rules) = @_;
71 return $class->named('finkel') unless defined $rules;
72 return $rules if Scalar::Util::blessed($rules) && $rules->isa($class);
73 return $class->custom(%$rules) if ref $rules eq 'HASH';
74 Carp::croak('Game::RoyalUr::Variant: a rule set is a name, a hash reference or a variant') if ref $rules;
75 return $class->named($rules);
76 }
77  
78 sub as_hash {
79 my ($self) = @_;
80 return { map { $_ => $self->$_ } @FIELDS };
81 }
82  
83 sub equals {
84 my ($self, $other) = @_;
85 return 0 unless Scalar::Util::blessed($other) && $other->isa(__PACKAGE__);
86 for my $field (@FIELDS) {
87 return 0 unless $self->$field eq $other->$field;
88 }
89 return 1;
90 }
91  
92 sub name {
93 my ($self) = @_;
94 for my $name ($self->names) {
95 return $name unless grep { $self->$_ ne $NAMED{$name}{$_} } @FIELDS;
96 }
97 return undef;
98 }
99  
100 sub describe {
101 my ($self) = @_;
102 return join ' ', map { $_ . '=' . $self->$_ } @FIELDS;
103 }
104  
105 1;
106  
107 __END__
« blib/lib/Game/RoyalUr/Rules.pm ↑ Summary blib/lib/Game/RoyalUr/Terminal.pm »