forked from Pigeon-Barry/dsp-calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fragment.js
113 lines (100 loc) · 3.59 KB
/
fragment.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
/*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} from "./align.js"
import {DEFAULT_TAB, currentTab} from "./events.js"
import {spec, DEFAULT_PURITY, DEFAULT_BELT, DEFAULT_ASSEMBLER, DEFAULT_SMELTER} from "./factory.js"
import {Rational} from "./rational.js"
export function formatSettings() {
let settings = ""
if (currentTab !== DEFAULT_TAB) {
settings += "tab=" + currentTab + "&"
}
if (spec.format.rateName !== DEFAULT_RATE) {
settings += "rate=" + spec.format.rateName + "&"
}
if (spec.format.ratePrecision !== DEFAULT_RATE_PRECISION) {
settings += "rp=" + spec.format.ratePrecision + "&"
}
if (spec.format.countPrecision !== DEFAULT_COUNT_PRECISION) {
settings += "cp=" + spec.format.countPrecision + "&"
}
if (spec.belt.key !== DEFAULT_BELT) {
settings += "belt=" + spec.belt.key + "&"
}
if (spec.assembler.key !== DEFAULT_ASSEMBLER) {
settings += "assembler=" + spec.assembler.key + "&"
}
if (spec.smelter.key !== DEFAULT_SMELTER) {
settings += "smelter=" + spec.smelter.key + "&"
}
settings += "items="
let targetStrings = []
for (let target of spec.buildTargets) {
let targetString = ""
if (target.changedBuilding) {
targetString = `${target.itemKey}:f:${target.buildingInput.value}`
} else {
targetString = `${target.itemKey}:r:${target.rate.mul(spec.format.rateFactor).toString()}`
}
targetStrings.push(targetString)
}
settings += targetStrings.join(",")
let ignore = []
for (let recipe of spec.ignore) {
ignore.push(recipe.key)
}
if (ignore.length > 0) {
settings += "&ignore=" + ignore.join(",")
}
let overclock = []
for (let [recipe, factor] of spec.overclock) {
let s = factor.mul(Rational.from_float(100)).toString()
overclock.push(`${recipe.key}:${s}`)
}
if (overclock.length > 0) {
settings += "&overclock=" + overclock.join(",")
}
let alt = []
for (let [item, recipe] of spec.altRecipes) {
alt.push(recipe.key)
}
if (alt.length > 0) {
settings += "&alt=" + alt.join(",")
}
let minerStrings = []
for (let [recipe, {miner, purity}] of spec.minerSettings) {
let miners = spec.buildings.get(recipe.category)
let defaultMiner = miners[0]
if (miner !== defaultMiner || purity !== DEFAULT_PURITY) {
let s = `${recipe.key}:${miner.key}:${purity.key}`
minerStrings.push(s)
}
}
if (minerStrings.length > 0) {
settings += "&miners=" + minerStrings.join(",")
}
return settings
}
export function loadSettings(fragment) {
let settings = new Map()
fragment = fragment.substr(1)
let pairs = fragment.split("&")
for (let pair of pairs) {
let i = pair.indexOf("=")
if (i === -1) {
continue
}
let name = pair.substr(0, i)
let value = pair.substr(i + 1)
settings.set(name, value)
}
return settings
}