]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/MemberIntel.pm
Merge branch 'master' into memberintel
[ndwebbie.git] / NDWeb / Pages / MemberIntel.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 package NDWeb::Pages::MemberIntel;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use NDWeb::Include;
25
26 use base qw/NDWeb::XMLPage/;
27
28 $NDWeb::Page::PAGES{memberIntel} = __PACKAGE__;
29
30 sub render_body {
31         my $self = shift;
32         my ($BODY) = @_;
33         $self->{TITLE} = 'Member Intel';
34         my $DBH = $self->{DBH};
35         my $error;
36
37         return $self->noAccess unless $self->isHC;
38
39         my $showticks = 'AND i.tick > tick()';
40         if (defined param('show')){
41                 if (param('show') eq 'all'){
42                         $showticks = '';
43                 }elsif (param('show') =~ /^(\d+)$/){
44                         $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
45                 }
46         }
47
48         my $user;
49         if (defined param('uid') && param('uid') =~ /^(\d+)$/){
50                 my $query = $DBH->prepare(q{SELECT username,uid FROM users WHERE uid = ?
51                         });
52                 $user = $DBH->selectrow_hashref($query,undef,$1);
53         }
54
55         if ($user){
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
59                         FROM users u
60                         LEFT OUTER JOIN (SELECT * 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
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)
72                         });
73                 $query->execute($user->{uid}) or $error .= $DBH->errstr;
74                 my @nd_attacks;
75                 my @retals;
76                 my @other;
77                 while (my $intel = $query->fetchrow_hashref){
78                         my $attack = {target => $intel->{coords}, tick => $intel->{tick}};
79                         if ($intel->{ndtarget}){
80                                 if (defined $intel->{launched}){
81                                         $attack->{Other} = 'Claimed '.($intel->{launched} ? 'and confirmed' : 'but NOT confirmed');
82                                 }else{
83                                         $attack->{Other} = 'Launched at a tick that was not claimed';
84                                 }
85                                 push @nd_attacks, $attack;
86                         }else{
87                                 push @other, $attack;
88                         }
89                 }
90                 my @attacks;
91                 push @attacks, {name => 'ND Attacks', list => \@nd_attacks};
92                 push @attacks, {name => 'Other', list => \@other};
93                 $BODY->param(Attacks => \@attacks);
94
95         }else{
96                 my $query = $DBH->prepare(q{SELECT u.uid,u.username,u.attack_points, u.defense_points, n.tick
97                         ,count(CASE WHEN i.mission = 'Attack' THEN 1 ELSE NULL END) AS attacks
98                         ,count(CASE WHEN (i.mission = 'Defend' OR i.mission = 'AllyDef') THEN 1 ELSE NULL END) AS defenses
99                         FROM users u
100                         JOIN groupmembers gm USING (uid)
101                         LEFT OUTER JOIN (SELECT DISTINCT ON (planet) planet,tick from scans where type = 'News' ORDER BY planet,tick DESC) n USING (planet)
102                         LEFT OUTER JOIN (SELECT * FROM intel WHERE amount = -1) i ON i.sender = u.planet
103                         LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
104                         WHERE gm.gid = 2
105                         GROUP BY u.uid,u.username,u.attack_points, u.defense_points,n.tick
106                         ORDER BY attacks DESC,defenses DESC, attack_points DESC, defense_points DESC});
107                 $query->execute() or $error .= $DBH->errstr;
108                 my @members;
109                 my $i = 0;
110                 while (my $intel = $query->fetchrow_hashref){
111                         $i++;
112                         $intel->{ODD} = $i % 2;
113                         $intel->{OLD} = 'OLD' if (!defined $intel->{tick} || $self->{TICK} > $intel->{tick} + 60);
114                         delete $intel->{tick};
115                         push @members,$intel;
116                 }
117                 $BODY->param(Members => \@members);
118         }
119         $BODY->param(Error => $error);
120         return $BODY;
121 }
122 1;