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 #**************************************************************************/
21 use warnings FATAL => 'all';
27 $ND::TEMPLATE->param(TITLE => 'Alliances');
29 die "You don't have access" unless isHC();
32 if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
33 my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
34 $alliance = $DBH->selectrow_hashref($query,undef,$1);
36 if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
38 if (param('crelationship')){
39 my $value = escapeHTML(param('relationship'));
40 if ($DBH->do(q{UPDATE alliances SET relationship = ? WHERE id =?}
41 ,undef,$value,$alliance->{id})){
42 $alliance->{relationship} = $value;
43 $LOG->execute($ND::UID,"HC set alliance: $alliance->{id} relationship: $value");
45 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
48 my $coords = param('coords');
49 my $addplanet = $DBH->prepare(q{
50 UPDATE planets SET alliance_id = ?, nick = coalesce(?,nick)
51 WHERE id = (SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?);
53 while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=\s*(\S+))?/g){
54 if ($addplanet->execute($alliance->{id},$4,$1,$2,$3)){
56 $nick = '(nick $4)' if defined $4;
57 $error .= "<p> Added planet $1:$2:$3 $nick to this alliance</p>";
58 $LOG->execute($ND::UID,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id}");
60 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
63 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
67 $BODY->param(Alliance => $alliance->{name});
68 $BODY->param(Id => $alliance->{id});
70 for my $relationship (" ","Friendly", "NAP", "Hostile"){
71 push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
73 $BODY->param(Relationships => \@relationships);
75 my $order = "p.x,p.y,p.z";
76 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
79 my $members = $DBH->prepare(qq{
80 SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
81 planet_status,hit_us, sizerank, scorerank, valuerank, xprank
82 FROM current_planet_stats p
83 WHERE p.alliance_id = ?
86 $members->execute($alliance->{id});
88 while (my $member = $members->fetchrow_hashref){
90 $member->{ODD} = $i % 2;
91 push @members,$member;
93 $BODY->param(Members => \@members);
95 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 = ?)
96 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
97 AND (t.alliance_id != ? OR t.alliance_id IS NULL OR o.alliance_id != ? OR o.alliance_id IS NULL)
98 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
100 $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
104 while (my $intel = $query->fetchrow_hashref){
105 if ($intel->{ingal}){
106 $intel->{missionclass} = 'ingal';
108 $intel->{missionclass} = $intel->{mission};
111 $intel->{ODD} = $i % 2;
114 $BODY->param(Intel => \@intel);
117 my $order = "score DESC";
118 if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
121 my $query = $DBH->prepare(qq{
122 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,
123 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,
124 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,
125 COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
127 LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
128 LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
129 GROUP BY a.id,a.name,s.score,s.size,s.members
131 })or $error .= $DBH->errstr;
132 $query->execute or $error .= $DBH->errstr;
135 while (my $alliance = $query->fetchrow_hashref){
136 next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
138 $alliance->{ODD} = $i % 2;
139 push @alliances, $alliance;
141 $BODY->param(Alliances => \@alliances);
143 $BODY->param(Error => $error);