File Coverage

blib/lib/Bracket/Controller/Region.pm
Criterion Covered Total %
statement 19 100 19.0
branch 0 16 0.0
condition 0 9 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 26 138 18.8


line stmt bran cond sub pod time code
1              
2             use Moose;
3 4     4   1919 BEGIN { extends 'Catalyst::Controller' }
  4         9  
  4         33  
4 4     4   22557 use Perl6::Junction qw/ any /;
5 4     4   22031 use DateTime;
  4         12  
  4         223  
6 4     4   33  
  4         9  
  4         1385  
7             my $PERFECT_BRACKET_MODE = 1;
8              
9             =head1 NAME
10              
11             Bracket::Controller::Region - Edit/View Regional picks
12              
13              
14             =cut
15              
16             my ($self, $c, $region, $player_id) = @_;
17              
18 0     0 0 0 my $player_object = $c->model('DBIC::Player')->find({ id => $player_id });
19             my $player_name = $player_object->first_name . ' ' . $player_object->last_name;
20 0         0 $c->stash->{player_id} = $player_id;
21 0         0 $c->stash->{player_name} = $player_name;
22 0         0 my $region_object = $c->model('DBIC::Region')->find($region);
23 0         0 my $region_name = $region_object->name;
24 0         0 $c->stash->{region} = $region;
25 0         0 $c->stash->{region_name} = $region_name;
26 0         0  
27 0         0 my $params = $c->request->params;
28              
29 0         0 # Do database insert
30             foreach my $pgame (keys %{$params}) {
31             $pgame =~ m{p(\d+)};
32 0         0 my $game = $1;
  0         0  
33 0         0 my $team = ${$params}{$pgame};
34 0         0 my ($pick) = $c->model('DBIC::Pick')->search({ player => $player_id, game => $game });
35 0         0 if (defined $pick) {
  0         0  
36 0         0 $pick->pick($team);
37 0 0       0 $pick->update;
38 0         0 }
39 0         0 else {
40             my $new_pick =
41             $c->model('DBIC::Pick')->new({ player => $player_id, game => $game, pick => $team });
42 0         0 $new_pick->insert;
43             }
44 0         0 }
45              
46             $c->stash->{params} = $params;
47             my $previous_user_id = $c->session->{previous_user_id};
48 0         0 $c->response->redirect(
49 0         0 $c->uri_for($c->controller('Player')->action_for('home'))
50 0         0 . "/${player_id}"
51             );
52              
53             return;
54             }
55 0         0  
56 4     4   32 my ($self, $c, $region_id, $player_id) = @_;
  4         9  
  4         28  
