]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Basic infrastructure for private messages/threads
[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 use Geo::IP;
9
10
11 #
12 # Sets the actions in this controller to be registered with no prefix
13 # so they function identically to actions created in MyApp.pm
14 #
15 __PACKAGE__->config->{namespace} = '';
16
17 =head1 NAME
18
19 NDWeb::Controller::Root - Root Controller for NDWeb
20
21 =head1 DESCRIPTION
22
23 [enter your description here]
24
25 =head1 METHODS
26
27 =cut
28
29 =head2 default
30
31 =cut
32
33 sub index : Local Path Args(0) {
34         my ( $self, $c ) = @_;
35 }
36
37 sub default : Path {
38         my ( $self, $c ) = @_;
39         $c->response->status(404);
40 }
41
42 sub login : Local {
43         my ($self, $c) = @_;
44
45         if ($c->login){
46                 my $gi = Geo::IP->new(GEOIP_STANDARD);
47                 my $country = $gi->country_code_by_addr($c->req->address) || '??';
48
49                 my $remember = 0;
50                 if ($c->req->param('remember')){
51                         $c->session_time_to_live( 604800 ); # expire in one week.
52                         $remember = 1;
53                 }
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)
57                 });
58                 $log->execute($c->user->id,$c->req->address
59                         ,$country,$c->sessionid,$remember);
60
61                 $c->res->redirect($c->req->referer);
62                 return;
63         }
64 }
65
66 sub logout : Local {
67         my ($self, $c) = @_;
68         $c->logout;
69         $c->delete_session("logout");
70         $c->res->redirect($c->uri_for('index'));
71 }
72
73 sub begin : Private {
74         my ($self, $c) = @_;
75
76          $c->res->header( 'Cache-Control' =>
77                 'no-store, no-cache, must-revalidate,'.
78                 'post-check=0, pre-check=0, max-age=0'
79         );
80         $c->res->header( 'Pragma' => 'no-cache' );
81         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
82 }
83
84 sub listTargets : Private {
85         my ($self, $c) = @_;
86
87         my $dbh = $c ->model;
88
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
91 FROM raid_claims c
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;
98         my @targets;
99         while (my $target = $query->fetchrow_hashref){
100                 push @targets, $target;
101         }
102
103         $c->stash(claimedtargets => \@targets);
104 }
105
106 sub listAlliances : Private {
107         my ($self, $c) = @_;
108         my @alliances;
109         push @alliances,{id => -1, name => ''};
110         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
111         $query->execute;
112         while (my $ally = $query->fetchrow_hashref){
113                 push @alliances,$ally;
114         }
115         $c->stash(alliances => \@alliances);
116 }
117
118 sub auto : Private {
119         my ($self, $c) = @_;
120         my $dbh = $c ->model;
121
122         $c->stash(dbh => $dbh);
123
124         $dbh->do(q{SET timezone = 'GMT'});
125
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};
129
130         if ($c->user_exists){
131                 $c->stash(UID => $c->user->id);
132         }else{
133                 $c->stash(UID => -4);
134         }
135
136 }
137
138 sub access_denied : Private {
139         my ($self, $c, $action) = @_;
140
141         $c->stash->{template} = 'access_denied.tt2';
142
143 }
144
145 =head2 end
146
147 Attempt to render a view, if needed.
148
149 =cut 
150
151 sub end : ActionClass('RenderView') {
152         my ($self, $c) = @_;
153
154         my $dbh = $c ->model;
155
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';
159                         $c->clear_errors;
160                 }elsif ($c->error->[0] =~ m/Missing roles: /){
161                         $c->stash->{template} = 'access_denied.tt2';
162                         $c->clear_errors;
163                 }
164         }
165
166         if ($c->user_exists && $c->res->status == 200){
167                 my $fleetupdate = 0;
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;
173                 }
174
175                 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
176                         ,undef,$c->user->id);
177
178                 $c->stash(user => {
179                         id => $c->user->id,
180                         name => $c->user->username,
181                         css => $c->user->css,
182                         newposts => $newposts,
183                         unreadposts => $unread
184                 });
185                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
186                         && (!$c->check_user_roles(qw/member_menu/)
187                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
188                                         || $c->check_user_roles(qw/no_fleet_update/)))),
189                 $c->forward('listTargets');
190         }
191 }
192
193 =head1 AUTHOR
194
195 Michael Andreen (harv@ruin.nu)
196
197 =head1 LICENSE
198
199 GPL 2, or later.
200
201 =cut
202
203 1;