DataObjects/Game.pm

filter: Statementstmt 62.9% 80 / 127 Branchbran 45.4% 20 / 44 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 74.1% 23 / 31 Podpod 44.4% 4 / 9 totaltotal 59.2% 138 / 233 CC 28 SCAR 21.1
CC
28
Cov
59%
handicap
42.0
opponent
12.0
auto_handicap
12.0
CRAP 8.2
« DataObjects/Register.pm ↑ Summary DataObjects/Directives.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::DataObjects::Game.pm
4 #
5 # USAGE: use Games::Go::AGA::DataObjects::Game;
6 #
7 # PODNAME: Games::Go::AGA::DataObjects::Game
8 # ABSTRACT: model an AGA game
9 #
10 # AUTHOR: Reid Augustin (REID), <reid@hellosix.com>
11 # CREATED: 11/19/2010 03:13:05 PM PST
12 #===============================================================================
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # the Game class is useful for tournament pairing
18 package Games::Go::AGA::DataObjects::Game;
19 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Games::Go::AGA::Parse::Util qw( Rank_to_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Games::Go::AGA::DataObjects::Types qw( isa_Int isa_CodeRef isa_Handicap isa_Komi );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 our $VERSION = '0.152'; # VERSION
29  
30 13 ▶sub isa_Player { die("$_[0] is not a Games::Go::AGA::DataObjects::Player\n") if (ref $_[0] ne 'Games::Go::AGA::DataObjects::Player') }
Subroutine isa_Player: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "Games::Go::AGA::DataObjects::Player" 0 13 13
31 has black => (
32 is => 'rw',
33 isa => \&isa_Player,
34 weak_ref => 1, # Players have Games, Games have Players, so weaken
35 trigger => sub
36 7 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 7 my $self = shift;
38 7 $self->_set_player('black', @_);
39 },
40 3 );
41 has white => (
42 is => 'rw',
43 isa => \&isa_Player,
44 weak_ref => 1,
45 trigger => sub
46 6 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 6 my $self = shift;
48 6 $self->_set_player('white', @_);
49 },
50 3 );
51 has table_number => (
52 is => 'rw',
53 isa => \&isa_Int,
54 lazy => 1,
55 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 5 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 3 );
58 has handi => (
59 is => 'rw',
60 isa => \&isa_Handicap,
61 lazy => 1,
62 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 6 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 # alias => 'handicap',
65 3 );
66 has komi => (
67 is => 'rw',
68 isa => \&isa_Komi,
69 lazy => 1,
70 0 ▶ default => sub { 5.5 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 7 ▶ trigger => sub { shift->changed; },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 3 );
73 has result => (
74 is => 'rw',
75 lazy => 1,
76 default => '?',
77 12 ▶ trigger => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
78 12 my ($self, $new) = @_;
79  
80 12 ▶ if (ref $new) { # better be a Games::Go::AGA::DataObjects::Player
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new) 5 7 12
81 5 my $id = $new->id;
82 5 ▶ if ($id eq $self->white->id) { $self->result('w'); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $self->white->id) 2 3 5
83 3 ▶ if ($id eq $self->black->id) { $self->result('b'); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $self->black->id) 2 1 3
84 }
85 8 ▶ $new ||= '?';
Condition50%1/2
$new ||= "?"
l!l
80
MC/DC0%0/1
$new ||= "?"
$new
Truth table50%1/2
A
A= $new
Aresult
11
00
86 8 $new = lc $new;
87 8 ▶ if ($new ne '?' and $new ne 'w' and $new ne 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($new ne "?" and $new ne "w" and $new ne "b") 1 7 8
Condition83%5/6
$new ne "?" and $new ne "w" and $new ne "b"
!ll&&!rl&&r
341
$new ne "?" and $new ne "w"
!ll&&!rl&&r
035
MC/DC66%2/3
$new ne "?" and $new ne "w" and $new ne "b"
$new ne "?" $new ne "w" $new ne "b"
Truth table75%3/4
A and B and C
A= $new ne "?"B= $new ne "w"C= $new ne "b"
ABCresult
0XX0
10X0
1100
1111
88 1 croak("result must be '?' (or false), 'w', 'b', or one of the players\n");
89 }
90 7 $self->{result} = $new;
91 7 $self->changed;
92 },
93 3 );
94 has change_callback => (
95 isa => \&isa_CodeRef,
96 is => 'rw',
97 lazy => 1,
98 6 ▶ default => sub { sub { } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
99 3 );
100 3 has built => (
101 is => 'rw',
102 );
103  
104 6 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=13.3
Pod undocumented
105 6 my ($self, $args) = @_;
106  
107 6 ▶ if (defined $args and exists $args->{winner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args and exists $args->{"winner"}) 0 6 6
Condition33%1/3
defined $args and exists $args->{"winner"}
!ll&&!rl&&r
060
MC/DC0%0/2
defined $args and exists $args->{"winner"}
defined $args exists $args->{"winner"}
Truth table33%1/3
A and B
A= defined $argsB= exists $args->{"winner"}
ABresult
0X0
100
111
108 0 $self->result(delete $args->{winner});
109 }
110 6 $self->built(1);
111 }
112  
113 38 ▶sub changed {
Subroutine changed: called CC=1 SCAR=0.0
Pod undocumented
114 38 my ($self) = @_;
115  
116 38 &{$self->change_callback}(@_);
117 }
118  
119 13 ▶sub _set_player {
Subroutine _set_player: called CC=3 SCAR=12.3
120 13 my ($self, $color, $new) = @_;
121  
122 13 ▶ if ( $self->built # after object is built,
Branch: 1 branch
BranchTrueFalseTotal
if ($self->built and $self->result ne "?") 0 13 13
Condition66%2/3
$self->built and $self->result ne "?"
!ll&&!rl&&r
1210
MC/DC0%0/2
$self->built and $self->result ne "?"
$self->built $self->result ne "?"
Truth table66%2/3
A and B
A= $self->builtB= $self->result ne "?"
ABresult
0X0
100
111
123 and $self->result ne '?') { # can't change players if result is set
124 0 $self->{$color} = $self->{"prev_$color"}; # restore
125 0 croak 'Result already set, cannot change players';
126 }
127 13 $self->{"prev_$color"} = $new;
128 13 $self->changed;
129 }
130  
131 28 ▶sub winner {
Subroutine winner: called CC=4 SCAR=13.9
Pod documented
132 28 my ($self, $new) = @_;
133  
134 28 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 8 20 28
135 8 $self->result($new);
136 }
137 27 ▶ return $self->white if ($self->result eq 'w');
Branch: 1 branch
BranchTrueFalseTotal
if $self->result eq "w" 8 19 27
138 19 ▶ return $self->black if ($self->result eq 'b');
Branch: 1 branch
BranchTrueFalseTotal
if $self->result eq "b" 15 4 19
139 4 return; # undef
140 }
141  
142 8 ▶sub loser {
Subroutine loser: called CC=3 SCAR=11.4
Pod documented
143 8 my ($self, $new) = @_;
144  
145 8 ▶ return $self->black if ($self->result eq 'w');
Branch: 1 branch
BranchTrueFalseTotal
if $self->result eq "w" 2 6 8
146 6 ▶ return $self->white if ($self->result eq 'b');
Branch: 1 branch
BranchTrueFalseTotal
if $self->result eq "b" 6 0 6
147 0 return; # undef
148 }
149  
150 0 ▶sub opponent {
Subroutine opponent: not called CC=3 SCAR=24.8
Pod undocumented
151 0 my ($self, $player) = @_;
152  
153 0 my $me = $player->id;
154 0 ▶ return $self->white if ($self->black->id eq $me);
Branch: 1 branch
BranchTrueFalseTotal
if $self->black->id eq $me 0 0 0
155 0 ▶ return $self->black if ($self->white->id eq $me);
Branch: 1 branch
BranchTrueFalseTotal
if $self->white->id eq $me 0 0 0
156 0 croak "ID $me is not in this game";
157 }
158  
159 0 ▶sub swap {
Subroutine swap: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my ($self) = @_;
161  
162 0 my $white = $self->white;
163 0 $self->{white} = $self->black;
164 0 $self->{black} = $white;
165 0 $self->changed;
166 }
167  
168 0 ▶sub handicap {
Subroutine handicap: not called CC=6 SCAR=37.4
Pod documented
169 0 my ($self, $default_komi) = @_;
170  
171 0 ▶ if (defined $self->winner) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->winner) 0 0 0
172 0 croak 'Winner already set, cannot change players';
173 }
174  
175 0 ▶ $default_komi = 7.5 if (not defined $default_komi);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_komi 0 0 0
176 0 my $white = $self->white;
177 0 my $black = $self->black;
178 0 my $rankDiff = $self->_rank_to_level($white) - $self->_rank_to_level($black);
179 0 ▶ if ($rankDiff < 0.5) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rankDiff < 0.5) { } 0 0 0
elsif ($rankDiff < 1.0) { } 0 0 0
elsif ($rankDiff < 1.5) { } 0 0 0
180 0 $self->handi(0);
181 0 $self->komi($default_komi); # normal komi game
182 }
183 elsif ($rankDiff < 1.0) {
184 0 $self->handi(0);
185 0 $self->komi(0.5); # no komi game, white wins ties
186 }
187 elsif ($rankDiff < 1.5) {
188 0 $self->handi(0);
189 0 $self->komi(-$default_komi); # reverse komi game
190 }
191 else {
192 0 $self->handi(int $rankDiff + 0.5); # handicap game
193 0 $self->komi(0.5); # white wins ties
194 }
195 # TODO handi/komi have different relationship in AGA vs ING rules...
196 0 $self->changed;
197 }
198  
199 0 ▶sub auto_handicap {
Subroutine auto_handicap: not called CC=3 SCAR=24.8
Pod documented
200 0 my ($self, $default_komi) = @_;
201  
202 0 ▶ if (defined $self->winner) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->winner) 0 0 0
203 0 croak 'Winner already set, cannot change players';
204 }
205  
206 0 my $white = $self->white;
207 0 my $black = $self->black;
208 0 my $rankDiff = $self->_rank_to_level($white) - $self->_rank_to_level($black);
209 0 ▶ if ($rankDiff < 0.1) { # black is significantly stronger than white - swap
Branch: 1 branch
BranchTrueFalseTotal
if ($rankDiff < "0.1") 0 0 0
210 0 $self->{white} = $black;
211 0 $self->{black} = $white;
212 };
213 0 $self->handicap($default_komi);
214 }
215  
216 # AGA ratings have a hole between +1 and -1 which messes up
217 # handicap/komi calculations. Collapse that hole to make a 'level'
218 0 ▶sub _rank_to_level {
Subroutine _rank_to_level: not called CC=2 SCAR=17.9
219 0 my ($self, $player) = @_;
220  
221 0 my $level = $player->adj_rating(-1);
222 0 ▶ return $level + (($level > 0) ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$level > 0 ? : 0 0 0
223 }
224  
225 3 1;
226  
227 __END__
« DataObjects/Register.pm ↑ Summary DataObjects/Directives.pm »