From e19ade73aabbc48e40c0f478bde54a9dc70854b5 Mon Sep 17 00:00:00 2001 From: Knose Date: Thu, 29 Jul 2021 19:01:40 +0200 Subject: [PATCH] Add spread commands --- Classes/TarotSpread.cs | 2 +- Options.cs | 22 +++++++++++--- Program.cs | 68 +++++++++++++++++++++++++++++++++++------- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/Classes/TarotSpread.cs b/Classes/TarotSpread.cs index 1f70aa1..dc0f6d3 100644 --- a/Classes/TarotSpread.cs +++ b/Classes/TarotSpread.cs @@ -23,7 +23,7 @@ public string SerializeSpread(){ public void PrintSpread(){ for (int i = 0; i < GetLength(); i++){ - Console.WriteLine($"{i}. {_spread_positions[i]}:\n\t{_spread_cards[i]}"); + Console.WriteLine($"{i+1}. {_spread_positions[i]}:\n\t{_spread_cards[i]}"); } } diff --git a/Options.cs b/Options.cs index d8b8588..c7abfd9 100644 --- a/Options.cs +++ b/Options.cs @@ -2,17 +2,19 @@ namespace tarot{ [Verb("get", HelpText = "Retrieve cards from the deck.")] - public class GetOptions{ - [Value(0, MetaName = "amount", MetaValue = "uint", Required = false, Default = 1u, HelpText = "Amount of cards to retrieve.")] - public uint Amount{get; set;} - } + public class GetOptions{ + [Value(0, MetaName = "amount", MetaValue = "uint", Required = false, Default = 1u, HelpText = "Amount of cards to retrieve.")] + public uint Amount{get; set;} + } [Verb("shuffle", HelpText = "Shuffle the deck.")] public class ShuffleOptions{ [Value(0, MetaName = "type", MetaValue = "string", Required = false, Default = "riffle", HelpText = "What shuffle to perform.")] public string Type{get; set;} + [Value(1, MetaName = "amount", MetaValue = "uint", Required = false, Default = 1u, HelpText = "Number of shuffles to perform.")] public uint Amount{get; set;} + [Option('q',"quiet", HelpText = "Suppress stdout.")] public bool Quiet{get; set;} } @@ -22,4 +24,16 @@ public class ResetOptions{ [Option('q',"quiet", HelpText = "Suppress stdout.")] public bool Quiet{get; set;} } + + [Verb("spread", HelpText = "Perform a tarot spread.")] + public class SpreadOptions{ + [Option('l',"list", SetName = "one", MetaValue = "string", Required = false, HelpText = "List a specific spread.")] + public bool List{get; set;} + + [Option('a',"list-all", SetName = "all", Required = false, HelpText = "List all spreads.")] + public bool ListAll{get; set;} + + [Value(0, MetaName = "name", MetaValue = "string", Required = false, Default = "", HelpText = "Name of spread to perform.")] + public string Name{get; set;} + } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index c203b78..de465fc 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Collections.Generic; using CommandLine; namespace tarot{ @@ -7,35 +8,40 @@ 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 defaultFilePath = @"Data/default_cards.json"; + static string defaultCardsFilePath = @"Data/default_cards.json"; + static string defaultSpreadsFilePath = @"Data/default_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(defaultFilePath); + deck.DeserializeDeck(defaultCardsFilePath); File.WriteAllText(filePath, deck.SerializeDeck()); } + spreads_uninitialized = Utilities.Deserialize>(defaultSpreadsFilePath); - Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions)}; + Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions)}; 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); } private static int Get(GetOptions options, TarotDeck deck){ + Console.WriteLine(options.Amount); options.Amount = Math.Max(1, options.Amount); - for (int i = 0; i < options.Amount; i++){ - Console.WriteLine(deck.RequeueCard().ToString()); - } - File.WriteAllText(filePath, deck.SerializeDeck()); - return 0; + for (int i = 0; i < options.Amount; i++){ + Console.WriteLine(deck.RequeueCard().ToString()); + } + SaveDeck(deck); + return 0; } private static int Shuffle(ShuffleOptions options, TarotDeck deck){ @@ -69,15 +75,55 @@ private static int Shuffle(ShuffleOptions options, TarotDeck deck){ Console.WriteLine("Performed {0} shuffle.", options.Type.ToLower()); } } - File.WriteAllText(filePath, deck.SerializeDeck()); + SaveDeck(deck); return 0; } private static int Reset(ResetOptions options, TarotDeck deck){ - deck.DeserializeDeck(defaultFilePath); + deck.DeserializeDeck(defaultCardsFilePath); if(!options.Quiet) Console.WriteLine("The deck has been reset to default."); - File.WriteAllText(filePath, deck.SerializeDeck()); + SaveDeck(deck); + return 0; + } + + private static int Spread(SpreadOptions options, Dictionary spreads_unitit, TarotDeck deck){ + List spread_names = 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]; + Console.WriteLine($"{i+1}. {name}:"); + string[] positions = spreads_unitit[name]; + for (int j = 0; j < positions.Length; j++){ + Console.WriteLine($"\t{j+1}) {positions[j]}"); + } + } + }else if(options.Name != default){ + if(spread_names.Contains(options.Name)){ + if(options.List){ + Console.WriteLine($"{options.Name}:"); + string[] positions = spreads_unitit[options.Name]; + for (int i = 0; i < positions.Length; i++){ + Console.WriteLine($"\t{i+1}) {positions[i]}"); + } + }else{ + TarotSpread spread = new TarotSpread(spreads_unitit[options.Name], deck); + spread.PrintSpread(); + } + }else{ + Console.WriteLine("Spread does not exist."); + return 1; + } + }else{ + return 1; + } + SaveDeck(deck); return 0; } + + static void SaveDeck(TarotDeck deck){ + File.WriteAllText(filePath, deck.SerializeDeck()); + } } } \ No newline at end of file