]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Alliances.pm
Make relationship and planet_status an enum
[ndwebbie.git] / NDWeb / Pages / Alliances.pm
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 package NDWeb::Pages::Alliances;
21 use strict;
22 use warnings FATAL => 'all';
23 use ND::Include;
24 use CGI qw/:standard/;
25 use NDWeb::Include;
26
27 use base qw/NDWeb::XMLPage/;
28
29 $NDWeb::Page::PAGES{alliances} = __PACKAGE__;
30
31 sub render_body {
32         my $self = shift;
33         my ($BODY) = @_;
34         $self->{TITLE} = 'Alliances';
35         my $DBH = $self->{DBH};
36         my $error;
37
38         return $self->noAccess unless $self->isHC;
39
40         my $alliance;
41         if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
42                 my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
43                 $alliance = $DBH->selectrow_hashref($query,undef,$1);
44         }
45         if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
46                 $DBH->begin_work;
47                 if (param('crelationship')){
48                         my $value = escapeHTML(param('relationship'));
49                         if ($DBH->do(q{UPDATE alliances SET relationship = ? WHERE id =?}
50                                         ,undef,$value,$alliance->{id})){
51                                 $alliance->{relationship} = $value;
52                                 log_message $ND::UID,"HC set alliance: $alliance->{id} relationship: $value";
53                         }else{
54                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
55                         }
56                 }
57                 my $coords = param('coords');
58                 my $findplanet = $DBH->prepare(q{SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?});
59                 my $addplanet = $DBH->prepare(q{
60                         UPDATE planets SET alliance_id = $2, nick = coalesce($3,nick)
61                         WHERE id = $1;
62                         });
63                 while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=(\S+))?/g){
64                         my ($id) = $DBH->selectrow_array($findplanet,undef,$1,$2,$3) or $ND::ERROR .= p $DBH->errstr;
65                         if ($addplanet->execute($id,$alliance->{id},$4)){
66                                 my $nick = '';
67                                 $nick = "(nick $4)" if defined $4;
68                                 $error .= "<p> Added planet $1:$2:$3 $nick to this alliance</p>";
69                                 intel_log $ND::UID,$id,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id} ($alliance->{name})";
70                         }else{
71                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
72                         }
73                 }
74                 $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
75         }
76
77         if ($alliance){
78                 $BODY->param(Alliance => $alliance->{name});
79                 $BODY->param(Id => $alliance->{id});
80                 my @relationships;
81                 for my $relationship ("","Friendly", "NAP", "Hostile"){
82                         push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
83                 }
84                 $BODY->param(Relationships => \@relationships);
85
86                 my $order = "p.x,p.y,p.z";
87                 if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
88                         $order = "$1 DESC";
89                 }
90                 my $members = $DBH->prepare(qq{
91                         SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
92                         planet_status,hit_us, sizerank, scorerank, valuerank, xprank
93                         FROM current_planet_stats p
94                         WHERE p.alliance_id = ?
95                         ORDER BY $order});
96                 my @members;
97                 $members->execute($alliance->{id});
98                 while (my $member = $members->fetchrow_hashref){
99                         push @members,$member;
100                 }
101                 $BODY->param(Members => \@members);
102
103                 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',q{not ingal AND (o.alliance_id = $1 OR t.alliance_id = $1)
104                                 AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
105                                 AND ((( t.alliance_id != o.alliance_id OR t.alliance_id IS NULL OR o.alliance_id IS NULL)))
106                                 AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
107                                 }));
108                 $query->execute($alliance->{id}) or $error .= $DBH->errstr;
109
110                 my @intel;
111                 while (my $intel = $query->fetchrow_hashref){
112                         if ($intel->{ingal}){
113                                 $intel->{missionclass} = 'ingal';
114                         }else{
115                                 $intel->{missionclass} = $intel->{mission};
116                         }
117                         push @intel,$intel;
118                 }
119                 $BODY->param(Intel => \@intel);
120         }else{
121
122                 my $order = "score DESC";
123                 if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
124                         $order = "$1 DESC";
125                 }
126                 my $query = $DBH->prepare(qq{
127                         SELECT DISTINCT a.id,name,COALESCE(s.score,SUM(p.score)) AS score,COALESCE(s.size,SUM(p.size)) AS size,s.members,count(p.score) AS kmem,
128                         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,
129                         COALESCE(s.score/s.members,-1) AS scavg, COALESCE(AVG(p.score)::int,-1) AS kscavg, COALESCE(s.size/s.members,-1) AS siavg,
130                         COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
131                         FROM alliances a 
132                         LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
133                         LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
134                         GROUP BY a.id,a.name,s.score,s.size,s.members
135                         ORDER BY $order
136                         })or $error .= $DBH->errstr;
137                 $query->execute or $error .= $DBH->errstr;
138                 my @alliances;
139                 while (my $alliance = $query->fetchrow_hashref){
140                         next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
141                         push @alliances, $alliance;
142                 }
143                 $BODY->param(Alliances => \@alliances);
144         }
145         $BODY->param(Error => $error);
146         return $BODY;
147 }
148 1;