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::Alliances;
22 use warnings FATAL => 'all';
24 use CGI qw/:standard/;
27 use base qw/NDWeb::XMLPage/;
29 $NDWeb::Page::PAGES{alliances} = __PACKAGE__;
34 $self->{TITLE} = 'Alliances';
35 my $DBH = $self->{DBH};
37 return $self->noAccess unless $self->isHC;
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);
44 if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
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";
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)
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)){
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})";
73 $DBH->commit or warn $DBH->errstr;
74 }elsif ($alliance && defined param('cmd') && param ('cmd') eq 'remove_all'){
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;
93 $BODY->param(Alliance => $alliance->{name});
94 $BODY->param(Id => $alliance->{id});
96 for my $relationship ("","Friendly", "NAP", "Hostile"){
97 push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
99 $BODY->param(Relationships => \@relationships);
101 my $order = "p.x,p.y,p.z";
102 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
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 = ?
112 $members->execute($alliance->{id});
113 while (my $member = $members->fetchrow_hashref){
114 push @members,$member;
116 $BODY->param(Members => \@members);
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)
124 $query->execute($alliance->{id}) or warn $DBH->errstr;
127 while (my $intel = $query->fetchrow_hashref){
128 if ($intel->{ingal}){
129 $intel->{missionclass} = 'ingal';
131 $intel->{missionclass} = $intel->{mission};
135 $BODY->param(Intel => \@intel);
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)$/){
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
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
152 })or warn $DBH->errstr;
153 $query->execute or warn $DBH->errstr;
155 while (my $alliance = $query->fetchrow_hashref){
156 next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
157 push @alliances, $alliance;
159 $BODY->param(Alliances => \@alliances);