Skip to content

Commit

Permalink
Add user cards-, and spreads-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Jul 29, 2021
1 parent e19ade7 commit 4eb1a48
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 23 deletions.
1 change: 0 additions & 1 deletion Classes/TarotDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public TarotDeck(){
}

public void DeserializeDeck(string filePath){
_deck = new List<TarotCard>();
_deck.AddRange(Utilities.Deserialize<List<TarotCard>>(filePath));
}

Expand Down
7 changes: 7 additions & 0 deletions Data/template_cards.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
/* Add any amount of cards that should be added to the deck in the following format */

/*
{"Name": "Name of Card"}
*/
]
13 changes: 13 additions & 0 deletions Data/template_spreads.json
Original file line number Diff line number Diff line change
@@ -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..."
]
*/
]
76 changes: 54 additions & 22 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> spreads_uninitialized = new Dictionary<string, string[]>();

if(File.Exists(filePath)){
deck.DeserializeDeck(filePath);
}else{
deck.DeserializeDeck(defaultCardsFilePath);
File.WriteAllText(filePath, deck.SerializeDeck());
}
spreads_uninitialized = Utilities.Deserialize<Dictionary<string, string[]>>(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<string, string[]> 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<Dictionary<string, string[]>>(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<Dictionary<string, string[]>>(userSpreadsFilePath));
}catch (System.Exception){};
}else{
File.WriteAllText(userSpreadsFilePath, File.ReadAllText(templateSpreadsFilePath));
}
}

private static int Get(GetOptions options, TarotDeck deck){
Expand Down Expand Up @@ -87,20 +119,20 @@ private static int Reset(ResetOptions options, TarotDeck deck){
}

private static int Spread(SpreadOptions options, Dictionary<string, string[]> spreads_unitit, TarotDeck deck){
List<string> spread_names = new List<string>(spreads_unitit.Keys);
List<string> spreadNames = new List<string>(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++){
Console.WriteLine($"\t{j+1}) {positions[j]}");
}
}
}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];
Expand All @@ -123,7 +155,7 @@ private static int Spread(SpreadOptions options, Dictionary<string, string[]> sp
}

static void SaveDeck(TarotDeck deck){
File.WriteAllText(filePath, deck.SerializeDeck());
File.WriteAllText(currentDeckFilePath, deck.SerializeDeck());
}
}
}
16 changes: 16 additions & 0 deletions Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T,U>(this Dictionary<T,U> target, Dictionary<T,U> 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);
}
}
}
}
}

0 comments on commit 4eb1a48

Please sign in to comment.