Skip to content

Commit

Permalink
Reset spreads/deck from file
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Jul 30, 2021
1 parent e4020a2 commit 0928726
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class ShuffleOptions{

[Verb("reset", HelpText = "Reset deck to default.")]
public class ResetOptions{
[Option('d',"load-deck", HelpText = "Filepath to custom deck-file to load.")]
public string Deck{get; set;}

[Option('s',"load-spreads", HelpText = "Filepath to custom spreads-file to load.")]
public string Spreads{get; set;}

[Option('q',"quiet", HelpText = "Suppress stdout.")]
public bool Quiet{get; set;}
}
Expand Down
48 changes: 37 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using CommandLine;

using TarotSpreads = System.Collections.Generic.Dictionary<string, tarot.TarotSpread>;

namespace tarot{
class Program{
static string localApplicationDataPath = Path.Combine(
Expand All @@ -24,21 +26,21 @@ class Program{

static int Main(string[] args){
TarotDeck deck = new TarotDeck();
Dictionary<string, TarotSpread> spreads = new Dictionary<string, TarotSpread>();
TarotSpreads spreads = new TarotSpreads();
Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions)};

HandleFiles(deck, spreads);

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, deck),
(ResetOptions options) => Reset(options, deck, spreads),
(SpreadOptions options) => Spread(options, deck, spreads),
errors => 1
);
}

private static void HandleFiles(TarotDeck deck, Dictionary<string, TarotSpread> spreads){
private static void HandleFiles(TarotDeck deck, TarotSpreads spreads){
Directory.CreateDirectory(localApplicationDataPath);
Directory.CreateDirectory(configurationDataPath);

Expand All @@ -50,9 +52,9 @@ private static void HandleFiles(TarotDeck deck, Dictionary<string, TarotSpread>
}

if(File.Exists(currentSpreadsFilePath)){
spreads.AddRange(Utilities.Deserialize<Dictionary<string, TarotSpread>>(currentSpreadsFilePath));
spreads.AddRange(Utilities.Deserialize<TarotSpreads>(currentSpreadsFilePath));
}else{
spreads.AddRange(spreads = Utilities.Deserialize<Dictionary<string, TarotSpread>>(defaultSpreadsFilePath));
spreads.AddRange(spreads = Utilities.Deserialize<TarotSpreads>(defaultSpreadsFilePath));
File.WriteAllText(currentSpreadsFilePath, Utilities.Serialize(spreads));
}

Expand All @@ -66,7 +68,7 @@ private static void HandleFiles(TarotDeck deck, Dictionary<string, TarotSpread>

if(File.Exists(userSpreadsFilePath)){
try{
spreads.AddRange(Utilities.Deserialize<Dictionary<string, TarotSpread>>(userSpreadsFilePath));
spreads.AddRange(Utilities.Deserialize<TarotSpreads>(userSpreadsFilePath));
}catch (System.Exception){};
}else{
File.WriteAllText(userSpreadsFilePath, File.ReadAllText(templateSpreadsFilePath));
Expand Down Expand Up @@ -121,14 +123,38 @@ private static int Shuffle(ShuffleOptions options, TarotDeck deck){
return 0;
}

private static int Reset(ResetOptions options, TarotDeck deck){
deck.DeserializeDeck(defaultCardsFilePath);
if(!options.Quiet) Console.WriteLine("The deck has been reset to default.");
private static int Reset(ResetOptions options, TarotDeck deck, TarotSpreads spreads){
deck = new TarotDeck();
spreads = new TarotSpreads();

if(options.Deck != default){
try{
deck.DeserializeDeck(options.Deck);
}catch(System.Exception){
if(!options.Quiet) Console.WriteLine($"Failed to load deck from '{options.Deck}' (Malformed file/path?).");
deck.DeserializeDeck(defaultCardsFilePath);
}
}else{
deck.DeserializeDeck(defaultCardsFilePath);
}

if(options.Spreads != default){
try{
spreads.AddRange(Utilities.Deserialize<TarotSpreads>(options.Spreads));
}catch(System.Exception){
if(!options.Quiet) Console.WriteLine($"Failed to load spreads from '{options.Spreads}' (Malformed file/path?).");
spreads.AddRange(Utilities.Deserialize<TarotSpreads>(defaultSpreadsFilePath));
}
}else{
spreads.AddRange(Utilities.Deserialize<TarotSpreads>(defaultSpreadsFilePath));
}

if(!options.Quiet) Console.WriteLine("The deck and spreads have been reset to default.");
SaveDeck(deck);
return 0;
}

private static int Spread(SpreadOptions options, Dictionary<string, TarotSpread> spreads, TarotDeck deck){
private static int Spread(SpreadOptions options, TarotDeck deck, TarotSpreads spreads){
List<string> names = new List<string>(spreads.Keys);

if(options.ListAll){
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ C# Tarot-reading application

### Todo

- [ ] Function to reset deck and/or spreads, either from original or from own chosen file.
- [ ] Flags for retrieving in .json-format.
- [ ] Add tarot card meanings/keywords.
- [ ] Customize help.
Expand All @@ -26,5 +25,6 @@ C# Tarot-reading application

### Done ✓

- [x] Function to reset deck and/or spreads, either from original or from own chosen file.
- [x] Current Spreads-file, similar to current deck.

0 comments on commit 0928726

Please sign in to comment.