From 333363698c09b2e8c112791a4cad654c58d39ce0 Mon Sep 17 00:00:00 2001 From: Josiah Bradbury Date: Fri, 11 Oct 2019 00:28:01 -0500 Subject: [PATCH] Added Saving to Core functionality, It's a Basic list of parties which are lists of characters, Added newtonsoft to forms to it works. #6 --- .../{ => CombatManagement}/Character.cs | 0 .../CombatManagerService.cs | 0 src/DnDCombatTracker.Core/Data/FileIO.cs | 31 ++++++++++++ .../DnDCombatTracker.Core.csproj | 6 ++- .../PartyManagement/Party.cs | 14 ++++++ .../PartyManagement/PartyManagerService.cs | 49 +++++++++++++++++++ .../DnDCombatTracker.Forms.csproj | 4 ++ src/DnDCombatTracker.Forms/packages.config | 4 ++ 8 files changed, 107 insertions(+), 1 deletion(-) rename src/DnDCombatTracker.Core/{ => CombatManagement}/Character.cs (100%) rename src/DnDCombatTracker.Core/{ => CombatManagement}/CombatManagerService.cs (100%) create mode 100644 src/DnDCombatTracker.Core/Data/FileIO.cs create mode 100644 src/DnDCombatTracker.Core/PartyManagement/Party.cs create mode 100644 src/DnDCombatTracker.Core/PartyManagement/PartyManagerService.cs create mode 100644 src/DnDCombatTracker.Forms/packages.config diff --git a/src/DnDCombatTracker.Core/Character.cs b/src/DnDCombatTracker.Core/CombatManagement/Character.cs similarity index 100% rename from src/DnDCombatTracker.Core/Character.cs rename to src/DnDCombatTracker.Core/CombatManagement/Character.cs diff --git a/src/DnDCombatTracker.Core/CombatManagerService.cs b/src/DnDCombatTracker.Core/CombatManagement/CombatManagerService.cs similarity index 100% rename from src/DnDCombatTracker.Core/CombatManagerService.cs rename to src/DnDCombatTracker.Core/CombatManagement/CombatManagerService.cs diff --git a/src/DnDCombatTracker.Core/Data/FileIO.cs b/src/DnDCombatTracker.Core/Data/FileIO.cs new file mode 100644 index 0000000..5015a91 --- /dev/null +++ b/src/DnDCombatTracker.Core/Data/FileIO.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading.Tasks; + +namespace DnDCombatTracker.Core.Data +{ + public static class FileIO + { + public static string GetFileString(string fileName, string folder) + { + if (!File.Exists(string.Join("\\", folder, fileName))) + { + return null; + } + + var path = Path.GetFullPath(string.Join("\\", folder, fileName)); + + + var str = File.ReadAllText(string.Join("\\", folder, fileName)); + + return str; + } + + public static void SaveFileString(string fileName, string folder, string content) + { + File.WriteAllText(string.Join("\\", folder, fileName), content); + } + } +} diff --git a/src/DnDCombatTracker.Core/DnDCombatTracker.Core.csproj b/src/DnDCombatTracker.Core/DnDCombatTracker.Core.csproj index 9f5c4f4..6218a75 100644 --- a/src/DnDCombatTracker.Core/DnDCombatTracker.Core.csproj +++ b/src/DnDCombatTracker.Core/DnDCombatTracker.Core.csproj @@ -1,7 +1,11 @@ - + netstandard2.0 + + + + diff --git a/src/DnDCombatTracker.Core/PartyManagement/Party.cs b/src/DnDCombatTracker.Core/PartyManagement/Party.cs new file mode 100644 index 0000000..f8bf0a1 --- /dev/null +++ b/src/DnDCombatTracker.Core/PartyManagement/Party.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DnDCombatTracker.Core +{ + public class Party + { + public Guid partyId { get; set; } + public string Name { get; set; } + public List Characters { get; set; } + public string Notes { get; set; } + } +} diff --git a/src/DnDCombatTracker.Core/PartyManagement/PartyManagerService.cs b/src/DnDCombatTracker.Core/PartyManagement/PartyManagerService.cs new file mode 100644 index 0000000..4da761c --- /dev/null +++ b/src/DnDCombatTracker.Core/PartyManagement/PartyManagerService.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DnDCombatTracker.Core +{ + public class PartyManagerService + { + + public List GetParties(string filename, string folder) + { + var partyStr = Data.FileIO.GetFileString(filename, folder); + + if (partyStr == null) + { + return new List(); + } + var parties = Newtonsoft.Json.JsonConvert.DeserializeObject>(partyStr); + + return parties; + } + + public void SaveParty(string filename, string folder, Party party) + { + var parties = GetParties(filename, folder); + + var PartyToUpdate = parties.FirstOrDefault(x => x.partyId == party.partyId); + + if (PartyToUpdate == null) + { + parties.Add(party); + } + else + { + PartyToUpdate = party; + } + + SaveParties(filename, folder, parties); + } + + public void SaveParties(string filename, string folder, List parties) + { + var partiesString = Newtonsoft.Json.JsonConvert.SerializeObject(parties); + + Data.FileIO.SaveFileString(filename, folder, partiesString); + } + } +} diff --git a/src/DnDCombatTracker.Forms/DnDCombatTracker.Forms.csproj b/src/DnDCombatTracker.Forms/DnDCombatTracker.Forms.csproj index b4664db..8f06964 100644 --- a/src/DnDCombatTracker.Forms/DnDCombatTracker.Forms.csproj +++ b/src/DnDCombatTracker.Forms/DnDCombatTracker.Forms.csproj @@ -75,6 +75,9 @@ Properties\app.manifest + + ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll + @@ -109,6 +112,7 @@ Resources.resx True + SettingsSingleFileGenerator diff --git a/src/DnDCombatTracker.Forms/packages.config b/src/DnDCombatTracker.Forms/packages.config new file mode 100644 index 0000000..f56d39a --- /dev/null +++ b/src/DnDCombatTracker.Forms/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file