]> ruin.nu Git - ndwebbie.git/blob - alliances.pl
initial commit of alliances
[ndwebbie.git] / alliances.pl
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 use strict;
21 use POSIX;
22 our $BODY;
23 our $DBH;
24 our $LOG;
25 my $error;
26
27 $ND::TEMPLATE->param(TITLE => 'Alliances');
28
29 die "You don't have access" unless isHC();
30
31 my $alliance;
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);
35 }
36 if ($alliance && param ('cmd') eq 'coords'){
37         $DBH->begin_work;
38         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
39 }
40 if ($alliance && param ('cmd') eq 'change'){
41         $DBH->begin_work;
42         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
43 }
44
45 if ($alliance){
46         $BODY->param(Alliance => $alliance->{name});
47         $BODY->param(Id => $alliance->{id});
48         my @relationships;
49         for my $relationship ("&nbsp;","Friendly", "NAP", "Hostile"){
50                 push @relationships,{Rel => $relationship, Selected => $relationship eq $alliance->{relationship}}
51         }
52         $BODY->param(Relationships => \@relationships);
53
54         my $order = "p.x,p.y,p.z";
55         if (param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
56                 $order = "$1 DESC";
57         }
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 = ?
63 ORDER BY $order});
64         my @members;
65         $members->execute($alliance->{id});
66         my $i = 0;
67         while (my $member = $members->fetchrow_hashref){
68                 $member->{ODD} = $i % 2;
69                 push @members,$member;
70                 $i++;
71         }
72         $BODY->param(Members => \@members);
73
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)
78                         }));
79         $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
80
81         my @intel;
82         my $i = 0;
83         while (my $intel = $query->fetchrow_hashref){
84                 if ($intel->{ingal}){
85                         $intel->{missionclass} = 'ingal';
86                 }else{
87                         $intel->{missionclass} = $intel->{mission};
88                 }
89                 $intel->{ODD} = $i % 2;
90                 push @intel,$intel;
91                 $i++;
92         }
93         $BODY->param(Intel => \@intel);
94 }else{
95
96         my $order = "score DESC";
97         if (param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
98                 $order = "$1 DESC";
99         }
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
105 FROM alliances a 
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
109 ORDER BY $order
110                 })or $error .= $DBH->errstr;
111         $query->execute or $error .= $DBH->errstr;
112         my @alliances;
113         my $i = 0;
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;
118                 $i++;
119         }
120         $BODY->param(Alliances => \@alliances);
121 }
122 $BODY->param(Error => $error);
123 1;