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 diff
     1.1 --- a/api/Exporters/WarFoundryHtmlExporter.cs	Fri Aug 28 19:57:10 2009 +0000
     1.2 +++ b/api/Exporters/WarFoundryHtmlExporter.cs	Tue Sep 01 16:06:39 2009 +0000
     1.3 @@ -3,6 +3,10 @@
     1.4  // 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.
     1.5  
     1.6  using System;
     1.7 +using System.Collections.Generic;
     1.8 +using System.IO;
     1.9 +using System.Xml;
    1.10 +using System.Xml.Schema;
    1.11  using IBBoard.WarFoundry.API.Objects;
    1.12  
    1.13  namespace IBBoard.WarFoundry.API.Exporters
    1.14 @@ -31,6 +35,92 @@
    1.15  		
    1.16  		public void ExportArmy(Army army, string path)
    1.17  		{
    1.18 +			XmlDocument doc = new XmlDocument();
    1.19 +			doc.AppendChild(doc.CreateDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null));
    1.20 +			XmlElement html = doc.CreateElement("html");
    1.21 +			doc.AppendChild(html);
    1.22 +			XmlElement head = doc.CreateElement("head");
    1.23 +			html.AppendChild(head);
    1.24 +			XmlElement title = doc.CreateElement("title");
    1.25 +			head.AppendChild(title);
    1.26 +			title.InnerXml = army.Name;
    1.27 +			XmlElement body = doc.CreateElement("body");
    1.28 +			html.AppendChild(body);
    1.29 +			XmlElement header = doc.CreateElement("h1");
    1.30 +			header.InnerText = army.Name;
    1.31 +			body.AppendChild(header);
    1.32 +			
    1.33 +			foreach (XmlElement table in CreateTables(army, doc))
    1.34 +			{
    1.35 +				body.AppendChild(table);
    1.36 +			}
    1.37 +			
    1.38 +			StreamWriter writer = new StreamWriter(path, false);
    1.39 +			
    1.40 +			try
    1.41 +			{
    1.42 +				writer.Write(doc.OuterXml);
    1.43 +			}
    1.44 +			finally
    1.45 +			{
    1.46 +				writer.Close();
    1.47 +			}
    1.48 +		}
    1.49 +		
    1.50 +		private XmlElement[] CreateTables(Army army, XmlDocument doc)
    1.51 +		{
    1.52 +			Dictionary<string, XmlElement> tables = new Dictionary<string, XmlElement>();
    1.53 +			
    1.54 +			foreach (SystemStats statSets in army.GameSystem.SystemStats)
    1.55 +			{
    1.56 +				tables[statSets.ID] = CreateTable(statSets, doc);
    1.57 +			}
    1.58 +			
    1.59 +			foreach (Unit unit in army.GetUnits())
    1.60 +			{
    1.61 +				tables[unit.UnitType.StatsID].AppendChild(CreateUnitRow(unit, doc));
    1.62 +			}
    1.63 +			
    1.64 +			return DictionaryUtils.ToArray(tables);
    1.65 +		}
    1.66 +		
    1.67 +		private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
    1.68 +		{
    1.69 +			XmlElement table = doc.CreateElement("table");
    1.70 +			XmlElement headerRow = doc.CreateElement("tr");
    1.71 +			table.AppendChild(headerRow);
    1.72 +			XmlElement name = doc.CreateElement("th");
    1.73 +			name.InnerText = "Name";
    1.74 +			headerRow.AppendChild(name);
    1.75 +			
    1.76 +			foreach (StatSlot stat in stats.StatSlots)
    1.77 +			{
    1.78 +				XmlElement statHeader = doc.CreateElement("th");
    1.79 +				statHeader.InnerText = stat.Name;
    1.80 +				headerRow.AppendChild(statHeader);
    1.81 +			}
    1.82 +			
    1.83 +			XmlElement notes = doc.CreateElement("th");
    1.84 +			notes.InnerText = "Notes";
    1.85 +			headerRow.AppendChild(notes);
    1.86 +			return table;
    1.87 +		}
    1.88 +		
    1.89 +		private XmlElement CreateUnitRow(Unit unit, XmlDocument doc)
    1.90 +		{
    1.91 +			XmlElement row = doc.CreateElement("tr");
    1.92 +			XmlElement name = doc.CreateElement("td");
    1.93 +			name.InnerText = unit.Name;
    1.94 +			row.AppendChild(name);
    1.95 +			
    1.96 +			foreach (Stat stat in unit.UnitStatsArray)
    1.97 +			{
    1.98 +				XmlElement statCell = doc.CreateElement("td");
    1.99 +				statCell.InnerText = stat.SlotValueString;
   1.100 +				row.AppendChild(statCell);
   1.101 +			}
   1.102 +			
   1.103 +			return row;
   1.104  		}
   1.105  	}
   1.106  }
     2.1 --- a/api/Objects/Stats.cs	Fri Aug 28 19:57:10 2009 +0000
     2.2 +++ b/api/Objects/Stats.cs	Tue Sep 01 16:06:39 2009 +0000
     2.3 @@ -93,5 +93,13 @@
     2.4  		{
     2.5  			get { return stats.Count; }
     2.6  		}
     2.7 +		
     2.8 +		public string StatsID
     2.9 +		{
    2.10 +			get
    2.11 +			{
    2.12 +				return sysStats.ID;
    2.13 +			}
    2.14 +		}
    2.15  	}
    2.16  }
     3.1 --- a/api/Objects/UnitType.cs	Fri Aug 28 19:57:10 2009 +0000
     3.2 +++ b/api/Objects/UnitType.cs	Tue Sep 01 16:06:39 2009 +0000
     3.3 @@ -432,5 +432,13 @@
     3.4  		{
     3.5  			return DictionaryUtils.GetValue(extraData, id);
     3.6  		}
     3.7 +		
     3.8 +		public string StatsID
     3.9 +		{
    3.10 +			get
    3.11 +			{
    3.12 +				return stats.StatsID;
    3.13 +			}
    3.14 +		}
    3.15  	}
    3.16  }
    3.17 \ No newline at end of file