blib/lib/Game/Brandubh/Variant.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/Result.pm ↑ Summary blib/lib/Game/Brandubh/Engine.pm »
1 package Game::Brandubh::Variant;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶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 (@FIELDS, %DEFAULT, %NAMED);
13 BEGIN {
14 @FIELDS = qw(
15 escape
16 king_everywhere_two
17 king_strong
18 throne_reentry
19 throne_pass
20 repeat
21 ply_cap
22 );
23 %DEFAULT = (
24 escape => 'corner',
25 king_everywhere_two => 0,
26 king_strong => 0,
27 throne_reentry => 0,
28 throne_pass => 1,
29 repeat => 3,
30 ply_cap => 400,
31 );
32 %NAMED = (
33 brandubh => {},
34 );
35 }
36  
37 use constant PLY_CAP_MAX => 4096;
38  
39 has name => (is => 'ro', default => 'brandubh');
40  
41 has escape => (is => 'ro', default => 'corner');
42  
43 has king_everywhere_two => (is => 'ro', default => 0);
44  
45 has king_strong => (is => 'ro', default => 0);
46  
47 has throne_reentry => (is => 'ro', default => 0);
48  
49 has throne_pass => (is => 'ro', default => 1);
50  
51 has repeat => (is => 'ro', default => 3);
52  
53 has ply_cap => (is => 'ro', default => 400);
54  
55 my $MAKING = 0;
56  
57 sub BUILD {
58 my ($self) = @_;
59 Carp::croak('Game::Brandubh::Variant: a rule set is made by named, custom or from_string')
60 unless $MAKING;
61 return;
62 }
63  
64 my $checked = sub {
65 my ($class, %fields) = @_;
66  
67 for my $field (sort keys %fields) {
68 Carp::croak("Game::Brandubh::Variant: no field is called '$field'")
69 unless exists $DEFAULT{$field};
70 Carp::croak("Game::Brandubh::Variant: $field has no value")
71 unless defined $fields{$field} && !ref $fields{$field};
72 }
73 my %all = (%DEFAULT, %fields);
74  
75 Carp::croak("Game::Brandubh::Variant: escape is 'corner' or 'edge', not '$all{escape}'")
76 unless $all{escape} eq 'corner' || $all{escape} eq 'edge';
77 for my $flag (qw(king_everywhere_two king_strong throne_reentry throne_pass)) {
78 Carp::croak("Game::Brandubh::Variant: $flag is 0 or 1, not '$all{$flag}'")
79 unless $all{$flag} =~ /\A[01]\z/;
80 $all{$flag} += 0;
81 }
82 Carp::croak("Game::Brandubh::Variant: repeat is a whole number from 2 up, not '$all{repeat}'")
83 unless $all{repeat} =~ /\A[0-9]{1,6}\z/ && $all{repeat} >= 2;
84 Carp::croak("Game::Brandubh::Variant: ply_cap is a whole number from 1 to " . PLY_CAP_MAX . ", not '$all{ply_cap}'")
85 unless $all{ply_cap} =~ /\A[0-9]{1,5}\z/ && $all{ply_cap} >= 1 && $all{ply_cap} <= PLY_CAP_MAX;
86 $all{$_} += 0 for qw(repeat ply_cap);
87  
88 Carp::croak('Game::Brandubh::Variant: king_everywhere_two and king_strong contradict each other')
89 if $all{king_everywhere_two} && $all{king_strong};
90  
91 my $name = 'custom';
92 for my $known (sort keys %NAMED) {
93 my %set = (%DEFAULT, %{ $NAMED{$known} });
94 next if grep { $set{$_} ne $all{$_} } @FIELDS;
95 $name = $known;
96 last;
97 }
98  
99 $MAKING = 1;
100 my $made = eval { $class->new(name => $name, %all) };
101 my $died = $@;
102 $MAKING = 0;
103 Carp::croak($died) unless $made;
104 return $made;
105 };
106  
107 sub named {
108 my ($class, $name) = @_;
109 $name = 'brandubh' unless defined $name;
110 Carp::croak("Game::Brandubh::Variant: no rule set is called '$name'")
111 unless !ref $name && exists $NAMED{$name};
112 return $checked->($class, %{ $NAMED{$name} });
113 }
114  
115 sub custom {
116 my ($class, @fields) = @_;
117 Carp::croak('Game::Brandubh::Variant: custom takes field => value pairs') if @fields % 2;
118 return $checked->($class, @fields);
119 }
120  
121 sub from_string {
122 my ($class, $text) = @_;
123 return undef unless defined $text && !ref $text;
124 $text =~ s/\A\s+//;
125 $text =~ s/\s+\z//;
126 return eval { $class->named($text) } if exists $NAMED{$text};
127  
128 my ($list) = $text =~ /\Acustom(?:[ \t]+(\S+))?\z/ or return undef;
129 my %fields;
130 for my $pair (split /,/, defined $list ? $list : '') {
131 my ($field, $value) = $pair =~ /\A(\w+)=(\w+)\z/ or return undef;
132 return undef if exists $fields{$field};
133 $fields{$field} = $value;
134 }
135 return eval { $checked->($class, %fields) };
136 }
137  
138 sub as_string {
139 my ($self) = @_;
140 return $self->name if exists $NAMED{ $self->name };
141 my @differ = grep { $self->$_ ne $DEFAULT{$_} } @FIELDS;
142 return 'custom ' . join(',', map { "$_=" . $self->$_ } @differ);
143 }
144  
145 sub as_hash {
146 my ($self) = @_;
147 return { map { $_ => $self->$_ } @FIELDS };
148 }
149  
150 sub equals {
151 my ($self, $other) = @_;
152 return 0 unless ref $other && ref $other eq ref $self;
153 return (grep { $self->$_ ne $other->$_ } @FIELDS) ? 0 : 1;
154 }
155  
156 sub fields { @FIELDS }
157  
158 sub names { sort keys %NAMED }
159  
160 1;
161  
162 __END__
« blib/lib/Game/Brandubh/Result.pm ↑ Summary blib/lib/Game/Brandubh/Engine.pm »