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 ) = @_;
38 my ( $self, $c ) = @_;
39 $c->response->status(404);
46 my $gi = Geo::IP->new(GEOIP_STANDARD);
47 my $country = $gi->country_code_by_addr($c->req->address) || '??';
50 if ($c->req->param('remember')){
51 $c->session_time_to_live( 604800 ); # expire in one week.
54 my $log = $c->model->prepare(q{INSERT INTO session_log
55 (uid,time,ip,country,session,remember)
56 VALUES ($1,NOW(),$2,$3,$4,$5)
58 $log->execute($c->user->id,$c->req->address
59 ,$country,$c->sessionid,$remember);
61 $c->res->redirect($c->req->referer);
69 $c->delete_session("logout");
70 $c->res->redirect($c->uri_for('index'));
76 $c->res->header( 'Cache-Control' =>
77 'no-store, no-cache, must-revalidate,'.
78 'post-check=0, pre-check=0, max-age=0'
80 $c->res->header( 'Pragma' => 'no-cache' );
81 $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
84 sub listTargets : Private {
89 my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick,
90 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
92 JOIN raid_targets t ON c.target = t.id
93 JOIN raids r ON t.raid = r.id
94 JOIN current_planet_stats p ON t.planet = p.id
95 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
96 ORDER BY r.tick+c.wave,x,y,z});
97 $query->execute($c->user->id) or die $dbh->errstr;
99 while (my $target = $query->fetchrow_hashref){
100 push @targets, $target;
103 $c->stash(claimedtargets => \@targets);
106 sub listAlliances : Private {
109 push @alliances,{id => -1, name => ''};
110 my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
112 while (my $ally = $query->fetchrow_hashref){
113 push @alliances,$ally;
115 $c->stash(alliances => \@alliances);
120 my $dbh = $c ->model;
122 $c->stash(dbh => $dbh);
124 $dbh->do(q{SET timezone = 'GMT'});
126 $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
127 $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
128 $c->stash->{game}->{tick} = $c->stash->{TICK};
130 if ($c->user_exists){
131 $c->stash(UID => $c->user->id);
133 $c->stash(UID => -4);
138 sub access_denied : Private {
139 my ($self, $c, $action) = @_;
141 $c->stash->{template} = 'access_denied.tt2';
147 Attempt to render a view, if needed.
151 sub end : ActionClass('RenderView') {
154 my $dbh = $c ->model;
156 if (scalar @{ $c->error } ){
157 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
158 $c->stash->{template} = 'access_denied.tt2';
160 }elsif ($c->error->[0] =~ m/Missing roles: /){
161 $c->stash->{template} = 'access_denied.tt2';
166 if ($c->user_exists && $c->res->status == 200){
168 if ($c->check_user_roles(qw/member_menu/)){
169 $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
170 AND mission = 'Full fleet' AND tick > tick() - 24
171 },undef,$c->user->planet);
172 $fleetupdate = 0 unless defined $fleetupdate;
175 my ($unread,$newposts) = $dbh->selectrow_array(unread_query,undef,$c->user->id) or die $dbh->errstr;
179 name => $c->user->username,
180 css => $c->user->css,
181 newposts => $newposts,
182 unreadposts => $unread
184 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
185 && (!$c->check_user_roles(qw/member_menu/)
186 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
187 || $c->check_user_roles(qw/no_fleet_update/)))),
188 $c->forward('listTargets');
194 Michael Andreen (harv@ruin.nu)