forked from AussieGuy0/ss14-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
222 lines (170 loc) · 7.77 KB
/
search.js
File metadata and controls
222 lines (170 loc) · 7.77 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Requires fuse.js imported.
function renderReactants(reactants) {
const ul = document.createElement('ul')
reactants.forEach(r => {
const li = document.createElement('li')
li.textContent = `${r.amount} ${r.id}`
ul.appendChild(li)
});
return ul
}
function formatEffect(effect) {
if (!effect.type || !effect.data) return '';
const type = effect.type;
switch (type) {
case 'HealthChange':
const damage = effect.data.damage || {};
let healingChanges = [];
let damageChanges = [];
const processChanges = (entries) => {
Object.entries(entries).forEach(([key, value]) => {
const colorClass = value < 0 ? 'healing-value' : 'damage-value';
const valueSpan = document.createElement('span');
valueSpan.className = colorClass;
valueSpan.textContent = Math.abs(value);
const change = `${valueSpan.outerHTML} ${key}`;
value < 0 ? healingChanges.push(change) : damageChanges.push(change);
});
};
if (damage.types) processChanges(damage.types);
if (damage.groups) processChanges(damage.groups);
let result = [];
if (healingChanges.length > 0) result.push(`Heals ${healingChanges.join(', ')}`);
if (damageChanges.length > 0) result.push(`Damages ${damageChanges.join(', ')}`);
return result.join(', ');
case 'GenericStatusEffect':
return `${effect.data.type || 'Apply'} ${effect.data.key}${effect.data.time ? ` for ${effect.data.time}s` : ''}`;
case 'AdjustReagent':
return `Adjust ${effect.data.reagent} by ${effect.data.amount}`;
case 'ChemVomit':
return `Cause vomiting${effect.data.probability ? ` (${effect.data.probability * 100}% chance)` : ''}`;
case 'AdjustTemperature':
return `Change temperature by ${effect.data.amount}`;
default:
return type;
}
}
function formatConditions(conditions) {
if (!conditions) return '';
function formatRange(min, max, unit = '') {
const minText = min !== undefined ? `>= ${min}${unit}` : '';
const maxText = max !== undefined ? `<= ${max}${unit}` : '';
const connector = minText && maxText ? ' and ' : '';
return `${minText}${connector}${maxText}`;
}
return conditions.map(condition => {
const type = condition.type;
const data = condition.data;
switch (type) {
case 'ReagentThreshold':
const reagentText = data.reagent ? `${data.reagent} ` : 'amount';
return `when ${reagentText} ${formatRange(data.min, data.max)}`;
case 'Temperature':
return `at temperature ${formatRange(data.min, data.max, 'K')}`;
case 'MobStateCondition':
return `when ${data.mobstate}`;
case 'TotalDamage':
return `when total damage ${formatRange(data.min, data.max)}`;
default:
return type;
}
}).join(' and ');
}
function renderMetabolismEffects(metabolism) {
const div = document.createElement('div');
div.className = 'metabolism-effects';
if (metabolism.effects) {
metabolism.effects.forEach(effect => {
const effectDiv = document.createElement('div');
effectDiv.className = 'effect';
const effectText = formatEffect(effect);
const conditions = formatConditions(effect.data?.conditions);
effectDiv.innerHTML = effectText + (conditions ? ` ${conditions}` : '');
div.appendChild(effectDiv);
});
}
return div;
}
function renderSearchResults(results, renderOptions) {
// Display the results
const resultsContainer = document.getElementById('results')
resultsContainer.innerHTML = ''
results.forEach(result => {
const item = result.item
const div = document.createElement('div')
div.className = 'result-item'
const titleContainer = document.createElement('div')
titleContainer.className = 'result-item-title-container'
const title = document.createElement('h3')
title.textContent = item.id
titleContainer.appendChild(title)
if (item.requiredMixerCategories) {
const requiredMixerCategories = document.createElement('div')
requiredMixerCategories.textContent = item.requiredMixerCategories.join("")
requiredMixerCategories.className = 'result-item-mixer'
titleContainer.appendChild(requiredMixerCategories)
}
if (item.minTemp) {
const temp = document.createElement('div')
temp.className = 'result-item-temp'
temp.textContent = `>= ${item.minTemp}K`
titleContainer.appendChild(temp)
}
const reactants = renderReactants(item.reactants)
div.appendChild(titleContainer)
div.appendChild(reactants)
if (renderOptions.includeOutputProduct) {
const productsContainer = document.createElement('div')
productsContainer.className = 'result-item-products'
item.products.forEach(product => {
const productDiv = document.createElement('div')
productDiv.className = 'result-item-product'
const productHeader = document.createElement('div')
productHeader.className = 'product-header'
productHeader.textContent = `Output: ${product.amount} ${product.id}`
productDiv.appendChild(productHeader)
if (product.reagentData) {
if (product.reagentData.metabolisms) {
Object.entries(product.reagentData.metabolisms).forEach(([key, value]) => {
const metabolismDiv = document.createElement('div')
metabolismDiv.className = 'metabolism-container'
const effectsDiv = renderMetabolismEffects(value)
metabolismDiv.appendChild(effectsDiv)
productDiv.appendChild(metabolismDiv)
})
}
}
productsContainer.appendChild(productDiv)
})
div.appendChild(productsContainer)
}
resultsContainer.appendChild(div)
});
if (results.length === 0) {
resultsContainer.innerHTML = '<strong>No results found for search</strong>';
}
}
function runSearch(fuse, data, renderOptions, searchText) {
// Data is in CamelCase so spaces are harmful for searching right now.
const cleanedSearchText = searchText.replace(/\s/g, "");
const searchResults = fuse.search(cleanedSearchText)
// Fuse will not show entire list if nothing searched :(
// https://github.com/krisk/Fuse/issues/229
const results = cleanedSearchText.length > 0
? searchResults
: data.map(item => ({item: item}))
renderSearchResults(results, renderOptions)
}
function render(data, renderOptions) {
// Fuse.js options
const options = {
keys: ['id', 'reactants.id', 'products.id'],
threshold: 0.1,
};
const fuse = new Fuse(data, options)
const searchInput = document.getElementById('search-input');
runSearch(fuse, data, renderOptions || {}, searchInput.value)
searchInput.addEventListener('input', function (e) {
runSearch(fuse, data, renderOptions || {}, e.target.value)
});
}