-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecipe.ts.bak
142 lines (112 loc) · 3.7 KB
/
recipe.ts.bak
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
import { Injectable } from '@angular/core';
import { IItem, IIngredient } from './types';
import BUILTINS from './item-builtins.json';
@Injectable()
export class ItemDatabase {
private items: IItem[];
constructor() {
this.refreshDB();
}
public refreshDB(): void {
const local = this.getLocalStorage();
if (local.length) {
this.items = local;
} else {
this.items = this.getBuiltins();
}
// const builtins = this.getBuiltins();
// this.items = ([] as any).concat(
// this.getBuiltins(),
// this.getLocalStorage(),
// );
}
public all(): IItem[] {
return this.items;
}
public add(item: IItem) {
const base = this.getBaseIngredients(item);
// console.log(`Setting base ingredients for ${item.id} to:`, base);
item.baseIngredients = base;
console.log(item);
// const idx = this.items.findIndex((i) => i.id === item.id);
// if (idx === -1) {
// this.items.push(item);
// } else {
// this.items[idx] = item;
// }
// this.saveItems();
}
public import(text: string): void {
localStorage.setItem('items', text);
this.refreshDB();
}
public remove(item: IItem): void {
this.removeByID(item.id);
}
public removeByID(id: string): void {
const idx = this.items.findIndex((i) => i.id === id);
if (idx) {
this.items.splice(idx, 1);
}
this.saveItems();
}
public resetToDefault(): void {
localStorage.removeItem('items');
this.refreshDB();
}
public getItemByID(id: string): IItem | undefined {
return this.items.find((item) => item.id === id);
}
private getBuiltins(): IItem[] {
const builtins = BUILTINS as IItem[];
return BUILTINS;
}
private getLocalStorage(): IItem[] {
return JSON.parse(localStorage.getItem('items')!) || [];
}
private saveItems(): void {
localStorage.setItem('items', JSON.stringify(this.items));
}
private getBaseIngredients(item: IItem): IIngredient[] | undefined {
const baseIngredients: IIngredient[] = [];
const baseMap: Map<string, number> = new Map();
// console.log('Getting base ingredients for: ' + item.id);
// console.log(item.baseIngredients);
if (item.baseIngredients) {
return item.baseIngredients;
}
if (item.recipe) {
item.recipe.ingredients.forEach((ingredient) => {
// console.log('getting base ingredients for subingredient: ' + ingredient.item);
// Could check for base ingredients of each ingredient for speed
this.getBaseIngredientsHelper(ingredient, baseMap, 1, item.recipe.outputCount);
});
// this.getBaseIngredientsHelper({ count: 1, item: item.id }, baseIngredients, 1, 1);
} else {
return;
}
baseMap.forEach((count, itemID) => {
baseIngredients.push({ count, itemID });
});
return baseIngredients;
}
private getBaseIngredientsHelper(ingredient: IIngredient, baseMap: Map<string, number>, parentCount: number, parentRecipeOutput: number): void {
// console.log('helper:', ingredient);
const item = this.getItemByID(ingredient.itemID);
// console.log(item);
if (!item) {
throw new Error(`Can't find item with ID: ${ingredient.itemID}`);
}
const count = Math.ceil(parentCount / parentRecipeOutput) * ingredient.count;
if (!item.recipe) {
// Is base item
// console.log('is base item', parentCount, parentRecipeOutput, ingredient.count);
const previousCount = baseMap.get(ingredient.itemID) || 0;
baseMap.set(ingredient.itemID, previousCount + count);
return;
}
item.recipe.ingredients.forEach((subIngredient) => {
this.getBaseIngredientsHelper(subIngredient, baseMap, count, item.recipe!.outputCount);
});
}
}