File Coverage

blib/lib/Bracket/Model/DBIC.pm
Criterion Covered Total %
statement 9 90 10.0
branch 0 10 0.0
condition n/a
subroutine 3 15 20.0
pod 1 6 16.6
total 13 121 10.7


line stmt bran cond sub pod time code
1              
2             use strict;
3 4     4   2766351 use base 'Catalyst::Model::DBIC::Schema';
  4         11  
  4         141  
4 4     4   22 use List::Util qw( max );
  4         8  
  4         2715  
5 4     4   1316974  
  4         9  
  4         3270  
6             __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             my $self = shift;
19             my $storage = $self->schema->storage;
20 0     0 1   return $storage->dbh_do(
21 0           sub {
22             my $self = shift;
23             my $dbh = shift;
24 0     0     my $sth = $dbh->prepare('delete from region_score;');
25 0           $sth->execute;
26 0           $sth = $dbh->prepare('
27 0           insert into region_score
28 0           (player, region, points)
29             select player.id, region.id, 0
30             from player, region
31             where player.active = 1;'
32             );
33             $sth->execute;
34             $sth = $dbh->prepare('
35 0           update region_score region_score,
36 0           (
37             select player_picks.player,
38             sum(game.round*(5 + game.lower_seed*team.seed)) as points,
39             team.region as region
40             from pick player_picks, pick perfect_picks, game game, team team
41             where perfect_picks.pick = player_picks.pick
42             and perfect_picks.game = player_picks.game
43             and player_picks.game = game.id
44             and player_picks.pick = team.id
45             and perfect_picks.player = 1
46             group by player_picks.player, team.region
47             ) computed_player_points
48             set region_score.points = computed_player_points.points
49             where region_score.player = computed_player_points.player
50             and region_score.region = computed_player_points.region
51             ;'
52             );
53             $sth->execute;
54             $sth = $dbh->prepare('
55 0           update player player,
56 0           (
57             select player, sum(points) as total_points from region_score
58             group by player
59             ) region_scores
60             set player.points = region_scores.total_points
61             where player.id = region_scores.player;
62             ');
63             $sth->execute;
64              
65 0           }
66             );
67             }
68 0            
69             =heads2 count_region_picks
70              
71             Count up how many picks a player has made for each region.
72             Displayed on Player home page.
73              
74             =cut
75              
76             my ($self, $player_id) = @_;
77             my $storage = $self->schema->storage;
78             return $storage->dbh_do(
79 0     0 0   sub {
80 0           my $self = shift;
81             my $dbh = shift;
82             my $sth = $dbh->prepare('
83 0     0     select region.id, count(*) from pick
84 0           join team on pick.pick = team.id
85 0           join region on team.region = region.id
86             join game on pick.game = game.id
87             where game.round < 5 and player = ?
88             group by region.id
89             ;'
90             );
91             $sth->execute($player_id) or die $sth->errstr;;
92             my $picks_per_region = { 1 => 0, 2 => 0, 3 => 0, 4 => 0 };
93             my $result = $sth->fetchall_arrayref;
94 0 0         foreach my $row (@{$result}) {
95 0           $picks_per_region->{$row->[0]} = $row->[1];
96 0           }
97 0           return $picks_per_region;
  0            
98 0           }
99             );
100 0           }
101              
102 0           =heads2 count_player_picks
103              
104             Count up how many picks a player has made out of the total (63).
105             Displayed on All Players home page.
106              
107             =cut
108              
109             my ($self) = @_;
110             my $storage = $self->schema->storage;
111             return $storage->dbh_do(
112             sub {
113 0     0 0   my $self = shift;
114 0           my $dbh = shift;
115             my $sth = $dbh->prepare('
116             select player.id, count(*) from player
117 0     0     join pick on player.id = pick.player
118 0           group by player.id
119 0           ;'
120             );
121             $sth->execute() or die $sth->errstr;
122             my $picks_per_player = {};
123             my $result = $sth->fetchall_arrayref;
124             foreach my $row (@{$result}) {
125 0 0         $picks_per_player->{$row->[0]} = $row->[1];
126 0           }
127 0           return $picks_per_player;
128 0           }
  0            
129 0           );
130             }
131 0            
132             =heads2 count_player_picks_correct
133 0            
134             Count up how many picks a player has made correct so far.
135             Displayed on All Players home page.
136              
137             =cut
138              
139             my $self = shift;
140             my $storage = $self->schema->storage;
141             return $storage->dbh_do(
142             sub {
143             my $self = shift;
144 0     0 0   my $dbh = shift;
145 0           my $sth = $dbh->prepare('
146             select player_picks.player, count(*)
147             from pick player_picks, pick perfect_picks, game game, team team
148 0     0     where perfect_picks.pick = player_picks.pick
149 0           and perfect_picks.game = player_picks.game
150 0           and player_picks.game = game.id
151             and player_picks.pick = team.id
152             and perfect_picks.player = 1
153             group by player_picks.player
154             ;'
155             );
156             $sth->execute() or die $sth->errstr;
157             my $picks_per_player = {};
158             my $result = $sth->fetchall_arrayref;
159             foreach my $row (@{$result}) {
160             $picks_per_player->{$row->[0]} = $row->[1];
161 0 0         }
162 0           my $max_correct = max map { $picks_per_player->{$_} } grep { $_ != 1 } keys %{$picks_per_player};
163 0           return $picks_per_player, $max_correct;
164 0           }
  0            
165 0           );
166             }
167 0            
  0            
  0            
  0            
