aboutsummaryrefslogtreecommitdiff
path: root/src/utils.js
blob: 05e519d10e2a6983a919aa8c99d9c1cf77d63e16 (plain)
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
function parseYdk(ydkContent) {
    const lines = ydkContent.split('\n');
    const deck = {
        main: [],
        extra: [],
        side: [],
    };
    let currentSection = null;
    for (const line of lines) {
        const trimmedLine = line.trim();
        if (!trimmedLine || trimmedLine.startsWith('#') && !['#main', '#extra'].includes(trimmedLine)) {
            continue;
        }
        if (trimmedLine === '#main') {
            currentSection = 'main';
        } else if (trimmedLine === '#extra') {
            currentSection = 'extra';
        } else if (trimmedLine === '!side') {
            currentSection = 'side';
        } else if (currentSection) {
            const cardId = parseInt(trimmedLine, 10);
            if (!isNaN(cardId)) {
                deck[currentSection].push(String(cardId));
            }
        }
    }
    return deck;
}

function genYdk(deck) {
    let ydkContent = '#created by ygodeck.mistivia.com\n';

    ydkContent += '#main\n';
    ydkContent += deck.main.join('\n') + '\n';

    ydkContent += '#extra\n';
    ydkContent += deck.extra.join('\n') + '\n';

    ydkContent += '!side\n';
    ydkContent += deck.side.join('\n') + '\n';

    return ydkContent;
}

export {
    parseYdk,
    genYdk,
};