From 0000c72e6700ef1af5dd4ef608d6a9f00b30442d Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sat, 14 Jan 2023 22:05:29 +0100 Subject: [PATCH] Add support for Military Scans --- lib/NDWeb/Scans.pm | 84 ++++++++++++++++++++++++++++++++++ scripts/scans.pl | 8 ++++ t/milscan.html | 93 +++++++++++++++++++++++++++++++++++++ t/scans.t | 111 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 lib/NDWeb/Scans.pm create mode 100644 t/milscan.html create mode 100644 t/scans.t 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; + } + } +} + + diff --git a/scripts/scans.pl b/scripts/scans.pl index b54cb54..525c969 100755 --- a/scripts/scans.pl +++ b/scripts/scans.pl @@ -35,6 +35,7 @@ use FindBin; use lib "$FindBin::Bin/../lib"; use ND::DB; +use NDWeb::Scans; our $dbh = ND::DB::DB(); @@ -143,6 +144,12 @@ sub parse_jumpgate { } +sub parse_military { + my ($scan,$file) = @_; + + doMilScan($dbh, $scan, $file); +} + my $adddevscan = $dbh->prepare(q{INSERT INTO development_scans (id,tick,pid,light_fac,medium_fac,heavy_fac,amps,distorters ,metal_ref,crystal_ref,eonium_ref,reslabs,fincents,milcents,seccents,structdefs @@ -157,6 +164,7 @@ my %parsers = ( Unit => \&parse_unit, 'Advanced Unit' => \&parse_unit, Jumpgate => \&parse_jumpgate, + Military => \&parse_military, ); diff --git a/t/milscan.html b/t/milscan.html new file mode 100644 index 0000000..686c6ee --- /dev/null +++ b/t/milscan.html @@ -0,0 +1,93 @@ + + + + + Planetarion > Scan + + + + + + + + + + + + + + + + +
+
+ +
+ +
+
+ +
+
+
+
+
Scan Result [Hide]
+
+

Scan Link
[scan]g79wu6yas8wt1ho[/scan]

+

Scan time: Fri, 06 Jan 21:15:35

+

Add To Bcalc

+

Military Scan on 2:2:2 in tick 3046

+ + + + + + + + + + + + + + + + +
ShipBaseFleet 1Fleet 2Fleet 3
Harpy0000
Centaur199,900000
Chimera054,50000
Pegasus0000
Drake0000
Syren0000
Titan50,000000
Wyvern3,000000
Medusa1,001000
Griffin0000
Demeter0000
Behemoth0000
Total Visible Ships253,90154,50000
Total Ships253,90154,50000
+

+
+ +
+ + +
+ +
+
+ + diff --git a/t/scans.t b/t/scans.t new file mode 100644 index 0000000..df404dd --- /dev/null +++ b/t/scans.t @@ -0,0 +1,111 @@ +use strict; +use warnings; +use Test2::V0; + +use DBD::Mock; + +use Data::Dumper; +use FindBin; + +use NDWeb::Scans; + +my $milscan = do { + open my $in, '<', "$FindBin::Bin/milscan.html" or die "Can't read file: $!"; + local $/; + <$in> +}; + +my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) + or die "Cannot create handle: $DBI::errstr\n"; + +{ + my @fleets = parseMilScan($milscan); + + #print Dumper(\@fleets), "\n"; + + is ($fleets[0]->{name}, 'Base'); + is (scalar @{$fleets[0]->{ships}}, 4); + is ($fleets[0]->{ships}->[0]->{ship}, 'Centaur'); + is ($fleets[0]->{ships}->[0]->{amount}, 199900); + is ($fleets[0]->{amount}, 253901); + is ($fleets[1]->{name}, 'Fleet 1'); + is (scalar @{$fleets[1]->{ships}}, 1); + is ($fleets[1]->{ships}->[0]->{ship}, 'Chimera'); + is ($fleets[1]->{ships}->[0]->{amount}, 54500); + is ($fleets[1]->{amount}, 54500); + is ($fleets[2]->{name}, 'Fleet 2'); + is ($fleets[2]->{amount}, 0); + is ($fleets[3]->{name}, 'Fleet 3'); + is ($fleets[3]->{amount}, 0); +} + +{ + my $scan = {id => 31337, type => 'Military', pid => 1337, tick => 123}; + my $fid = 666; + $dbh->{mock_add_resultset} = { + sql => qr/^INSERT INTO fleets \(name, mission, pid, tick, amount\)/i, + results => [['fid'], [$fid]], + }; + doMilScan($dbh, $scan, $milscan); + my $history = $dbh->{mock_all_history}; + is(scalar(@{$history}), 3, 'Correct number of statements executed'); + + #print Dumper($history), "\n"; + { + my $sth = $history->[0]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleets \(name, mission, pid, tick, amount\).*}sm, + ); + is(scalar(@{$exec_history}), 2); + is($exec_history->[0]->{params}->[0], "Base"); + is($exec_history->[0]->{params}->[1], "Military"); + is($exec_history->[0]->{params}->[2], $scan->{pid}); + is($exec_history->[0]->{params}->[3], $scan->{tick}); + is($exec_history->[0]->{params}->[4], 253901); + + is($exec_history->[1]->{params}->[0], "Fleet 1"); + is($exec_history->[1]->{params}->[1], "Military"); + is($exec_history->[1]->{params}->[2], $scan->{pid}); + is($exec_history->[1]->{params}->[3], $scan->{tick}); + is($exec_history->[1]->{params}->[4], 54500); + + } + { + my $sth = $history->[1]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleet_scans \(fid, id\).*}sm, + ); + is(scalar(@{$exec_history}), 2); + is($exec_history->[0]->{params}->[0], $fid); + is($exec_history->[0]->{params}->[1], $scan->{id}); + is($exec_history->[1]->{params}->[0], $fid); + is($exec_history->[1]->{params}->[1], $scan->{id}); + } + { + my $sth = $history->[2]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleet_ships \(fid, ship, amount\).*}sm, + ); + is(scalar(@{$exec_history}), 5); + is($exec_history->[0]->{params}->[0], $fid); + is($exec_history->[0]->{params}->[1], 'Centaur'); + is($exec_history->[0]->{params}->[2], 199900); + is($exec_history->[1]->{params}->[0], $fid); + is($exec_history->[1]->{params}->[1], 'Titan'); + is($exec_history->[1]->{params}->[2], 50000); + is($exec_history->[2]->{params}->[0], $fid); + is($exec_history->[2]->{params}->[1], 'Wyvern'); + is($exec_history->[2]->{params}->[2], 3000); + is($exec_history->[3]->{params}->[0], $fid); + is($exec_history->[3]->{params}->[1], 'Medusa'); + is($exec_history->[3]->{params}->[2], 1001); + is($exec_history->[4]->{params}->[0], $fid); + is($exec_history->[4]->{params}->[1], 'Chimera'); + is($exec_history->[4]->{params}->[2], 54500); + } +} + +done_testing(); -- 2.39.2