line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Ratings; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
## TODO: Error handling |
4
|
|
|
|
|
|
|
## * croak() |
5
|
|
|
|
|
|
|
## check arguments for subroutines (use Data::Checker)? |
6
|
|
|
|
|
|
|
|
7
|
37
|
|
|
37
|
|
24319
|
use warnings; |
|
37
|
|
|
|
|
70
|
|
|
37
|
|
|
|
|
1604
|
|
8
|
37
|
|
|
37
|
|
204
|
use strict; |
|
37
|
|
|
|
|
71
|
|
|
37
|
|
|
|
|
1108
|
|
9
|
37
|
|
|
37
|
|
257
|
use Carp; |
|
37
|
|
|
|
|
74
|
|
|
37
|
|
|
|
|
3261
|
|
10
|
|
|
|
|
|
|
|
11
|
37
|
|
|
37
|
|
910
|
use 5.6.1; # 'our' was introduced in perl 5.6 |
|
37
|
|
|
|
|
214
|
|
|
37
|
|
|
|
|
10874
|
|
12
|
37
|
|
|
37
|
|
1238
|
use version; our $VERSION = qv('0.0.5'); |
|
37
|
|
|
|
|
2424
|
|
|
37
|
|
|
|
|
234
|
|
13
|
|
|
|
|
|
|
|
14
|
37
|
|
|
37
|
|
70872
|
use Class::Std::Utils; # we are using inside-out objects |
|
37
|
|
|
|
|
30577
|
|
|
37
|
|
|
|
|
224
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
{ |
17
|
|
|
|
|
|
|
## objects of this class (players) will have the following attributes |
18
|
|
|
|
|
|
|
my %rating_of; # rating of player |
19
|
|
|
|
|
|
|
my %coefficient_of; # coefficient of player |
20
|
|
|
|
|
|
|
my %games_of; # list of games of player |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
## create new object (inside-out object -- see "Encapsulation" in PBP) |
23
|
|
|
|
|
|
|
sub new { |
24
|
34
|
|
|
34
|
1
|
840
|
my ($class) = @_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
## bless a scalar to instantiate the new object |
27
|
34
|
|
|
|
|
262
|
my $new_player_object = bless anon_scalar(), $class; |
28
|
|
|
|
|
|
|
|
29
|
34
|
|
|
|
|
325
|
return $new_player_object; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## set rating according to passed argument |
33
|
|
|
|
|
|
|
sub set_rating { |
34
|
126
|
|
|
126
|
1
|
479
|
my ($self, $rating) = @_; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
## check that rating is passed as argument |
37
|
126
|
50
|
|
|
|
387
|
croak( 'Usage: $obj->set_rating($rating)' ) |
38
|
|
|
|
|
|
|
if @_ < 2; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
## store rating in player object |
41
|
126
|
|
|
|
|
465
|
$rating_of{ident $self} = $rating; |
42
|
|
|
|
|
|
|
|
43
|
126
|
|
|
|
|
323
|
return; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
## return previously set rating |
47
|
|
|
|
|
|
|
sub get_rating { |
48
|
258
|
|
|
258
|
1
|
389
|
my ($self) = @_; |
49
|
258
|
|
|
|
|
1092
|
return $rating_of{ident $self}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
## set coefficient according to passed argument |
53
|
|
|
|
|
|
|
sub set_coefficient { |
54
|
33
|
|
|
33
|
1
|
341
|
my ($self, $coefficient) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
## check that coefficient is passed as argument |
57
|
33
|
50
|
|
|
|
833
|
croak( 'Usage: $obj->set_coefficient($coefficient)' ) |
58
|
|
|
|
|
|
|
if @_ < 2; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
## store coefficient in player object |
61
|
33
|
|
|
|
|
173
|
$coefficient_of{ident $self} = $coefficient; |
62
|
|
|
|
|
|
|
|
63
|
33
|
|
|
|
|
108
|
return; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
## return previously set coefficient |
67
|
|
|
|
|
|
|
sub get_coefficient { |
68
|
37
|
|
|
37
|
1
|
100
|
my ($self) = @_; |
69
|
37
|
|
|
|
|
240
|
return $coefficient_of{ident $self}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
## add new game to list of games ($games_of{ident $self} is list of games) |
73
|
|
|
|
|
|
|
sub add_game { |
74
|
285
|
|
|
285
|
1
|
2391
|
my ($self, $game_ref) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
## check that new game is passed as argument (hash reference) |
77
|
285
|
50
|
|
|
|
851
|
croak( 'Usage: $obj->add_game( { |
78
|
|
|
|
|
|
|
opponent_rating => 2300, |
79
|
|
|
|
|
|
|
result => \'draw\', |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
' ) |
83
|
|
|
|
|
|
|
if @_ < 2; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
## store additional game in player object |
86
|
285
|
|
|
|
|
337
|
push @{ $games_of{ident $self} }, $game_ref; |
|
285
|
|
|
|
|
863
|
|
87
|
|
|
|
|
|
|
|
88
|
285
|
|
|
|
|
591
|
return; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
## return all previously added games as a list of hash references |
92
|
|
|
|
|
|
|
sub get_all_games { |
93
|
343
|
|
|
343
|
1
|
510
|
my ($self) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
## return list of games or throw an error if $self doesn't have games |
96
|
343
|
50
|
|
|
|
1109
|
if ( $games_of{ident $self} ) { |
97
|
343
|
|
|
|
|
756
|
return @{ $games_of{ident $self} }; |
|
343
|
|
|
|
|
1892
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
0
|
|
|
|
|
0
|
croak( 'There aren\'t any games played. Please use add_game().' ) |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
## delete all previously added games |
105
|
|
|
|
|
|
|
sub remove_all_games { |
106
|
5
|
|
|
5
|
1
|
19
|
my ($self) = @_; |
107
|
5
|
|
|
|
|
11
|
$games_of{ident $self} = undef; |
108
|
5
|
|
|
|
|
12
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
## clean up attributes when object is destroyed |
112
|
|
|
|
|
|
|
sub DESTROY { |
113
|
34
|
|
|
34
|
|
80391
|
my ($self) = @_; |
114
|
|
|
|
|
|
|
|
115
|
34
|
|
|
|
|
568
|
delete $rating_of{ident $self}; |
116
|
34
|
|
|
|
|
167
|
delete $coefficient_of{ident $self}; |
117
|
34
|
|
|
|
|
310
|
delete $games_of{ident $self}; |
118
|
|
|
|
|
|
|
|
119
|
34
|
|
|
|
|
4159
|
return; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
## return number of played games |
124
|
|
|
|
|
|
|
sub get_number_of_games_played { |
125
|
99
|
|
|
99
|
1
|
178
|
my ($self) = @_; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
## number of played games equals length of array of played games |
128
|
99
|
|
|
|
|
449
|
return scalar $self->get_all_games(); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
## calculate and return scored points |
132
|
|
|
|
|
|
|
sub get_points_scored { |
133
|
69
|
|
|
69
|
1
|
301
|
my ($self) = @_; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
## compute scored points from list of played games |
136
|
69
|
|
|
|
|
103
|
my $points_scored; |
137
|
69
|
|
|
|
|
556
|
foreach my $game_ref ( $self->get_all_games() ) { |
138
|
560
|
|
|
|
|
2506
|
$points_scored += _get_numerical_result( $game_ref->{result} ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
## return scored points |
142
|
69
|
|
|
|
|
265
|
return $points_scored; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
## calculate percentage score |
146
|
|
|
|
|
|
|
sub get_percentage_score { |
147
|
29
|
|
|
29
|
1
|
71
|
my ($self) = @_; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
## compute percentage score |
150
|
29
|
|
|
|
|
293
|
my $percentage_score = $self->get_points_scored() |
151
|
|
|
|
|
|
|
/ $self->get_number_of_games_played(); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
## return percentage score |
154
|
29
|
|
|
|
|
371
|
return $percentage_score; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
## calculate and return return average rating of opponents |
158
|
|
|
|
|
|
|
sub get_average_rating_of_opponents { |
159
|
30
|
|
|
30
|
1
|
94
|
my ($self) = @_; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
## calculate average rating of opponents from (list of) stored games |
162
|
30
|
|
|
|
|
233
|
my $rat_opps; |
163
|
30
|
|
|
|
|
142
|
foreach my $game_ref ( $self->get_all_games() ) { |
164
|
251
|
|
|
|
|
545
|
$rat_opps += $game_ref->{opponent_rating}, |
165
|
|
|
|
|
|
|
} |
166
|
30
|
|
|
|
|
351
|
$rat_opps = sprintf( "%.f", $rat_opps |
167
|
|
|
|
|
|
|
/ $self->get_number_of_games_played() ); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
## return average rating of opponents |
170
|
30
|
|
|
|
|
121
|
return $rat_opps; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
## define lookup table for conversion 'verbal results' -> 'numerical results' |
174
|
|
|
|
|
|
|
my %numerical_results = ( |
175
|
|
|
|
|
|
|
win => 1, |
176
|
|
|
|
|
|
|
draw => 0.5, |
177
|
|
|
|
|
|
|
loss => 0, |
178
|
|
|
|
|
|
|
); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
## get numerical result of $result (win, draw, loss) |
181
|
|
|
|
|
|
|
sub _get_numerical_result { |
182
|
688
|
|
|
688
|
|
874
|
my ($result) = @_; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
## numerical result is looked up in a small table (see above) |
185
|
688
|
|
|
|
|
1607
|
my $numerical_result = $numerical_results{$result}; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
## return numerical result |
188
|
688
|
|
|
|
|
2129
|
return $numerical_result; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
192
|
|
|
|
|
|
|
__END__ |