2
2
3
3
var fs = require ( 'fs' ) ;
4
4
var path = require ( 'path' ) ;
5
+ var tiptoe = require ( 'tiptoe' ) ;
5
6
6
7
var uuid = require ( './uuid' ) ;
7
8
@@ -15,36 +16,48 @@ var set_load = function(set_code, callback) {
15
16
16
17
var setPath = path . join ( __dirname , 'db' , set_code + '.json' ) ;
17
18
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
+ }
29
37
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
+ }
45
53
46
- getData ( setPath ) ;
47
- } ) ;
54
+ cb ( ) ;
55
+ } ) ;
56
+ } ,
57
+ function ( err ) {
58
+ callback ( err , set_cache [ set_code ] ) ;
59
+ }
60
+ ) ;
48
61
} ;
49
62
50
63
/* ********************************************************************
@@ -92,8 +105,9 @@ var set_save = function(set, callback) {
92
105
// Sort cards
93
106
if ( set . cards ) {
94
107
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 ) ) ;
97
111
} ) ;
98
112
99
113
set . cards . forEach ( function ( card ) {
@@ -107,9 +121,15 @@ var set_save = function(set, callback) {
107
121
} ) ;
108
122
}
109
123
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
+ } ) ;
111
131
112
- fs . writeFile ( setPath , JSON . stringify ( set , null , 2 ) , 'utf-8' , callback ) ;
132
+ fs . writeFile ( setPath , JSON . stringify ( _set , null , 2 ) , 'utf-8' , callback ) ;
113
133
} ;
114
134
115
135
/**
@@ -130,8 +150,11 @@ var set_add = function(set, card, callback) {
130
150
}
131
151
132
152
if ( card . _title ) {
153
+ card . _title = card . _title . replace ( / * : / , ':' ) ; // Fix some name nonsense.
133
154
// 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 ) ;
135
158
card . layout = 'flip' ;
136
159
card . names = [ card . _title , card . name ] ;
137
160
@@ -226,6 +249,51 @@ var set_add = function(set, card, callback) {
226
249
setCard [ key ] = card [ key ] ;
227
250
} ) ;
228
251
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
+
229
297
// Delete unused/internal fields
230
298
[
231
299
'_title' ,
@@ -270,6 +338,45 @@ var findTokenInSet = function(name, set) {
270
338
return ( set . tokens . find ( findCB ) ) ;
271
339
} ;
272
340
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
+
273
380
module . exports = {
274
381
load : set_load ,
275
382
save : set_save ,
0 commit comments