forked from Pigeon-Barry/dsp-calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.js
355 lines (311 loc) · 10.2 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
/*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_ASSEMBLER, DEFAULT_SMELTER} 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 (type === "f") {
target.setBuildings(parts[2])
} else if (type === "r") {
target.setRate(parts[2])
} else {
throw new Error("unknown target type")
}
}
} 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)
}
// assembler
function assemblerHandler(assembler) {
spec.assembler = assembler
spec.updateSolution()
}
function renderAssemblers(settings) {
let assemblerKey = DEFAULT_ASSEMBLER
if (settings.has("assembler")) {
assemblerKey = settings.get("assembler")
}
spec.assembler = spec.assemblers.get(assemblerKey)
let assemblers = []
for (let [assemblerKey, assembler] of spec.assemblers) {
assemblers.push(assembler)
}
let form = d3.select("#assembler_selector")
form.selectAll("*").remove()
let assemblerOption = form.selectAll("span")
.data(assemblers)
.join("span")
assemblerOption.append("input")
.attr("id", d => "assembler." + d.key)
.attr("type", "radio")
.attr("name", "assembler")
.attr("value", d => d.key)
.attr("checked", d => d === spec.assembler ? "" : null)
.on("change", assemblerHandler)
assemblerOption.append("label")
.attr("for", d => "assembler." + d.key)
.append("img")
.classed("icon", true)
.attr("src", d => d.iconPath())
.attr("width", 32)
.attr("height", 32)
.attr("title", d => d.name)
}
// smelters
function smelterHandler(smelter) {
spec.smelter = smelter
spec.updateSolution()
}
function renderSmelters(settings) {
let smelterKey = DEFAULT_SMELTER
if (settings.has("smelter")) {
smelterKey = settings.get("smelter")
}
spec.smelter = spec.smelters.get(smelterKey)
let smelters = []
for (let [smelterKey, smelter] of spec.smelters) {
smelters.push(smelter)
}
let form = d3.select("#smelting_selector")
form.selectAll("*").remove()
let smelterOption = form.selectAll("span")
.data(smelters)
.join("span")
smelterOption.append("input")
.attr("id", d => "smelter." + d.key)
.attr("type", "radio")
.attr("name", "smelter")
.attr("value", d => d.key)
.attr("checked", d => d === spec.smelter ? "" : null)
.on("change", smelterHandler)
smelterOption.append("label")
.attr("for", d => "smelter." + 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.product.item.key}`,
d => spec.getRecipe(d.product.item) === d,
changeAltRecipe,
)
let productSpan = recipeLabel.append("span")
.selectAll("span")
.data(d => [d.product])
.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()
}
export function renderSettings(settings) {
renderTargets(settings)
renderIgnore(settings)
renderOverclock(settings)
renderRateOptions(settings)
renderPrecisions(settings)
renderBelts(settings)
renderAssemblers(settings)
renderSmelters(settings)
renderAltRecipes(settings)
renderTab(settings)
}