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 ND::Web::Pages::Alliances;
22 use warnings FATAL => 'all';
24 use CGI qw/:standard/;
27 use base qw/ND::Web::XMLPage/;
29 $ND::Web::Page::PAGES{alliances} = __PACKAGE__;
34 $self->{TITLE} = 'Alliances';
35 my $DBH = $self->{DBH};
38 return $self->noAccess unless $self->isHC;
41 if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
42 my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
43 $alliance = $DBH->selectrow_hashref($query,undef,$1);
45 if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
47 if (param('crelationship')){
48 my $value = escapeHTML(param('relationship'));
49 if ($DBH->do(q{UPDATE alliances SET relationship = NULLIF(?,'') WHERE id =?}
50 ,undef,$value,$alliance->{id})){
51 $alliance->{relationship} = $value;
52 log_message $ND::UID,"HC set alliance: $alliance->{id} relationship: $value";
54 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
57 my $coords = param('coords');
58 my $findplanet = $DBH->prepare(q{SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?});
59 my $addplanet = $DBH->prepare(q{
60 UPDATE planets SET alliance_id = $2, nick = coalesce($3,nick)
63 while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=(\S+))?/g){
64 my ($id) = $DBH->selectrow_array($findplanet,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
65 if ($addplanet->execute($id,$alliance->{id},$4)){
67 $nick = "(nick $4)" if defined $4;
68 $error .= "<p> Added planet $1:$2:$3 $nick to this alliance</p>";
69 intel_log $ND::UID,$id,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id} ($alliance->{name})";
71 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
74 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
78 $BODY->param(Alliance => $alliance->{name});
79 $BODY->param(Id => $alliance->{id});
81 for my $relationship ("","Friendly", "NAP", "Hostile"){
82 push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
84 $BODY->param(Relationships => \@relationships);
86 my $order = "p.x,p.y,p.z";
87 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
90 my $members = $DBH->prepare(qq{
91 SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
92 planet_status,hit_us, sizerank, scorerank, valuerank, xprank
93 FROM current_planet_stats p
94 WHERE p.alliance_id = ?
97 $members->execute($alliance->{id});
99 while (my $member = $members->fetchrow_hashref){
101 $member->{ODD} = $i % 2;
102 push @members,$member;
104 $BODY->param(Members => \@members);
106 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)
107 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
108 AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL)))
109 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
111 $query->execute($alliance->{id}) or $error .= $DBH->errstr;
115 while (my $intel = $query->fetchrow_hashref){
116 if ($intel->{ingal}){
117 $intel->{missionclass} = 'ingal';
119 $intel->{missionclass} = $intel->{mission};
122 $intel->{ODD} = $i % 2;
125 $BODY->param(Intel => \@intel);
128 my $order = "score DESC";
129 if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
132 my $query = $DBH->prepare(qq{
133 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,
134 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,
135 COALESCE(s.score/s.members,-1) AS scavg, COALESCE(AVG(p.score)::int,-1) AS kscavg, COALESCE(s.size/s.members,-1) AS siavg,
136 COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
138 LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
139 LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
140 GROUP BY a.id,a.name,s.score,s.size,s.members
142 })or $error .= $DBH->errstr;
143 $query->execute or $error .= $DBH->errstr;
146 while (my $alliance = $query->fetchrow_hashref){
147 next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
149 $alliance->{ODD} = $i % 2;
150 push @alliances, $alliance;
152 $BODY->param(Alliances => \@alliances);
154 $BODY->param(Error => $error);