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