diff --git a/Classes/TarotDeck.cs b/Classes/TarotDeck.cs index a72bf7a..fbd6833 100644 --- a/Classes/TarotDeck.cs +++ b/Classes/TarotDeck.cs @@ -16,7 +16,6 @@ public TarotDeck(){ } public void DeserializeDeck(string filePath){ - _deck = new List(); _deck.AddRange(Utilities.Deserialize>(filePath)); } diff --git a/Data/template_cards.json b/Data/template_cards.json new file mode 100644 index 0000000..eb00baf --- /dev/null +++ b/Data/template_cards.json @@ -0,0 +1,7 @@ +[ + /* Add any amount of cards that should be added to the deck in the following format */ + + /* + {"Name": "Name of Card"} + */ +] \ No newline at end of file diff --git a/Data/template_spreads.json b/Data/template_spreads.json new file mode 100644 index 0000000..00c469b --- /dev/null +++ b/Data/template_spreads.json @@ -0,0 +1,13 @@ +[ + /* Add any amount of spreads that should be available in the following format */ + /* Entering the same key-value as an already existing spread will overwrite the previous spread */ + + /* + "Spread name": [ + "First card meaning", + "Second card meaning", + "Third card meaning", + "And more..." + ] + */ +] \ No newline at end of file diff --git a/Program.cs b/Program.cs index de465fc..d3cd0b6 100644 --- a/Program.cs +++ b/Program.cs @@ -5,33 +5,65 @@ namespace tarot{ class Program{ - static string userPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString(); - static string configPath = Path.Combine(userPath,@"tarot/"); - static string filePath = Path.Combine(configPath,@"current_deck.json"); + static string localApplicationDataPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"tarot/" + ); + static string currentDeckFilePath = Path.Combine(localApplicationDataPath,@"current_deck.json"); + + static string configurationDataPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @".config/tarot/" + ); + static string userCardsFilePath = Path.Combine(configurationDataPath, "cards.json"); + static string userSpreadsFilePath = Path.Combine(configurationDataPath, "spreads.json"); + static string defaultCardsFilePath = @"Data/default_cards.json"; static string defaultSpreadsFilePath = @"Data/default_spreads.json"; + static string templateCardsFilePath = @"Data/template_cards.json"; + static string templateSpreadsFilePath = @"Data/template_spreads.json"; static int Main(string[] args){ - Directory.CreateDirectory(configPath); TarotDeck deck = new TarotDeck(); Dictionary spreads_uninitialized = new Dictionary(); - - if(File.Exists(filePath)){ - deck.DeserializeDeck(filePath); - }else{ - deck.DeserializeDeck(defaultCardsFilePath); - File.WriteAllText(filePath, deck.SerializeDeck()); - } - spreads_uninitialized = Utilities.Deserialize>(defaultSpreadsFilePath); - Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions)}; - return Parser.Default.ParseArguments(args, types) - .MapResult( + + HandleFiles(deck, spreads_uninitialized); + + return Parser.Default.ParseArguments(args, types).MapResult( (GetOptions options) => Get(options, deck), (ShuffleOptions options) => Shuffle(options, deck), (ResetOptions options) => Reset(options, deck), (SpreadOptions options) => Spread(options, spreads_uninitialized, deck), - errors => 1); + errors => 1 + ); + } + + private static void HandleFiles(TarotDeck deck, Dictionary spreads){ + Directory.CreateDirectory(localApplicationDataPath); + Directory.CreateDirectory(configurationDataPath); + + if(File.Exists(currentDeckFilePath)){ + deck.DeserializeDeck(currentDeckFilePath); + }else{ + deck.DeserializeDeck(defaultCardsFilePath); + File.WriteAllText(currentDeckFilePath, deck.SerializeDeck()); + } + spreads = Utilities.Deserialize>(defaultSpreadsFilePath); + + if(File.Exists(userCardsFilePath)){ + try{ + deck.DeserializeDeck(userCardsFilePath); + }catch (System.Exception){}; + }else{ + File.WriteAllText(userCardsFilePath, File.ReadAllText(templateCardsFilePath)); + } + + if(File.Exists(userSpreadsFilePath)){ + try{ + spreads.AddRange(Utilities.Deserialize>(userSpreadsFilePath)); + }catch (System.Exception){}; + }else{ + File.WriteAllText(userSpreadsFilePath, File.ReadAllText(templateSpreadsFilePath)); + } } private static int Get(GetOptions options, TarotDeck deck){ @@ -87,12 +119,12 @@ private static int Reset(ResetOptions options, TarotDeck deck){ } private static int Spread(SpreadOptions options, Dictionary spreads_unitit, TarotDeck deck){ - List spread_names = new List(spreads_unitit.Keys); + List spreadNames = new List(spreads_unitit.Keys); if(options.ListAll){ - spread_names.Sort(); - for (int i = 0; i < spread_names.Count; i++){ - string name = spread_names[i]; + spreadNames.Sort(); + for (int i = 0; i < spreadNames.Count; i++){ + string name = spreadNames[i]; Console.WriteLine($"{i+1}. {name}:"); string[] positions = spreads_unitit[name]; for (int j = 0; j < positions.Length; j++){ @@ -100,7 +132,7 @@ private static int Spread(SpreadOptions options, Dictionary sp } } }else if(options.Name != default){ - if(spread_names.Contains(options.Name)){ + if(spreadNames.Contains(options.Name)){ if(options.List){ Console.WriteLine($"{options.Name}:"); string[] positions = spreads_unitit[options.Name]; @@ -123,7 +155,7 @@ private static int Spread(SpreadOptions options, Dictionary sp } static void SaveDeck(TarotDeck deck){ - File.WriteAllText(filePath, deck.SerializeDeck()); + File.WriteAllText(currentDeckFilePath, deck.SerializeDeck()); } } } \ No newline at end of file diff --git a/Utilities.cs b/Utilities.cs index e76e5b2..67f4306 100644 --- a/Utilities.cs +++ b/Utilities.cs @@ -51,4 +51,20 @@ public static string Multiply(this string text, int multiplier){ return String.Concat(Enumerable.Repeat(text, multiplier)); } } + + static class DictionaryExtension{ + public static void AddRange(this Dictionary target, Dictionary source) + { + if(target is null) throw new ArgumentNullException(nameof(target)); + if(source is null) throw new ArgumentNullException(nameof(source)); + foreach(T key in source.Keys){ + U value = target[key]; + if(target.ContainsKey(key)){ + target[key] = value; + }else{ + target.Add(key, value); + } + } + } + } } \ No newline at end of file