]> ruin.nu Git - ndwebbie.git/blob - ND/Include.pm
fatal warnings and possible to comment targets again
[ndwebbie.git] / ND / Include.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 ND::Include;
21 use strict;
22 use warnings FATAL => 'all';
23 require Exporter;
24
25 our @ISA = qw/Exporter/;
26
27 our @EXPORT = qw/isMember isHC isDC isBC isOfficer isScanner isIntel parseMarkup min max listTargets
28         alliances intelquery generateClaimXml/;
29
30 sub isMember {
31         return exists $ND::GROUPS{Members};
32 }
33
34 sub isHC {
35         return exists $ND::GROUPS{HC};
36 }
37
38 sub isDC {
39         return exists $ND::GROUPS{DC};
40 }
41
42 sub isBC {
43         return exists $ND::GROUPS{BC};
44 }
45
46 sub isOfficer {
47         return exists $ND::GROUPS{Officers};
48 }
49
50 sub isScanner {
51         return exists $ND::GROUPS{Scanners};
52 }
53
54 sub isIntel {
55         return exists $ND::GROUPS{Intel};
56 }
57
58 sub parseMarkup {
59         my ($text) = @_;
60
61         $text =~ s{\n}{\n<br/>}g;
62         $text =~ s{\[B\](.*?)\[/B\]}{<b>$1</b>};
63         return $text;
64 }
65
66 sub min {
67     my ($x,$y) = @_;
68     return ($x > $y ? $y : $x);
69 }
70
71 sub max {
72     my ($x,$y) = @_;
73     return ($x < $y ? $y : $x);
74 }
75
76 sub listTargets {
77         my $query = $ND::DBH->prepare(qq{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
78 FROM raid_claims c
79         JOIN raid_targets t ON c.target = t.id
80         JOIN raids r ON t.raid = r.id
81         JOIN current_planet_stats p ON t.planet = p.id
82 WHERE c.uid = ? AND r.tick+c.wave > ? AND r.open AND not r.removed
83 ORDER BY r.tick+c.wave,x,y,z});
84         $query->execute($ND::UID,$ND::TICK);
85         my @targets;
86         while (my $target = $query->fetchrow_hashref){
87                 my $coords = "Target $target->{id}";
88                 $coords = $target->{coords} if $target->{released_coords};
89                 push @targets,{Coords => $coords, Launched => $target->{launched}, Raid => $target->{raid}
90                         , Target => $target->{id}, Tick => $target->{landingtick}, Wave => $target->{wave}
91                         , AJAX => $ND::AJAX, JoinName => $target->{joinable} ? 'N' : 'J'
92                         , Joinable => $target->{joinable} ? 'FALSE' : 'TRUE'};
93         }
94         my $template = HTML::Template->new(filename => "templates/targetlist.tmpl", cache => 1);
95         $template->param(Targets => \@targets);
96         return $template->output;
97 }
98
99 sub alliances {
100         my ($alliance) = @_;
101         my @alliances;
102         $alliance = -1 unless defined $alliance;
103         push @alliances,{Id => -1, Name => '&nbsp;', Selected => not $alliance};
104         my $query = $ND::DBH->prepare(q{SELECT id,name FROM alliances ORDER BY name});
105         $query->execute;        
106         while (my $ally = $query->fetchrow_hashref){
107                 push @alliances,{Id => $ally->{id}, Name => $ally->{name}, Selected => $alliance == $ally->{id}};
108         }
109         return @alliances;
110 }
111
112 sub intelquery {
113         my ($columns,$where) = @_;
114         return qq{
115 SELECT $columns, i.mission, i.tick AS landingtick,MIN(i.eta) AS eta, i.amount, i.ingal, u.username
116 FROM (intel i NATURAL JOIN users u)
117         JOIN current_planet_stats t ON i.target = t.id
118         JOIN current_planet_stats o ON i.sender = o.id
119 WHERE $where 
120 GROUP BY i.tick,i.mission,t.x,t.y,t.z,o.x,o.y,o.z,i.amount,i.ingal,u.username,t.alliance,o.alliance,t.nick,o.nick
121 ORDER BY i.tick DESC, i.mission};
122 }
123
124
125 sub generateClaimXml {
126         my ($raid, $from, $target) = @_;
127
128         my ($timestamp) = $ND::DBH->selectrow_array("SELECT MAX(modified)::timestamp AS modified FROM raid_targets");
129         $ND::BODY->param(Timestamp => $timestamp);
130         if ($target){
131                 $target = "r.id = $target";
132                 $_ = listTargets();
133                 $ND::BODY->param(TargetList => $_);
134         }else{
135                 $target = "r.raid = $raid->{id}";
136         }
137
138         if ($from){
139                 $from = "AND modified > '$from'";
140         }else{
141                 $from = '';
142         }
143         my $targets = $ND::DBH->prepare(qq{SELECT r.id,r.planet FROM raid_targets r WHERE $target $from});
144         $targets->execute or print p($ND::DBH->errstr);
145         my $claims =  $ND::DBH->prepare(qq{ SELECT username,joinable,launched FROM raid_claims
146                 NATURAL JOIN users WHERE target = ? AND wave = ?});
147         my @targets;
148         while (my $target = $targets->fetchrow_hashref){
149                 my %target;
150                 $target{Id} = $target->{id};
151                 $target{Coords} = $target->{id};
152                 my @waves;
153                 for (my $i = 1; $i <= $raid->{waves}; $i++){
154                         my %wave;
155                         $wave{Id} = $i;
156                         $claims->execute($target->{id},$i);
157                         my $joinable = 0;
158                         my $claimers;
159                         if ($claims->rows != 0){
160                                 my $owner = 0;
161                                 my @claimers;
162                                 while (my $claim = $claims->fetchrow_hashref){
163                                         $owner = 1 if ($ND::USER eq $claim->{username});
164                                         $joinable = 1 if ($claim->{joinable});
165                                         $claim->{username} .= '*' if ($claim->{launched});
166                                         push @claimers,$claim->{username};
167                                 }
168                                 $claimers = join '/', @claimers;
169                                 if ($owner){
170                                         $wave{Command} = 'Unclaim';
171                                         if ($raid->{released_coords}){
172                                                 $target{Coords} = $ND::DBH->selectrow_array('SELECT coords(x,y,z) FROM current_planet_stats WHERE id = ?',undef,$target->{planet});
173                                         }
174                                 }elsif ($joinable){
175                                         $wave{Command} = 'Join';
176                                 }else{
177                                         $wave{Command} = 'none';
178                                 }
179                         }else{
180                                 #if (!isset($planet) || ($target->value/$planet->value > 0.4 || $target->score/$planet->score > 0.4))
181                                 $wave{Command} = 'Claim';
182                         }
183                         $wave{Claimers} = $claimers;
184                         $wave{Joinable} = $joinable;
185                         push @waves,\%wave;
186                 }
187                 $target{Waves} = \@waves;
188                 push @targets,\%target;
189         }
190         $ND::BODY->param(Targets => \@targets);
191 }
192
193 1;