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 #**************************************************************************/
27 $ND::TEMPLATE->param(TITLE => 'Alliances');
29 die "You don't have access" unless isHC();
32 if (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 && param ('cmd') eq 'coords'){
38 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
40 if ($alliance && param ('cmd') eq 'change'){
42 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
46 $BODY->param(Alliance => $alliance->{name});
47 $BODY->param(Id => $alliance->{id});
49 for my $relationship (" ","Friendly", "NAP", "Hostile"){
50 push @relationships,{Rel => $relationship, Selected => $relationship eq $alliance->{relationship}}
52 $BODY->param(Relationships => \@relationships);
54 my $order = "p.x,p.y,p.z";
55 if (param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
58 my $members = $DBH->prepare(qq{
59 SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
60 planet_status,hit_us, sizerank, scorerank, valuerank, xprank
61 FROM current_planet_stats p
62 WHERE p.alliance_id = ?
65 $members->execute($alliance->{id});
67 while (my $member = $members->fetchrow_hashref){
68 $member->{ODD} = $i % 2;
69 push @members,$member;
72 $BODY->param(Members => \@members);
74 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 = ?)
75 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
76 AND (t.alliance_id != ? OR t.alliance_id IS NULL OR o.alliance_id != ? OR o.alliance_id IS NULL)
77 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
79 $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
83 while (my $intel = $query->fetchrow_hashref){
85 $intel->{missionclass} = 'ingal';
87 $intel->{missionclass} = $intel->{mission};
89 $intel->{ODD} = $i % 2;
93 $BODY->param(Intel => \@intel);
96 my $order = "score DESC";
97 if (param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
100 my $query = $DBH->prepare(qq{
101 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,
102 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,
103 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,
104 COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
106 LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
107 LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
108 GROUP BY a.id,a.name,s.score,s.size,s.members
110 })or $error .= $DBH->errstr;
111 $query->execute or $error .= $DBH->errstr;
114 while (my $alliance = $query->fetchrow_hashref){
115 next if ($alliance->{score} < 1 && $alliance->{kscore} < 1);
116 $alliance->{ODD} = $i % 2;
117 push @alliances, $alliance;
120 $BODY->param(Alliances => \@alliances);
122 $BODY->param(Error => $error);