]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/CovOp.pm
c1c9a046f60a1eca83ccb729fa0cd59cf9447b46
[ndwebbie.git] / NDWeb / Pages / CovOp.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 ND::Web::Pages::CovOp;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use ND::Web::Include;
25
26 use base qw/ND::Web::XMLPage/;
27
28 $ND::Web::Page::PAGES{covop} = __PACKAGE__;
29
30 sub parse {
31         my ($uri) = @_;
32         if ($uri =~ m{^/.*/(\w+)$}){
33                 param('list',$1);
34         }
35 }
36
37 sub render_body {
38         my $self = shift;
39         my ($BODY) = @_;
40         $self->{TITLE} = 'CovOp Targets';
41         my $DBH = $self->{DBH};
42
43         return $self->noAccess unless $self->isHC;
44
45         my $show = q{AND ((planet_status IS NULL OR NOT planet_status IN ('Friendly','NAP')) AND  (relationship IS NULL OR NOT relationship IN ('Friendly','NAP')))};
46         $show = '' if defined param('show') && param('show') eq 'all';
47         if (defined param('covop') && param('covop') =~ /^(\d+)$/){
48                 my $update = $DBH->prepare('UPDATE covop_targets SET covop_by = ?, last_covop = tick() WHERE planet = ? ');
49                 $update->execute($ND::UID,$1);
50         }
51
52         my $list = '';
53         my $where = '';
54         if (defined param('list') && param('list') eq 'distwhores'){
55                 $list = '&amp;list=distwhores';
56                 $where = qq{WHERE dists > 0 $show
57                 ORDER BY dists DESC,COALESCE(sec_centres::float/structures*100,0)ASC}
58         }else{
59                 $where = qq{WHERE MaxResHack > 130000 
60                 $show
61                 ORDER BY COALESCE(sec_centres::float/structures*100,0) ASC,MaxResHack DESC,metal+crystal+eonium DESC};
62         }
63
64         my $query = $DBH->prepare(qq{SELECT id, coords, metal, crystal, eonium, sec_centres::float/structures*100 AS secs, dists, last_covop, username, MaxResHack
65                 FROM (SELECT p.id,coords(x,y,z), metal,crystal,eonium,
66                 sec_centres,NULLIF(structures,0) AS structures,dists,last_covop,
67                 u.username,max_bank_hack(metal,crystal,eonium,p.value,(SELECT value FROM
68                 current_planet_stats WHERE id = ?)) AS MaxResHack, planet_status, relationship
69                 FROM covop_targets c JOIN current_planet_stats p ON p.id = c.planet
70                 LEFT OUTER JOIN users u ON u.uid = c.covop_by) AS foo
71                 $where});
72         $query->execute($self->{PLANET});
73
74         my @targets;
75         my $i = 0;
76         while (my ($id,$coords,$metal,$crystal,$eonium,$seccents,$dists,$lastcovop,$user,$max) = $query->fetchrow){
77                 $i++;
78                 push @targets,{Username => $user, Target => $id, Coords => $coords
79                         , Metal => $metal, Crystal => $crystal, Eonium => $eonium, SecCents => $seccents
80                         , Dists => $dists, MaxResHack => $max, LastCovOp => $lastcovop, List => $list, ODD => $i % 2};
81         }
82         $BODY->param(Targets => \@targets);
83         return $BODY;
84 }
85
86 1;