diff API/DefaultWarFoundryLoader.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/DefaultWarFoundryLoader.cs	Sun Apr 03 18:50:32 2011 +0000
@@ -0,0 +1,304 @@
+// This file (DefaultWarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 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 IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API
+{
+	/// <summary>
+	/// The default implementation of a <see cref="AbstractWarFoundryLoader"/>
+	/// </summary>
+	public class DefaultWarFoundryLoader : AbstractWarFoundryLoader
+	{
+		private Dictionary<string, GameSystem> systemsTable;
+		private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace
+		private bool loaded = false;
+		private bool loading = false;
+
+		public DefaultWarFoundryLoader()
+		{
+			systemsTable = new Dictionary<string,GameSystem>();
+			racesTable = new Dictionary<string,Dictionary<string,Dictionary<string,Race>>>();
+		}
+
+		protected override void PrepareForFileLoad()
+		{
+			loading = true;
+			systemsTable.Clear();
+			racesTable.Clear();
+		}
+
+		protected override void FinishFileLoad()
+		{
+			loaded = true;
+			loading = false;
+		}
+
+		protected override GameSystem GetExistingSystemForSystem(GameSystem system)
+		{
+			return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower());
+		}
+
+		protected override void DoStoreGameSystem(GameSystem system)
+		{
+			systemsTable[system.ID.ToLower()] = system;
+		}
+
+		protected override void DoStoreRace(Race race)
+		{
+			Dictionary<string, Dictionary<string, Race>> systemRaces;
+			
+			string systemID = race.GameSystem.ID.ToLower();
+			racesTable.TryGetValue(systemID, out systemRaces);
+			
+			if (systemRaces == null)
+			{
+				systemRaces = new Dictionary<string,Dictionary<string,Race>>();
+				racesTable.Add(systemID, systemRaces);
+			}
+			
+			Dictionary<string, Race> subRaces;
+			systemRaces.TryGetValue(race.ID.ToLower(), out subRaces);
+			
+			if (subRaces == null)
+			{
+				subRaces = new Dictionary<string,Race>();
+				systemRaces.Add(race.ID.ToLower(), subRaces);
+			}
+			
+			string subID = race.SubID.ToLower();
+
+			if (subRaces.ContainsKey(subID))
+			{
+				Race existingRace = subRaces[subID];
+				
+				if (!race.Equals(existingRace))
+				{
+					//TODO: Raise an event to say we got a different duplicate
+					//We can't just fail, because failing is for completely unhandled files, not for objects in a file
+				}
+			}
+			else
+			{
+				subRaces.Add(race.SubID.ToLower(), race);
+			}
+		}
+
+		public override GameSystem[] GetGameSystems()
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+			
+			return DictionaryUtils.ToArray<string, GameSystem>(systemsTable);
+		}
+
+		public override GameSystem GetGameSystem(string systemID)
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+			
+			GameSystem system;
+			systemsTable.TryGetValue(systemID.ToLower(), out system);
+			return system;
+		}
+
+		protected internal override void RemoveGameSystem(GameSystem system)
+		{
+			systemsTable.Remove(system.ID.ToLower());
+		}
+
+		public override Race[] GetRaces(GameSystem system)
+		{
+			return GetRaces(system.ID);
+		}
+
+		/// <summary>
+		/// Gets an array of the races for a game system by ID.
+		/// </summary>
+		/// <param name="systemID">
+		/// The <see cref="System.String"/> ID of the game system to get races for
+		/// </param>
+		/// <returns>
+		/// An array of <see cref="Race"/>s for the specified game system
+		/// </returns>
+		public Race[] GetRaces(string systemID)
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+			
+			systemID = systemID.ToLower();
+			Dictionary<string, Dictionary<string, Race>> system;
+			racesTable.TryGetValue(systemID, out system);
+			
+			if (system == null)
+			{
+				return new Race[0];
+			}
+
+			int count = 0;
+
+			foreach (Dictionary<string, Race> racesDict in system.Values)
+			{
+				count += racesDict.Count;
+			}
+
+			Race[] races = new Race[count];
+			int i = 0;
+
+			foreach (string raceID in system.Keys)
+			{
+				foreach (string raceSubId in system[raceID].Keys)
+				{
+					races[i++] = GetRace(systemID, raceID, raceSubId);
+				}
+			}
+
+			return races;
+		}
+
+		public override Race GetRace(GameSystem system, string raceID)
+		{
+			return GetRace(system.ID, raceID);
+		}
+
+		/// <summary>
+		/// Gets a single race for a given game system by ID of the game system and race.
+		/// </summary>
+		/// <param name="systemID">
+		/// The <see cref="System.String"/> ID of the game system that the race is part of.
+		/// </param>
+		/// <param name="raceID">
+		/// The <see cref="System.String"/> ID for the race to load.
+		/// </param>
+		/// <returns>
+		/// A <see cref="Race"/> with the specified ID from the game system with the specified ID, or <code>null</code> if there is no race or game system with those IDs.
+		/// </returns>
+		public Race GetRace(string systemID, string raceID)
+		{
+			return GetRace(systemID, raceID, "");
+		}
+
+		public override Race GetRace(GameSystem system, string raceID, string raceSubID)
+		{
+			return GetRace(system.ID, raceID, raceSubID);
+		}
+
+		/// <summary>
+		/// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID.
+		/// </summary>
+		/// <param name="systemID">
+		/// The <see cref="System.String"/> ID of the game system that the race is part of.
+		/// </param>
+		/// <param name="raceID">
+		/// The <see cref="System.String"/> ID for the race to load.
+		/// </param>
+		/// <param name="raceSubID">
+		/// A <see cref="System.String"/>
+		/// </param>
+		/// <returns>
+		/// A <see cref="Race"/>
+		/// </returns>
+		public Race GetRace(string systemID, string raceID, string raceSubID)
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+			
+			Race race = null;
+			
+			Dictionary<string, Race> subraces = GetRaceTable(systemID, raceID);
+
+			if (subraces != null)
+			{
+				subraces.TryGetValue(raceSubID.ToLower(), out race);
+			}
+			
+			return race;
+		}
+
+		private Dictionary<string, Race> GetRaceTable(string systemID, string raceID)
+		{
+			Dictionary<string, Dictionary<string, Race>> races;
+			racesTable.TryGetValue(systemID.ToLower(), out races);
+			Dictionary<string, Race> subraces = null;
+
+			if (races != null)
+			{
+				races.TryGetValue(raceID.ToLower(), out subraces);
+			}
+
+			return subraces;
+		}
+
+		protected internal override void RemoveRace(Race race)
+		{
+			Dictionary<string, Race> subraces = GetRaceTable(race.GameSystem.ID, race.ID);
+
+			if (subraces != null)
+			{
+				subraces.Remove(race.SubID.ToLower());
+			}
+		}
+
+		public override string[] GetGameSystemIDs()
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+
+			return DictionaryUtils.ToKeyArray(systemsTable);
+		}
+
+		public override string[] GetSystemRaceIDs(GameSystem system)
+		{
+			return GetSystemRaceIDs(system.ID);
+		}
+
+		/// <summary>
+		/// Gets the IDs of all of the races of a specified game system.
+		/// </summary>
+		/// <param name="systemID">
+		/// The <see cref="System.String"/> ID of the game system to get the available races for.
+		/// </param>
+		/// <returns>
+		/// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system.
+		/// </returns>
+		public string[] GetSystemRaceIDs(string systemID)
+		{
+			if (!loaded && !loading)
+			{
+				LoadFiles();
+			}
+
+			Dictionary<string, Dictionary<string, Race>> races = racesTable[systemID.ToLower()];
+
+			if (races == null)
+			{
+				return new string[0];
+			}
+			else
+			{
+				string[] keys = new string[races.Keys.Count];
+				int i = 0;
+
+				foreach (string key in races.Keys)
+				{
+					keys[i++] = key;
+				}
+
+				return keys;
+			}
+		}
+	}
+}