]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/MemberIntel.pm
Converted alliances page
[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;
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 DISTINCT eta,tick,sender,target,mission,name FROM fleets WHERE amount IS NULL) 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)
72                         });
73                 $query->execute($user->{uid}) or $error .= $DBH->errstr;
74                 my @nd_attacks;
75                 my @other_attacks;
76                 while (my $intel = $query->fetchrow_hashref){
77                         my $attack = {target => $intel->{coords}, tick => $intel->{tick}};
78                         if ($intel->{ndtarget}){
79                                 if (defined $intel->{launched}){
80                                         $attack->{Other} = 'Claimed '.($intel->{launched} ? 'and confirmed' : 'but NOT confirmed');
81                                 }else{
82                                         $attack->{Other} = 'Launched at a tick that was not claimed';
83                                 }
84                                 push @nd_attacks, $attack;
85                         }else{
86                                 push @other_attacks, $attack;
87                         }
88                 }
89                 my @attacks;
90                 push @attacks, {name => 'ND Attacks', list => \@nd_attacks, class => 'AllyDef'};
91                 push @attacks, {name => 'Other', list => \@other_attacks, class => 'Attack'};
92                 $BODY->param(Attacks => \@attacks);
93
94                 $query = $DBH->prepare(q{
95                         SELECT coords(t.x,t.y,t.z),t.alliance_id, t.alliance, i.eta, i.tick, i.ingal
96                         FROM users u
97                         JOIN (SELECT DISTINCT name,eta,tick,sender,target,mission,ingal FROM fleets WHERE amount IS NULL) i ON i.sender = u.planet
98                         LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
99                         WHERE u.uid = $1 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
100                         ORDER BY (i.tick - i.eta)
101                         });
102                 $query->execute($user->{uid}) or $error .= $DBH->errstr;
103                 my @nd_def;
104                 my @ingal_def;
105                 my @other_def;
106                 while (my $intel = $query->fetchrow_hashref){
107                         my $def = {target => $intel->{coords}.(defined $intel->{alliance} ? " ($intel->{alliance})" : ''), tick => $intel->{tick}};
108                         if (defined $intel->{alliance_id} && $intel->{alliance_id} == 1){
109                                 push @nd_def, $def;
110                         }elsif($intel->{ingal}){
111                                 push @ingal_def, $def;
112                         }else{
113                                 push @other_def, $def;
114                         }
115                 }
116                 my @defenses;
117                 push @defenses, {name => 'ND Def', list => \@nd_def, class => 'AllyDef'};
118                 push @defenses, {name => 'Ingal Def', list => \@ingal_def, class => 'Defend'};
119                 push @defenses, {name => 'Other', list => \@other_def, class => 'Attack'};
120                 $BODY->param(Defenses => \@defenses);
121
122         }else{
123                 my $order = "attacks";
124                 if (defined param('order') && param('order') =~ /^(attacks|defenses|attack_points|defense_points|solo|bad_def)$/){
125                         $order = $1;
126                 }
127
128                 my $query = $DBH->prepare(qq{SELECT u.uid,u.username,u.attack_points, u.defense_points, n.tick
129                         ,count(CASE WHEN i.mission = 'Attack' THEN 1 ELSE NULL END) AS attacks
130                         ,count(CASE WHEN (i.mission = 'Defend' OR i.mission = 'AllyDef') THEN 1 ELSE NULL END) AS defenses
131                         ,count(CASE WHEN i.mission = 'Attack' AND rt.id IS NULL THEN 1 ELSE NULL END) AS solo
132                         ,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
133                         FROM users u
134                         JOIN groupmembers gm USING (uid)
135                         LEFT OUTER JOIN (SELECT DISTINCT ON (planet) planet,tick from scans where type = 'News' ORDER BY planet,tick DESC) n USING (planet)
136                         LEFT OUTER JOIN (SELECT DISTINCT name,eta,tick,sender,target,mission,ingal FROM fleets WHERE amount IS NULL) i ON i.sender = u.planet
137                         LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
138                         LEFT OUTER JOIN (SELECT rt.id,planet,tick FROM raids r 
139                                         JOIN raid_targets rt ON r.id = rt.raid) rt ON rt.planet = i.target 
140                                 AND (rt.tick + 12) > i.tick AND rt.tick <= i.tick 
141                         LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target AND rc.uid = u.uid AND i.tick = rt.tick + rc.wave - 1
142                         WHERE gm.gid = 2
143                         GROUP BY u.uid,u.username,u.attack_points, u.defense_points,n.tick
144                         ORDER BY $order DESC});
145                 $query->execute() or $error .= $DBH->errstr;
146                 my @members;
147                 while (my $intel = $query->fetchrow_hashref){
148                         $intel->{OLD} = 'OLD' if (!defined $intel->{tick} || $self->{TICK} > $intel->{tick} + 60);
149                         delete $intel->{tick};
150                         push @members,$intel;
151                 }
152                 $BODY->param(Members => \@members);
153         }
154         $BODY->param(Error => $error);
155         return $BODY;
156 }
157 1;