blib/lib/Games/Tournament/Contestant/Swiss/Preference.pm

filter: Statementstmt 91.7% 100 / 109 Branchbran 85.4% 41 / 48 Conditioncond 79.1% 19 / 24 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 90.4% 19 / 21 Podpod 100% 9 / 9 totaltotal 88.0% 198 / 225 CC 33 SCAR 19.8
CC
33
Cov
88%
update
16.2
asString
12.0
round
6.0
CRAP 7.3
« blib/lib/Games/Tournament/Card.pm ↑ Summary blib/lib/Games/Tournament/Swiss/Config.pm »
1 package Games::Tournament::Contestant::Swiss::Preference;
2 35 $Games::Tournament::Contestant::Swiss::Preference::VERSION = '0.21';
3 # Last Edit: 2016 Jan 01, 13:45:02
4 # $Id: $
5  
6 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 35 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use List::MoreUtils qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 35 ▶use Games::Tournament::Swiss::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 35 ▶use constant ROLES => @Games::Tournament::Swiss::Config::roles?
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@Games::Tournament::Swiss::Config::roles ? : 29 6 35
16 @Games::Tournament::Swiss::Config::roles:
17 35 Games::Tournament::Swiss::Config->roles;
18  
19 35 ▶use base qw/Games::Tournament/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # use overload qw/0+/ => 'next', qw/""/ => 'value', fallback => 1;
22  
23 =head1 NAME
24  
25 Games::Tournament::Contestant::Swiss::Preference A competitor's right to a role.
26  
27 =cut
28  
29 =head1 SYNOPSIS
30  
31 pray if $preference->role eq 'Black' and $preference->strength eq 'Strong';
32  
33 =head1 DESCRIPTION
34  
35 The preference, or expectation/right/duty one has with reference to a role, eg White, in the next round depends on the difference between the number of games previously played in it and in the alternative roles, and is either Mild, Strong, or Absolute. The more games played in other roles than in this role, the greater the right/duty to play the next game in this role. The FIDE Swiss Rules (C04.1) represent the difference as the number of Games as White minus the number as Black, so a greater number of games as Black is a negative number and of White a positive number. For equal number of games, +0 indicates the last game was as White, and -0 indicates the last game was as Black. So +0 represents a Mild preference for Black and -0 for White. This implementation uses a 'sign' field to perform the same function as the +/- sign.
36 As an API, the strength method returns 'Mild', 'Strong', or 'Absolute' and the role method returns 'Black', 'White', or whatever the preferred role is, respecting the 2 consecutive games in the same role rule. A7
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 $pref = Games::Tournament::Contestant::Swiss::Preference->new(
43 difference => 0, sign => 'Black', round => 0 );
44  
45 The default difference is 0. The default sign is ''.
46  
47 =cut
48  
49 1947 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
50 1947 my $self = shift;
51 1947 my %args = @_;
52 1947 ▶ $args{sign} = '' unless $args{sign};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"sign"} 1902 45 1947
53 1947 ▶ $args{difference} = 0 unless $args{difference};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"difference"} 1919 28 1947
54 1947 my $pref = bless \%args, $self;
55 1947 return $pref;
56 }
57  
58  
59 =head2 update
60  
61 $pref->update( \@oldRoles )
62  
63 Updates the difference (ie, the internal representation of preference) on the basis of the last role (and second-last role) in \@oldRoles. A minimal sanity check is performed. \@oldRoles is a history of roles in previous rounds, and it is expected only the last role of this history has not yet been used to update the preference. That is, this method must be used round-by-round to keep a players preference up to date. However, the second-last role (in addition to the last role) is also needed to determine the preference in cases when the same role was taken in the last 2 games. So for updates after the second round, make sure the history is at least 2 elements long. Byes and unplayed games have no effect on the preference, so make sure that roles in unplayed games don't make it into oldRoles A5, F2 .
64  
65 =cut
66  
67 761 ▶sub update {
Subroutine update: called CC=16 SCAR=27.8
Pod documented
68 761 my $self = shift;
69 761 my $roles = shift;
70 761 my $message = "Preference update: ";
71 761 ▶ return unless $roles->[-1] and any { $roles->[-1] eq $_ } ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $roles->[-1] and &any(sub { $roles->[-1] eq $_; } , ("White", "Black")) 22 739 761
Condition66%2/3
$roles->[-1] and &any(sub { $roles->[-1] eq $_; } , ("White", "Black"))
!ll&&!rl&&r
022739
MC/DC50%1/2
$roles->[-1] and &any(sub { $roles->[-1] eq $_; } , ("White", "Black"))
$roles->[-1] &any(sub { $roles->[-1] eq $_; } , ("White", "Black"))
Truth table66%2/3
A and B
A= $roles->[-1]B= &any(sub { $roles->[-1] eq $_; } , ("White", "Black"))
ABresult
0X0
100
111
72 739 my @reverseRoles = reverse @$roles;
73 739 my $lastRole = $reverseRoles[0];
74 739 my $before = $reverseRoles[1];
75 739 my $oneBeforeThat = $reverseRoles[2];
76 739 ▶ $message .= "3-game run as $lastRole\n" if $before and $oneBeforeThat and
Branch: 1 branch
BranchTrueFalseTotal
if $before and $oneBeforeThat and $oneBeforeThat eq $before and $before eq $lastRole 24 715 739
Condition100%9/9
$before and $oneBeforeThat and $oneBeforeThat eq $before and $before eq $lastRole
!ll&&!rl&&r
6922324
$before and $oneBeforeThat and $oneBeforeThat eq $before
!ll&&!rl&&r
47022247
$before and $oneBeforeThat
!ll&&!rl&&r
206264269
MC/DC100%4/4
$before and $oneBeforeThat and $oneBeforeThat eq $before and $before eq $lastRole
$before $oneBeforeThat $oneBeforeThat eq $before $before eq $lastRole
Truth table100%5/5
A and B and C and D
A= $beforeB= $oneBeforeThatC= $oneBeforeThat eq $beforeD= $before eq $lastRole
ABCDresult
0XXX0
10XX0
110X0
11100
11111
77 $oneBeforeThat eq $before and $before eq $lastRole;
78 739 my $difference = $self->difference;
79 739 my $sign = $self->sign;
80 739 ▶ my $otherDirection = first { $_ ne $sign } ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 739 ▶ if ( not $sign or not defined $difference ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $sign && defined $difference) { } 117 622 739
elsif ($lastRole eq $otherDirection) { } 496 126 622
elsif ($lastRole eq $sign) { } 126 0 126
Condition66%2/3
$sign && defined $difference
!ll&&!rl&&r
1170622
MC/DC50%1/2
$sign && defined $difference
$sign defined $difference
Truth table66%2/3
A && B
A= $signB= defined $difference
ABresult
0X0
100
111
82 117 $sign = $lastRole;
83 117 $difference = 1;
84 }
85 elsif ( $lastRole eq $otherDirection ) {
86 496 ▶ if ( $difference > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($difference > 0) { } 292 204 496
elsif ($difference == 0) { } 204 0 204
87 292 $difference--;
88 292 ▶ if ( $difference == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($difference == 0) 247 45 292
89 247 $sign = $otherDirection;
90 }
91 }
92 elsif ( $difference == 0 ) {
93 204 $sign = $lastRole;
94 204 $difference = 1;
95 }
96 else {
97 0 die "$difference games more as $sign after $lastRole role?";
98 }
99 }
100 elsif ( $lastRole eq $sign ) {
101 126 $difference++;
102 126 ▶ if ( $difference > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($difference > 2) 32 94 126
103 32 $message .= "$difference games more as $lastRole\n";
104 }
105 }
106 else {
107 0 die
108 "$lastRole role update on ${difference}-game difference in $sign role?";
109 }
110 739 $self->sign($sign);
111 739 $self->difference($difference);
112 739 ▶ if ($before) { $self->lastTwo( [ $before, $lastRole ] ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($before) { } 533 206 739
113 206 else { $self->lastTwo( [$lastRole] ); }
114 }
115  
116  
117 =head2 asString
118  
119 $pref->asString
120  
121 The difference as a string, ^[+-][012]$. '0' represents a mild preference, '1' a strong one and '2' an absolute one. '-' represents a preference for White, or the first element of @Games::Tournament::Swiss::Config::roles, and '+' represents a preference for Black or the second element. A player may have an absolute preference even if the difference is 0, because it played the previous 2 rounds in the other color.
122  
123 =cut
124  
125  
126 0 ▶sub asString {
Subroutine asString: not called CC=3 SCAR=24.8
Pod documented
127 0 my $self = shift;
128 0 ▶ my $string = $self->sign eq (ROLES)[0] ? '+' :
Branch: 2 branches
BranchTrueFalseTotal
$self->sign eq ('White', 'Black')[0] ? : 0 0 0
$self->sign eq ('White', 'Black')[1] ? : 0 0 0
129 $self->sign eq (ROLES)[1] ? '-' : '';
130 0 $string .= $self->difference;
131 }
132  
133  
134 =head2 difference
135  
136 $pref->difference(2)
137  
138 Sets/gets the value of the difference in games played in one role over those played in other alternative roles. Equals either 0,1,2.
139  
140 =cut
141  
142  
143 7067 ▶sub difference {
Subroutine difference: called CC=2 SCAR=6.9
Pod documented
144 7067 my $self = shift;
145 7067 my $difference = shift();
146 7067 ▶ $self->{difference} = $difference if defined $difference;
Branch: 1 branch
BranchTrueFalseTotal
if defined $difference 2362 4705 7067
147 7067 return $self->{difference};
148 }
149  
150  
151 =head2 sign
152  
153 $pref->sign('Black')
154 $pref->sign('-')
155  
156 Sets/gets the role which the player has taken more often, or more recently, than other alternative roles. The preference is thus for the other role.
157  
158 =cut
159  
160 77207 ▶sub sign {
Subroutine sign: called CC=3 SCAR=11.0
Pod documented
161 77207 my $self = shift;
162 77207 ▶ my $sign = shift() || $self->{sign};
Condition100%3/3
shift() || $self->{'sign'}
l!l&&r!l&&!r
2421730881698
MC/DC100%2/2
shift() || $self->{'sign'}
shift() $self->{'sign'}
Truth table100%3/3
A || B
A= shift()B= $self->{'sign'}
ABresult
1X1
011
000
163 77207 my %abbrev = ( White => '+', Black => '-' );
164 77207 my %expando = reverse %abbrev;
165 77207 ▶ $sign = $expando{$sign} if $expando{$sign};
Branch: 1 branch
BranchTrueFalseTotal
if $expando{$sign} 0 77207 77207
166 77207 $self->{sign} = $sign;
167 77207 return $sign;
168 }
169  
170  
171 =head2 strength
172  
173 $pref->strength
174  
175 Gets the strength of the preference, 'Mild,' 'Strong,' or 'Absolute.'
176  
177 =cut
178  
179 3966 ▶sub strength {
Subroutine strength: called CC=4 SCAR=13.9
Pod documented
180 3966 my $self = shift;
181 3966 my @degree = qw/Mild Strong Absolute/;
182 3966 my $diff = $self->difference;
183 3966 my $strength = $degree[$diff];
184 3966 ▶ $strength = 'Absolute' if $diff > 2 ;
Branch: 1 branch
BranchTrueFalseTotal
if $diff > 2 298 3668 3966
185 3966 my @lastRoles = @{ $self->lastTwo };
186 3966 ▶ if ( @lastRoles == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lastRoles == 2) 3322 644 3966
187 3322 ▶ $strength = 'Absolute' if $lastRoles[0] eq $lastRoles[1];
Branch: 1 branch
BranchTrueFalseTotal
if $lastRoles[0] eq $lastRoles[1] 1029 2293 3322
188 }
189 3966 return $strength;
190 }
191  
192  
193 =head2 role
194  
195 $pref->role
196  
197 Gets the role which the preference entitles/requires the player to take in the next round. Not defined if sign is ''.
198  
199 =cut
200  
201 30752 ▶sub role {
Subroutine role: called CC=4 SCAR=13.9
Pod documented
202 30752 my $self = shift;
203 30752 my $role;
204 30752 ▶ $role = first { $_ ne $self->sign } ROLES if $self->sign;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $self->sign 29192 1560 30752
205 30752 my @lastRoles = @{ $self->lastTwo };
206 30752 ▶ if ( @lastRoles == 2 and $lastRoles[0] eq $lastRoles[1] )
Branch: 1 branch
BranchTrueFalseTotal
if (@lastRoles == 2 and $lastRoles[0] eq $lastRoles[1]) 3909 26843 30752
Condition100%3/3
@lastRoles == 2 and $lastRoles[0] eq $lastRoles[1]
!ll&&!rl&&r
8120187233909
MC/DC100%2/2
@lastRoles == 2 and $lastRoles[0] eq $lastRoles[1]
@lastRoles == 2 $lastRoles[0] eq $lastRoles[1]
Truth table100%3/3
A and B
A= @lastRoles == 2B= $lastRoles[0] eq $lastRoles[1]
ABresult
0X0
100
111
207 {
208 3909 ▶ $role = first { $_ ne $lastRoles[0] } ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.0
209 }
210 30752 return $role;
211 }
212  
213  
214 =head2 round
215  
216 $pref->round
217  
218 Sets/gets the round in this game up to which play is used to calculate the preference . The default is 0.
219  
220 =cut
221  
222 0 ▶sub round {
Subroutine round: not called CC=2 SCAR=17.9
Pod documented
223 0 my $self = shift;
224 0 ▶ my $round = shift() || $self->{round};
Condition0%0/3
shift() || $self->{'round'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'round'}
shift() $self->{'round'}
Truth table0%0/3
A || B
A= shift()B= $self->{'round'}
ABresult
1X1
011
000
225 0 $self->{round} = $round;
226 0 return $round;
227 }
228  
229  
230 =head2 lastTwo
231  
232 $pref->lastTwo
233  
234 Sets/gets a list of the roles in the last 2 games. If the 2 roles are the same, there is an absolute preference for the other role.
235  
236 =cut
237  
238 35502 ▶sub lastTwo {
Subroutine lastTwo: called CC=3 SCAR=11.0
Pod documented
239 35502 my $self = shift;
240 35502 my $lastTwo = shift;
241 35502 ▶ if ( defined $lastTwo ) { $self->{lastTwo} = $lastTwo; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $lastTwo) { } 739 34763 35502
elsif ($self->{'lastTwo'}) { } 31130 3633 34763
242 31130 elsif ( $self->{lastTwo} ) { return $self->{lastTwo}; }
243 3633 else { return []; }
244 }
245  
246 =head1 AUTHOR
247  
248 Dr Bean, C<< <drbean, followed by the at mark (@), cpan, then a dot, and finally, org> >>
249  
250 =head1 BUGS
251  
252 Please report any bugs or feature requests to
253 C<bug-games-tournament-contestant at rt.cpan.org>, or through the web interface at
254 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Tournament-Swiss>.
255 I will be notified, and then you'll automatically be notified of progress on
256 your bug as I make changes.
257  
258 =head1 SUPPORT
259  
260 You can find documentation for this module with the perldoc command.
261  
262 perldoc Games::Tournament::Contestant::Swiss::Preference
263  
264 You can also look for information at:
265  
266 =over 4
267  
268 =item * AnnoCPAN: Annotated CPAN documentation
269  
270 L<http://annocpan.org/dist/Games-Tournament-Swiss>
271  
272 =item * CPAN Ratings
273  
274 L<http://cpanratings.perl.org/d/Games-Tournament-Swiss>
275  
276 =item * RT: CPAN's request tracker
277  
278 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Tournament-Swiss>
279  
280 =item * Search CPAN
281  
282 L<http://search.cpan.org/dist/Games-Tournament-Swiss>
283  
284 =back
285  
286 =head1 ACKNOWLEDGEMENTS
287  
288 =head1 COPYRIGHT & LICENSE
289  
290 Copyright 2006 Dr Bean, all rights reserved.
291  
292 This program is free software; you can redistribute it and/or modify it
293 under the same terms as Perl itself.
294  
295 =cut
296  
297 35 1; # End of Games::Tournament::Contestant::Preference
298  
299 # vim: set ts=8 sts=4 sw=4 noet:
« blib/lib/Games/Tournament/Card.pm ↑ Summary blib/lib/Games/Tournament/Swiss/Config.pm »