X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=blobdiff_plain;f=lib%2FNDWeb%2FScans.pm;fp=lib%2FNDWeb%2FScans.pm;h=bb05e99408d6bbe1cb69aea55b30a29f220f4df4;hp=0000000000000000000000000000000000000000;hb=0000c72e6700ef1af5dd4ef608d6a9f00b30442d;hpb=75707e9b9cb6a816379ac2a8ac0c215a4bbe2545 diff --git a/lib/NDWeb/Scans.pm b/lib/NDWeb/Scans.pm new file mode 100644 index 0000000..bb05e99 --- /dev/null +++ b/lib/NDWeb/Scans.pm @@ -0,0 +1,84 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package NDWeb::Scans; +use strict; +use warnings; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/parseMilScan doMilScan/; + +my %classes = (Fighter => 'Fi', Corvette => 'Co', Frigate => 'Fr', Destroyer => 'De', Cruiser => 'Cr', Battleship => 'Bs', Structure => 'St', Roids => 'Ro', Resources => 'Re', '-' => '-'); + +sub parseMilScan ($) { + my ($file) = @_; + my @fleets; + + while ($file =~ m{([^<]+)}g) { + push @fleets, {name => $1, ships => []}; + } + + while ($file =~ m{([^<]+)(.+?)}g) { + my $ship = $1; + next if $ship eq 'Total Ships'; + my $amounts = $2; + my $i = 0; + while ($amounts =~ m{([\d,]+)}g) { + my $fleet = $fleets[$i]; + my $amount = $1; + $amount =~ s/,//g; + if ($ship eq 'Total Visible Ships') { + $fleet->{amount} = $amount; + } elsif ($amount > 0) { + push @{$fleet->{ships}}, {ship => $ship, amount => $amount}; + } + ++$i; + } + } + + return @fleets; +} + +my $addfleet_sql = q{INSERT INTO fleets (name, mission, pid, tick, amount) VALUES(?,?,?,?,?) RETURNING fid}; +my $addships_sql = q{INSERT INTO fleet_ships (fid, ship, amount) VALUES(?,?,?)}; +my $fleetscan_sql = q{INSERT INTO fleet_scans (fid, id) VALUES(?,?)}; + +sub doMilScan ($$$) { + my ($dbh, $scan,$file) = @_; + + my $addfleet = $dbh->prepare_cached($addfleet_sql); + my $fleetscan = $dbh->prepare_cached($fleetscan_sql); + my $addships = $dbh->prepare_cached($addships_sql); + + my @fleets = parseMilScan($file); + for my $fleet (@fleets) { + next if $fleet->{amount} == 0; + $addfleet->execute($fleet->{name},$scan->{type},$scan->{pid} + ,$scan->{tick}, $fleet->{amount}); + my ($id) = $addfleet->fetchrow_array; + $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr; + for my $s (@{$fleet->{ships}}){ + $addships->execute($id, $s->{ship}, $s->{amount}) or die $dbh->errstr; + } + } +} + +