/
GathererSymbols.java
133 lines (114 loc) · 4.94 KB
/
GathererSymbols.java
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
package org.mage.plugins.card.dl.sources;
import com.google.common.collect.AbstractIterator;
import mage.client.constants.Constants;
import org.mage.plugins.card.dl.DownloadJob;
import java.io.File;
import java.util.Iterator;
import static java.lang.String.format;
import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
import static org.mage.plugins.card.utils.CardImageUtils.getImagesDir;
/**
* The class GathererSymbols.
*
* @author Clemens Koza
* @version V0.0 25.08.2010
*/
public class GathererSymbols implements Iterable<DownloadJob> {
//TODO chaos and planeswalker symbol
//chaos: https://gatherer.wizards.com/Images/Symbols/chaos.gif
private static File outDir;
private static final String urlFmt = "https://gatherer.wizards.com/handlers/image.ashx?size=%1$s&name=%2$s&type=symbol";
private static final String[] sizes = {"small", "medium", "large"};
private static final String[] symbols = {"W", "U", "B", "R", "G",
"W/U", "U/B", "B/R", "R/G", "G/W", "W/B", "U/R", "B/G", "R/W", "G/U",
"W/U/P", "U/B/P", "B/R/P", "R/G/P", "G/W/P", "W/B/P", "U/R/P", "B/G/P", "R/W/P", "G/U/P",
"2/W", "2/U", "2/B", "2/R", "2/G",
"WP", "UP", "BP", "RP", "GP",
"X", "S", "T", "Q", "C", "E", "P"};
private static final int minNumeric = 0, maxNumeric = 16;
public GathererSymbols() {
outDir = new File(getImagesDir() + Constants.RESOURCE_PATH_SYMBOLS);
if (!outDir.exists()) {
outDir.mkdirs();
}
}
@Override
public Iterator<DownloadJob> iterator() {
return new AbstractIterator<DownloadJob>() {
private int sizeIndex, symIndex, numeric = minNumeric;
private File dir = new File(outDir, sizes[sizeIndex]);
@Override
protected DownloadJob computeNext() {
while (true) {
String sym;
if (symIndex < symbols.length) {
// take next char symbol
sym = symbols[symIndex++];
} else if (numeric <= maxNumeric) {
// take next numeric symbol
sym = String.valueOf(numeric++);
} else {
// switch to next size
sizeIndex++;
if (sizeIndex == sizes.length) {
return endOfData();
}
symIndex = 0;
numeric = 0;
dir = new File(outDir, sizes[sizeIndex]);
continue;
}
String symbol = sym.replaceAll("/", "");
File dst = new File(dir, symbol + ".gif");
// workaround for miss icons on Gatherer (no cards with it, so no icons)
// TODO: comment and try download without workaround, keep fix for symbols with "Resource not found" error
switch (symbol) {
case "WUP":
case "BRP":
case "BGP":
case "WBP":
case "UBP":
case "URP":
// skip icon download
continue;
}
// workaround for miss large size icons on Gatherer (replace it by medium size)
// TODO: comment and try download without workaround, keep fix for symbols with "Resource not found" error
int modSizeIndex = sizeIndex;
if (sizeIndex == 2) {
switch (symbol) {
case "GUP":
case "GWP":
case "RGP":
case "RWP":
case "P":
// need replace to medium size
modSizeIndex = 1;
break;
default:
// normal size
break;
}
}
switch (symbol) {
case "T":
symbol = "tap";
break;
case "Q":
symbol = "untap";
break;
case "S":
symbol = "snow";
break;
case "P":
symbol = "p";
break;
}
String url = format(urlFmt, sizes[modSizeIndex], symbol);
return new DownloadJob(sym, fromURL(url), toFile(dst), false);
}
}
};
}
}