1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
20 package NDWeb::Pages::MemberIntel;
23 use CGI qw/:standard/;
26 use base qw/NDWeb::XMLPage/;
28 $NDWeb::Page::PAGES{memberIntel} = __PACKAGE__;
33 $self->{TITLE} = 'Member Intel';
34 my $DBH = $self->{DBH};
37 return $self->noAccess unless $self->isHC;
39 my $showticks = 'AND i.tick > tick()';
40 if (defined param('show')){
41 if (param('show') eq 'all'){
43 }elsif (param('show') =~ /^(\d+)$/){
44 $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
49 if (defined param('uid') && param('uid') =~ /^(\d+)$/){
50 my $query = $DBH->prepare(q{SELECT username,uid FROM users WHERE uid = ?
52 $user = $DBH->selectrow_hashref($query,undef,$1);
56 $BODY->param(UID => $user->{uid});
57 my $query = $DBH->prepare(q{
58 SELECT coords(t.x,t.y,t.z), i.eta, i.tick, rt.id AS ndtarget, rc.launched, inc.landing_tick
60 LEFT OUTER JOIN (SELECT DISTINCT * FROM intel WHERE amount = -1) i ON i.sender = u.planet
61 LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
62 LEFT OUTER JOIN (SELECT rt.id,planet,tick FROM raids r
63 JOIN raid_targets rt ON r.id = rt.raid) rt ON rt.planet = i.target
64 AND (rt.tick + 12) > i.tick AND rt.tick <= i.tick
65 LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target AND rc.uid = u.uid AND i.tick = rt.tick + rc.wave - 1
66 LEFT OUTER JOIN (SELECT sender, eta, landing_tick FROM calls c
67 JOIN incomings i ON i.call = c.id) inc ON inc.sender = i.target
68 AND (inc.landing_tick + inc.eta) >= i.tick
69 AND (inc.landing_tick - inc.eta - 1) <= (i.tick - i.eta)
70 WHERE u.uid = $1 AND i.mission = 'Attack'
71 ORDER BY (i.tick - i.eta)
73 $query->execute($user->{uid}) or $error .= $DBH->errstr;
78 while (my $intel = $query->fetchrow_hashref){
79 my $attack = {target => $intel->{coords}, tick => $intel->{tick}};
80 if ($intel->{ndtarget}){
81 if (defined $intel->{launched}){
82 $attack->{Other} = 'Claimed '.($intel->{launched} ? 'and confirmed' : 'but NOT confirmed');
84 $attack->{Other} = 'Launched at a tick that was not claimed';
86 $attack->{ODD} = ++$ndi % 2;
87 push @nd_attacks, $attack;
89 $attack->{ODD} = ++$oi % 2;
90 push @other_attacks, $attack;
94 push @attacks, {name => 'ND Attacks', list => \@nd_attacks, class => 'AllyDef'};
95 push @attacks, {name => 'Other', list => \@other_attacks, class => 'Attack'};
96 $BODY->param(Attacks => \@attacks);
98 $query = $DBH->prepare(q{
99 SELECT coords(t.x,t.y,t.z),t.alliance_id, t.alliance, i.eta, i.tick, i.ingal
101 JOIN (SELECT DISTINCT * FROM intel WHERE amount = -1) i ON i.sender = u.planet
102 LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
103 WHERE u.uid = $1 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
104 ORDER BY (i.tick - i.eta)
106 $query->execute($user->{uid}) or $error .= $DBH->errstr;
113 while (my $intel = $query->fetchrow_hashref){
114 my $def = {target => $intel->{coords}.(defined $intel->{alliance} ? " ($intel->{alliance})" : ''), tick => $intel->{tick}};
115 if (defined $intel->{alliance_id} && $intel->{alliance_id} == 1){
116 $def->{ODD} = ++$ndi % 2;
118 }elsif($intel->{ingal}){
119 $def->{ODD} = ++$gi % 2;
120 push @ingal_def, $def;
122 $def->{ODD} = ++$oi % 2;
123 push @other_def, $def;
127 push @defenses, {name => 'ND Def', list => \@nd_def, class => 'AllyDef'};
128 push @defenses, {name => 'Ingal Def', list => \@ingal_def, class => 'Defend'};
129 push @defenses, {name => 'Other', list => \@other_def, class => 'Attack'};
130 $BODY->param(Defenses => \@defenses);
133 my $order = "attacks";
134 if (defined param('order') && param('order') =~ /^(attacks|defenses|attack_points|defense_points|solo|bad_def)$/){
138 my $query = $DBH->prepare(qq{SELECT u.uid,u.username,u.attack_points, u.defense_points, n.tick
139 ,count(CASE WHEN i.mission = 'Attack' THEN 1 ELSE NULL END) AS attacks
140 ,count(CASE WHEN (i.mission = 'Defend' OR i.mission = 'AllyDef') THEN 1 ELSE NULL END) AS defenses
141 ,count(CASE WHEN i.mission = 'Attack' AND rt.id IS NULL THEN 1 ELSE NULL END) AS solo
142 ,count(CASE WHEN i.mission = 'Defend' OR i.mission = 'AllyDef' THEN NULLIF(i.ingal OR (t.alliance_id = 1),TRUE) ELSE NULL END) AS bad_def
144 JOIN groupmembers gm USING (uid)
145 LEFT OUTER JOIN (SELECT DISTINCT ON (planet) planet,tick from scans where type = 'News' ORDER BY planet,tick DESC) n USING (planet)
146 LEFT OUTER JOIN (SELECT DISTINCT * FROM intel WHERE amount = -1) i ON i.sender = u.planet
147 LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
148 LEFT OUTER JOIN (SELECT rt.id,planet,tick FROM raids r
149 JOIN raid_targets rt ON r.id = rt.raid) rt ON rt.planet = i.target
150 AND (rt.tick + 12) > i.tick AND rt.tick <= i.tick
151 LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target AND rc.uid = u.uid AND i.tick = rt.tick + rc.wave - 1
153 GROUP BY u.uid,u.username,u.attack_points, u.defense_points,n.tick
154 ORDER BY $order DESC});
155 $query->execute() or $error .= $DBH->errstr;
158 while (my $intel = $query->fetchrow_hashref){
160 $intel->{ODD} = $i % 2;
161 $intel->{OLD} = 'OLD' if (!defined $intel->{tick} || $self->{TICK} > $intel->{tick} + 60);
162 delete $intel->{tick};
163 push @members,$intel;
165 $BODY->param(Members => \@members);
167 $BODY->param(Error => $error);