Skip to content

Commit

Permalink
Add spread commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Jul 29, 2021
1 parent 6d68695 commit e19ade7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Classes/TarotSpread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}");
}
}

Expand Down
22 changes: 18 additions & 4 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
}
Expand All @@ -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;}
}
}
68 changes: 57 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
using System;
using System.IO;
using System.Collections.Generic;
using CommandLine;

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 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<string, string[]> spreads_uninitialized = new Dictionary<string, string[]>();

if(File.Exists(filePath)){
deck.DeserializeDeck(filePath);
}else{
deck.DeserializeDeck(defaultFilePath);
deck.DeserializeDeck(defaultCardsFilePath);
File.WriteAllText(filePath, deck.SerializeDeck());
}
spreads_uninitialized = Utilities.Deserialize<Dictionary<string, string[]>>(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){
Expand Down Expand Up @@ -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<string, string[]> spreads_unitit, TarotDeck deck){
List<string> spread_names = 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];
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());
}
}
}

0 comments on commit e19ade7

Please sign in to comment.