Skip to content
This repository was archived by the owner on Dec 21, 2018. It is now read-only.

Commit 3100a85

Browse files
committed
Initial fixes support
1 parent c6e590b commit 3100a85

File tree

2 files changed

+193
-33
lines changed

2 files changed

+193
-33
lines changed

data/fixes_LEA.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[
2+
{
3+
"match": {
4+
"name": [
5+
"Elvish Archers",
6+
"Goblin Balloon Brigade",
7+
"Wall of Air"
8+
]
9+
},
10+
"flavorAddExclamation": true
11+
},
12+
{
13+
"match": {
14+
"name": [
15+
"Jade Statue",
16+
"Scathe Zombies"
17+
]
18+
},
19+
"flavorAddDash": true
20+
},
21+
{
22+
"match": {
23+
"multiverseid": 243
24+
},
25+
"replace": {
26+
"artist": "Mark Tedin"
27+
}
28+
},
29+
{
30+
"match": {
31+
"multiverseid": 248
32+
},
33+
"replace": {
34+
"artist": "Mark Poole"
35+
}
36+
},
37+
{
38+
"match": {
39+
"multiverseid": 220
40+
},
41+
"replace": {
42+
"artist": "Dan Frazier"
43+
}
44+
},
45+
{
46+
"match": {
47+
"multiverseid": 285
48+
},
49+
"replace": {
50+
"artist": "Jesper Myrfors"
51+
}
52+
}
53+
]

sets.js

+140-33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var fs = require('fs');
44
var path = require('path');
5+
var tiptoe = require('tiptoe');
56

67
var uuid = require('./uuid');
78

@@ -15,36 +16,48 @@ var set_load = function(set_code, callback) {
1516

1617
var setPath = path.join(__dirname, 'db', set_code + '.json');
1718

18-
var getData = function(path) {
19-
fs.readFile(path, function(err, data) {
20-
if (err) {
21-
callback(err);
22-
return;
23-
}
24-
25-
set_cache[set_code] = JSON.parse(data);
26-
callback(null, set_cache[set_code]);
27-
});
28-
};
19+
tiptoe(
20+
function() {
21+
var cb = this;
22+
// Check if we have a database with this code
23+
fs.stat(setPath, function(err, stats) {
24+
if (err) {
25+
// Check if we have instructions for the set
26+
var fallbackPath = path.join(__dirname, 'data', 'header_' + set_code + '.json');
27+
fs.stat(fallbackPath, function(err2, stats) {
28+
if (err2) {
29+
// TODO: Create a proper error message
30+
cb(err2);
31+
return;
32+
}
33+
cb(null, fallbackPath);
34+
});
35+
return;
36+
}
2937

30-
// Check if we have a database with this code
31-
fs.stat(setPath, function(err, stats) {
32-
if (err) {
33-
// Check if we have instructions for the set
34-
var fallbackPath = path.join(__dirname, 'data', 'header_' + set_code + '.json');
35-
fs.stat(fallbackPath, function(err2, stats) {
36-
if (err2) {
37-
// TODO: Create a proper error message
38-
callback(err2);
39-
return;
40-
}
41-
getData(fallbackPath);
42-
});
43-
return;
44-
}
38+
cb(null, setPath);
39+
});
40+
},
41+
function(dataPath) {
42+
fs.readFile(dataPath, this);
43+
},
44+
function(setData) {
45+
var cb = this;
46+
47+
set_cache[set_code] = JSON.parse(setData);
48+
49+
fs.readFile(path.join(__dirname, 'data', 'fixes_' + set_code + '.json'), 'utf-8', function(err, data) {
50+
if (!err) {
51+
set_cache[set_code]._fixes = JSON.parse(data);
52+
}
4553

46-
getData(setPath);
47-
});
54+
cb();
55+
});
56+
},
57+
function(err) {
58+
callback(err, set_cache[set_code]);
59+
}
60+
);
4861
};
4962

