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
|
import { writable } from "svelte/store";
import { parseYdke } from '../utils';
import { cardDb } from '../data/cardDb';
let deck = writable({main: [], extra: [], side: []});
let deckState = {main: [], extra: [], side: []};
function sanitizeDeck(deck) {
let ret = [];
for (let id of deck) {
if (cardDb[id] !== undefined) {
ret.push(id);
}
}
return ret;
}
function setDeck(d) {
d.main = sanitizeDeck(d.main);
d.side = sanitizeDeck(d.side);
d.extra = sanitizeDeck(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 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) => {
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) => {
console.log(targetIdx);
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)
return;
}
}
initDeck();
export {
deck,
setDeck,
deckOps,
};
|