-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (131 loc) · 5.11 KB
/
Program.cs
File metadata and controls
155 lines (131 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.CommandLine;
namespace Magic;
internal class Program
{
static string DataFile = "";
static string InputFile = "";
static string OutputFile = "";
static string CorrectionFile = "";
static bool DownloadData = false;
static bool DownloadImages = false;
static void Main(string[] args)
{
// dotnet run -- -h
Console.WriteLine("Magic Card Manager running\n");
// define command line usage
var dataSourceOption = new Option<FileInfo?>(
name: "--data",
description: "Scryfall JSON card data file. (Used as filename if --download selected)"
);
var dataDownloadOption = new Option<bool>(
name: "--download",
description: "Download new bulk data file from Scryfall? (True if --data not provided)",
getDefaultValue: () => false
);
var imageDownloadOption = new Option<bool>(
name: "--download-images",
description: "Download card images from Scryfall? (Stored in output folder)",
getDefaultValue: () => false
);
var cardInputOption = new Option<FileInfo?>(
name: "--input",
description: "TCG CSV user cards file."
);
var correctionInputOption = new Option<FileInfo?>(
name: "--corrections",
description: "CSV with corrections for TCG user cards file."
);
var cardOutputOption = new Option<FileInfo>(
name: "--output",
description: "CSV output file for organized user cards.",
getDefaultValue: () => new FileInfo("output/results.csv")
);
var rootCommand = new RootCommand("Magic card manager");
rootCommand.AddOption(dataSourceOption);
rootCommand.AddOption(dataDownloadOption);
rootCommand.AddOption(imageDownloadOption);
rootCommand.AddOption(cardInputOption);
rootCommand.AddOption(correctionInputOption);
rootCommand.AddOption(cardOutputOption);
rootCommand.SetHandler((data, download, images, input, corrections, output) =>
{
if (ProcessArgs(data, download, images, input, corrections, output))
RunCardManagement();
},
dataSourceOption, dataDownloadOption, imageDownloadOption, cardInputOption, correctionInputOption, cardOutputOption
);
rootCommand.Invoke(args);
}
static void PrintWarning(string message, bool includeUsage = false)
{
Console.WriteLine("\nWarning: " + message);
if (includeUsage)
Console.WriteLine("Run program with -h for usage directions.");
}
static bool ProcessArgs(FileInfo? data, bool download, bool images, FileInfo? input, FileInfo? corrections, FileInfo output)
{
OutputFile = output.FullName;
DownloadImages = images;
// verify input source
if (input == null)
{
PrintWarning("Input file for user cards is a required argument.", includeUsage: true);
return false;
}
if (!File.Exists(input.FullName)) {
PrintWarning($"Input file {input.Name} not found.", includeUsage: true);
return false;
}
InputFile = input.FullName;
// verify corrections
if (corrections != null)
{
if (!File.Exists(corrections.FullName)) {
PrintWarning($"Corrections file {corrections.Name} not found.", includeUsage: true);
return false;
}
CorrectionFile = corrections.FullName;
}
// verify data source
DownloadData = download;
if (data == null)
{
DownloadData = true; // default to downloading if no data file provided
}
else
{
DataFile = data.FullName;
// if not downloading, ensure data is valid file
if (!DownloadData && !File.Exists(data.FullName)) {
PrintWarning($"Data file {data.Name} not found.", includeUsage: true);
return false;
}
}
if (string.IsNullOrEmpty(DataFile))
DataFile = ScryfallAPIHandler.DefaultFilePath;
return true;
}
static void RunCardManagement()
{
var cardManager = new CardManager(JSONManager.Instance, CSVManager.Instance, CSVManager.Instance);
// import cards from user's TCG data
if (!cardManager.ImportUserCards(InputFile, CorrectionFile))
{
PrintWarning("No user data to merge; exiting program.");
return;
}
// import all card data from Scryfall
if (DownloadData && !cardManager.DownloadCardData(DataFile))
{
PrintWarning("No source data available; exiting program.");
return;
}
if (!cardManager.ImportCardData(DataFile))
{
PrintWarning("No source data to reference; exiting program.");
return;
}
// match up user cards with Scryfall data; export to file
cardManager.MatchCards(OutputFile, DownloadImages);
}
}