forked from KirkMcDonald/satisfactory-calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.js
403 lines (365 loc) · 10.9 KB
/
settings.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*Copyright 2019 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
import { DEFAULT_RATE,DEFAULT_RATE_PRECISION,DEFAULT_COUNT_PRECISION,longRateNames } from "./align.js"
import { dropdown } from "./dropdown.js"
import { DEFAULT_TAB,clickTab } from "./events.js"
import { spec,resourcePurities,DEFAULT_BELT,DEFAULT_PIPE } from "./factory.js"
import { Rational } from "./rational.js"
// There are several things going on with this control flow. Settings should
// work like this:
// 1) Settings are parsed from the URL fragment into the settings Map.
// 2) Each setting's `render` function is called.
// 3) If the setting is not present in the map, a default value is used.
// 4) The setting is applied.
// 5) The setting's GUI is placed into a consistent state.
// Remember to add the setting to fragment.js, too!
// tab
function renderTab(settings) {
let tabName = DEFAULT_TAB
if (settings.has("tab")) {
tabName = settings.get("tab")
}
clickTab(tabName)
}
// build targets
function renderTargets(settings) {
spec.buildTargets = []
d3.select("#targets li.target").remove()
let targetSetting = settings.get("items")
if (targetSetting !== undefined && targetSetting !== "") {
let targets = targetSetting.split(",")
for (let targetString of targets) {
let parts = targetString.split(":")
let itemKey = parts[0]
let target = spec.addTarget(itemKey)
let type = parts[1]
if (!target) {
continue;
} else if (target && type === "f") {
target.setBuildings(parts[2])
} else if (target && type === "r") {
target.setRate(parts[2])
} else {
throw new Error("unknown target type")
}
}
if (!spec.buildTargets.length) {
spec.addTarget();
}
} else {
spec.addTarget()
}
}
// ignore
function renderIgnore(settings) {
spec.ignore.clear()
// UI will be rendered later, as part of the solution.
let ignoreSetting = settings.get("ignore")
if (ignoreSetting !== undefined && ignoreSetting !== "") {
let ignore = ignoreSetting.split(",")
for (let recipeKey of ignore) {
let recipe = spec.recipes.get(recipeKey)
spec.ignore.add(recipe)
}
}
}
// overclock
function renderOverclock(settings) {
spec.overclock.clear()
// UI will be rendered later, as part of the solution.
let overclockSetting = settings.get("overclock")
if (overclockSetting !== undefined && overclockSetting !== "") {
let overclock = overclockSetting.split(",")
for (let pair of overclock) {
let [recipeKey,percentString] = pair.split(":")
let recipe = spec.recipes.get(recipeKey)
let percent = Rational.from_string(percentString).div(Rational.from_float(100))
spec.setOverclock(recipe,percent)
}
}
}
// display rate
function rateHandler() {
spec.format.setDisplayRate(this.value)
spec.updateSolution()
}
function renderRateOptions(settings) {
let rateName = DEFAULT_RATE
if (settings.has("rate")) {
rateName = settings.get("rate")
}
spec.format.setDisplayRate(rateName)
let rates = []
for (let [rateName,longRateName] of longRateNames) {
rates.push({ rateName,longRateName })
}
let form = d3.select("#display_rate")
form.selectAll("*").remove()
let rateOption = form.selectAll("span")
.data(rates)
.join("span")
rateOption.append("input")
.attr("id",d => d.rateName + "_rate")
.attr("type","radio")
.attr("name","rate")
.attr("value",d => d.rateName)
.attr("checked",d => d.rateName === rateName ? "" : null)
.on("change",rateHandler)
rateOption.append("label")
.attr("for",d => d.rateName + "_rate")
.text(d => "items/" + d.longRateName)
rateOption.append("br")
}
// precisions
function renderPrecisions(settings) {
spec.format.ratePrecision = DEFAULT_RATE_PRECISION
if (settings.has("rp")) {
spec.format.ratePrecision = Number(settings.get("rp"))
}
d3.select("#rprec").attr("value",spec.format.ratePrecision)
spec.format.countPrecision = DEFAULT_COUNT_PRECISION
if (settings.has("cp")) {
spec.format.countPrecision = Number(settings.get("cp"))
}
d3.select("#cprec").attr("value",spec.format.countPrecision)
}
// belt
function beltHandler(belt) {
spec.belt = belt
spec.updateSolution()
}
function renderBelts(settings) {
let beltKey = DEFAULT_BELT
if (settings.has("belt")) {
beltKey = settings.get("belt")
}
spec.belt = spec.belts.get(beltKey)
let belts = []
for (let [beltKey,belt] of spec.belts) {
belts.push(belt)
}
let form = d3.select("#belt_selector")
form.selectAll("*").remove()
let beltOption = form.selectAll("span")
.data(belts)
.join("span")
beltOption.append("input")
.attr("id",d => "belt." + d.key)
.attr("type","radio")
.attr("name","belt")
.attr("value",d => d.key)
.attr("checked",d => d === spec.belt ? "" : null)
.on("change",beltHandler)
beltOption.append("label")
.attr("for",d => "belt." + d.key)
.append("img")
.classed("icon",true)
.attr("src",d => d.iconPath())
.attr("width",32)
.attr("height",32)
.attr("title",d => d.name)
}
function pipeHandler(pipe) {
spec.pipe = pipe
spec.updateSolution()
}
function renderPipes(settings) {
let pipeKey = DEFAULT_PIPE
if (settings.has("pipe")) {
pipeKey = settings.get("pipe")
}
spec.pipe = spec.pipes.get(pipeKey)
let pipes = []
for (let [__,pipe] of spec.pipes) {
pipes.push(pipe)
}
let form = d3.select("#pipe_selector")
form.selectAll("*").remove()
let pipeOption = form.selectAll("span")
.data(pipes)
.join("span")
pipeOption.append("input")
.attr("id",d => "pipe." + d.key)
.attr("type","radio")
.attr("name","pipe")
.attr("value",d => d.key)
.attr("checked",d => d === spec.pipe ? "" : null)
.on("change",pipeHandler)
pipeOption.append("label")
.attr("for",d => "pipe." + d.key)
.append("img")
.classed("icon",true)
.attr("src",d => d.iconPath())
.attr("width",32)
.attr("height",32)
.attr("title",d => d.name)
}
// alternate recipes
function changeAltRecipe(recipe) {
spec.setRecipe(recipe)
spec.updateSolution()
}
function renderIngredient(ingSpan) {
ingSpan.classed("ingredient",true)
.attr("title",d => d.item.name)
.append("img")
.classed("icon",true)
.attr("src",d => d.item.iconPath())
ingSpan.append("span")
.classed("count",true)
.text(d => spec.format.count(d.amount))
}
function renderAltRecipes(settings) {
spec.altRecipes = new Map()
if (settings.has("alt")) {
let alt = settings.get("alt").split(",")
for (let recipeKey of alt) {
let recipe = spec.recipes.get(recipeKey)
spec.setRecipe(recipe)
}
}
let items = []
for (let tier of spec.itemTiers) {
for (let item of tier) {
if (item.recipes.length > 1) {
items.push(item)
}
}
}
let div = d3.select("#alt_recipe_settings")
div.selectAll("*").remove()
let dropdowns = div.selectAll("div")
.data(items)
.enter().append("div")
let recipeLabel = dropdown(
dropdowns,
d => d.recipes,
d => `altrecipe-${d.products[0].item.key}`,
d => spec.getRecipe(d.products[0].item) === d,
changeAltRecipe,
)
let productSpan = recipeLabel.append("span")
.selectAll("span")
.data(d => [d.products[0]])
.join("span")
renderIngredient(productSpan)
recipeLabel.append("span")
.classed("arrow",true)
.text("\u21d0")
let ingredientSpan = recipeLabel.append("span")
.selectAll("span")
.data(d => d.ingredients)
.join("span")
renderIngredient(ingredientSpan)
}
// miners
function mineHandler(d) {
spec.setMiner(d.recipe,d.miner,d.purity)
spec.updateSolution()
}
function renderResources(settings) {
spec.initMinerSettings()
if (settings.has("miners")) {
let miners = settings.get("miners").split(",")
for (let minerString of miners) {
let [recipeKey,minerKey,purityKey] = minerString.split(":")
let recipe = spec.recipes.get(recipeKey)
let miner = spec.miners.get(minerKey)
let purity = resourcePurities[Number(purityKey)]
spec.setMiner(recipe,miner,purity)
}
}
let div = d3.select("#resource_settings")
div.selectAll("*").remove()
let resources = []
for (let [recipe,{ miner,purity }] of spec.minerSettings) {
let minerDefs = spec.buildings.get(recipe.category)
let purities = []
for (let purityDef of resourcePurities) {
let miners = []
for (let minerDef of spec.buildings.get(recipe.category)) {
let selected = miner === minerDef && purity === purityDef
miners.push({
recipe: recipe,
purity: purityDef,
miner: minerDef,
selected: selected,
id: `miner.${recipe.key}.${purityDef.key}.${minerDef.key}`
})
}
purities.push({ miners,purityDef })
}
resources.push({ recipe,purities,minerDefs })
}
let resourceTable = div.selectAll("table")
.data(resources)
.join("table")
.classed("resource",true)
let header = resourceTable.append("tr")
header.append("th")
.append("img")
.classed("icon",true)
.attr("src",d => d.recipe.iconPath())
.attr("width",32)
.attr("height",32)
.attr("title",d => d.recipe.name)
header.selectAll("th")
.filter((d,i) => i > 0)
.data(d => d.minerDefs)
.join("th")
.append("img")
.classed("icon",true)
.attr("src",d => d.iconPath())
.attr("width",32)
.attr("height",32)
.attr("title",d => d.name)
let purityRow = resourceTable.selectAll("tr")
.filter((d,i) => i > 0)
.data(d => d.purities)
.join("tr")
purityRow.append("td")
.text(d => d.purityDef.name)
let cell = purityRow.selectAll("td")
.filter((d,i) => i > 0)
.data(d => d.miners)
.join("td")
cell.append("input")
.attr("id",d => d.id)
.attr("type","radio")
.attr("name",d => d.recipe.key)
.attr("checked",d => d.selected ? "" : null)
.on("change",mineHandler)
cell.append("label")
.attr("for",d => d.id)
.append("svg")
.attr("viewBox","0,0,32,32")
.style("width",32)
.style("height",32)
.append("rect")
.attr("x",0)
.attr("y",0)
.attr("width",32)
.attr("height",32)
.attr("rx",4)
.attr("ry",4)
}
export function renderSettings(settings) {
renderTargets(settings)
renderIgnore(settings)
renderOverclock(settings)
renderRateOptions(settings)
renderPrecisions(settings)
renderBelts(settings)
renderPipes(settings)
renderAltRecipes(settings)
renderResources(settings)
renderTab(settings)
}