-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeck.coffee
111 lines (96 loc) · 2.65 KB
/
Deck.coffee
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
`'use strict'`
fs = require 'fs'
Card = require './Card'
class Deck
constructor: ->
@main = []
@side = []
@ex = []
@classifiedMain = {}
@classifiedSide = {}
@classifiedEx = {}
@form = 'id'
classify: ->
@classifyPack @main, @classifiedMain
@classifyPack @side, @classifiedSide
@classifyPack @ex, @classifiedEx
this
classifyPack: (from, to) ->
for obj in from
obj = obj.id if @form == 'card'
if to[obj]
to[obj] += 1
else
to[obj] = 1
separateExFromMain: (environment) ->
@transformToCards environment if @form != 'card'
newMain = []
for card in @main
continue unless card
if card.isEx
@ex.push card
else
newMain.push card
@main = newMain
this
transformToCards: (environment) ->
return if @form == 'card'
@main = @transformPackToCards environment, @main
@side = @transformPackToCards environment, @side
@ex = @transformPackToCards environment, @ex
@form = 'card'
this
transformPackToCards: (environment, pack) ->
answer = []
answer.push environment[id] for id in pack
answer
transformToId: ->
return if @form == 'id'
@main = @transformPackToIds @main
@side = @transformPackToIds @side
@ex = @transformPackToIds @ex
@form = 'id'
this
transformPackToIds: (pack) ->
answer = []
answer.push card.id for card in pack
answer
sort: (environment) ->
@transformToCards environment if @form != 'card'
@main.sort Card.deckSortLevel
@side.sort Card.deckSortLevel
@ex.sort Card.deckSortLevel
this
@fromString: (str) ->
deck = new Deck()
focus = deck.main
lines = str.split "\n"
for line in lines
line = line.trim()
if line.endsWith 'main'
focus = deck.main
else if line.endsWith 'side'
focus = deck.side
else if line.endsWith('ex') or line.endsWith('extra')
focus = deck.ex
else
continue if line.startsWith '#'
id = parseInt line
focus.push id if id and id > 0
deck
toString: ->
getId = (card) -> if typeof card == 'number' then card else card.id
lines = ['# generated by ygojs-data.', '#main']
lines = lines.concat @main.map getId
lines.push '#extra'
lines = lines.concat @ex.map getId
lines.push '!side'
lines = lines.concat @side.map getId
lines = lines.map (id) -> if id then id else 0
lines.join "\n"
@fromFile: (filePath, callback) ->
fs.readFile filePath, (buffer) ->
callback @fromString buffer.toStirng()
@fromFileSync: (filePath) ->
@fromString fs.readFileSync(filePath).toString()
module.exports = Deck