forked from mzabsky/gauntletprinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
548 lines (455 loc) · 21.7 KB
/
MainWindow.xaml.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using MessageBox = System.Windows.MessageBox;
using TextBox = System.Windows.Controls.TextBox;
namespace GauntletPrinter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private List<TextBox> deckInputs = new List<TextBox>();
private List<TextBox> sideboardInputs = new List<TextBox>();
public MainWindow()
{
InitializeComponent();
this.deckInputs.Add(this.deck1);
this.deckInputs.Add(this.deck2);
this.deckInputs.Add(this.deck3);
this.deckInputs.Add(this.deck4);
this.deckInputs.Add(this.deck5);
this.sideboardInputs.Add(this.sideboard1);
this.sideboardInputs.Add(this.sideboard2);
this.sideboardInputs.Add(this.sideboard3);
this.sideboardInputs.Add(this.sideboard4);
this.sideboardInputs.Add(this.sideboard5);
}
private void LoadFromFile(string path, int deckNumber)
{
if (File.Exists(path))
{
var content = File.ReadAllText(path);
var decks = content.Split(new string[] { "\n==========\n", "\n\r==========\n\r", "\r\n==========\r\n" }, StringSplitOptions.None);
for (int i = 0; i < decks.Length; i++)
{
if (deckNumber + i < deckInputs.Count)
{
var deckString = decks[i].Trim();
var sideboardSeparatorPosition = Math.Max(
deckString.LastIndexOf("\n\r", System.StringComparison.Ordinal),
deckString.LastIndexOf("\n\n", System.StringComparison.Ordinal));
if (sideboardSeparatorPosition != -1)
{
this.deckInputs[deckNumber + i].Text =
deckString.Substring(0, sideboardSeparatorPosition).Trim();
this.sideboardInputs[deckNumber + i].Text =
deckString.Substring(sideboardSeparatorPosition + 1).Trim();
}
else
{
this.deckInputs[deckNumber + i].Text = deckString;
this.sideboardInputs[deckNumber + i].Text = "";
}
}
}
this.generate.Focus();
}
else
{
MessageBox.Show("Input file \"" + path + "\" not found.");
}
}
private void LoadFromFileWithDialog(int deckNumber)
{
var dialog = new OpenFileDialog();
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.LoadFromFile(dialog.FileName, deckNumber);
}
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
this.LoadFromFile(args[1], 0);
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
this.IsEnabled = false;
//this.InvalidateVisual();
// Give the window a chance to redraw
await Task.Delay(TimeSpan.FromMilliseconds(1));
try
{
var allCards = JsonConvert.DeserializeObject<Dictionary<string, Card>>(File.ReadAllText("AllCards.json", Encoding.UTF8)).Values.ToList();
var additionalCards = new List<Card>();
// Split, double faced and flip cards are represented as a separate left and right half in the source data
foreach (var leftHalf in allCards.Where(p => p.Layout == "split" && p.Names.FirstOrDefault() == p.Name))
{
var rightHalfName = leftHalf.Names.LastOrDefault();
var rightHalf = allCards.Single(p => p.Name == rightHalfName);
var splitCard = new Card
{
Name = leftHalf.Name + " // " + rightHalf.Name,
ManaCost = leftHalf.ManaCost + "//" + rightHalf.ManaCost,
Type = leftHalf.Type + "//" + rightHalf.Type,
Text = leftHalf.Text + "\n // " + rightHalf.Text
};
additionalCards.Add(splitCard);
}
foreach (var leftHalf in allCards.Where(p => p.Layout == "double-faced" && p.Names.FirstOrDefault() == p.Name))
{
var rightHalfName = leftHalf.Names.LastOrDefault();
var rightHalf = allCards.Single(p => p.Name == rightHalfName);
leftHalf.Name = leftHalf.Name;
leftHalf.ManaCost = leftHalf.ManaCost;
leftHalf.Type = leftHalf.Type;
leftHalf.Text = "Transforms into " + rightHalf.Name + " [" + rightHalf.Type + ", " + rightHalf.Power + "/" + rightHalf.Toughness + "]; " + leftHalf.Text + "\n // " + (rightHalf.Text != null ? rightHalf.Text.Replace(rightHalf.Name, "~") : "");
}
foreach (var leftHalf in allCards.Where(p => p.Layout == "flip" && p.Names.FirstOrDefault() == p.Name))
{
var rightHalfName = leftHalf.Names.LastOrDefault();
var rightHalf = allCards.Single(p => p.Name == rightHalfName);
leftHalf.Name = leftHalf.Name;
leftHalf.ManaCost = leftHalf.ManaCost;
leftHalf.Type = leftHalf.Type;
leftHalf.Text = "Flips to " + rightHalf.Name + " [" + rightHalf.Type + (rightHalf.Power != null ? ", " + rightHalf.Power + "/" + rightHalf.Toughness : "") + "]; " + leftHalf.Text + "\n // " + (rightHalf.Text != null ? rightHalf.Text.Replace(rightHalf.Name, "~") : "");
}
allCards.AddRange(additionalCards);
// Parse the deck strings and shorten the text of cards that are included in one or more decks
var shortener = new CardTextShortener();
var decks = new List<List<Card>>();
var sideboards = new List<List<Card>>();
for (int deckNumber = 0; deckNumber < this.deckInputs.Count; deckNumber++)
{
if (this.deckInputs[deckNumber].Text.Trim().Length == 0)
{
continue;
}
var deck = ParseDeckString(this.deckInputs[deckNumber].Text, allCards);
var sideboard = ParseDeckString(this.sideboardInputs[deckNumber].Text, allCards);
foreach (var card in deck.Concat(sideboard))
{
shortener.ProcessCard(card, this.grayscaleSymbols.IsChecked == true, this.omitTypeLineForBasics.IsChecked == true);
}
decks.Add(deck);
sideboards.Add(sideboard);
}
// Validate sizes of the decks
if (decks.Any(p => p.Count != decks[0].Count))
{
var numberOfValidDecks = decks.TakeWhile(p => p.Count == decks[0].Count).Count();
var invalidDeck = decks.First(p => p.Count != decks[0].Count);
throw new ApplicationException("All decks must contain the same number of cards (deck 1 contains " + decks[0].Count + " cards, deck " + (numberOfValidDecks + 1) + " contains " + invalidDeck.Count + " cards.)");
}
// Validate sizes of the sideboards
if (sideboards.Any(p => p.Count != sideboards[0].Count))
{
var numberOfValidDecks = sideboards.TakeWhile(p => p.Count == sideboards[0].Count).Count();
var invalidDeck = sideboards.First(p => p.Count != sideboards[0].Count);
throw new ApplicationException("All sideboards must contain the same number of cards (sideboard 1 contains " + sideboards[0].Count + " cards, sideboard " + (numberOfValidDecks + 1) + " contains " + invalidDeck.Count + " cards.)");
}
// Format the decks into HTML
var arranger = new SimpleArranger();
decks = arranger.ArrangeCards(decks);
sideboards = arranger.ArrangeCards(sideboards);
string str = @"
<html>
<head>
<meta charset=""utf-8"">
<style>
.card
{
table-layout: fixed;
display:inline-block;
text-overflow:ellipsis;
}
.card > div {
width: 57mm;
height: 83mm;
border: 1px black solid;";
if(this.cardSpacing.IsChecked == true) str += @"margin: 0.5mm;";
str += @"}
.card table {
width: 100%;
height: 100%;
font-size: 3mm; /*11px;*/
padding: 0;
border-spacing: 0;
}";
if(this.colorCode.IsChecked == true)
{
str += @"tr.card1 td{
border-left: 4px #B2F1BA solid;
}
tr.card2 td{
border-left: 4px #C3B5EB solid;
}
tr.card3 td{
border-left: 4px #FFF4BC solid;
}
tr.card4 td{
border-left: 4px #FFBFBC solid;
}
tr.card5 td{
border-left: 4px #83EBE4 solid;
}";
}
str += @".card td {
padding: 1px 1px 1px 1mm;
}
div.sideboard table
{
border-top: 3px black dashed;
}
.cardNameRow {
height: 3.5mm;
white-space:nowrap;
}
.deckNumber {
float:left;
margin-right: 1mm;
/*border-radius: 50%;
width: 10px;
height: 10px;
padding: 1px;
background: #fff;
border: 2px solid #666;
color: #666;
text-align: center;*/
}
.cardName {
float:left;
font-weight: bold;
font-size: 1.1em;
}
.manaCost {
float:right;
}
.cardSeparator {
height:1px;
font-size: 1px;
padding: 0;
}
.cardSeparator hr{
border: 0;
border-top: 1px black solid;
background: none;
}
.cardTypeRow {
height: 3.5mm;
/*white-space:nowrap;*/
}
.cardType {
float:left;
}
.powerToughness {
float:right;
}
</style>
</head>
<body>
";
var deckSize = decks.Max(p => p.Count);
var totalSize = deckSize + sideboards.Max(p => p.Count);
for (var i = 0; i < totalSize; i++)
{
str += @"<div class=""card " + (i >= deckSize ? "sideboard" : "") + @""">
<div>
<table>";
for (int j = 0; j < decks.Count; j++ )
{
var deck = decks[j].Concat(sideboards[j]).ToList();
if (deck.Count > i)
{
var card = deck[i];
if(j > 0)
{
str += @"
<tr class=""cardSeparator card" + (j + 1) + @""">
<td><hr /></td>
</tr>";
}
str += @"<tr class=""cardNameRow card" + (j + 1) + @""">
<td>
" +
(this.deckNumbers.IsChecked == true
? @"<span class=""deckNumber"">" + (j + 1) + @"</span> "
: "") + @"
<span class=""cardName"">" + card.Name + @"</span>
<span class=""manaCost"">" + (card.ManaCost ?? "") + @"</span>
</td>
</tr>";
if (!(this.omitTypeLineForBasics.IsChecked == true && card.Type.Contains("Basic")))
{
str += @"<tr class=""cardTypeRow card" + (j + 1) + @""">
<td><span class=""cardType"">" + card.Type + @"</span> <span class=""powerToughness"">" + (card.Power != null ? card.Power + "/" + card.Toughness : (card.Loyalty ?? "")) + @"</span></td>
</tr>";
}
/*if (card.Text != null)
{*/
str += @"<tr class=""cardText card" + (j + 1) + @""">
<td>" + card.Text + @"</td>
</tr>";
/*}*/
}
else
{
str += @"<tr class=""cardNameRow"">
<td>-</td>
</tr>";
}
}
str += @"
<!--<tr></tr>-->
</table>
</div>
</div>";
}
File.WriteAllText("cards.html", str, new UTF8Encoding(true));
if (MessageBox.Show("File generated. Do you wish to open it?" + Environment.NewLine + Environment.NewLine + "WARNING: Internet Explorer does not display the cards correctly. Google Chrome is recommended to print the cards.", "Finished", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Process.Start("cards.html");
}
}
catch (ApplicationException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
this.IsEnabled = true;
}
private static List<Card> ParseDeckString(string deckString, List<Card> allCards)
{
var deck = new List<Card>();
var deckMatch = Regex.Match(deckString, @"\s*(\s*(?<count>\d*)(?<card>.*))*");
for (int i = 0; i < deckMatch.Groups["card"].Captures.Count; i++)
{
var name = deckMatch.Groups["card"].Captures[i].ToString().Trim();
if (name == "")
{
continue;
}
var countString = deckMatch.Groups["count"].Captures[i].ToString() == ""
? "1"
: deckMatch.Groups["count"].Captures[i].ToString().Trim();
var count = int.Parse(countString);
for (int j = 0; j < count; j++)
{
var foundCard =
allCards.SingleOrDefault(p => p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
if (foundCard == null)
{
throw new ApplicationException("Card \"" + name + "\" not found.");
}
else
{
deck.Add(foundCard);
}
}
}
return deck;
}
private void GetDeckFromWeb(int deckNumber)
{
var dialog = new GetFromWebDialog();
if (dialog.ShowDialog() != true)
{
return;
}
this.deckInputs[deckNumber].Text = dialog.DeckString;
this.sideboardInputs[deckNumber].Text = dialog.SideboardString;
}
private void GetFromWeb1_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(0);
}
private void GetFromWeb2_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(1);
}
private void GetFromWeb3_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(2);
}
private void GetFromWeb4_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(3);
}
private void GetFromWeb5_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(4);
}
/*private void GetFromWeb6_OnClick(object sender, RoutedEventArgs e)
{
this.GetDeckFromWeb(this.deck6);
}*/
private void LoadFromFile1_OnClick(object sender, RoutedEventArgs e)
{
this.LoadFromFileWithDialog(0);
}
private void LoadFromFile2_OnClick(object sender, RoutedEventArgs e)
{
this.LoadFromFileWithDialog(1);
}
private void LoadFromFile3_OnClick(object sender, RoutedEventArgs e)
{
this.LoadFromFileWithDialog(2);
}
private void LoadFromFile4_OnClick(object sender, RoutedEventArgs e)
{
this.LoadFromFileWithDialog(3);
}
private void LoadFromFile5_OnClick(object sender, RoutedEventArgs e)
{
this.LoadFromFileWithDialog(4);
}
private async void UpdateCardData_OnClick(object sender, RoutedEventArgs e)
{
if (
MessageBox.Show(
"GauntletPrinter will download latest card data from mtgjson.com. This operation requires internet connection and will download ~5 MB of data. It may take a moment.",
"", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
return;
}
this.IsEnabled = false;
var client = new WebClient();
client.Encoding = Encoding.UTF8;
var requestUri = new Uri("http://mtgjson.com/json/AllCards.json");
try
{
string result = await client.DownloadStringTaskAsync(requestUri);
File.WriteAllText("AllCards.json", result, Encoding.UTF8);
MessageBox.Show("Card data updated!");
}
catch (WebException)
{
MessageBox.Show("Could not download card data from the internet.");
}
catch (IOException)
{
MessageBox.Show("Could not save the downloaded data to the hard drive.");
}
this.IsEnabled = true;
}
}
}