168 0           =heads2 count_player_picks_upset
169              
170 0           Count up how many upset picks a player has made correct so far.
171              
172             =cut
173              
174             my $self = shift;
175             my $storage = $self->schema->storage;
176             return $storage->dbh_do(
177             sub {
178             my $self = shift;
179             my $dbh = shift;
180 0     0 0   my $sth = $dbh->prepare('
181 0           select player_picks.player, count(*)
182             from pick player_picks, pick perfect_picks, game game, team team
183             where perfect_picks.pick = player_picks.pick
184 0     0     and perfect_picks.game = player_picks.game
185 0           and player_picks.game = game.id
186 0           and player_picks.pick = team.id
187             and perfect_picks.player = 1
188             and game.lower_seed = 1
189             group by player_picks.player
190             ;'
191             );
192             $sth->execute() or die $sth->errstr;
193             my $upset_picks_per_player = {};
194             my $result = $sth->fetchall_arrayref;
195             foreach my $row (@{$result}) {
196             $upset_picks_per_player->{$row->[0]} = $row->[1];
197             }
198 0 0         my $max_upsets = max map { $upset_picks_per_player->{$_} } grep { $_ != 1 } keys %{$upset_picks_per_player};
199 0           return $upset_picks_per_player, $max_upsets;
200 0           }
201 0           );
  0            
202 0           }
203              
204 0           =heads2 count_final4_picks
  0            
  0            
  0            
205 0            
206             Count up how many picks a player has made in the final 4 (3 total).
207 0           Displayed on Players home page.
208              
209             =cut
210              
211             my ($self, $player_id) = @_;
212             my $storage = $self->schema->storage;
213             return $storage->dbh_do(
214             sub {
215             my $self = shift;
216             my $dbh = shift;
217             my $sth = $dbh->prepare('
218 0     0 0   select count(*) from player
219 0           join pick on player.id = pick.player
220             where pick.game > 60
221             and player.id = ?
222 0     0     ;'
223 0           );
224 0           $sth->execute($player_id) or die $sth->errstr;
225            
226             return $sth->fetchall_arrayref->[0]->[0];
227             }
228             );
229             }
230              
231 0 0         =head1 NAME
232              
233 0           Bracket::Model::DBIC - Catalyst DBIC Schema Model
234             =head1 SYNOPSIS
235 0            
236             See L<Bracket>
237              
238             =head1 DESCRIPTION
239              
240             L<Catalyst::Model::DBIC::Schema> Model using schema L<Bracket::Schema::DBIC>
241              
242             =head1 AUTHOR
243              
244             Mateu X Hunter
245              
246             =head1 LICENSE
247              
248             This library is free software, you can redistribute it and/or modify
249             it under the same terms as Perl itself.
250              
251             =cut
252              
253             1;