5063
/* ********************************************************************
@@ -92,8 +105,9 @@ var set_save = function(set, callback) {
92105
// Sort cards
93106
if (set.cards) {
94107
set.cards = set.cards.sort(function(a, b) {
95-
96-
return(sortAlphaNum(a.number, b.number));
108+
if (a.number && b.number)
109+
return(sortAlphaNum(a.number, b.number));
110+
return(a.name.localeCompare(b.name));
97111
});
98112

99113
set.cards.forEach(function(card) {
@@ -107,9 +121,15 @@ var set_save = function(set, callback) {
107121
});
108122
}
109123

110-
delete set.meldcards;
124+
// Create a new object excluding internal stuff
125+
var _set = {};
126+
var ignoreKeys = [ 'meldcards', '_fixes' ];
127+
Object.keys(set).forEach(function(k) {
128+
if (ignoreKeys.indexOf(k) < 0)
129+
_set[k] = set[k];
130+
});
111131

112-
fs.writeFile(setPath, JSON.stringify(set, null, 2), 'utf-8', callback);
132+
fs.writeFile(setPath, JSON.stringify(_set, null, 2), 'utf-8', callback);
113133
};
114134

115135
/**
@@ -130,8 +150,11 @@ var set_add = function(set, card, callback) {
130150
}
131151

132152
if (card._title) {
153+
card._title = card._title.replace(/ *:/, ':'); // Fix some name nonsense.
133154
// Check if we're consistent. Make actions if we're not.
134-
if (card._title != card.name && card.layout != 'double-sided' && card.layout != 'split') {
155+
if (card._title.toLowerCase() != card.name.toLowerCase() && card.layout != 'double-sided' && card.layout != 'split') {
156+
console.log('===FIX===');
157+
console.log('"%s"\n"%s"', card._title, card.name);
135158
card.layout = 'flip';
136159
card.names = [ card._title, card.name ];
137160

@@ -226,6 +249,51 @@ var set_add = function(set, card, callback) {
226249
setCard[key] = card[key];
227250
});
228251

252+
// FIXES
253+
if (set._fixes) {
254+
set._fixes.forEach(function(fix) {
255+
var matches = fix.match;
256+
var match = false;
257+
258+
// Check for name
259+
if (matches.name) {
260+
if (Array.isArray(matches.name)) {
261+
if (matches.name.indexOf(setCard.name) >= 0)
262+
match = true;
263+
}
264+
else
265+
if (matches.name == setCard.name)
266+
match = true;
267+
}
268+
// Check for multiverseid
269+
if (matches.multiverseid) {
270+
if (Array.isArray(matches.multiverseid)) {
271+
if (matches.multiverseid.indexOf(setCard.multiverseid) >= 0)
272+
match = true;
273+
}
274+
else
275+
if (matches.multiverseid == setCard.multiverseid)
276+
match = true;
277+
}
278+
279+
if (match) {
280+
// Apply fix.
281+
console.log('Applying fix for "%s"...', setCard.name);
282+
283+
var availableFixes = Object.keys(fixes);
284+
Object.keys(fix).forEach(function(fixName) {
285+
if (fixName == 'match') return;
286+
if (availableFixes.indexOf(fixName) < 0) {
287+
console.log('Unavailable fix "%s" for card "%s".', fixName, setCard.name);
288+
return;
289+
}
290+
291+
fixes[fixName](setCard, fix[fixName]);
292+
});
293+
}
294+
});
295+
}
296+
229297
// Delete unused/internal fields
230298
[
231299
'_title',
@@ -270,6 +338,45 @@ var findTokenInSet = function(name, set) {
270338
return(set.tokens.find(findCB));
271339
};
272340

341+
var fixes = {
342+
'flavorAddExclamation': function(card, fix) {
343+
var flavor = card.flavor;
344+
var lastIndexOfQuote = flavor.lastIndexOf('"');
345+
if (lastIndexOfQuote < 0) {
346+
console.log('trying to apply fix flavorAddExclamation, but there are no quotes in the flavor: >>%s<<', flavor);
347+
return;
348+
}
349+
else {
350+
if (flavor[lastIndexOfQuote] != '!')
351+
flavor = flavor.substr(0, lastIndexOfQuote) + '!' + flavor.substr(lastIndexOfQuote);
352+
}
353+
354+
card.flavor = flavor;
355+
},
356+
'flavorAddDash': function(card, fix) {
357+
var flavor = card.flavor;
358+
var firstQuote = flavor.indexOf('"');
359+
var secondQuote = flavor.indexOf('"', firstQuote + 1);
360+
var lastIndexOfQuote = secondQuote;
361+
362+
if (lastIndexOfQuote < 0) {
363+
console.log('trying to apply fix flavorAddDash, but there are no quotes in the flavor: >>%s<<', flavor);
364+
return;
365+
}
366+
else {
367+
if (flavor[lastIndexOfQuote + 1] != '—')
368+
flavor = flavor.substr(0, lastIndexOfQuote + 1) + '—' + flavor.substr(lastIndexOfQuote + 1);
369+
}
370+
371+
card.flavor = flavor;
372+
},
373+
'replace': function(card, fix) {
374+
Object.keys(fix).forEach(function(field) {
375+
card[field] = fix[field];
376+
});
377+
}
378+
};
379+
273380
module.exports = {
274381
load: set_load,
275382
save: set_save,

0 commit comments

Comments
 (0)