]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Alliances.pm
Converted main page
[ndwebbie.git] / NDWeb / Pages / Alliances.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::Alliances;
21 use strict;
22 use warnings FATAL => 'all';
23 use ND::Include;
24 use CGI qw/:standard/;
25 use NDWeb::Include;
26
27 use base qw/NDWeb::XMLPage/;
28
29 $NDWeb::Page::PAGES{alliances} = __PACKAGE__;
30
31 sub render_body {
32         my $self = shift;
33         my ($BODY) = @_;
34         $self->{TITLE} = 'Alliances';
35         my $DBH = $self->{DBH};
36
37         return $self->noAccess unless $self->isHC;
38
39         my $alliance;
40         if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
41                 my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
42                 $alliance = $DBH->selectrow_hashref($query,undef,$1);
43         }
44         if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
45                 $DBH->begin_work;
46                 if (param('crelationship')){
47                         my $value = escapeHTML(param('relationship'));
48                         if ($DBH->do(q{UPDATE alliances SET relationship = ? WHERE id =?}
49                                         ,undef,$value,$alliance->{id})){
50                                 $alliance->{relationship} = $value;
51                                 log_message $ND::UID,"HC set alliance: $alliance->{id} ($alliance->{name}) relationship: $value";
52                         }else{
53                                 warn $DBH->errstr;
54                         }
55                 }
56                 my $coords = param('coords');
57                 my $findplanet = $DBH->prepare(q{SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?});
58                 my $addplanet = $DBH->prepare(q{
59                         UPDATE planets SET alliance_id = $2, nick = coalesce($3,nick)
60                         WHERE id = $1;
61                         });
62                 while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=(\S+))?/g){
63                         my ($id) = $DBH->selectrow_array($findplanet,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
64                         if ($addplanet->execute($id,$alliance->{id},$4)){
65                                 my $nick = '';
66                                 $nick = "(nick $4)" if defined $4;
67                                 warn "Added planet $1:$2:$3 $nick to this alliance\n";
68                                 intel_log $ND::UID,$id,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id} ($alliance->{name})";
69                         }else{
70                                 warn $DBH->errstr;
71                         }
72                 }
73                 $DBH->commit or warn $DBH->errstr;
74         }elsif ($alliance && defined param('cmd') && param ('cmd') eq 'remove_all'){
75                 $DBH->begin_work;
76                 my ($coords) = $DBH->selectrow_array(q{SELECT CONCAT(coords(x,y,z) || ' ') 
77                                 FROM current_planet_stats where alliance_id = $1
78                         },undef,$alliance->{id});
79                 my $removeplanets = $DBH->prepare(q{
80                         UPDATE planets SET alliance_id = NULL
81                         WHERE alliance_id = $1;
82                 }) or warn $DBH->errstr;
83                 if ($removeplanets->execute($alliance->{id})){
84                         log_message $ND::UID, "HC cleaned alliance: $alliance->{id} ($alliance->{name})\n\n$coords";
85                         $DBH->commit or warn $DBH->errstr;
86                 }else{
87                         warn $DBH->errstr;
88                         $DBH->rollback;
89                 }
90         }
91
92         if ($alliance){
93                 $BODY->param(Alliance => $alliance->{name});
94                 $BODY->param(Id => $alliance->{id});
95                 my @relationships;
96                 for my $relationship ("","Friendly", "NAP", "Hostile"){
97                         push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
98                 }
99                 $BODY->param(Relationships => \@relationships);
100
101                 my $order = "p.x,p.y,p.z";
102                 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
103                         $order = "$1 DESC";
104                 }
105                 my $members = $DBH->prepare(qq{
106                         SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
107                         planet_status,hit_us, sizerank, scorerank, valuerank, xprank
108                         FROM current_planet_stats p
109                         WHERE p.alliance_id = ?
110                         ORDER BY $order});
111                 my @members;
112                 $members->execute($alliance->{id});
113                 while (my $member = $members->fetchrow_hashref){
114                         push @members,$member;
115                 }
116                 $BODY->param(Members => \@members);
117
118                 my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin, t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',q{not ingal AND (o.alliance_id = $1 OR t.alliance_id = $1)
119                                 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
120                                 AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL)))
121                                 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
122                                 AND NOT (i.back IS NOT NULL AND i.back = i.tick + 4)
123                                 }));
124                 $query->execute($alliance->{id}) or warn $DBH->errstr;
125
126                 my @intel;
127                 while (my $intel = $query->fetchrow_hashref){
128                         if ($intel->{ingal}){
129                                 $intel->{missionclass} = 'ingal';
130                         }else{
131                                 $intel->{missionclass} = $intel->{mission};
132                         }
133                         push @intel,$intel;
134                 }
135                 $BODY->param(Intel => \@intel);
136         }else{
137
138                 my $order = "score DESC";
139                 if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
140                         $order = "$1 DESC";
141                 }
142                 my $query = $DBH->prepare(qq{
143                         SELECT DISTINCT a.id,name,COALESCE(s.score,SUM(p.score)) AS score,COALESCE(s.size,SUM(p.size)) AS size,s.members,count(p.score) AS kmem,
144                         COALESCE(SUM(p.score),-1) AS kscore, COALESCE(SUM(p.size),-1) AS ksize, COALESCE(SUM(p.xp),-1) AS kxp,COALESCE(SUM(p.value),-1) AS kvalue,
145                         COALESCE(s.score/s.members,-1) AS scavg, COALESCE(AVG(p.score)::int,-1) AS kscavg, COALESCE(s.size/s.members,-1) AS siavg,
146                         COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
147                         FROM alliances a 
148                         LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
149                         LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
150                         GROUP BY a.id,a.name,s.score,s.size,s.members
151                         ORDER BY $order
152                         })or warn $DBH->errstr;
153                 $query->execute or warn $DBH->errstr;
154                 my @alliances;
155                 while (my $alliance = $query->fetchrow_hashref){
156                         next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
157                         push @alliances, $alliance;
158                 }
159                 $BODY->param(Alliances => \@alliances);
160         }
161         return $BODY;
162 }
163 1;