1 package NDWeb::Controller::Root;
5 use parent 'Catalyst::Controller';
12 # Sets the actions in this controller to be registered with no prefix
13 # so they function identically to actions created in MyApp.pm
15 __PACKAGE__->config->{namespace} = '';
19 NDWeb::Controller::Root - Root Controller for NDWeb
23 [enter your description here]
33 sub index : Local Path Args(0) {
34 my ( $self, $c ) = @_;
36 $c->res->redirect($c->uri_for('/wiki'));
40 my ( $self, $c ) = @_;
41 $c->response->status(404);
48 my $gi = Geo::IP->new(GEOIP_STANDARD);
49 my $country = $gi->country_code_by_addr($c->req->address) || '??';
52 if ($c->req->param('remember')){
53 $c->session_time_to_live( 604800 ); # expire in one week.
56 my $log = $c->model->prepare(q{INSERT INTO session_log
57 (uid,time,ip,country,session,remember)
58 VALUES ($1,NOW(),$2,$3,$4,$5)
60 $log->execute($c->user->id,$c->req->address
61 ,$country,$c->sessionid,$remember);
63 $c->forward('redirect');
65 } elsif ($c->req->method eq 'POST'){
73 $c->delete_session("logout");
74 $c->res->redirect($c->uri_for('index'));
80 $c->res->header( 'Cache-Control' =>
81 'no-store, no-cache, must-revalidate,'.
82 'post-check=0, pre-check=0, max-age=0'
84 $c->res->header( 'Pragma' => 'no-cache' );
85 $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
88 sub listTargets : Private {
93 my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick,
94 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
96 JOIN raid_targets t ON c.target = t.id
97 JOIN raids r ON t.raid = r.id
98 JOIN current_planet_stats p ON t.planet = p.id
99 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
100 ORDER BY r.tick+c.wave,x,y,z});
101 $query->execute($c->user->id) or die $dbh->errstr;
103 while (my $target = $query->fetchrow_hashref){
104 push @targets, $target;
107 $c->stash(claimedtargets => \@targets);
110 sub listAlliances : Private {
113 push @alliances,{id => -1, name => ''};
114 my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
116 while (my $ally = $query->fetchrow_hashref){
117 push @alliances,$ally;
119 $c->stash(alliances => \@alliances);
128 my $dbh = $c ->model;
130 $c->stash(dbh => $dbh);
132 $c->stash(sslurl => \&sslurl);
134 $dbh->do(q{SET timezone = 'GMT'});
136 $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
137 $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
138 $c->stash->{game}->{tick} = $c->stash->{TICK};
140 if ($c->user_exists){
141 $c->stash(UID => $c->user->id);
143 $c->stash(UID => -4);
147 sub redirect : Private {
149 $c->res->redirect($c->uri_for('/'.$c->flash->{referrer}));
152 sub access_denied : Private {
153 my ($self, $c, $action) = @_;
155 $c->stash->{template} = 'access_denied.tt2';
156 $c->res->status(403);
162 Attempt to render a view, if needed.
166 sub end : ActionClass('RenderView') {
169 if ($c->res->status == 302){
173 my $dbh = $c ->model;
175 if (scalar @{ $c->error } ){
176 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
177 $c->stash->{template} = 'access_denied.tt2';
178 $c->res->status(403);
180 }elsif ($c->error->[0] =~ m/Missing roles: /){
181 $c->stash->{template} = 'access_denied.tt2';
182 $c->res->status(403);
187 if ($c->user_exists){
189 if ($c->check_user_roles(qw/member_menu/)){
190 $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
191 AND mission = 'Full fleet' AND tick > tick() - 24
192 },undef,$c->user->planet);
193 $fleetupdate = 0 unless defined $fleetupdate;
196 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
197 ,undef,$c->user->id);
201 name => $c->user->username,
202 css => $c->user->css,
203 newposts => $newposts,
204 unreadposts => $unread
206 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
207 && (!$c->check_user_roles(qw/member_menu/)
208 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
209 || $c->check_user_roles(qw/no_fleet_update/)))),
210 $c->forward('listTargets');
212 my $birthdays = $dbh->prepare(q{SELECT username
213 ,date_part('year',age(birthday)) AS age
214 FROM users WHERE birthday IS NOT NULL
215 AND mmdd(birthday) = mmdd(CURRENT_DATE)
218 $c->stash(birthdays => $birthdays->fetchall_arrayref({}));
220 if ($c->res->status == 200 || $c->req->method eq 'GET'){
221 $c->flash(referrer => $c->req->path);
227 Michael Andreen (harv@ruin.nu)