From c9c885f1465519d76dc5dd83500f8c538494faf8 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 15 Jan 2023 13:28:59 +0100 Subject: [PATCH] Make Planet Scan parsing testable --- lib/NDWeb/Scans.pm | 30 +++++++++++++- scripts/scans.pl | 22 +---------- t/planetscan.html | 97 ++++++++++++++++++++++++++++++++++++++++++++++ t/scans.t | 40 +++++++++++++++++++ 4 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 t/planetscan.html diff --git a/lib/NDWeb/Scans.pm b/lib/NDWeb/Scans.pm index bb05e99..cfec2fd 100644 --- a/lib/NDWeb/Scans.pm +++ b/lib/NDWeb/Scans.pm @@ -24,7 +24,7 @@ require Exporter; our @ISA = qw/Exporter/; -our @EXPORT = qw/parseMilScan doMilScan/; +our @EXPORT = qw/parseMilScan doMilScan doPlanetScan/; my %classes = (Fighter => 'Fi', Corvette => 'Co', Frigate => 'Fr', Destroyer => 'De', Cruiser => 'Cr', Battleship => 'Bs', Structure => 'St', Roids => 'Ro', Resources => 'Re', '-' => '-'); @@ -81,4 +81,32 @@ sub doMilScan ($$$) { } } +my $addplanetscan_sql = q{INSERT INTO planet_scans + (id,tick,pid,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium + ,agents,guards,light,medium,heavy,hidden) + VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}; + +sub doPlanetScan ($$$) { + my ($dbh, $scan,$file) = @_; + + my $addplanetscan = $dbh->prepare_cached($addplanetscan_sql); + + my @values = ($scan->{id},$scan->{tick},$scan->{pid}); + $file =~ s/(\d),(\d)/$1$2/g; + + while($file =~ m{"center">(Metal|Crystal|Eonium)\D+(\d+)\D+([\d,]+)}g){ + push @values,$2,$3; + } + if($file =~ m{Security\ Guards .+? "center">(\d+) + .+? "center">(\d+)}sx){ + push @values,$1,$2; + } + if($file =~ m{([A-Z][a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)}){ + push @values,$1,$2,$3; + } + if($file =~ m{([\d,]+)}){ + push @values,$1; + } + $addplanetscan->execute(@values); +} diff --git a/scripts/scans.pl b/scripts/scans.pl index 525c969..200e33f 100755 --- a/scripts/scans.pl +++ b/scripts/scans.pl @@ -85,31 +85,11 @@ my $addintel = $dbh->prepare(q{INSERT INTO intel (name,mission,sender,target,tic VALUES(?,?,?,?,?,?,?,?,?,-1) RETURNING id}); my $intelscan = $dbh->prepare(q{INSERT INTO intel_scans (intel,id) VALUES(?,?)}); my $addships = $dbh->prepare(q{INSERT INTO fleet_ships (fid,ship,amount) VALUES(?,?,?)}); -my $addplanetscan = $dbh->prepare(q{INSERT INTO planet_scans - (id,tick,pid,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium - ,agents,guards,light,medium,heavy,hidden) - VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}); sub parse_planet { my ($scan,$file) = @_; - my @values = ($scan->{id},$scan->{tick},$scan->{pid}); - $file =~ s/(\d),(\d)/$1$2/g; - - while($file =~ m{"center">(Metal|Crystal|Eonium)\D+(\d+)\D+([\d,]+)}g){ - push @values,$2,$3; - } - if($file =~ m{Security\ Guards .+? "center">(\d+) - .+? "center">(\d+)}sx){ - push @values,$1,$2; - } - if($file =~ m{([A-Z][a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)}){ - push @values,$1,$2,$3; - } - if($file =~ m{([\d,]+)}){ - push @values,$1; - } - $addplanetscan->execute(@values); + doPlanetScan($dbh, $scan, $file); } sub parse_incoming { diff --git a/t/planetscan.html b/t/planetscan.html new file mode 100644 index 0000000..396fa2f --- /dev/null +++ b/t/planetscan.html @@ -0,0 +1,97 @@ + + + + + Planetarion > Scan + + + + + + + + + + + + + + + + +
+
+ +
+ +
+
+ +
+
+
+
+
Scan Result [Hide]
+
+

Scan Link
[scan]aaxq2l9pspfhb88[/scan]

+

Scan time: Sat, 14 Jan 19:32:27

+

Add To Bcalc

+

Planet Scan on 4:4:7 in tick 24

+ + + + +
Sandwich Full of Billy Roll (Ter)
ValueScore
149,656160,936
+ + + +
AgentsSecurity Guards
100805
+ + + + + + + + + +
TypeAsteroidsResources
Metal10078,363
Crystal130147,874
Eonium100169,077
Factory Usage
LightMediumHeavy
NoneHighNone
+

Total Amount of Resources in Production: 4,612,500
+Total Amount of Resources From Ships Being Sold: 0

+

+
+ +
+ + +
+ +
+
+ + diff --git a/t/scans.t b/t/scans.t index df404dd..37647d9 100644 --- a/t/scans.t +++ b/t/scans.t @@ -106,6 +106,46 @@ my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) is($exec_history->[4]->{params}->[1], 'Chimera'); is($exec_history->[4]->{params}->[2], 54500); } + +} + +my $planetscan = do { + open my $in, '<', "$FindBin::Bin/planetscan.html" or die "Can't read file: $!"; + local $/; + <$in> +}; + +$dbh->{mock_clear_history} = 1; +{ + my $scan = {id => 31337, type => 'Planet', pid => 1337, tick => 123}; + doPlanetScan($dbh, $scan, $planetscan); + my $history = $dbh->{mock_all_history}; + is(scalar(@{$history}), 1, 'Add planet scan'); + + my $sth = $history->[0]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{\QINSERT INTO planet_scans\E\s* + \(id,tick,pid,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium + \s*,agents,guards,light,medium,heavy,hidden\).*}xsi, + ); + is(scalar(@{$exec_history}), 1); + is(scalar(@{$exec_history->[0]->{params}}), 15); + is($exec_history->[0]->{params}->[0], $scan->{id}); + is($exec_history->[0]->{params}->[1], $scan->{tick}); + is($exec_history->[0]->{params}->[2], $scan->{pid}); + is($exec_history->[0]->{params}->[3], 100); + is($exec_history->[0]->{params}->[4], 78363); + is($exec_history->[0]->{params}->[5], 130); + is($exec_history->[0]->{params}->[6], 147874); + is($exec_history->[0]->{params}->[7], 100); + is($exec_history->[0]->{params}->[8], 169077); + is($exec_history->[0]->{params}->[9], 100); + is($exec_history->[0]->{params}->[10], 805); + is($exec_history->[0]->{params}->[11], 'None'); + is($exec_history->[0]->{params}->[12], 'High'); + is($exec_history->[0]->{params}->[13], 'None'); + is($exec_history->[0]->{params}->[14], '4612500'); } done_testing(); -- 2.39.2