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
|
import { writable } from "svelte/store";
import { parseYdke } from './utils';
import { getCardDb, getAltId, cardLimit } from './card_db';
let deck = writable({main: [], extra: [], side: []});
let deckState = {main: [], extra: [], side: []};
let defaultFormat = 'none';
let format = writable(defaultFormat);
let formatState = defaultFormat;
function sanitizeDeck(cardCnt, deck) {
let cardDb = getCardDb();
let altId = getAltId();
let ret = [];
for (let id of deck) {
if (altId[id] !== undefined) {
id = altId[id];
}
if (cardDb[id] === undefined) {
continue;
}
if (!cardCnt.has(id)) {
cardCnt.set(id, 0);
}
if (cardCnt.get(id) >= 3) {
continue;
}
ret.push(id);
cardCnt.set(id, cardCnt.get(id) + 1);
}
return ret;
}
function setDeck(d) {
let cardCnt = new Map();
d.main = sanitizeDeck(cardCnt, d.main);
d.side = sanitizeDeck(cardCnt, d.side);
d.extra = sanitizeDeck(cardCnt, d.extra);
deckState = d;
deck.set(d);
localStorage.setItem('cachedDeck', JSON.stringify(d));
};
function canAdd(d, id) {
let count = 0;
for (let c of d.main) {
if (c === id) count += 1;
}
for (let c of d.side) {
if (c === id) count += 1;
}
for (let c of d.extra) {
if (c === id) count += 1;
}
if (count + 1 > 3) return false;
return true;
}
let deckOps = {
"deleteCard": (from, idx) => {
if (from === 'main') {
deckState.main.splice(idx, 1);
setDeck(deckState);
} else if (from === 'side') {
deckState.side.splice(idx, 1);
setDeck(deckState);
} else if (from === 'extra') {
deckState.extra.splice(idx, 1);
setDeck(deckState);
}
},
"move": (from, to, fromIdx, toIdx) => {
let cardDb = getCardDb();
let id = deckState[from][fromIdx];
if (cardDb[id].isExtra && to === 'main') return;
if (!cardDb[id].isExtra && to === 'extra') return;
deckState[from].splice(fromIdx, 1);
if (toIdx === -1) deckState[to].push(id);
else deckState[to].splice(toIdx, 0, id);
setDeck(deckState);
},
"add2extra": (id, targetIdx) => {
let cardDb = getCardDb();
if (!cardDb[id].isExtra) return;
let d = deckState;
if (canAdd(d, id)) {
if (targetIdx === -1)
d.extra.push(id);
else {
d.extra.splice(targetIdx, 0, id);
}
setDeck(d);
}
},
"add2main": (id, targetIdx) => {
let cardDb = getCardDb();
if (cardDb[id].isExtra) return;
let d = deckState;
if (canAdd(d, id)) {
if (targetIdx === -1)
d.main.push(id);
else {
d.main.splice(targetIdx, 0, id);
}
setDeck(d);
}
},
"add2side": (id, targetIdx) => {
let d = deckState;
if (canAdd(d, id)) {
if (targetIdx === -1)
d.side.push(id);
else {
d.side.splice(targetIdx, 0, id);
}
setDeck(d);
}
}
};
function initDeck() {
let url = window.location.href.split('#');
if (url.length === 2) {
let deck = parseYdke(url[1]);
if (deck.main.length > 0 || deck.extra.length > 0 || deck.extra.length > 0) {
setDeck(deck);
window.location.href = url[0];
return;
}
}
let cachedDeck = localStorage.getItem('cachedDeck');
if (cachedDeck !== null) {
cachedDeck = JSON.parse(cachedDeck);
setDeck(cachedDeck)
}
let cachedFormat = localStorage.getItem('format');
if (cachedFormat !== null) {
setFormat(cachedFormat);
}
}
function setFormat(newFormat) {
if (newFormat === 'none' || newFormat === 'ocg') {
localStorage.setItem('format', newFormat);
formatState = newFormat;
format.set(newFormat);
setDeck(deckState);
}
}
export {
deck,
format,
setFormat,
setDeck,
deckOps,
initDeck,
};
|