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