1 package NDWeb::Controller::Intel;
5 use parent 'Catalyst::Controller';
11 NDWeb::Controller::Intel - Catalyst Controller
26 sub index :Path : Args(0) {
27 my ( $self, $c ) = @_;
30 my $ticks = $c->req->param('ticks') || 48;
31 $c->stash(showticks => $ticks);
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
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)
43 $query->execute($ticks);
44 $c->stash(intel => $query->fetchall_arrayref({}) );
46 if (defined $c->req->param('coords')){
47 my $coords = $c->req->param('coords');
49 while ($coords =~ m/(\d+:\d+:\d+)/g){
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
57 $planets->execute(\@coords);
58 $c->stash(coordslist => $planets->fetchall_arrayref({}) );
63 my ( $self, $c, $id ) = @_;
66 $c->forward('findPlanet');
67 my $p = $c->stash->{p};
69 $c->stash(checkcoords => "$p->{x}:$p->{y}:$p->{z}");
70 my $ticks = $c->req->param('ticks') || 48;
71 $c->stash(showticks => $ticks);
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
77 $query->execute($p->{channel});
78 $c->stash(channelusers => $query->fetchall_arrayref({}) );
80 $c->forward('/listAlliances');
81 $c->forward('/forum/findPosts',[$p->{ftid}]);
83 $c->stash(govs => ["","Feu", "Dic", "Dem","Uni"]);
84 $c->stash(planetstatus => ["","Friendly", "NAP", "Hostile"]);
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({}) );
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({}) );
100 sub postplanetcomment : Local {
101 my ($self, $c, $p) = @_;
103 $c->forward('findPlanet');
106 $c->forward('/forum/insertPost',[$p->{ftid}]);
107 $c->res->redirect($c->uri_for('planet',$p->{id}));
110 sub postplanetupdate : Local {
111 my ($self, $c, $p) = @_;
114 $c->forward('findPlanet');
118 my $log = $dbh->prepare(q{INSERT INTO forum_posts (ftid,uid,message)
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");
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");
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");
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");
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");
153 $c->res->redirect($c->uri_for('planet',$p->{id}));
158 my ( $self, $c, $find ) = @_;
161 local $_ = $find || $c->req->param('coords');
163 if (/(\d+)(?: |:)(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?/){
164 my $planet = $dbh->selectrow_array(q{SELECT planetid($1,$2,$3,$4)}
166 $c->res->redirect($c->uri_for('planet',$planet));
170 sub findPlanet : Private {
171 my ( $self, $c, $id ) = @_;
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
179 $query->execute($id);
180 $c->stash(p => $query->fetchrow_hashref);
186 Michael Andreen (harv@ruin.nu)