#**************************************************************************
#   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::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;
}

