]> ruin.nu Git - ndwebbie.git/blob - alliances.pl
restructure
[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 warnings FATAL => 'all';
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 (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);
35 }
36 if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
37         $DBH->begin_work;
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");
44                 }else{
45                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
46                 }
47         }
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 = ?);
52                 });
53         while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=\s*(\S+))?/g){
54                 if ($addplanet->execute($alliance->{id},$4,$1,$2,$3)){
55                         my $nick = '';
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}");
59                 }else{
60                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
61                 }
62         }
63         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
64 }
65
66 if ($alliance){
67         $BODY->param(Alliance => $alliance->{name});
68         $BODY->param(Id => $alliance->{id});
69         my @relationships;
70         for my $relationship ("&nbsp;","Friendly", "NAP", "Hostile"){
71                 push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
72         }
73         $BODY->param(Relationships => \@relationships);
74
75         my $order = "p.x,p.y,p.z";
76         if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
77                 $order = "$1 DESC";
78         }
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 = ?
84 ORDER BY $order});
85         my @members;
86         $members->execute($alliance->{id});
87         my $i = 0;
88         while (my $member = $members->fetchrow_hashref){
89                 $i++;
90                 $member->{ODD} = $i % 2;
91                 push @members,$member;
92         }
93         $BODY->param(Members => \@members);
94
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)
99                         }));
100         $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
101
102         my @intel;
103         $i = 0;
104         while (my $intel = $query->fetchrow_hashref){
105                 if ($intel->{ingal}){
106                         $intel->{missionclass} = 'ingal';
107                 }else{
108                         $intel->{missionclass} = $intel->{mission};
109                 }
110                 $i++;
111                 $intel->{ODD} = $i % 2;
112                 push @intel,$intel;
113         }
114         $BODY->param(Intel => \@intel);
115 }else{
116
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)$/){
119                 $order = "$1 DESC";
120         }
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
126 FROM alliances a 
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
130 ORDER BY $order
131                 })or $error .= $DBH->errstr;
132         $query->execute or $error .= $DBH->errstr;
133         my @alliances;
134         my $i = 0;
135         while (my $alliance = $query->fetchrow_hashref){
136                 next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
137                 $i++;
138                 $alliance->{ODD} = $i % 2;
139                 push @alliances, $alliance;
140         }
141         $BODY->param(Alliances => \@alliances);
142 }
143 $BODY->param(Error => $error);
144 1;