changeset 131:5145b7c61ae0

Re #68: Add "export army list" function * Add initial body of an export method (doesn't list costs, total or equipment)
author IBBoard <dev@ibboard.co.uk>
date Tue, 01 Sep 2009 16:06:39 +0000
parents e147a1e9399b
children e9c415839462
files api/Exporters/WarFoundryHtmlExporter.cs api/Objects/Stats.cs api/Objects/UnitType.cs
diffstat 3 files changed, 106 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/api/Exporters/WarFoundryHtmlExporter.cs	Fri Aug 28 19:57:10 2009 +0000
+++ b/api/Exporters/WarFoundryHtmlExporter.cs	Tue Sep 01 16:06:39 2009 +0000
@@ -3,6 +3,10 @@
 // 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.WarFoundry.API.Objects;
 
 namespace IBBoard.WarFoundry.API.Exporters
@@ -31,6 +35,92 @@
 		
 		public void ExportArmy(Army army, string path)
 		{
+			XmlDocument doc = new XmlDocument();
+			doc.AppendChild(doc.CreateDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null));
+			XmlElement html = doc.CreateElement("html");
+			doc.AppendChild(html);
+			XmlElement head = doc.CreateElement("head");
+			html.AppendChild(head);
+			XmlElement title = doc.CreateElement("title");
+			head.AppendChild(title);
+			title.InnerXml = army.Name;
+			XmlElement body = doc.CreateElement("body");
+			html.AppendChild(body);
+			XmlElement header = doc.CreateElement("h1");
+			header.InnerText = army.Name;
+			body.AppendChild(header);
+			
+			foreach (XmlElement table in CreateTables(army, doc))
+			{
+				body.AppendChild(table);
+			}
+			
+			StreamWriter writer = new StreamWriter(path, false);
+			
+			try
+			{
+				writer.Write(doc.OuterXml);
+			}
+			finally
+			{
+				writer.Close();
+			}
+		}
+		
+		private XmlElement[] CreateTables(Army army, XmlDocument doc)
+		{
+			Dictionary<string, XmlElement> tables = new Dictionary<string, XmlElement>();
+			
+			foreach (SystemStats statSets in army.GameSystem.SystemStats)
+			{
+				tables[statSets.ID] = CreateTable(statSets, doc);
+			}
+			
+			foreach (Unit unit in army.GetUnits())
+			{
+				tables[unit.UnitType.StatsID].AppendChild(CreateUnitRow(unit, doc));
+			}
+			
+			return DictionaryUtils.ToArray(tables);
+		}
+		
+		private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
+		{
+			XmlElement table = doc.CreateElement("table");
+			XmlElement headerRow = doc.CreateElement("tr");
+			table.AppendChild(headerRow);
+			XmlElement name = doc.CreateElement("th");
+			name.InnerText = "Name";
+			headerRow.AppendChild(name);
+			
+			foreach (StatSlot stat in stats.StatSlots)
+			{
+				XmlElement statHeader = doc.CreateElement("th");
+				statHeader.InnerText = stat.Name;
+				headerRow.AppendChild(statHeader);
+			}
+			
+			XmlElement notes = doc.CreateElement("th");
+			notes.InnerText = "Notes";
+			headerRow.AppendChild(notes);
+			return table;
+		}
+		
+		private XmlElement CreateUnitRow(Unit unit, XmlDocument doc)
+		{
+			XmlElement row = doc.CreateElement("tr");
+			XmlElement name = doc.CreateElement("td");
+			name.InnerText = unit.Name;
+			row.AppendChild(name);
+			
+			foreach (Stat stat in unit.UnitStatsArray)
+			{
+				XmlElement statCell = doc.CreateElement("td");
+				statCell.InnerText = stat.SlotValueString;
+				row.AppendChild(statCell);
+			}
+			
+			return row;
 		}
 	}
 }
--- a/api/Objects/Stats.cs	Fri Aug 28 19:57:10 2009 +0000
+++ b/api/Objects/Stats.cs	Tue Sep 01 16:06:39 2009 +0000
@@ -93,5 +93,13 @@
 		{
 			get { return stats.Count; }
 		}
+		
+		public string StatsID
+		{
+			get
+			{
+				return sysStats.ID;
+			}
+		}
 	}
 }
--- a/api/Objects/UnitType.cs	Fri Aug 28 19:57:10 2009 +0000
+++ b/api/Objects/UnitType.cs	Tue Sep 01 16:06:39 2009 +0000
@@ -432,5 +432,13 @@
 		{
 			return DictionaryUtils.GetValue(extraData, id);
 		}
+		
+		public string StatsID
+		{
+			get
+			{
+				return stats.StatsID;
+			}
+		}
 	}
 }
\ No newline at end of file