]> ruin.nu Git - ndwebbie.git/blob - raids.pl
working on raids
[ndwebbie.git] / raids.pl
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 use strict;
21 use POSIX;
22 our $BODY;
23 our $DBH;
24 our $LOG;
25 our $XML;
26
27 my $raid;
28 if (param('raid') =~ /^(\d+)$/){
29         my $query = $DBH->prepare(q{SELECT id,tick,waves,message,released_coords FROM raids WHERE id = ? AND open AND not removed AND id IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?)});
30         $raid = $DBH->selectrow_hashref($query,undef,$1,$ND::UID);
31 }
32
33 unless ($XML){
34         $ND::TEMPLATE->param(TITLE => 'Raids');
35         $ND::TEMPLATE->param(HEADER => '<script type="text/javascript" src="raid.js"></script>');
36         if ($raid){#We have a raid, so list all targets
37                 $BODY->param(Raid => $raid->{id});
38                 my $noingal = '';
39                 my $planet;
40                 if ($ND::PLANET){
41                         my $query = $DBH->prepare("SELECT value, score,x,y FROM current_planet_stats WHERE id = ?");
42                         $planet = $DBH->selectrow_hashref($query,undef,$ND::PLANET);
43                         $noingal = "AND NOT (x = $planet->{x} AND y = $planet->{y})";
44                 }
45                 $BODY->param(Message => parseMarkup($raid->{message}));
46                 $BODY->param(LandingTick => parseMarkup($raid->{tick}));
47                 my $targetquery = $DBH->prepare(qq{SELECT r.id, r.planet, size, score, value, coords(p.x,p.y,p.z), race, p.value - p.size*200 -coalesce(c.metal+c.crystal+c.eonium,0)/150 - coalesce(c.structures,(SELECT avg(structures) FROM covop_targets)::int)*1500 AS fleetvalue,(c.metal+c.crystal+c.eonium)/100 AS resvalue, comment
48 FROM current_planet_stats p 
49         JOIN raid_targets r ON p.id = r.planet 
50         LEFT OUTER JOIN covop_targets c ON p.id = c.planet
51 WHERE r.raid = ?
52         $noingal
53 ORDER BY size});
54                 $targetquery->execute($raid->{id});
55                 my @targets;
56                 while (my $target = $targetquery->fetchrow_hashref){
57                         my %target;
58                         $target{Id} = $target->{id};
59                         my $num = pow(10,length($target->{score})-2);
60                         $target{Score} = ceil($target->{score}/$num)*$num;
61                         $num = pow(10,length($target->{value})-2);
62                         $target{Value} = ceil($target->{value}/$num)*$num;
63                         $num = pow(10,length($target->{size})-2);
64                         $target{Size} = floor($target->{size}/$num)*$num;
65                         $num = pow(10,length($target->{fleetvalue})-2);
66                         $target{FleetValue} = floor($target->{fleetvalue}/$num)*$num;
67                         $num = pow(10,length($target->{resvalue})-2);
68                         $target{ResValue} = floor($target->{resvalue}/$num)*$num;
69                         $target{comment} = parseMarkup($target->{comment}) if ($target->{comment});
70                         my @roids;
71                         my $size = $target{Size};
72                         for (my $i = 1; $i <= $raid->{waves}; $i++){
73                                 my $roids = floor(0.25*$size);
74                                 $size -= $roids;
75                                 my $xp;
76                                 if ($planet){
77                                         $xp = max(0,floor($roids * 10 * (min(2,$target{Score}/$planet->{score}) + min(2,$target{Value}/$planet->{value})-1)));
78                                 }
79                                 push @roids,{Wave => $i, Roids => $roids, XP => $xp};
80                         }
81                         $target{Roids} = \@roids;
82
83                         push @targets,\%target;
84                 }
85                 $BODY->param(Targets => \@targets);
86         }else{#list raids if we haven't chosen one yet
87                 my $query = $DBH->prepare(q{SELECT id,released_coords FROM raids WHERE open AND not removed AND
88 id IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?)});
89                 $query->execute($ND::UID);
90                 my @raids;
91                 while (my $raid = $query->fetchrow_hashref){
92                         push @raids,{Raid => $raid->{id}, ReleasedCoords => $raid->{released_coords}, isBC => isBC()};
93                 }
94                 $BODY->param(Raids => \@raids);
95
96                 if (isBC()){
97                         my $query = $DBH->prepare(q{SELECT id,open FROM raids WHERE not removed AND (not open 
98 OR id NOT IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?))});
99                         $query->execute($ND::UID);
100                         my @raids;
101                         while (my $raid = $query->fetchrow_hashref){
102                                 push @raids,{Raid => $raid->{id}, Open => $raid->{open}};
103                         }
104                         $BODY->param(ClosedRaids => \@raids);
105                 }
106         }
107 }
108 1;