Skip to content

Commit

Permalink
#6 Started working on the saving issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander ten Brinke committed Mar 28, 2017
1 parent cb02fdd commit 5898cc2
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 6 deletions.
9 changes: 8 additions & 1 deletion CombatTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -87,7 +91,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Character.cs" />
<Compile Include="Models\Category.cs" />
<Compile Include="Models\Character.cs" />
<Compile Include="DataSaver.cs" />
<Compile Include="OverviewForm.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -109,6 +115,7 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
109 changes: 109 additions & 0 deletions DataSaver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//Retrieved from: http://blog.danskingdom.com/saving-and-loading-a-c-objects-data-to-an-xml-json-or-binary-file/

using CombatTracker.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CombatTracker
{
public class DataSaver
{
private const string FILE_NAME = "Characters.json";
private const string FOLDER_NAME = "D&D Combat Tracker";

private readonly string FOLDER_PATH = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\{FOLDER_NAME}";
private readonly string FILE_PATH = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\{FOLDER_NAME}\{FILE_NAME}";

private static DataSaver _instance;

public static DataSaver Instance
{
get
{
if (_instance == null)
{
_instance = new DataSaver();
}
return _instance;
}
}

/// <summary>
/// Creates
/// </summary>
private DataSaver()
{
if (!Directory.Exists(FOLDER_PATH))
{
try
{
Directory.CreateDirectory(FOLDER_PATH);
File.Create(FILE_PATH);
}
catch (Exception)
{
//TODO: Handle Exception (Maybe tell the user that things cannot be saved and thus saving data will be disabled for the user (To implement later)
}
}
}


/// <summary>
/// Writes the given object instance to a Json file.
/// </summary>
/// <typeparam name="T">The type of object being written to the file.</typeparam>
/// <param name="filePath">The file path to write the object instance to.</param>
/// <param name="objectToWrite">The object instance to write to the file.</param>
/// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param>
/// <returns>True if writing succeeded, false otherwise</returns>
public bool WriteToJsonFile(List<Category> objectToWrite, bool append = false)
{
TextWriter writer = null;
try
{
string contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
writer = new StreamWriter(FILE_PATH, append);
writer.Write(contentsToWriteToFile);
}
catch (Exception ex) //TODO: Implement better exception handling
{
return false;
}
finally
{
if (writer != null)
writer.Close();
}
return true;
}

/// <summary>
/// Reads an object instance from an Json file.
/// </summary>
/// <para>Object type must have a parameterless constructor.</para>
/// <typeparam name="T">The type of object to read from the file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <returns>Returns a new instance of the object read from the Json file.</returns>
public List<Category> ReadFromJsonFile()
{
TextReader reader = null;
try
{
reader = new StreamReader(FILE_PATH);
var fileContents = reader.ReadToEnd();
var deserialized = JsonConvert.DeserializeObject<List<Category>>(fileContents);
return deserialized;
}
finally
{
if (reader != null)
reader.Close();
}
}
}
}
29 changes: 29 additions & 0 deletions Models/Category.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CombatTracker.Models
{
/// <summary>
/// For saving categories of characters
/// </summary>
public class Category
{
public string Name { get; set; }

public List<Character> Characters { get; set; }

public Category()
{

}

public Category(string name, List<Character> characters)
{
Name = name;
Characters = characters;
}
}
}
9 changes: 7 additions & 2 deletions Character.cs → Models/Character.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace CombatTracker
namespace CombatTracker.Models
{

public enum Conditions
Expand Down Expand Up @@ -40,7 +40,7 @@ public class Character : IComparable<Character>

public List<Conditions> Conditions { get; set; }

public Character(string name, short initiative, short? maxHP, short? HP, short? tempHP, short fail, short success, List<Conditions> conditions)
public Character(string name, short initiative, short fail, short success, List<Conditions> conditions, short? maxHP, short? HP, short? tempHP)
{
Name = name;
Initiative = initiative;
Expand All @@ -52,6 +52,11 @@ public Character(string name, short initiative, short? maxHP, short? HP, short?
Conditions = conditions;
}

public Character()
{

}

public override string ToString()
{
//Determine if the HP part should be shown or not:
Expand Down
5 changes: 3 additions & 2 deletions OverviewForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CombatTracker.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down Expand Up @@ -349,7 +350,7 @@ where item.Key.Checked
short fails = (short)ListDeathSavesFail.Count(x => x.Checked);
short successes = (short)ListDeathSavesSuccess.Count(x => x.Checked);

Character character = new Character(name, initiative, maxHP, hp, tempHP, fails, successes, chosenConditions);
Character character = new Character(name, initiative, fails, successes, chosenConditions, maxHP, hp, tempHP);

//Update or insert the character
if (CharacterList.ToList().Exists(x => x.Name == name))
Expand Down
44 changes: 43 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using CombatTracker.Models;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CombatTracker
Expand All @@ -11,9 +13,49 @@ static class Program
[STAThread]
static void Main()
{

//Create/Check save data
var x = DataSaver.Instance;


var o = DataSaver.Instance.ReadFromJsonFile();

//Test some stuff
var condition_list_1 = new List<Conditions>() { Conditions.Blinded, Conditions.Deafened, Conditions.Grappled };
var condition_list_2 = new List<Conditions>() { Conditions.Incapacitated };
var condition_list_3 = new List<Conditions>() { Conditions.Stunned };
var condition_list_4 = new List<Conditions>() { Conditions.Prone };
var condition_list_5 = new List<Conditions>() { Conditions.Unconscious };
var condition_list_6 = new List<Conditions>();

Character character_1_1 = new Character("Flyrion", 15, 0, 0, condition_list_1, 52, 22, null);
Character character_1_2 = new Character("Yuri", 18, 1, 1, condition_list_5, 48, 0, null);
Character character_1_3 = new Character("Xerxes", 8, 0, 0, condition_list_6, null, null, null);
Character character_1_4 = new Character("Lea", 20, 0, 0, condition_list_3, 77, 77, 15);

Character character_2_1 = new Character("Kollis", 14, 0, 0, condition_list_2, 38, 1, null);
Character character_2_2 = new Character("Torrin", 3, 2, 0, condition_list_5, null, null, null);
Character character_2_3 = new Character("Tasha", 17, 0, 0, condition_list_4, 47, 43, 4);

Character character_3_1 = new Character("Mardik", 18, 0, 2, condition_list_6, 20, 20, null);
Character character_3_2 = new Character("Treak", 11, 0, 0, condition_list_3, 33, 30, 0);

Category category_1 = new Category("Aryeh", new List<Character>() { character_1_1, character_1_2, character_1_3, character_1_4 });
Category category_2 = new Category("Robin", new List<Character>() { character_2_1, character_2_2, character_2_3 });
Category category_3 = new Category("YourHosting", new List<Character>() { character_3_1, character_3_2 });
Category category_4 = new Category("Empty", new List<Character>());


var listOfCategories = new List<Category>() { category_1, category_2, category_3, category_4 };

DataSaver.Instance.WriteToJsonFile(listOfCategories);


Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new OverviewForm());


}
}
}
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net452" />
</packages>

0 comments on commit 5898cc2

Please sign in to comment.