]> ruin.nu Git - ndwebbie.git/blob - scripts/createretal.pl
Update retal raid script for the new infrastructure
[ndwebbie.git] / scripts / createretal.pl
1 #!/usr/bin/perl
2 q{
3 /***************************************************************************
4  *   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
20  ***************************************************************************/
21 };
22
23 use strict;
24 use warnings;
25 use DBI;
26 use DBD::Pg qw(:pg_types);
27
28 use FindBin;
29 use lib "$FindBin::Bin/../lib";
30
31 use ND::DB;
32
33 our $dbh = ND::DB::DB();
34
35 $dbh->begin_work;
36
37 my $query = $dbh->prepare(q{INSERT INTO raids (tick,waves,message)
38         VALUES(tick() + 10,3,'Retal raid') RETURNING (id)});
39 $query->execute;
40 my $raid = $query->fetchrow_array;
41
42 $query->finish;
43
44 print "$raid\n";
45
46 $dbh->do(q{INSERT INTO raid_access (raid,gid) VALUES(?,'M')}
47         ,undef,$raid);
48
49 my $addtarget = $dbh->prepare(q{INSERT INTO raid_targets(raid,pid,comment)
50         VALUES($1,$2,$3)});
51
52 my $incs = $dbh->prepare(q{SELECT pid,array_agg(i.eta) AS eta,array_agg(amount) AS amount
53         ,array_agg(shiptype) AS type,array_agg(fleet) AS name,array_agg(c.landing_tick) AS landing
54         FROM calls c
55                 JOIN incomings i USING (call)
56         WHERE c.status <> 'Covered' AND c.landing_tick BETWEEN tick() AND tick() + 6
57                 AND c.landing_tick + GREATEST(i.eta,7) > tick() + 10
58         GROUP BY pid
59         });
60 $incs->execute;
61
62 while (my $inc = $incs->fetchrow_hashref){
63         my $comment = '';
64         for my $eta (@{$inc->{eta}}){
65                 my $amount = shift @{$inc->{amount}};
66                 my $type = shift @{$inc->{type}};
67                 my $name = shift @{$inc->{name}};
68                 my $landing = shift @{$inc->{landing}};
69                 my $back = $landing + $eta;
70                 $comment .= "$name: ETA=$eta Amount=$amount Type:'$type' Landing tick=$landing Estimated back:$back\n";
71         }
72         $addtarget->execute($raid,$inc->{pid},$comment);
73 }
74
75 $dbh->commit;
76 #$dbh->rollback;
77
78 $dbh->disconnect;