Basic spell list and character model functionality tested? Maybe.
Signed-off-by: Melissa Avery-Weir <averymd@irrsinn.net>
This commit is contained in:
275
d20 SRD Spell Lists/Models/Character.cs
Normal file
275
d20 SRD Spell Lists/Models/Character.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using d20_SRD_Spell_Lists.Exceptions;
|
||||
|
||||
namespace d20_SRD_Spell_Lists.Models {
|
||||
public class Character {
|
||||
|
||||
private string charXmlFile;
|
||||
private XElement charDetails;
|
||||
|
||||
public Character() {
|
||||
charDetails = new XElement("character");
|
||||
charDetails.Add(new XElement("spells"));
|
||||
}
|
||||
|
||||
public Character(string _charXmlFile) {
|
||||
charXmlFile = _charXmlFile;
|
||||
charDetails = XElement.Load(charXmlFile);
|
||||
}
|
||||
|
||||
internal void save() {
|
||||
if (charXmlFile == null) {
|
||||
throw new NoCharacterFileException("Don't have a file location for the character.");
|
||||
}
|
||||
charDetails.Save(charXmlFile);
|
||||
}
|
||||
|
||||
internal XElement spells() {
|
||||
return charDetails.Element("spells");
|
||||
}
|
||||
|
||||
private int modifier(int score) {
|
||||
return (int)Math.Floor((score - 10) / 2.00);
|
||||
}
|
||||
|
||||
public string Name {
|
||||
get {
|
||||
if (charDetails.Element("name") != null) {
|
||||
return charDetails.Element("name").Value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("name") != null) {
|
||||
charDetails.Element("name").Value = value;
|
||||
} else {
|
||||
charDetails.Add(new XElement("name", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Strength {
|
||||
get {
|
||||
if (charDetails.Element("strength") != null) {
|
||||
return int.Parse(charDetails.Element("strength").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("strength") != null) {
|
||||
charDetails.Element("strength").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("strength", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Dexterity {
|
||||
get {
|
||||
if (charDetails.Element("dexterity") != null) {
|
||||
return int.Parse(charDetails.Element("dexterity").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("dexterity") != null) {
|
||||
charDetails.Element("dexterity").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("dexterity", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Constitution {
|
||||
get {
|
||||
if (charDetails.Element("constitution") != null) {
|
||||
return int.Parse(charDetails.Element("constitution").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("constitution") != null) {
|
||||
charDetails.Element("constitution").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("constitution", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Intelligence {
|
||||
get {
|
||||
if (charDetails.Element("intelligence") != null) {
|
||||
return int.Parse(charDetails.Element("intelligence").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("intelligence") != null) {
|
||||
charDetails.Element("intelligence").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("intelligence", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Wisdom {
|
||||
get {
|
||||
if (charDetails.Element("wisdom") != null) {
|
||||
return int.Parse(charDetails.Element("wisdom").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("wisdom") != null) {
|
||||
charDetails.Element("wisdom").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("wisdom", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Charisma {
|
||||
get {
|
||||
if (charDetails.Element("charisma") != null) {
|
||||
return int.Parse(charDetails.Element("charisma").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("charisma") != null) {
|
||||
charDetails.Element("charisma").Value = value.ToString();
|
||||
} else {
|
||||
charDetails.Add(new XElement("charisma", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int SpellCastingAttribute {
|
||||
get {
|
||||
switch (CharacterClass) {
|
||||
case SpellCastingClasses.Bard:
|
||||
return Charisma;
|
||||
case SpellCastingClasses.Cleric:
|
||||
return Wisdom;
|
||||
case SpellCastingClasses.Druid:
|
||||
return Wisdom;
|
||||
case SpellCastingClasses.Paladin:
|
||||
return Wisdom;
|
||||
case SpellCastingClasses.Ranger:
|
||||
return Wisdom;
|
||||
case SpellCastingClasses.Sorcerer:
|
||||
return Charisma;
|
||||
case SpellCastingClasses.Wizard:
|
||||
return Intelligence;
|
||||
default:
|
||||
return Wisdom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int SpellCastingAttributeMod {
|
||||
get {
|
||||
switch (CharacterClass) {
|
||||
case SpellCastingClasses.Bard:
|
||||
return CharismaMod;
|
||||
case SpellCastingClasses.Cleric:
|
||||
return WisdomMod;
|
||||
case SpellCastingClasses.Druid:
|
||||
return WisdomMod;
|
||||
case SpellCastingClasses.Paladin:
|
||||
return WisdomMod;
|
||||
case SpellCastingClasses.Ranger:
|
||||
return WisdomMod;
|
||||
case SpellCastingClasses.Sorcerer:
|
||||
return CharismaMod;
|
||||
case SpellCastingClasses.Wizard:
|
||||
return IntelligenceMod;
|
||||
default:
|
||||
return WisdomMod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SpellCastingClasses CharacterClass {
|
||||
get {
|
||||
if (charDetails.Element("class") != null) {
|
||||
return (SpellCastingClasses)Enum.Parse(typeof(SpellCastingClasses), charDetails.Element("class").Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set {
|
||||
if (charDetails.Element("class") != null) {
|
||||
charDetails.Element("class").Value = getClassName(value);
|
||||
} else {
|
||||
charDetails.Add(new XElement("class", getClassName(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int StrengthMod {
|
||||
get {
|
||||
return modifier(Strength);
|
||||
}
|
||||
}
|
||||
|
||||
public int DexterityMod {
|
||||
get {
|
||||
return modifier(Dexterity);
|
||||
}
|
||||
}
|
||||
|
||||
public int ConstitutionMod {
|
||||
get {
|
||||
return modifier(Constitution);
|
||||
}
|
||||
}
|
||||
|
||||
public int IntelligenceMod {
|
||||
get {
|
||||
return modifier(Intelligence);
|
||||
}
|
||||
}
|
||||
|
||||
public int WisdomMod {
|
||||
get {
|
||||
return modifier(Wisdom);
|
||||
}
|
||||
}
|
||||
|
||||
public int CharismaMod {
|
||||
get {
|
||||
return modifier(Charisma);
|
||||
}
|
||||
}
|
||||
|
||||
public enum SpellCastingClasses {
|
||||
Bard = 0,
|
||||
Cleric = 1,
|
||||
Druid = 2,
|
||||
Paladin = 3,
|
||||
Ranger = 4,
|
||||
Sorcerer = 5,
|
||||
Wizard = 6
|
||||
}
|
||||
|
||||
public static string getClassName(SpellCastingClasses spellCastingClass) {
|
||||
return Enum.GetName(typeof(SpellCastingClasses), spellCastingClass);
|
||||
}
|
||||
|
||||
public int[] BonusSpells {
|
||||
get {
|
||||
int[] bs = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
for (int i = 1; i < bs.Length; i++ ) {
|
||||
bs[i] = (int)Math.Floor((SpellCastingAttribute - ((i + 1) * 2)) / 8.00);
|
||||
}
|
||||
return bs;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
221
d20 SRD Spell Lists/Models/SpellSet.cs
Normal file
221
d20 SRD Spell Lists/Models/SpellSet.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using d20_SRD_Spell_Lists.Exceptions;
|
||||
|
||||
namespace d20_SRD_Spell_Lists.Models {
|
||||
public class SpellSet {
|
||||
private XElement masterSpellList;
|
||||
private XElement userSpellList;
|
||||
private XElement charSpellList;
|
||||
private string userXmlFile;
|
||||
private Character character;
|
||||
|
||||
public SpellSet(string _masterXmlFile = null, string _userXmlFile = null, Character _character = null) {
|
||||
userXmlFile = _userXmlFile;
|
||||
character = _character;
|
||||
loadXML(ref _masterXmlFile);
|
||||
}
|
||||
|
||||
private void loadXML(ref string _masterXmlFile) {
|
||||
// Load the XML file.
|
||||
if (_masterXmlFile == null) {
|
||||
_masterXmlFile = Properties.Settings.Default.MasterSpells;
|
||||
}
|
||||
if (userXmlFile == null) {
|
||||
userXmlFile = Properties.Settings.Default.UserSpells;
|
||||
}
|
||||
masterSpellList = XElement.Load(_masterXmlFile);
|
||||
userSpellList = XElement.Load(userXmlFile);
|
||||
|
||||
if (character != null) {
|
||||
charSpellList = character.spells();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a spell to the application's list of custom spells.
|
||||
/// </summary>
|
||||
/// <param name="xElement">The XML details of the new spell.</param>
|
||||
public void addUserSpell(XElement xElement) {
|
||||
userSpellList.Add(xElement);
|
||||
}
|
||||
|
||||
public IEnumerable<XElement> byClass(Character.SpellCastingClasses spellCastingClass) {
|
||||
XElement spells = new XElement("spells");
|
||||
|
||||
filterMasterSpellsByClass(spellCastingClass, spells);
|
||||
filterUserSpellsByClass(spellCastingClass, spells);
|
||||
filterCharacterSpellsByClass(spellCastingClass, spells);
|
||||
|
||||
removeHiddenSpells(spells);
|
||||
|
||||
return spells.Elements("spell");
|
||||
}
|
||||
|
||||
private void filterMasterSpellsByClass(Character.SpellCastingClasses spellCastingClass, XElement spells) {
|
||||
spells.Add((from sp in masterSpellList.Elements("spell")
|
||||
let level = (string)sp.Element("level")
|
||||
where level != null && level.Contains(Character.getClassName(spellCastingClass))
|
||||
select sp));
|
||||
}
|
||||
|
||||
private void filterCharacterSpellsByClass(Character.SpellCastingClasses spellCastingClass, XElement spells) {
|
||||
if (characterSpellCount() > 0) {
|
||||
spells.Add((from sp in charSpellList.Elements("spell")
|
||||
let level = (string)sp.Element("level")
|
||||
where level != null && level.Contains(Character.getClassName(spellCastingClass))
|
||||
select sp));
|
||||
}
|
||||
}
|
||||
|
||||
private void filterUserSpellsByClass(Character.SpellCastingClasses spellCastingClass, XElement spells) {
|
||||
if (userSpellCount() > 0) {
|
||||
spells.Add((from sp in userSpellList.Elements("spell")
|
||||
let level = (string)sp.Element("level")
|
||||
where level != null && level.Contains(Character.getClassName(spellCastingClass))
|
||||
select sp));
|
||||
}
|
||||
}
|
||||
|
||||
private void removeHiddenSpells(XElement spells) {
|
||||
if (hiddenSpellCount() > 0) {
|
||||
foreach (string hiddenSpellName in (from hp in userSpellList.Elements("hidden_spell").Elements("spell")
|
||||
select (string)hp.Element("name"))) {
|
||||
(from sp in spells.Elements("spell")
|
||||
where (string)sp.Element("name") == hiddenSpellName
|
||||
select sp).Remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (hiddenCharacterSpellCount() > 0) {
|
||||
foreach (string hiddenSpellName in (from hp in charSpellList.Elements("hidden_spell").Elements("spell")
|
||||
select (string)hp.Element("name"))) {
|
||||
(from sp in spells.Elements("spell")
|
||||
where (string)sp.Element("name") == hiddenSpellName
|
||||
select sp).Remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<XElement> byClassAndLevel(Character.SpellCastingClasses spellCastingClass, int level) {
|
||||
XElement spells = new XElement("spells");
|
||||
|
||||
filterMasterSpellsByClassAndLevel(spellCastingClass, level, spells);
|
||||
filterUserSpellsByClassAndLevel(spellCastingClass, level, spells);
|
||||
filterCharacterSpellsByClassAndLevel(spellCastingClass, level, spells);
|
||||
|
||||
removeHiddenSpells(spells);
|
||||
|
||||
return spells.Elements("spell");
|
||||
}
|
||||
|
||||
private void filterCharacterSpellsByClassAndLevel(Character.SpellCastingClasses spellCastingClass, int level, XElement spells) {
|
||||
if (characterSpellCount() > 0) {
|
||||
spells.Add((from sp in charSpellList.Elements("spell")
|
||||
let xmlLevel = (string)sp.Element("level")
|
||||
where xmlLevel != null && xmlLevel.Contains(Character.getClassName(spellCastingClass) + " " + level.ToString())
|
||||
select sp));
|
||||
}
|
||||
}
|
||||
|
||||
private void filterUserSpellsByClassAndLevel(Character.SpellCastingClasses spellCastingClass, int level, XElement spells) {
|
||||
if (userSpellCount() > 0) {
|
||||
spells.Add((from sp in userSpellList.Elements("spell")
|
||||
let xmlLevel = (string)sp.Element("level")
|
||||
where xmlLevel != null && xmlLevel.Contains(Character.getClassName(spellCastingClass) + " " + level.ToString())
|
||||
select sp));
|
||||
}
|
||||
}
|
||||
|
||||
private void filterMasterSpellsByClassAndLevel(Character.SpellCastingClasses spellCastingClass, int level, XElement spells) {
|
||||
spells.Add((from sp in masterSpellList.Elements("spell")
|
||||
let xmlLevel = (string)sp.Element("level")
|
||||
where xmlLevel != null && xmlLevel.Contains(Character.getClassName(spellCastingClass) + " " + level.ToString())
|
||||
select sp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves all XML files to the system.
|
||||
/// </summary>
|
||||
public void save() {
|
||||
userSpellList.Save(userXmlFile);
|
||||
if (character != null) {
|
||||
character.save();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a node from the application's custom spell list.
|
||||
/// </summary>
|
||||
/// <param name="nodeName">The name of the node to be searched on. Could be "name", "school", etc.</param>
|
||||
/// <param name="nodeValue">The value of the node matching the nodeName.</param>
|
||||
public void removeUserSpell(string nodeName, string nodeValue) {
|
||||
(from sp in userSpellList.Elements("spell")
|
||||
where (string)sp.Element(nodeName) == nodeValue
|
||||
select sp).Remove();
|
||||
}
|
||||
|
||||
public void hideMasterSpell(string nodeName, string nodeValue) {
|
||||
userSpellList.Add(new XElement("hidden_spell", (from msp in masterSpellList.Elements("spell")
|
||||
where (string)msp.Element(nodeName) == nodeValue
|
||||
select msp)));
|
||||
}
|
||||
|
||||
public void hideMasterSpellForCharacter(string nodeName, string nodeValue) {
|
||||
charSpellList.Add(new XElement("hidden_spell", (from msp in masterSpellList.Elements("spell")
|
||||
where (string)msp.Element(nodeName) == nodeValue
|
||||
select msp)));
|
||||
}
|
||||
|
||||
public void showMasterSpell(string nodeName, string nodeValue) {
|
||||
(from hsp in userSpellList.Elements("hidden_spell")
|
||||
where (string)hsp.Element("spell").Element(nodeName) == nodeValue
|
||||
select hsp).Remove();
|
||||
}
|
||||
|
||||
public void showMasterSpellForCharacter(string nodeName, string nodeValue) {
|
||||
(from hsp in charSpellList.Elements("hidden_spell")
|
||||
where (string)hsp.Element("spell").Element(nodeName) == nodeValue
|
||||
select hsp).Remove();
|
||||
}
|
||||
|
||||
public int hiddenSpellCount() {
|
||||
return userSpellList.Descendants("hidden_spell").Count();
|
||||
}
|
||||
|
||||
public int hiddenCharacterSpellCount() {
|
||||
if (charSpellList != null) {
|
||||
return charSpellList.Descendants("hidden_spell").Count();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int totalCount() {
|
||||
int total = masterSpellList.Elements("spell").Count() + userSpellList.Elements("spell").Count();
|
||||
if (charSpellList != null) {
|
||||
total += charSpellList.Elements("spell").Count();
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public int masterSpellCount() {
|
||||
return masterSpellList.Elements("spell").Count();
|
||||
}
|
||||
|
||||
public int userSpellCount() {
|
||||
return userSpellList.Elements("spell").Count();
|
||||
}
|
||||
|
||||
public int characterSpellCount() {
|
||||
if (character != null) {
|
||||
return character.spells().Elements("spell").Count();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user