57              
58             my @perfect_picks = $c->model('DBIC::Pick')->search({ player => 1 });
59 0     0 0 0 my @player_picks = $c->model('DBIC::Pick')->search({ player => $player_id });
60              
61 0         0 my %picks;
62 0         0 my %class_for;
63             my $region_points = 0;
64 0         0 my @show_regions;
65             foreach my $player_pick (@player_picks) {
66 0         0  
67 0         0 # Operate only on the current region
68 0         0 if ($player_pick->pick->region->id == $region_id) {
69              
70             # Compare player pick to actual winner for the perfect player bracket
71 0 0       0 # Build the css class name accordingly
72             my ($winning_pick) =
73             $c->model('DBIC::Pick')->search({ player => 1, game => $player_pick->game->id });
74             if (defined $winning_pick) {
75 0         0 if ($winning_pick->pick->id == $player_pick->pick->id) {
76             $class_for{ $player_pick->game->id } = 'in';
77 0 0       0  
78 0 0       0 # Formula to compute points for correct picks
79 0         0 my $points_for_pick =
80             (5 + $player_pick->pick->seed * $player_pick->game->lower_seed) *
81             $player_pick->game->round;
82 0         0 $region_points += $points_for_pick;
83             }
84             else {
85 0         0 $class_for{ $player_pick->game->id } = 'out';
86             }
87             }
88 0         0 else {
89              
90             # Need to determine if player pick has already been ousted in a
91             # previous round using round_out variable.
92             if ($player_pick->game->round >= $player_pick->pick->round_out) {
93              
94             # warn "round greater than round out: ",
95 0 0       0 # $player_pick->game->round, ' and ',
96             # $player_pick->pick->round_out;
97             $class_for{ $player_pick->game->id } = 'out';
98             }
99             else {
100 0         0 $class_for{ $player_pick->game->id } = 'pending';
101             }
102             }
103 0         0 $picks{ $player_pick->game->id } = $player_pick->pick;
104             }
105             }
106 0         0 $c->stash->{class_for} = \%class_for;
107             $c->stash->{picks} = \%picks;
108             $c->stash->{region_points} = $region_points;
109 0         0  
110 0         0 my $player = $c->model('DBIC::Player')->find($player_id);
111 0         0 $c->stash->{player} = $player;
112             my $region = $c->model('DBIC::Region')->find($region_id);
113 0         0 $c->stash->{region} = $region;
114 0         0 $c->stash->{teams} = $c->model('DBIC::Team')->search({region => $region_id});
115 0         0 $c->stash->{regions} = $c->model('DBIC::Region')->search({},{order_by => 'id'});
116 0         0 $c->stash->{show_regions} = \@show_regions;
117 0         0 $c->stash->{template} = 'region/view_region_status.tt';
118 0         0  
119 0         0 return;
120 0         0 }
121              
122 0         0 my ($self, $c, $region, $player) = @_;
123 4     4   5789  
  4         11  
  4         28  
124             # Restrict edits to user or admin role.
125             my @user_roles = $c->user->roles;
126 0     0 0   $c->go('/error_404') if (($player != $c->user->id) && !('admin' eq any(@user_roles)));
127              
128             # Go to home if edits are attempted after closing time
129 0           # NOTE: Put a player's id on this list and they can make edits after the cut-off.
130 0 0 0       my @open_edit_ids = qw/ /;
131             my $edit_allowed = 1 if ($c->user->id eq any(@open_edit_ids));
132             if ( $c->stash->{is_game_time} && (!($c->stash->{is_admin} || $edit_allowed)) ) {
133             $c->flash->{status_msg} = 'Regional edits are closed';
134 0           $c->response->redirect($c->uri_for($c->controller('Player')->action_for('home')));
135 0 0         }
136 0 0 0        
      0        
137 0           # Player picks
138 0           my @picks = $c->model('DBIC::Pick')->search({ player => $player });
139             my %picks;
140             foreach my $pick (@picks) {
141             $picks{ $pick->game->id } = $pick->pick;
142 0           }
143 0           $c->stash->{picks} = \%picks;
144 0            
145 0           # Player info
146             my $player_object = $c->model('DBIC::Player')->find($player);
147 0           my $player_name = $player_object->first_name . ' ' . $player_object->last_name;
148             $c->stash->{player} = $player;
149             $c->stash->{player_name} = $player_name;
150 0            
151 0           # Region object
152 0           my $region_object = $c->model('DBIC::Region')->find($region);
153 0           my $region_name = $region_object->name;
154             $c->stash->{region} = $region;
155             $c->stash->{region_name} = $region_name;
156 0            
157 0           # Teams
158 0           $c->stash->{teams} = $c->model('DBIC::Team')->search({region => $region});
159 0          
160              
161             $c->stash->{template} = 'region/edit_region_picks.tt';
162 0            
163             return;
164             }
165 0            
166             =head1 AUTHOR
167 0            
168 4     4   4353 mateu x hunter
  4         18  
  4         19  
169              
170             =head1 LICENSE
171              
172             This library is free software, you can redistribute it and/or modify
173             it under the same terms as Perl itself.
174              
175             =cut
176              
177             1;