]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Converted editRaid page
[ndwebbie.git] / lib / NDWeb / Controller / Root.pm
1 package NDWeb::Controller::Root;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use ND::Include;
8
9 #
10 # Sets the actions in this controller to be registered with no prefix
11 # so they function identically to actions created in MyApp.pm
12 #
13 __PACKAGE__->config->{namespace} = '';
14
15 =head1 NAME
16
17 NDWeb::Controller::Root - Root Controller for NDWeb
18
19 =head1 DESCRIPTION
20
21 [enter your description here]
22
23 =head1 METHODS
24
25 =cut
26
27 =head2 default
28
29 =cut
30
31 sub index : Local Path Args(0) {
32         my ( $self, $c ) = @_;
33 }
34
35 sub default : Path {
36         my ( $self, $c ) = @_;
37         $c->res->body( 'Page not found' );
38         $c->response->status(404);
39 }
40
41 sub login : Local {
42         my ($self, $c) = @_;
43         if ($c->login){
44                 $c->res->redirect($c->uri_for('index'));
45                 return;
46         }
47
48         $c->stash(error => 'Bad password');
49         $c->stash(template => 'index.tt2');
50         $c->forward('index');
51 }
52
53 sub logout : Local {
54         my ($self, $c) = @_;
55         $c->logout;
56         $c->res->redirect($c->uri_for('index'));
57 }
58
59 sub begin : Private {
60         my ($self, $c) = @_;
61
62          $c->res->header( 'Cache-Control' =>
63                 'no-store, no-cache, must-revalidate,'.
64                 'post-check=0, pre-check=0, max-age=0'
65         );
66         $c->res->header( 'Pragma' => 'no-cache' );
67         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
68 }
69
70 sub listTargets : Private {
71         my ($self, $c) = @_;
72
73         my $dbh = $c ->model;
74
75         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, 
76                 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
77 FROM raid_claims c
78         JOIN raid_targets t ON c.target = t.id
79         JOIN raids r ON t.raid = r.id
80         JOIN current_planet_stats p ON t.planet = p.id
81 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
82 ORDER BY r.tick+c.wave,x,y,z});
83         $query->execute($c->user->id) or die $dbh->errstr;
84         my @targets;
85         while (my $target = $query->fetchrow_hashref){
86                 push @targets, $target;
87         }
88
89         $c->stash(claimedtargets => \@targets);
90 }
91
92 sub listAlliances : Private {
93         my ($self, $c) = @_;
94         my @alliances;
95         push @alliances,{id => -1, name => ''};
96         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
97         $query->execute;
98         while (my $ally = $query->fetchrow_hashref){
99                 push @alliances,$ally;
100         }
101         $c->stash(alliances => \@alliances);
102 }
103
104 sub auto : Private {
105         my ($self, $c) = @_;
106         my $dbh = $c ->model;
107
108         $c->stash(dbh => $dbh);
109
110         $dbh->do(q{SET timezone = 'GMT'});
111
112         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
113         $c->stash->{game}->{tick} = $c->stash->{TICK};
114
115         if ($c->user_exists){
116                 $c->stash(UID => $c->user->id);
117         }else{
118                 $c->stash(UID => -4);
119         }
120
121 }
122
123 sub access_denied : Private {
124         my ($self, $c, $action) = @_;
125
126         $c->log->debug('moo' . $action);
127
128         # Set the error message
129         $c->stash->{template} = 'access_denied.tt2';
130
131 }
132
133 =head2 end
134
135 Attempt to render a view, if needed.
136
137 =cut 
138
139 sub end : ActionClass('RenderView') {
140         my ($self, $c) = @_;
141
142         my $dbh = $c ->model;
143
144         if ($c->user_exists && $c->res->status == 200){
145                 my $fleetupdate = 0;
146                 if ($c->check_user_roles(qw/member_menu/)){
147                         $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
148                                 AND mission = 'Full fleet' AND tick > tick() - 24
149                                 },undef,$c->user->planet);
150                         $fleetupdate = 0 unless defined $fleetupdate;
151                 }
152
153                 my ($unread,$newposts) = $dbh->selectrow_array(unread_query,undef,$c->user->id) or die $dbh->errstr;
154
155                 $c->stash(user => {
156                         id => $c->user->id,
157                         name => $c->user->username,
158                         css => $c->user->css,
159                         newposts => $newposts,
160                         unreadposts => $unread
161                 });
162                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
163                         && (!$c->check_user_roles(qw/member_menu/)
164                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
165                                         || $c->check_user_roles(qw/no_fleet_update/)))),
166                 $c->forward('listTargets');
167         }
168 }
169
170 =head1 AUTHOR
171
172 Michael Andreen (harv@ruin.nu)
173
174 =head1 LICENSE
175
176 GPL 2, or later.
177
178 =cut
179
180 1;