aboutsummaryrefslogtreecommitdiff
path: root/src/deconstruct.ts
blob: 32126458b950c1992b621b1d432f626e5e999131 (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
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
export type SFItem = {
    name: string,
    id: string,
    type: 'base' | 'derived',
    recipe: Recipe,
}

export type BaseComponent = [string, number];
export type BaseComponents = Array<BaseComponent>;

export type Recipe = {
    in: Array<[string, number]>,
    out: number
};

const ITEMS: Array<SFItem> = [
    {
        name: 'Iron Plate',
        id: 'iron_plate',
        type: 'base',
        recipe: {
            in: [['iron_ingot', 30]],
            out: 20
        }
    },
    {
        name: 'Iron Rod',
        id: 'iron_rod',
        type: 'base',
        recipe: {
            in: [['iron_ingot', 15]],
            out: 15
        }
    },
    {
        name: 'Screw',
        id: 'screw',
        type: 'derived',
        recipe: {
            in: [['iron_rod', 10]],
            out: 40
        }
    },
];

export function get(id: string, items: Array<SFItem>): SFItem | undefined {
    for (const item of items) {
        if(item.id === id) {
            return item;
        }
    }
    return undefined;
}

export function add(toAdd: BaseComponent, components: BaseComponents) {
    for (const [i, val] of components.entries()) {
        if (toAdd[0] === val[0]) {
            components[i][1] += toAdd[1];
            return components;
        }
    }
    return components.push(toAdd);
}

function ratio(item: SFItem) {
    const 
}

export function derive(item: SFItem): BaseComponents {
    switch(item.type) {
        case "derived":
            let total = 0;
            for (const input of item.recipe.in) {
                const component = get(input[0], ITEMS);
                if (component) {
                    total += input[1] * ratio(component);
                }
            }
        case "base":
            return [item.recipe.in[0]];
    }
}