--- /dev/null
+#**************************************************************************
+# Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
+# *
+# 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{<th class="center">([^<]+)</th>}g) {
+ push @fleets, {name => $1, ships => []};
+ }
+
+ while ($file =~ m{<tr><td class="left">([^<]+)</td>(.+?)</tr>}g) {
+ my $ship = $1;
+ next if $ship eq 'Total Ships';
+ my $amounts = $2;
+ my $i = 0;
+ while ($amounts =~ m{<td class="center">([\d,]+)</td>}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;
+ }
+ }
+}
+
+