view API/Savers/Xml/WarFoundryXmlGameSystemSaver.cs @ 426:61fae5cbba02

Re #338: WarFoundry.API - Save System Data * Fix copy-and-paste errors to pass test
author IBBoard <dev@ibboard.co.uk>
date Fri, 28 Oct 2011 20:52:29 +0100
parents 5b3e3fb11de9
children
line wrap: on
line source

// This file (WarFoundryXmlGameSystemSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
//
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using IBBoard.Lang;
using IBBoard.Xml;
using IBBoard.WarFoundry.API.Factories.Xml.Zip;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Savers;
using IBBoard.WarFoundry.API.Util;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Savers.Xml
{
	public class WarFoundryXmlGameSystemSaver
	{
		public string CreateXmlString(GameSystem toSave)
		{
			XmlDocument doc = new XmlDocument();
			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
			doc.AppendChild(declaration);
			XmlSchema schema = new XmlSchema();
			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
			schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
			doc.Schemas.Add(schema);
			XmlElement root = doc.CreateElement("system");
			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
			root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
			doc.AppendChild(root);
			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
			root.SetAttribute("name", toSave.Name);
			root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
			root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
			root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
			root.SetAttribute("defaultPtsAbbreviationSingular", toSave.SystemPtsAbbrevSingle);
			root.SetAttribute("defaultPtsAbbreviationPlural", toSave.SystemPtsAbbrevPlural);
			root.SetAttribute("defaultPtsNameSingular", toSave.SystemPtsNameSingle);
			root.SetAttribute("defaultPtsNamePlural", toSave.SystemPtsNamePlural);
			XmlElement cats = doc.CreateElement("categories");
			root.AppendChild(cats);

			foreach (Category cat in toSave.Categories)
			{
				cats.AppendChild(CreateCategoryElement(cat, doc));
			}

			XmlElement sysStatsList = doc.CreateElement("sysStatsList");
			sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
			root.AppendChild(sysStatsList);

			foreach(SystemStats stats in toSave.SystemStats)
			{
				sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
			}

			return doc.OuterXml;
		}

		private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
		{
			XmlElement catElem = doc.CreateElement("cats:cat");
			catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
			catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
			if (cat.MinimumPoints > 0)
			{
				catElem.SetAttribute("minPoints", cat.MinimumPoints.ToString());
			}
			if (cat.MaximumPoints != WarFoundryCore.INFINITY)
			{
				catElem.SetAttribute("maxPoints", cat.MaximumPoints.ToString());
			}
			if(cat.MinimumPercentage > 0)
			{
				catElem.SetAttribute("minPercentage", cat.MinimumPercentage.ToString());
			}
			if(cat.MaximumPercentage < 100)
			{
				catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
			}

			return catElem;
		}

		private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
		{
			XmlElement statsElem = doc.CreateElement("sysStats");
			statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));

			foreach(StatSlot stat in stats.StatSlots)
			{
				statsElem.AppendChild(CreateSystemStatElement(stat, doc));
			}

			return statsElem;
		}

		private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
		{
			XmlElement statElem = doc.CreateElement("sysStat");
			statElem.SetAttribute("name", stat.Name);

			return statElem;
		}
	}
}