-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
119 lines (89 loc) · 2.94 KB
/
script.js
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
function createElement(elType, contents, classes) {
let el = document.createElement(elType)
let text = document.createTextNode(contents)
el.appendChild(text)
el.className = classes
//document.body.appendChild(el)
return el;
}
async function getData(uri) {
let response = await fetch(uri);
let data = await response.json();
return data
}
//global variables to increase speed
let craftingCodeList;
let itemList;
async function searchAndRenderItem() {
console.log("asdfasfafdad")
//if the data doesn't exist, load it and show the search bar
if (!craftingCodeList || !itemList) {
craftingCodeList = await getData("craftingCodeList.json")
itemList = await getData("itemList.json")
let searchbar = createElement("input")
searchbar.setAttribute("onchange", "searchAndRenderItem()")
searchbar.id = "searchbar"
searchbar.setAttribute("placeholder", "Type an item name...")
document.body.appendChild(searchbar)
document.getElementById("loading").style.display = "none"
}
/*
let container = createElement("div", "", "container");
while (container.firstChild) { parent.removeChild(parent.firstChild);
}
*/
function parseIngredients(recipe) {
let final;
let ingredientsRaw = Object.entries(craftingCodeList).find(([key, value]) => value == recipe)
if (ingredientsRaw) {
let ingredientsList = ingredientsRaw[0].split(/(?<=-0|-16|-32|X)/gi);
final = ingredientsList.map((x, index) => {
if (x !== "X") {
return index + 1 + ": " + itemList[x].name
} else {
return index + 1 + ": nothing"
}
})
} else {
final = "No recipe"
}
return final;
}
let query = document.getElementById("searchbar").value
console.log(query)
//if there's something to parse
if (query) {
//remove the old container, if it exists
if (document.getElementsByClassName("container")[0]) {
document.getElementsByClassName("container")[0].remove()
}
let container = createElement("div", "", "container")
let regex = new RegExp(".*" + query + ".*", "gi")
//`.*${query}.*`
let results = Object.entries(itemList).filter(([key, value]) => value.name.match(regex))
if (results) {
results.forEach(x => {
let name = createElement("p", x[1].name, "bolded")
container.appendChild(name)
let recipe = createElement("p", parseIngredients(x[0]), "")
container.appendChild(recipe)
if (x[1].effects) {
let effects = createElement("p", "Effects: " + JSON.stringify(x[1].effects), "")
container.appendChild(effects)
}
console.log(x[1].name)
console.log(x)
console.log(parseIngredients(x[0]))
console.log("----------\n")
})
}
document.body.appendChild(container)
}
}
searchAndRenderItem()
/*
let par = document.createElement("p")
let text = document.createTextNode("asdfasdfsa")
let el = par.appendChild(text)
document.body.appendChild(el)
*/