| 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@lucidport.com> | ||||||||||||||||||||||||||||
| 11 | # COMPANY: LucidPort Technology, Inc. | ||||||||||||||||||||||||||||
| 12 | # CREATED: 11/19/2010 03:13:05 PM PST | ||||||||||||||||||||||||||||
| 13 | #=============================================================================== | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # the Game class is useful for tournament pairing | ||||||||||||||||||||||||||||
| 19 | package Games::Go::AGA::DataObjects::Game; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Scalar::Util qw(refaddr weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Games::Go::AGA::Parse::Util qw( Rank_to_Rating ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Games::Go::AGA::DataObjects::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | our $VERSION = '0.107'; # VERSION | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | has 'black' => ( | ||||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||
| 32 | isa => 'Games::Go::AGA::DataObjects::Player', | ||||||||||||||||||||||||||||
| 33 | weak_ref => 1, # Players have Games, Games have Players, so weaken | ||||||||||||||||||||||||||||
| 34 | trigger => sub | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 5 | $self->_set_player('black', @_); | |||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | 2 | ); | |||||||||||||||||||||||||||
| 40 | has 'white' => ( | ||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||
| 42 | isa => 'Games::Go::AGA::DataObjects::Player', | ||||||||||||||||||||||||||||
| 43 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 44 | trigger => sub | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 4 | $self->_set_player('white', @_); | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 2 | ); | |||||||||||||||||||||||||||
| 50 | has 'table_number' => ( | ||||||||||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||||||||||
| 52 | isa => 'Int', | ||||||||||||||||||||||||||||
| 53 | default => 0, | ||||||||||||||||||||||||||||
| 54 | 3 | ▶ | trigger => sub { shift->changed; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 2 | ); | |||||||||||||||||||||||||||
| 56 | has 'handi' => ( | ||||||||||||||||||||||||||||
| 57 | is => 'rw', | ||||||||||||||||||||||||||||
| 58 | isa => 'Handicap', | ||||||||||||||||||||||||||||
| 59 | default => 0, | ||||||||||||||||||||||||||||
| 60 | 4 | ▶ | trigger => sub { shift->changed; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | # alias => 'handicap', | ||||||||||||||||||||||||||||
| 62 | 2 | ); | |||||||||||||||||||||||||||
| 63 | has 'komi' => ( | ||||||||||||||||||||||||||||
| 64 | is => 'rw', | ||||||||||||||||||||||||||||
| 65 | isa => 'Komi', | ||||||||||||||||||||||||||||
| 66 | default => 5.5, | ||||||||||||||||||||||||||||
| 67 | 5 | ▶ | trigger => sub { shift->changed; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 2 | ); | |||||||||||||||||||||||||||
| 69 | has 'winner' => ( | ||||||||||||||||||||||||||||
| 70 | is => 'rw', | ||||||||||||||||||||||||||||
| 71 | default => undef, | ||||||||||||||||||||||||||||
| 72 | trigger => sub { | ||||||||||||||||||||||||||||
| 73 | 6 | ▶ | my ($self, $new) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 74 | #printf("winner(%d args: %s)\n", scalar @_, join(q{, }, @_)); | ||||||||||||||||||||||||||||
| 75 | 6 | ▶ | if (defined $new) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::refaddr($new) != builtin::refaddr($self->black) and builtin::refaddr($new) != builtin::refaddr($self->white)
MC/DC100%2/2
builtin::refaddr($new) != builtin::refaddr($self->black) and builtin::refaddr($new) != builtin::refaddr($self->white)
builtin::refaddr($new) != builtin::refaddr($self->black)
builtin::refaddr($new) != builtin::refaddr($self->white)
Truth table100%3/3
A and B
A= builtin::refaddr($new) != builtin::refaddr($self->black)B= builtin::refaddr($new) != builtin::refaddr($self->white)
| |||||||||||||||||||||||||||||
| 77 | (refaddr($new) != refaddr($self->black)) and | ||||||||||||||||||||||||||||
| 78 | (refaddr($new) != refaddr($self->white))) { | ||||||||||||||||||||||||||||
| 79 | 1 | $self->winner($self->{prev_winner}); # change-back | |||||||||||||||||||||||||||
| 80 | 1 | croak("winner must be either undef, or one of the players\n"); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | $self->{loser} = (refaddr($new) == refaddr($self->black)) ? | ||||||||||||||||||||||||||||
| 83 | 5 | ▶ | $self->{white} : $self->{black}; # not the winner | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 5 | $self->{prev_winner} = $new; # save for change-back (above) | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||
| 87 | 0 | delete $self->{loser}; # remove winner and loser | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 5 | $self->changed; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 2 | ); | |||||||||||||||||||||||||||
| 92 | has 'change_callback' => ( | ||||||||||||||||||||||||||||
| 93 | isa => 'Maybe[CodeRef]', | ||||||||||||||||||||||||||||
| 94 | is => 'rw', | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | default => sub { sub { } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 2 | ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub changed { | ||||||||||||||||||||||||||||
| 99 | 26 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
changed: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 26 | ▶ | &{$self->change_callback}($self) if ($self->{change_callback}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub _set_player { | ||||||||||||||||||||||||||||
| 105 | 9 | ▶ | my ($self, $color, $new) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_player: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 9 | ▶ | if (defined $self->winner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | $self->{$color} = $self->{"prev_$color"}; # restore | |||||||||||||||||||||||||||
| 109 | 0 | croak 'Winner already set, cannot change players'; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 9 | $self->{"prev_$color"} = $new; | |||||||||||||||||||||||||||
| 112 | 9 | $self->changed; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub loser { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
loser: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $self->{loser}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub opponent { | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my ($self, $player) = @_; | ||||||||||||||||||||||||||
|
Subroutine
opponent: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $me = $player->id; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return $self->white if ($self->black->id eq $me); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return $self->black if ($self->white->id eq $me); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | croak "ID $me is not in this game"; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub swap { | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
swap: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $white = $self->white; | |||||||||||||||||||||||||||
| 134 | 0 | $self->{white} = $self->black; | |||||||||||||||||||||||||||
| 135 | 0 | $self->{black} = $white; | |||||||||||||||||||||||||||
| 136 | 0 | $self->changed; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub handicap { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | my ($self, $default_komi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
handicap: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (defined $self->winner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | croak 'Winner already set, cannot change players'; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $default_komi = 7.5 if (not defined $default_komi); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my $white = $self->white; | |||||||||||||||||||||||||||
| 148 | 0 | my $black = $self->black; | |||||||||||||||||||||||||||
| 149 | 0 | my $rankDiff = $self->_rank_to_level($white) - $self->_rank_to_level($black); | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($rankDiff < 0.5) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 151 | 0 | $self->handi(0); | |||||||||||||||||||||||||||
| 152 | 0 | $self->komi($default_komi); # normal komi game | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | elsif ($rankDiff < 1.0) { | ||||||||||||||||||||||||||||
| 155 | 0 | $self->handi(0); | |||||||||||||||||||||||||||
| 156 | 0 | $self->komi(0.5); # no komi game, white wins ties | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | elsif ($rankDiff < 1.5) { | ||||||||||||||||||||||||||||
| 159 | 0 | $self->handi(0); | |||||||||||||||||||||||||||
| 160 | 0 | $self->komi(-$default_komi); # reverse komi game | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||
| 163 | 0 | $self->handi(int $rankDiff + 0.5); # handicap game | |||||||||||||||||||||||||||
| 164 | 0 | $self->komi(0.5); # white wins ties | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | # TODO handi/komi have different relationship in AGA vs ING rules... | ||||||||||||||||||||||||||||
| 167 | 0 | $self->changed; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub auto_handicap { | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my ($self, $default_komi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
auto_handicap: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (defined $self->winner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | croak 'Winner already set, cannot change players'; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | my $white = $self->white; | |||||||||||||||||||||||||||
| 178 | 0 | my $black = $self->black; | |||||||||||||||||||||||||||
| 179 | 0 | my $rankDiff = $self->_rank_to_level($white) - $self->_rank_to_level($black); | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($rankDiff < 0.1) { # black is significantly stronger than white - swap | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $self->{white} = $black; | |||||||||||||||||||||||||||
| 182 | 0 | $self->{black} = $white; | |||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||
| 184 | 0 | $self->handicap($default_komi); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # AGA ratings have a hole between +1 and -1 which messes up | ||||||||||||||||||||||||||||
| 188 | # handicap/komi calculations. Collapse that hole to make a 'level' | ||||||||||||||||||||||||||||
| 189 | sub _rank_to_level { | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my ($self, $player) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_rank_to_level: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | my $level = $player->adj_rating; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | return $level + (($level > 0) ? -1 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | no Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 2 | 1; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||