X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=blobdiff_plain;f=lib%2FNDWeb%2FStats.pm;fp=lib%2FNDWeb%2FStats.pm;h=61598e2f05eac35127068fd744da08879c5be2b3;hp=0000000000000000000000000000000000000000;hb=7ec7b824749bb077086f6eeeabe3be6ffad39c66;hpb=3dc80cedd5deba53a4082992de317fbc3acf805c 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; +} +