File Coverage

blib/lib/Bracket/Controller/Final4.pm
Criterion Covered Total %
statement 19 94 20.2
branch 0 16 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 26 126 20.6


line stmt bran cond sub pod time code
1             use Moose;
2 4     4   1823 BEGIN { extends 'Catalyst::Controller' }
  4         8  
  4         25  
3 4     4   22317 use Perl6::Junction qw/ any /;
4 4     4   21423 use Data::Dumper::Concise;
  4         9  
  4         192  
5 4     4   23  
  4         7  
  4         1621  
6             =head1 NAME
7              
8             Bracket::Controller::Final4 - Edit/View Final 4 Picks
9              
10             =cut
11              
12             my %region_winner_picks = (
13             1 => 15,
14             2 => 30,
15             3 => 45,
16             4 => 60,
17             );
18              
19             my ($self, $c, $player_id) = @_;
20              
21 0     0 0 0 # Restrict edits to user or admin role.
22             my @user_roles = $c->user->roles;
23             $c->go('/error_404') if (($player_id != $c->user->id) && !('admin' eq any(@user_roles)));
24 0         0  
25 0 0 0     0 my $player = $c->model('DBIC::Player')->find($player_id);
26             $c->stash->{player} = $player;
27 0         0  
28 0         0 # Get the player's regional winner picks. Later we deal w/ whether they actually won or not.
29             # region_id => game_id
30             foreach my $region_id (keys %region_winner_picks) {
31             my $region_name = 'region' . "_${region_id}";
32 0         0 my $game = $region_winner_picks{$region_id};
33 0         0 $c->stash->{$region_name} =
34 0         0 $c->model('DBIC::Pick')->search({ player => $player_id, game => $game })->first;
35 0         0 }
36              
37             # Get all player picks for loading when in edit of existing picks mode
38             my @picks = $c->model('DBIC::Pick')->search({ player => $player_id });
39             my %picks;
40 0         0 foreach my $pick (@picks) {
41 0         0 $picks{ $pick->game->id } = $pick->pick;
42 0         0 }
43 0         0 $c->stash->{picks} = \%picks;
44              
45 0         0 # Create Class for Final Four Teams
46             my %class_for;
47             foreach my $player_pick (@picks) {
48 0         0 my ($winning_pick) =
49 0         0 $c->model('DBIC::Pick')->search({ player => 1, game => $player_pick->game->id });
50 0         0 if (defined $winning_pick) {
51             if ($winning_pick->pick->id == $player_pick->pick->id) {
52 0 0       0 $class_for{ $player_pick->game->id } = 'in';
53 0 0       0 }
54 0         0 else {
55             $class_for{ $player_pick->game->id } = 'out';
56             }
57 0         0 }
58             else {
59             if ($player_pick->game->round >= $player_pick->pick->round_out) {
60              
61 0 0       0 #if ($player_pick->game == 63) {
62             # warn "round: " . $player_pick->game->round . "\n";
63             # warn "round_out: " . $player_pick->pick->round_out . "\n";
64              
65             #}
66             $class_for{ $player_pick->game->id } = 'out';
67             }
68 0         0 else {
69             $class_for{ $player_pick->game->id } = 'pending';
70             }
71 0         0 }
72             }
73             $c->stash->{class_for} = \%class_for;
74              
75 0         0 # Inform to load final 4 javascript
76             $c->stash->{final_4_javascript} = 1;
77             $c->stash->{template} = 'final4/make_final4_picks.tt';
78 0         0  
79 0         0 return;
80             }
81 0         0  
82 4     4   30 my ($self, $c, $player_id) = @_;
  4         8  
  4         38  
83              
84             my $player = $c->model('DBIC::Player')->find($player_id);
85 0     0 0 0 $c->stash->{player} = $player;
86             $c->stash->{player_id} = $player_id;
87 0         0  
88 0         0 my $params = $c->request->params;
89 0         0  
90             # Do database insert
91 0         0 foreach my $pgame (keys %{$params}) {
92             $pgame =~ m{p(\d+)};
93             my $game_id = $1;
94 0         0 my $team_id = ${$params}{$pgame};
  0         0  
95 0         0 my ($pick) = $c->model('DBIC::Pick')->search({ player => $player_id, game => $game_id });
96 0         0 if (defined $pick) {
97 0         0 $pick->pick($team_id);
  0         0  
98 0         0 $pick->update;
99 0 0       0 }
100 0         0 else {
101 0         0 my $new_pick = $c->model('DBIC::Pick')->new(
102             {
103             player => $player_id,
104 0         0 game => $game_id,
105             pick => $team_id
106             }
107             );
108             $new_pick->insert;
109             }
110             }
111 0         0 $c->stash->{params} = $params;
112             $c->response->redirect(
113             $c->uri_for($c->controller('Player')->action_for('home'))
114 0         0 . "/${player_id}"
115 0         0 );
116              
117             return;
118             }
119              
120 0         0 my ($self, $c, $player_id) = @_;
121 4     4   4909  
  4         10  
  4         18  
122             my $player = $c->model('DBIC::Player')->find($player_id);
123             $c->stash->{player} = $player;
124 0     0 0    
125             # Get the player's regional winner picks.
126 0           foreach my $region_id (keys %region_winner_picks) {
127 0           my $region_name = 'region' . "_${region_id}";
128             my $game = $region_winner_picks{$region_id};
129             $c->stash->{$region_name} =
130 0           $c->model('DBIC::Pick')->search({ player => $player_id, game => $game })->first;
131 0           }
132 0            
133 0           # Get all player picks for loading when in edit of existing picks mode
134             my @picks = $c->model('DBIC::Pick')->search({ player => $player_id });
135             my %picks;
136             foreach my $pick (@picks) {
137             $picks{ $pick->game->id } = $pick->pick;
138 0           }
139 0           $c->stash->{picks} = \%picks;
140 0            
141 0           # Create Class for Final Four Teams
142             my %class_for;
143 0           foreach my $player_pick (@picks) {
144             my ($winning_pick) =
145             $c->model('DBIC::Pick')->search({ player => 1, game => $player_pick->game->id });
146 0           if (defined $winning_pick) {
147 0           if ($winning_pick->pick->id == $player_pick->pick->id) {
148 0           $class_for{ $player_pick->game->id } = 'in';
149             }
150 0 0         else {
151 0 0         $class_for{ $player_pick->game->id } = 'out';
152 0           }
153             }
154             else {
155 0           if ($player_pick->game->round >= $player_pick->pick->round_out) {
156             $class_for{ $player_pick->game->id } = 'out';
157             }
158             else {
159 0 0         $class_for{ $player_pick->game->id } = 'pending';
160 0           }
161             }
162             }
163 0           $c->stash->{class_for} = \%class_for;
164             $c->stash->{regions} = $c->model('DBIC::Region')->search({},{order_by => 'id'});
165              
166             # Turn off javascript
167 0           $c->stash->{no_javascript} = 1;
168 0           $c->stash->{template} = 'final4/view_final4_picks.tt';
169             return;
170             }
171 0            
172 0           =head1 AUTHOR
173 0            
174 4     4   4542 mateu x hunter
  4         11  
  4         28  
175              
176             =head1 LICENSE
177              
178             This library is free software, you can redistribute it and/or modify
179             it under the same terms as Perl itself.
180              
181             =cut
182              
183             1;