]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Intel.pm
Converted intel page
[ndwebbie.git] / lib / NDWeb / Controller / Intel.pm
1 package NDWeb::Controller::Intel;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use NDWeb::Include;
8
9 =head1 NAME
10
11 NDWeb::Controller::Intel - Catalyst Controller
12
13 =head1 DESCRIPTION
14
15 Catalyst Controller.
16
17 =head1 METHODS
18
19 =cut
20
21
22 =head2 index 
23
24 =cut
25
26 sub index :Path : Args(0) {
27         my ( $self, $c ) = @_;
28         my $dbh = $c->model;
29
30         my $ticks = $c->req->param('ticks') || 48;
31         $c->stash(showticks => $ticks);
32
33         my $query = $dbh->prepare(intelquery q{
34                         o.alliance AS oalliance ,coords(o.x,o.y,o.z) AS ocoords, i.sender
35                         ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords, i.target
36                 },q{not ingal
37                         AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL) AND (i.mission = 'Defend' OR i.mission = 'AllyDef' ))
38                         OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
39                         AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
40                         AND NOT (i.back IS NOT NULL AND i.back = i.tick + 4)
41                         AND i.tick > (tick() - $1)
42                 });
43         $query->execute($ticks);
44         $c->stash(intel => $query->fetchall_arrayref({}) );
45
46         if (defined $c->req->param('coords')){
47                 my $coords = $c->req->param('coords');
48                 my @coords;
49                 while ($coords =~ m/(\d+:\d+:\d+)/g){
50                         push @coords,$1;
51                 }
52                 my $planets = $dbh->prepare(q{SELECT id,coords(x,y,z), alliance
53                         FROM current_planet_stats p
54                         WHERE coords(x,y,z) = ANY($1)
55                         ORDER BY alliance, p.x, p.y, p.z
56                 });
57                 $planets->execute(\@coords);
58                 $c->stash(coordslist => $planets->fetchall_arrayref({}) );
59         }
60 }
61
62 sub planet : Local {
63         my ( $self, $c, $id ) = @_;
64         my $dbh = $c->model;
65
66         $c->forward('findPlanet');
67         my $p = $c->stash->{p};
68
69         $c->stash(checkcoords => "$p->{x}:$p->{y}:$p->{z}");
70         my $ticks = $c->req->param('ticks') || 48;
71         $c->stash(showticks => $ticks);
72
73         my $query = $dbh->prepare(q{SELECT id,coords(x,y,z),alliance,nick,channel
74                 FROM current_planet_stats WHERE channel ILIKE ?
75                 ORDER BY alliance,x,y,z
76                 });
77         $query->execute($p->{channel});
78         $c->stash(channelusers => $query->fetchall_arrayref({}) );
79
80         $c->forward('/listAlliances');
81         $c->forward('/forum/findPosts',[$p->{ftid}]);
82
83         $c->stash(govs => ["","Feu", "Dic", "Dem","Uni"]);
84         $c->stash(planetstatus => ["","Friendly", "NAP", "Hostile"]);
85
86         $query = $dbh->prepare(intelquery q{i.sender
87                         ,o.alliance AS oalliance,coords(o.x,o.y,o.z) AS ocoords
88                 },q{i.target = $1 AND i.tick > (tick() - $2)});
89         $query->execute($id,$ticks);
90         $c->stash(incoming => $query->fetchall_arrayref({}) );
91
92         $query = $dbh->prepare(intelquery q{i.target
93                         ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords
94                 },q{i.sender = $1 AND i.tick > (tick() - $2)});
95         $query->execute($id,$ticks);
96         $c->stash(outgoing => $query->fetchall_arrayref({}) );
97
98 }
99
100 sub postplanetcomment : Local {
101         my ($self, $c, $p) = @_;
102
103         $c->forward('findPlanet');
104         $p = $c->stash->{p};
105
106         $c->forward('/forum/insertPost',[$p->{ftid}]);
107         $c->res->redirect($c->uri_for('planet',$p->{id}));
108 }
109
110 sub postplanetupdate : Local {
111         my ($self, $c, $p) = @_;
112         my $dbh = $c->model;
113
114         $c->forward('findPlanet');
115         $p = $c->stash->{p};
116
117         $dbh->begin_work;
118         my $log = $dbh->prepare(q{INSERT INTO forum_posts (ftid,uid,message)
119                 VALUES($2,$1,$3)
120                 });
121         if ($c->req->param('cnick')){
122                 my $value = html_escape $c->req->param('nick');
123                 $dbh->do(q{UPDATE planets SET nick = ? WHERE id =?}
124                         ,undef,$value,$p->{id});
125                 $log->execute($c->user->id,$p->{ftid},"Set nick to: $value");
126         }
127         if ($c->req->param('cchannel')){
128                 my $value = html_escape $c->req->param('channel');
129                 $dbh->do(q{UPDATE planets SET channel = ? WHERE id =?}
130                         ,undef,$value,$p->{id});
131                 $log->execute($c->user->id,$p->{ftid},"Set channel to: $value");
132         }
133         if ($c->req->param('cstatus')){
134                 my $value = $c->req->param('status');
135                 $dbh->do(q{UPDATE planets SET planet_status = ? WHERE id =?}
136                         ,undef,$value,$p->{id});
137                 $log->execute($c->user->id,$p->{ftid},"Set planet_status to: $value");
138         }
139         if ($c->req->param('cgov')){
140                 my $value = $c->req->param('gov');
141                 $dbh->do(q{UPDATE planets SET gov = ? WHERE id =?}
142                         ,undef,$value,$p->{id});
143                 $log->execute($c->user->id,$p->{ftid},"Set gov to: $value");
144         }
145         if ($c->req->param('calliance')){
146                 my $value = $c->req->param('alliance');
147                 $dbh->do(q{UPDATE planets SET alliance_id = NULLIF(?,-1) WHERE id =?}
148                         ,undef,$value,$p->{id});
149                 $log->execute($c->user->id,$p->{ftid},"Set alliance_id to: $value");
150         }
151         $dbh->commit;
152
153         $c->res->redirect($c->uri_for('planet',$p->{id}));
154 }
155
156
157 sub find : Local {
158         my ( $self, $c, $find ) = @_;
159         my $dbh = $c->model;
160
161         local $_ = $find || $c->req->param('coords');
162
163         if (/(\d+)(?: |:)(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?/){
164                 my $planet = $dbh->selectrow_array(q{SELECT planetid($1,$2,$3,$4)}
165                         ,undef,$1,$2,$3,$4);
166                 $c->res->redirect($c->uri_for('planet',$planet));
167         }
168 }
169
170 sub findPlanet : Private {
171         my ( $self, $c, $id ) = @_;
172         my $dbh = $c->model;
173
174         my $query = $dbh->prepare(q{SELECT x,y,z,id, nick, alliance,alliance_id
175                 , planet_status,channel,ftid,gov
176                 FROM current_planet_stats
177                 WHERE id = $1
178                 });
179         $query->execute($id);
180         $c->stash(p => $query->fetchrow_hashref);
181 }
182
183
184 =head1 AUTHOR
185
186 Michael Andreen (harv@ruin.nu)
187
188 =head1 LICENSE
189
190 GPL 2.0, or later.
191
192 =cut
193
194 1;