]> ruin.nu Git - ndwebbie.git/commitdiff
Add support for Military Scans
authorMichael Andreen <michael@andreen.dev>
Sat, 14 Jan 2023 21:05:29 +0000 (22:05 +0100)
committerMichael Andreen <michael@andreen.dev>
Sat, 14 Jan 2023 21:05:29 +0000 (22:05 +0100)
lib/NDWeb/Scans.pm [new file with mode: 0644]
scripts/scans.pl
t/milscan.html [new file with mode: 0644]
t/scans.t [new file with mode: 0644]

diff --git a/lib/NDWeb/Scans.pm b/lib/NDWeb/Scans.pm
new file mode 100644 (file)
index 0000000..bb05e99
--- /dev/null
@@ -0,0 +1,84 @@
+#**************************************************************************
+#   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;
+               }
+       }
+}
+
+
index b54cb54f81fbc83eb5efca351cb83e3ccc78ee92..525c969c9d42c8876200491b1813909415900438 100755 (executable)
@@ -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 (file)
index 0000000..686c6ee
--- /dev/null
@@ -0,0 +1,93 @@
+<!DOCTYPE html>
+<html>
+       <head>
+               <meta charset="iso-8859-1">
+               <title>Planetarion > Scan</title>
+               <link href="images/template53/planetarion.css?rn=r88.0001" rel="stylesheet" type="text/css">
+               <link rel="stylesheet" type="text/css" href="javascript/jquery/ui-1.10.3.css">
+               <link rel="shortcut icon" type="image/ico" href="images/favicon.ico">
+               <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
+               <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" integrity="sha256-wS9gmOZBqsqWxgIVgA8Y9WcQOa7PgSIX+rPA0VL2rbQ=" crossorigin="anonymous"></script>
+               <script>window.jQuery || document.write('<script src=\"javascript/jquery/jquery-1.9.1.js\"><\/script>')</script>
+               <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" integrity="sha256-lnH4vnCtlKU2LmD0ZW1dU7ohTTKrcKP50WA9fa350cE=" crossorigin="anonymous"></script>
+               <script>window.jQuery.ui || document.write('<script src=\"javascript/jquery/ui-1.10.3.js\"><\/script>')</script>
+               <script src="javascript/jq_0.3.4.js"></script>
+               <script src="javascript/global.js?r88.0001"></script>
+               <script src="javascript/bcalc_0.3.5.js"></script>
+               <script>
+                       (function(i,s,o,g,r,a,m) { i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+                       ga('create', 'UA-44044400-2', 'planetarion.com');
+                       ga('send', 'pageview');
+               </script>
+       </head>
+               <body class="linux chrome chrome108">
+                       <script>
+                       var PA = {
+                               "servertime": 1673108306000,
+                               "timediff": (1673108306000 - (new Date().getTime())),
+                               "ticking": 0,
+                               "tick_speed": 10,
+                               "last_tick": 3047,
+                               "round_start": 1395432000000,
+                               "round_end": 63072000000,
+                               "page": "scan",
+                               "mobile": 0
+                       };
+                       </script>
+                       <div id="load_box" style="display: none;"></div>
+                       <div id="page_scan" class="pages">
+<div id="top">
+       <a href="http://www.planetarion.com" id="logo"></a>
+       <div id="tnav">
+               <ul id="tnav_links">
+                       <li class="tnav_link"><a href="manual.pl" target="_blank"  id="bar_manual"><span>Manual</span></a></li>
+                       <li class="tnav_link"><a href="support.pl" target="_blank"  id="bar_support"><span>Support</span></a></li>
+                       <li class="tnav_link"><a href="http://www.planetarion.com/" target="_blank" id="bar_portal"><span>Portal</span></a></li>
+                       <li class="tnav_link"><a href="http://pirate.planetarion.com" target="_blank" id="bar_forums"><span>Forums</span></a></li>
+                       <li class="tnav_link"><a href="logout.pl" id="bar_logout"><span>Logout</span></a></li>
+               </ul>
+       </div>
+</div>
+
+<div id="full_page">
+       <div id="contents">
+               <div id="contents_header"></div>
+<div class="container" id="scan">
+       <div class="header">Scan Result <a href="#" class="show_hide" id="scan_showhide">[Hide]</a></div>
+       <div class="maintext">
+<p class="center"><a href="showscan.pl?scan_id=g79wu6yas8wt1ho" target="scan">Scan Link</a><br>[scan]g79wu6yas8wt1ho[/scan]</p>
+<p class="right scan_time">Scan time: Fri, 06 Jan 21:15:35</p>
+<p class="center"><a href="#" onclick="set_ships_cookie(event, 'scan', 'g79wu6yas8wt1ho', 'Centaur: 199900\nChimera: 54500\nTitan: 50000\nWyvern: 3000\nMedusa: 1001\n');">Add To Bcalc</a><span class="bcalc_links" id="bcalc_link_g79wu6yas8wt1ho"></span><br><span class="bcalc_target" id="bcalc_target_g79wu6yas8wt1ho"></span></p>
+<h2 class="center header">Military Scan on 2:2:2 in tick 3046</h2>
+<table>
+       <tr><th class="left">Ship</th><th class="center">Base</th><th class="center">Fleet 1</th><th class="center">Fleet 2</th><th class="center">Fleet 3</th></tr>
+       <tr><td class="left">Harpy</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Centaur</td><td class="center">199,900</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Chimera</td><td class="center">0</td><td class="center">54,500</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Pegasus</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Drake</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Syren</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Titan</td><td class="center">50,000</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Wyvern</td><td class="center">3,000</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Medusa</td><td class="center">1,001</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Griffin</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Demeter</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Behemoth</td><td class="center">0</td><td class="center">0</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Total Visible Ships</td><td class="center">253,901</td><td class="center">54,500</td><td class="center">0</td><td class="center">0</td></tr>
+       <tr><td class="left">Total Ships</td><td class="center">253,901</td><td class="center">54,500</td><td class="center">0</td><td class="center">0</td></tr>
+</table>
+<hr><br>
+       </div>
+       <div class="footer"></div>
+</div>
+<div id="side_select_box" style="display: none;">
+<p class="right"><a href="#" title="close" onclick="hide(event, 'side_select_box')">X</a>&nbsp;&nbsp;</p>
+<p class="center" id="side_select"></p>
+</div>
+               <div id="contents_footer"></div>
+       </div>
+       <div id="footer"></div>
+</div>
+                       </div>
+       </body>
+</html>
diff --git a/t/scans.t b/t/scans.t
new file mode 100644 (file)
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();