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 $ND::PAGES{alliances} = {parse => \&parse, process => \&process, render=> \&render};
41 $ND::TEMPLATE->param(TITLE => 'Alliances');
43 return $ND::NOACCESS unless isHC();
46 if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
47 my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
48 $alliance = $DBH->selectrow_hashref($query,undef,$1);
50 if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
52 if (param('crelationship')){
53 my $value = escapeHTML(param('relationship'));
54 if ($DBH->do(q{UPDATE alliances SET relationship = ? WHERE id =?}
55 ,undef,$value,$alliance->{id})){
56 $alliance->{relationship} = $value;
57 log_message $ND::UID,"HC set alliance: $alliance->{id} relationship: $value";
59 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
62 my $coords = param('coords');
63 my $findplanet = $DBH->prepare(q{SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?});
64 my $addplanet = $DBH->prepare(q{
65 UPDATE planets SET alliance_id = $2, nick = coalesce($3,nick)
68 while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=\s*(\S+))?/g){
69 my ($id) = $DBH->selectrow_array($findplanet,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
70 if ($addplanet->execute($id,$alliance->{id},$4)){
72 $nick = '(nick $4)' if defined $4;
73 $error .= "<p> Added planet $1:$2:$3 $nick to this alliance</p>";
74 intel_log $ND::UID,$id,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id} ($alliance->{name})";
76 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
79 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
83 $BODY->param(Alliance => $alliance->{name});
84 $BODY->param(Id => $alliance->{id});
86 for my $relationship (" ","Friendly", "NAP", "Hostile"){
87 push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
89 $BODY->param(Relationships => \@relationships);
91 my $order = "p.x,p.y,p.z";
92 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
95 my $members = $DBH->prepare(qq{
96 SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
97 planet_status,hit_us, sizerank, scorerank, valuerank, xprank
98 FROM current_planet_stats p
99 WHERE p.alliance_id = ?
102 $members->execute($alliance->{id});
104 while (my $member = $members->fetchrow_hashref){
106 $member->{ODD} = $i % 2;
107 push @members,$member;
109 $BODY->param(Members => \@members);
111 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',qq{not ingal AND (t.alliance_id = ? OR t.alliance_id = ?)
112 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
113 AND (t.alliance_id != ? OR t.alliance_id IS NULL OR o.alliance_id != ? OR o.alliance_id IS NULL)
114 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
116 $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
120 while (my $intel = $query->fetchrow_hashref){
121 if ($intel->{ingal}){
122 $intel->{missionclass} = 'ingal';
124 $intel->{missionclass} = $intel->{mission};
127 $intel->{ODD} = $i % 2;
130 $BODY->param(Intel => \@intel);
133 my $order = "score DESC";
134 if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
137 my $query = $DBH->prepare(qq{
138 SELECT DISTINCT a.id,name,COALESCE(s.score,SUM(p.score)) AS score,COALESCE(s.size,SUM(p.size)) AS size,s.members,count(*) AS kmem,
139 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,
140 COALESCE(s.score/LEAST(s.members,60),-1) AS scavg, COALESCE(AVG(p.score)::int,-1) AS kscavg, COALESCE(s.size/s.members,-1) AS siavg,
141 COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
143 LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
144 LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
145 GROUP BY a.id,a.name,s.score,s.size,s.members
147 })or $error .= $DBH->errstr;
148 $query->execute or $error .= $DBH->errstr;
151 while (my $alliance = $query->fetchrow_hashref){
152 next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
154 $alliance->{ODD} = $i % 2;
155 push @alliances, $alliance;
157 $BODY->param(Alliances => \@alliances);
159 $BODY->param(Error => $error);