From 7ec7b824749bb077086f6eeeabe3be6ffad39c66 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 10 Jan 2023 23:22:53 +0100 Subject: [PATCH] Stat updates for r100 --- database/r100.sql | 3 + dependencies | 1 + lib/NDWeb/Stats.pm | 61 ++ scripts/stats.pl | 31 +- t/stats.t | 66 ++ t/stats.xml | 1538 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1688 insertions(+), 12 deletions(-) create mode 100644 lib/NDWeb/Stats.pm create mode 100644 t/stats.t create mode 100644 t/stats.xml diff --git a/database/r100.sql b/database/r100.sql index b5f644c..debbc44 100644 --- a/database/r100.sql +++ b/database/r100.sql @@ -29,9 +29,12 @@ DROP FUNCTION covop_alert(integer, integer, integer, integer, govs, integer); \i functions/find_planet_id.sql \i functions/find_planet_id.sql ALTER TABLE ship_stats ADD FOREIGN KEY (race) REFERENCES races(race) ON UPDATE CASCADE ON DELETE CASCADE; +ALTER TABLE ship_stats ADD COLUMN cloaked BOOLEAN NOT NULL DEFAULT false, ADD COLUMN baseeta INTEGER NOT NULL DEFAULT 0; +ALTER TABLE ship_stats ALTER COLUMN cloaked DROP DEFAULT, ALTER COLUMN baseeta DROP DEFAULT, ALTER COLUMN guns DROP DEFAULT, ALTER COLUMN eres DROP DEFAULT; \d ship_stats DROP TYPE race; DROP TYPE govs; + \i functions/hostile_alliances.sql diff --git a/dependencies b/dependencies index dd82421..57cf3d3 100644 --- a/dependencies +++ b/dependencies @@ -18,6 +18,7 @@ - Email::Simple - Email::StripMIME - Template::Plugin::JSON +- XML::LibXML Bot deps: diff --git a/lib/NDWeb/Stats.pm b/lib/NDWeb/Stats.pm new file mode 100644 index 0000000..61598e2 --- /dev/null +++ b/lib/NDWeb/Stats.pm @@ -0,0 +1,61 @@ +#************************************************************************** +# 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::Stats; +use strict; +use warnings; +require Exporter; +use XML::LibXML; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/parseStats/; + +my %classes = (Fighter => 'Fi', Corvette => 'Co', Frigate => 'Fr', Destroyer => 'De', Cruiser => 'Cr', Battleship => 'Bs', Structure => 'St', Roids => 'Ro', Resources => 'Re', '-' => '-'); + +sub parseStats ($) { + my ($xml) = @_; + my @ships; + + my $parser = XML::LibXML->new(); + my $xmldoc = $parser->parse_string($xml); + + for my $sample ( $xmldoc->findnodes('/stats/ship') ) { + my %ship; + for my $child ( $sample->getChildnodes ) { + if ( $child->nodeType() == XML_ELEMENT_NODE ) { + my $nodeName = $child->nodeName(); + $nodeName = 'ship' if $nodeName eq 'name'; + $nodeName = 'init' if $nodeName eq 'initiative'; + $nodeName = 'eres' if $nodeName eq 'empres'; + my $text = $child->textContent(); + $text = $classes{$text} if $nodeName eq 'class'; + $text = 0 if $text eq '-' && $nodeName eq 'damage'; + if ($nodeName =~ /^target(\d+)$/) { + $nodeName = "t$1"; + $text = $classes{$text}; + } + $ship{$nodeName} = $text; + } + } + push @ships, \%ship; + } + return @ships; +} + diff --git a/scripts/stats.pl b/scripts/stats.pl index d69d42c..ee12e93 100755 --- a/scripts/stats.pl +++ b/scripts/stats.pl @@ -24,6 +24,7 @@ use strict; use warnings; use DBI; use DBD::Pg qw(:pg_types); +use Data::Dumper; use LWP::Simple qw/get $ua/; $ua->agent("Stupid user agent check is stupid"); @@ -32,22 +33,28 @@ use FindBin; use lib "$FindBin::Bin/../lib"; use ND::DB; +use NDWeb::Stats; our $dbh = ND::DB::DB(); -my %classes = (Fighter => 'Fi', Corvette => 'Co', Frigate => 'Fr', Destroyer => 'De', Cruiser => 'Cr', Battleship => 'Bs'); - -my $file = get("http://game.planetarion.com/manual.pl?page=stats"); +my $file = get("http://game.planetarion.com/manual.pl?action=statsxml"); $dbh->begin_work; -my $st = $dbh->prepare(q{INSERT INTO ship_stats (ship,"class",t1,t2,t3,"type",init,guns,armor,damage,eres,metal,crystal,eonium,race) VALUES(?,?,NULLIF(?,'-'),NULLIF(?,'-'),NULLIF(?,'-'),?,?,?,?,?,?,?,?,?,?)}); -while ($file =~ /class="(\w+)">((?:\w| )+)<\/td>(\w+)<\/td>(\w+|-)<\/td>(\w+|-)<\/td>(\w+|-)<\/td>(\w+)\D+(\d+)\D+(\d+)\D+(\d+)\D+?(\d+|-)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+\d+\D+\d+.+?(\w+)<\/td>/g){ - my $dmg = $11; - $dmg = 0 if $dmg eq '-'; - my $class = $classes{$3}; - my $type = $7; - $type = 'Emp' if $type eq 'EMP'; - $st->execute($2,$class,$4,$5,$6,$7,$8,$9,$10,$dmg,$12,$13,$14,$15,$1) or die $dbh->errstr; - print "$2,$class,$4,$5,$6,$7,$8,$9,$10,$dmg,$12,$13,$14,$15,$1\n"; +my $st = $dbh->prepare(q{ +INSERT INTO ship_stats + (ship,"class",t1,t2,t3,"type",init,guns,armor,damage,eres,metal,crystal,eonium,race, cloaked, baseeta) + VALUES(?,?,NULLIF(?,'-'),NULLIF(?,'-'),NULLIF(?,'-'),?,?,?,?,?,?,?,?,?,(SELECT race FROM races WHERE race_name = ?), ?, ?)}); + +my @columns = qw/ship class t1 t2 t3 type init guns armor damage eres metal crystal eonium race cloaked baseeta/; + +my @ships = parseStats($file); +for my $ship (@ships) { + my @data; + for my $c (@columns) { + push @data, $ship->{$c}; + } + #print Dumper(\@data), "\n"; + print "@data\n"; + $st->execute(@data) or die $dbh->errstr; } diff --git a/t/stats.t b/t/stats.t new file mode 100644 index 0000000..952daca --- /dev/null +++ b/t/stats.t @@ -0,0 +1,66 @@ +use strict; +use warnings; +use Test2::V0; + +use Data::Dumper; +use FindBin; + +use NDWeb::Stats; + +my $xml = do { + open my $in, '<', "$FindBin::Bin/stats.xml" or die "Can't read file: $!"; + local $/; + <$in> +}; +my @ships = parseStats($xml); + +#print Dumper(\@ships), "\n"; + +is ($ships[0]->{ship}, 'Harpy'); +is ($ships[0]->{race}, 'Terran'); +is ($ships[0]->{class}, 'Fi'); +is ($ships[0]->{type}, 'Normal'); +is ($ships[0]->{baseeta}, 12); +is ($ships[0]->{init}, 11); +is ($ships[0]->{damage}, 14); +is ($ships[8]->{class}, 'Fr'); +is ($ships[8]->{type}, 'Pod'); +is ($ships[8]->{t1}, 'Ro'); +is ($ships[8]->{cloaked}, 0); +is ($ships[11]->{class}, 'De'); +is ($ships[11]->{type}, 'Structure'); +is ($ships[11]->{t1}, 'St'); +is ($ships[16]->{ship}, 'Black Widow'); +is ($ships[16]->{race}, 'Cathaar'); +is ($ships[16]->{class}, 'De'); +is ($ships[16]->{type}, 'EMP'); +is ($ships[16]->{t1}, 'Cr'); +is ($ships[16]->{t2}, '-'); +is ($ships[16]->{baseeta}, 13); +is ($ships[16]->{init}, 2); +is ($ships[16]->{cloaked}, 0); +is ($ships[16]->{damage}, 0); +is ($ships[15]->{t2}, 'Bs'); +is ($ships[22]->{class}, 'Cr'); +is ($ships[22]->{type}, 'Pod'); +is ($ships[22]->{t1}, 'Ro'); +is ($ships[55]->{ship}, 'Gas giant'); +is ($ships[55]->{race}, 'Kinthia'); +is ($ships[55]->{class}, 'Bs'); +is ($ships[55]->{type}, 'EMP'); +is ($ships[55]->{t1}, 'Cr'); +is ($ships[55]->{t2}, '-'); +is ($ships[55]->{baseeta}, 14); +is ($ships[55]->{init}, 1); +is ($ships[55]->{cloaked}, 1); +is ($ships[55]->{damage}, 0); +is ($ships[69]->{ship}, 'Ram'); +is ($ships[69]->{race}, 'Slythonian'); +is ($ships[69]->{class}, 'Co'); +is ($ships[69]->{type}, 'Resources'); +is ($ships[69]->{t1}, 'Re'); +is ($ships[69]->{t2}, '-'); +is ($ships[69]->{baseeta}, 12); +is ($ships[69]->{init}, 40); + +done_testing(); diff --git a/t/stats.xml b/t/stats.xml new file mode 100644 index 0000000..9ba042a --- /dev/null +++ b/t/stats.xml @@ -0,0 +1,1538 @@ + + + + + + + + + + + + + + + + + + + + + + +]> + + + Harpy + Terran + Fighter + Destroyer + Corvette + - + Normal + 0 + 11 + 1 + 15 + 14 + 12 + 113 + 103 + 103 + 470 + 438 + 12 + + + Centaur + Terran + Frigate + Corvette + - + - + Normal + 0 + 5 + 1 + 78 + 64 + 83 + 602 + 551 + 551 + 457 + 375 + 12 + + + Chimera + Terran + Frigate + Battleship + - + - + Normal + 0 + 9 + 1 + 89 + 71 + 84 + 625 + 572 + 572 + 503 + 401 + 13 + + + Pegasus + Terran + Destroyer + Fighter + - + - + Normal + 0 + 6 + 1 + 106 + 77 + 82 + 805 + 737 + 737 + 465 + 337 + 13 + + + Drake + Terran + Destroyer + Cruiser + - + - + Normal + 0 + 9 + 1 + 127 + 98 + 83 + 848 + 776 + 776 + 529 + 408 + 13 + + + Syren + Terran + Cruiser + Destroyer + - + - + Normal + 0 + 7 + 1 + 193 + 138 + 92 + 1420 + 1300 + 1300 + 480 + 343 + 13 + + + Titan + Terran + Cruiser + Battleship + - + - + Normal + 0 + 8 + 1 + 189 + 137 + 92 + 1420 + 1300 + 1300 + 470 + 340 + 14 + + + Wyvern + Terran + Battleship + Frigate + - + - + Normal + 0 + 10 + 1 + 287 + 216 + 92 + 1890 + 1730 + 1730 + 536 + 403 + 14 + + + Medusa + Terran + Frigate + Roids + - + - + Pod + 0 + 30 + 1 + 69 + 55 + 80 + 475 + 475 + 475 + 484 + 385 + 13 + + + Demeter + Terran + Destroyer + Roids + - + - + Pod + 0 + 30 + 1 + 106 + 85 + 81 + 730 + 730 + 730 + 484 + 388 + 13 + + + Griffin + Terran + Frigate + Resources + - + - + Resources + 0 + 40 + 1 + 58 + 47 + 79 + 475 + 475 + 475 + 407 + 329 + 13 + + + Behemoth + Terran + Destroyer + Structure + - + - + Structure + 0 + 40 + 1 + 75 + 47 + 80 + 750 + 750 + 750 + 333 + 208 + 13 + + + Moth + Cathaar + Fighter + Destroyer + - + - + EMP + 0 + 2 + 1 + 13 + - + 4 + 97 + 106 + 97 + 433 + 0 + 12 + + + Spider + Cathaar + Fighter + Corvette + - + - + EMP + 0 + 1 + 1 + 15 + - + 11 + 106 + 116 + 106 + 457 + 0 + 12 + + + Beetle + Cathaar + Corvette + Fighter + Frigate + - + EMP + 0 + 3 + 4 + 36 + - + 46 + 274 + 299 + 274 + 425 + 0 + 12 + + + Scarab + Cathaar + Frigate + Corvette + Battleship + - + EMP + 0 + 3 + 5 + 70 + - + 82 + 528 + 577 + 528 + 428 + 0 + 13 + + + Black Widow + Cathaar + Destroyer + Cruiser + - + - + EMP + 0 + 2 + 10 + 108 + - + 82 + 765 + 836 + 765 + 456 + 0 + 13 + + + Roach + Cathaar + Cruiser + Destroyer + - + - + EMP + 0 + 2 + 11 + 146 + - + 90 + 1088 + 1188 + 1088 + 434 + 0 + 14 + + + Tarantula + Cathaar + Cruiser + Battleship + - + - + EMP + 0 + 1 + 11 + 153 + - + 91 + 1138 + 1244 + 1138 + 434 + 0 + 14 + + + Scorpion + Cathaar + Battleship + Frigate + - + - + Normal + 0 + 6 + 1 + 207 + 154 + 90 + 1407 + 1537 + 1407 + 475 + 353 + 13 + + + Wasp + Cathaar + Fighter + Roids + - + - + Pod + 0 + 30 + 1 + 14 + 13 + 8 + 105 + 105 + 105 + 444 + 412 + 12 + + + Ant + Cathaar + Fighter + Resources + - + - + Resources + 0 + 40 + 1 + 12 + 10 + 4 + 103 + 103 + 103 + 388 + 323 + 12 + + + Hornet + Cathaar + Cruiser + Roids + - + - + Pod + 0 + 30 + 1 + 145 + 133 + 90 + 1085 + 1085 + 1085 + 445 + 408 + 14 + + + Termite + Cathaar + Cruiser + Structure + - + - + Structure + 0 + 40 + 1 + 98 + 65 + 89 + 1050 + 1050 + 1050 + 311 + 206 + 14 + + + Phantom + Xandathrii + Fighter + Corvette + - + - + Normal + 1 + 4 + 1 + 11 + 12 + 1 + 100 + 100 + 100 + 366 + 400 + 12 + + + Banshee + Xandathrii + Fighter + Destroyer + - + - + Normal + 1 + 7 + 1 + 15 + 12 + 6 + 105 + 105 + 105 + 476 + 380 + 12 + + + Apparition + Xandathrii + Corvette + Frigate + - + - + Normal + 1 + 9 + 1 + 36 + 33 + 39 + 250 + 250 + 250 + 480 + 440 + 12 + + + Shadow + Xandathrii + Frigate + Battleship + - + - + Normal + 1 + 11 + 1 + 74 + 73 + 82 + 550 + 550 + 550 + 448 + 442 + 13 + + + Reaper + Xandathrii + Destroyer + Cruiser + - + - + Normal + 1 + 4 + 1 + 89 + 85 + 81 + 741 + 741 + 741 + 400 + 382 + 13 + + + Spirit + Xandathrii + Destroyer + Fighter + - + - + Normal + 1 + 10 + 1 + 104 + 98 + 82 + 780 + 780 + 780 + 444 + 418 + 13 + + + Nightmare + Xandathrii + Cruiser + Destroyer + - + - + Normal + 1 + 7 + 1 + 157 + 144 + 91 + 1230 + 1230 + 1230 + 425 + 390 + 14 + + + Wraith + Xandathrii + Battleship + Cruiser + Frigate + - + Normal + 1 + 12 + 1 + 210 + 183 + 89 + 1350 + 1350 + 1350 + 518 + 451 + 13 + + + Illusion + Xandathrii + Fighter + Roids + - + - + Pod + 0 + 30 + 1 + 13 + 12 + 10 + 110 + 110 + 110 + 393 + 363 + 12 + + + Vampyre + Xandathrii + Destroyer + Roids + - + - + Pod + 0 + 30 + 1 + 90 + 89 + 81 + 740 + 740 + 740 + 405 + 400 + 13 + + + Poltergeist + Xandathrii + Fighter + Resources + - + - + Resources + 0 + 40 + 1 + 12 + 10 + 6 + 107 + 107 + 107 + 373 + 311 + 12 + + + Haunt + Xandathrii + Destroyer + Structure + - + - + Structure + 0 + 40 + 1 + 70 + 46 + 80 + 750 + 750 + 750 + 311 + 204 + 13 + + + Interceptor + Zikonian + Fighter + Destroyer + - + - + Normal + 0 + 9 + 1 + 15 + 11 + 8 + 112 + 102 + 102 + 474 + 348 + 12 + + + Cutlass + Zikonian + Corvette + Fighter + Frigate + - + Steal + 0 + 17 + 1 + 42 + 34 + 43 + 254 + 254 + 277 + 535 + 433 + 12 + + + Cutter + Zikonian + Frigate + Battleship + - + - + Normal + 0 + 4 + 1 + 75 + 69 + 84 + 632 + 579 + 579 + 418 + 385 + 13 + + + Thief + Zikonian + Frigate + Corvette + - + - + Steal + 0 + 16 + 1 + 96 + 74 + 84 + 582 + 582 + 636 + 533 + 411 + 13 + + + Clipper + Zikonian + Destroyer + Fighter + - + - + Normal + 0 + 6 + 1 + 89 + 77 + 81 + 780 + 715 + 715 + 402 + 348 + 12 + + + Marauder + Zikonian + Cruiser + Battleship + - + - + Steal + 0 + 18 + 1 + 170 + 148 + 90 + 1078 + 1078 + 1178 + 509 + 443 + 14 + + + Brigand + Zikonian + Battleship + Cruiser + - + - + Steal + 0 + 13 + 1 + 239 + 206 + 91 + 1574 + 1574 + 1721 + 490 + 423 + 14 + + + Pirate + Zikonian + Battleship + Frigate + - + - + Normal + 0 + 6 + 1 + 213 + 197 + 91 + 1735 + 1587 + 1587 + 433 + 401 + 14 + + + Privateer + Zikonian + Frigate + Roids + - + - + Pod + 0 + 30 + 1 + 70 + 70 + 83 + 560 + 560 + 560 + 416 + 416 + 13 + + + Rover + Zikonian + Frigate + Resources + - + - + Resources + 0 + 40 + 1 + 71 + 56 + 83 + 583 + 583 + 583 + 405 + 320 + 13 + + + Pillager + Zikonian + Battleship + Roids + - + - + Pod + 0 + 30 + 1 + 213 + 198 + 91 + 1650 + 1650 + 1650 + 430 + 400 + 14 + + + Galleon + Zikonian + Battleship + Structure + - + - + Structure + 0 + 40 + 1 + 147 + 96 + 90 + 1550 + 1550 + 1550 + 316 + 206 + 14 + + + Firefly + Kinthia + Fighter + Corvette + - + - + EMP + 1 + 2 + 1 + 15 + - + 20 + 117 + 128 + 117 + 414 + 0 + 12 + + + Candle + Kinthia + Corvette + Fighter + - + - + EMP + 1 + 1 + 4 + 37 + - + 51 + 300 + 329 + 300 + 398 + 0 + 12 + + + Torch + Kinthia + Corvette + Frigate + - + - + EMP + 1 + 2 + 4 + 39 + - + 51 + 300 + 329 + 300 + 419 + 0 + 12 + + + Blacklight + Kinthia + Frigate + Battleship + - + - + EMP + 1 + 2 + 5 + 76 + - + 84 + 580 + 633 + 580 + 423 + 0 + 13 + + + Laser + Kinthia + Destroyer + Fighter + Cruiser + - + EMP + 1 + 3 + 10 + 103 + - + 82 + 758 + 829 + 758 + 439 + 0 + 13 + + + Moonlight + Kinthia + Cruiser + Destroyer + Battleship + - + EMP + 1 + 3 + 9 + 130 + - + 89 + 978 + 1069 + 978 + 429 + 0 + 14 + + + Red dwarf + Kinthia + Battleship + Frigate + - + - + EMP + 1 + 2 + 20 + 192 + - + 90 + 1456 + 1591 + 1456 + 426 + 0 + 14 + + + Gas giant + Kinthia + Battleship + Cruiser + - + - + EMP + 1 + 1 + 19 + 210 + - + 91 + 1604 + 1752 + 1604 + 423 + 0 + 14 + + + Ember + Kinthia + Corvette + Roids + - + - + Pod + 0 + 30 + 1 + 37 + 35 + 49 + 300 + 300 + 300 + 411 + 388 + 12 + + + Spark + Kinthia + Corvette + Resources + - + - + Resources + 0 + 40 + 1 + 37 + 29 + 50 + 310 + 310 + 310 + 397 + 311 + 12 + + + Pulsar + Kinthia + Battleship + Roids + - + - + Pod + 0 + 30 + 1 + 196 + 178 + 90 + 1500 + 1500 + 1500 + 435 + 395 + 14 + + + Supernova + Kinthia + Battleship + Structure + - + - + Structure + 0 + 40 + 1 + 143 + 96 + 89 + 1550 + 1550 + 1550 + 307 + 206 + 14 + + + Mace + Slythonian + Fighter + Corvette + Destroyer + - + Steal + 1 + 17 + 1 + 19 + 16 + 21 + 118 + 118 + 129 + 520 + 438 + 12 + + + Spear + Slythonian + Corvette + Fighter + - + - + Steal + 1 + 15 + 1 + 37 + 36 + 47 + 277 + 277 + 303 + 431 + 420 + 12 + + + Pike + Slythonian + Corvette + Frigate + - + - + Steal + 1 + 15 + 1 + 38 + 38 + 47 + 277 + 277 + 303 + 443 + 443 + 12 + + + Arbalest + Slythonian + Frigate + Corvette + - + - + Steal + 1 + 14 + 1 + 89 + 71 + 84 + 588 + 588 + 643 + 489 + 390 + 13 + + + Broadsword + Slythonian + Destroyer + Cruiser + Fighter + - + Steal + 1 + 18 + 1 + 128 + 99 + 81 + 714 + 714 + 780 + 579 + 448 + 13 + + + Lance + Slythonian + Cruiser + Destroyer + - + - + Steal + 1 + 13 + 1 + 171 + 148 + 90 + 1062 + 1062 + 1161 + 520 + 450 + 13 + + + Halberd + Slythonian + Cruiser + Battleship + - + - + Steal + 1 + 14 + 1 + 173 + 142 + 90 + 1062 + 1062 + 1161 + 526 + 432 + 14 + + + Trebuchet + Slythonian + Battleship + Cruiser + - + - + Steal + 1 + 17 + 1 + 256 + 198 + 90 + 1443 + 1443 + 1577 + 573 + 443 + 14 + + + Warhammer + Slythonian + Corvette + Roids + - + - + Pod + 0 + 30 + 1 + 36 + 34 + 49 + 300 + 300 + 300 + 400 + 377 + 12 + + + Ram + Slythonian + Corvette + Resources + - + - + Resources + 0 + 40 + 1 + 35 + 27 + 47 + 290 + 290 + 290 + 402 + 310 + 12 + + + Glaive + Slythonian + Cruiser + Roids + - + - + Pod + 0 + 30 + 1 + 125 + 114 + 89 + 1007 + 1007 + 1007 + 413 + 377 + 14 + + + Siegetower + Slythonian + Cruiser + Structure + - + - + Structure + 0 + 40 + 1 + 97 + 65 + 89 + 1050 + 1050 + 1050 + 307 + 206 + 14 + + -- 2.39.2