lib/Bracket/Model/DBIC.pm

filter: Statementstmt 9.8% 8 / 81 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 14.2% 2 / 14 Podpod 16.6% 1 / 6 totaltotal 9.9% 11 / 111 CC 10 SCAR 20.1
CC
10
Cov
9%
__ANON__
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 7.5
« lib/Bracket/Controller/Admin.pm ↑ Summary lib/Bracket/Controller/Player.pm »
1 package Bracket::Model::DBIC;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use base 'Catalyst::Model::DBIC::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 __PACKAGE__->config(schema_class => 'Bracket::Schema',);
7  
8 =head2 update_points
9  
10 SQL update of points that is way faster than player_points action in Admin.
11 DRAWBACK: only tested on MySQL, may be MySQL specfic update.
12 SOLUTION: Find DBIC way of doing it? Use sub-query.
13  
14 Note: sqlite3 does not like the syntax on this update
15  
16 =cut
17  
18 sub update_points {
19 0 ▶ my $self = shift;
Subroutine update_points: not called CC=1 SCAR=6.9
Pod documented
20 0 my $storage = $self->schema->storage;
21 return $storage->dbh_do(
22 sub {
23 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 my $dbh = shift;
25 0 my $sth = $dbh->prepare('delete from region_score;');
26 0 $sth->execute;
27 0 $sth = $dbh->prepare('
28 insert into region_score
29 (player, region)
30 select player.id, region.id
31 from player, region
32 where player.active = 1;'
33 );
34 0 $sth->execute;
35 0 $sth = $dbh->prepare('
36 update region_score region_score,
37 (
38 select player_picks.player,
39 sum(game.round*(5 + game.lower_seed*team.seed)) as points,
40 team.region as region
41 from pick player_picks, pick perfect_picks, game game, team team
42 where perfect_picks.pick = player_picks.pick
43 and perfect_picks.game = player_picks.game
44 and player_picks.game = game.id
45 and player_picks.pick = team.id
46 and perfect_picks.player = 1
47 group by player_picks.player, team.region
48 ) computed_player_points
49 set region_score.points = computed_player_points.points
50 where region_score.player = computed_player_points.player
51 and region_score.region = computed_player_points.region
52 ;'
53 );
54 0 $sth->execute;
55 0 $sth = $dbh->prepare('
56 update player player,
57 (
58 select player, sum(points) as total_points from region_score
59 group by player
60 ) region_scores
61 set player.points = region_scores.total_points
62 where player.id = region_scores.player;
63 ');
64 0 $sth->execute;
65  
66 }
67 0 );
68 }
69  
70 =heads2 count_region_picks
71  
72 Count up how many picks a player has made for each region.
73 Displayed on Player home page.
74  
75 =cut
76  
77 sub count_region_picks {
78 0 ▶ my ($self, $player_id) = @_;
Subroutine count_region_picks: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $storage = $self->schema->storage;
80 return $storage->dbh_do(
81 sub {
82 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
83 0 my $dbh = shift;
84 0 my $sth = $dbh->prepare('
85 select region.id, count(*) from pick
86 join team on pick.pick = team.id
87 join region on team.region = region.id
88 join game on pick.game = game.id
89 where game.round < 5 and player = ?
90 group by region.id
91 ;'
92 );
93 0 ▶ $sth->execute($player_id) or die $sth->errstr;;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($player_id) or die $sth->errstr 0 0 0
94 0 my $picks_per_region = { 1 => 0, 2 => 0, 3 => 0, 4 => 0 };
95 0 my $result = $sth->fetchall_arrayref;
96 0 foreach my $row (@{$result}) {
97 0 $picks_per_region->{$row->[0]} = $row->[1];
98 }
99 0 return $picks_per_region;
100 }
101 0 );
102 }
103  
104 =heads2 count_player_picks
105  
106 Count up how many picks a player has made out of the total (63).
107 Displayed on All Players home page.
108  
109 =cut
110  
111 sub count_player_picks {
112 0 ▶ my ($self) = @_;
Subroutine count_player_picks: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $storage = $self->schema->storage;
114 return $storage->dbh_do(
115 sub {
116 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
117 0 my $dbh = shift;
118 0 my $sth = $dbh->prepare('
119 select player.id, count(*) from player
120 join pick on player.id = pick.player
121 group by player.id
122 ;'
123 );
124 0 ▶ $sth->execute() or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
125 0 my $picks_per_player = {};
126 0 my $result = $sth->fetchall_arrayref;
127 0 foreach my $row (@{$result}) {
128 0 $picks_per_player->{$row->[0]} = $row->[1];
129 }
130 0 return $picks_per_player;
131 }
132 0 );
133 }
134  
135 =heads2 count_player_picks_correct
136  
137 Count up how many picks a player has made correct so far.
138 Displayed on All Players home page.
139  
140 =cut
141  
142 sub count_player_picks_correct {
143 0 ▶ my $self = shift;
Subroutine count_player_picks_correct: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my $storage = $self->schema->storage;
145 return $storage->dbh_do(
146 sub {
147 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
148 0 my $dbh = shift;
149 0 my $sth = $dbh->prepare('
150 select player_picks.player, count(*)
151 from pick player_picks, pick perfect_picks, game game, team team
152 where perfect_picks.pick = player_picks.pick
153 and perfect_picks.game = player_picks.game
154 and player_picks.game = game.id
155 and player_picks.pick = team.id
156 and perfect_picks.player = 1
157 group by player_picks.player
158 ;'
159 );
160 0 ▶ $sth->execute() or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
161 0 my $picks_per_player = {};
162 0 my $result = $sth->fetchall_arrayref;
163 0 foreach my $row (@{$result}) {
164 0 $picks_per_player->{$row->[0]} = $row->[1];
165 }
166 0 return $picks_per_player;
167 }
168 0 );
169 }
170  
171 =heads2 count_player_picks_upset
172  
173 Count up how many upset picks a player has made correct so far.
174  
175 =cut
176  
177 sub count_player_picks_upset {
178 0 ▶ my $self = shift;
Subroutine count_player_picks_upset: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my $storage = $self->schema->storage;
180 return $storage->dbh_do(
181 sub {
182 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
183 0 my $dbh = shift;
184 0 my $sth = $dbh->prepare('
185 select player_picks.player, count(*)
186 from pick player_picks, pick perfect_picks, game game, team team
187 where perfect_picks.pick = player_picks.pick
188 and perfect_picks.game = player_picks.game
189 and player_picks.game = game.id
190 and player_picks.pick = team.id
191 and perfect_picks.player = 1
192 and game.lower_seed = 1
193 group by player_picks.player
194 ;'
195 );
196 0 ▶ $sth->execute() or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
197 0 my $upset_picks_per_player = {};
198 0 my $result = $sth->fetchall_arrayref;
199 0 foreach my $row (@{$result}) {
200 0 $upset_picks_per_player->{$row->[0]} = $row->[1];
201 }
202 0 return $upset_picks_per_player;
203 }
204 0 );
205 }
206  
207 =heads2 count_final4_picks
208  
209 Count up how many picks a player has made in the final 4 (3 total).
210 Displayed on Players home page.
211  
212 =cut
213  
214 sub count_final4_picks {
215 0 ▶ my ($self, $player_id) = @_;
Subroutine count_final4_picks: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my $storage = $self->schema->storage;
217 return $storage->dbh_do(
218 sub {
219 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
220 0 my $dbh = shift;
221 0 my $sth = $dbh->prepare('
222 select count(*) from player
223 join pick on player.id = pick.player
224 where pick.game > 60
225 and player.id = ?
226 ;'
227 );
228 0 ▶ $sth->execute($player_id) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($player_id) or die $sth->errstr 0 0 0
229
230 0 return $sth->fetchall_arrayref->[0]->[0];
231 }
232 0 );
233 }
234  
235 =head1 NAME
236  
237 Bracket::Model::DBIC - Catalyst DBIC Schema Model
238 =head1 SYNOPSIS
239  
240 See L<Bracket>
241  
242 =head1 DESCRIPTION
243  
244 L<Catalyst::Model::DBIC::Schema> Model using schema L<Bracket::Schema::DBIC>
245  
246 =head1 AUTHOR
247  
248 Mateu X Hunter
249  
250 =head1 LICENSE
251  
252 This library is free software, you can redistribute it and/or modify
253 it under the same terms as Perl itself.
254  
255 =cut
256  
257 4 1;
« lib/Bracket/Controller/Admin.pm ↑ Summary lib/Bracket/Controller/Player.pm »