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