forked from clarabstract/factorio-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.jsx
352 lines (330 loc) · 9.94 KB
/
calc.jsx
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
/** @jsx React.DOM */
// react component
// Create the input graph
update = function(props) {
return function(me) {
var g = new dagreD3.graphlib.Graph()
.setGraph({})
.setDefaultEdgeLabel(function() { return {}; });
// Here we"re setting nodeclass, which is used by our custom drawNodes function
// below.
var i = 0;
var dict = {};
var visit = function (inp, k) {
console.log(inp.name + ' ' + k);
dict[inp.name] = k;
g.setNode(k, { label: inp.name, class: "type-TOP"});
if (inp.inputs && inp.inputs.length) {
for (var j = 0; j < inp.inputs.length; j++) {
if(!(inp.inputs[j].name in dict)){
var n = ++i;
visit(inp.inputs[j], n);
}
if(dict[inp.inputs[j].name] != dict[inp.name]) {
g.setEdge(dict[inp.inputs[j].name], dict[inp.name],
{}); //lineInterpolate: 'linear'
}
};
}
}
visit(props.req, 0);
// Create the renderer
// var renderer = new dagreD3.Renderer().edgeInterpolate("linear");
var render = new dagreD3.render();
// render.edgeInterpolate("linear");
// Set up an SVG group so that we can translate the final graph.
// var svg = d3.select("svg"),
// svgGroup = svg.append("g");
// Run the renderer. This is what draws the final graph.
render(me, g);
var svgGroup = me;
var svg = d3.select("svg");
// Center the graph
var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
svg.attr("height", g.graph().height + 40);
// me
// .attr("cx", 3 + props.r)
// .attr("cy", 3 + props.r)
// .attr("r", props.r)
// .attr("fill", props.color);
};
};
var Graph = React.createClass({
render: function() {
return <svg width="960" height="600"></svg>;
},
componentDidMount: function () {
d3.select(this.getDOMNode())
.append("g")
.call(update(this.props));
},
shouldComponentUpdate: function(props) {
d3.select(this.getDOMNode())
.select("g")
.call(update(props));
// always skip React's render step
return false;
}
});
var Calc = React.createClass({
getInitialState: function() {
return {
recipe: null,
ips: 1,
opts: {
asslvl: "0.5",
smeltlvl: "1",
beltlvl: "5.7",
difficulty: "normal",
}
};
},
changeDataLib: function(ev) {
window.location.hash = "#"+ ev.target.value;
window.location.reload();
},
calculate: function() {
req = window.calcRequest.call(this.state.recipe, parseFloat(this.state.ips), this.state.opts)
this.setState({result: req})
},
setIPS: function(ev) {
this.setState({ips:ev.target.value}, this.calculate)
},
setRecipe: function(ev) {
this.setState({recipe:ev.target.value}, this.calculate)
},
setOption: function(ev) {
var state = {opts:this.state.opts};
state.opts[ev.target.name] = ev.target.value;
this.setState(state, this.calculate);
},
getSubtotals: function(req, subtotals) {
if (!subtotals) {
subtotals = {}
}
if (!subtotals[req.name]) {
subtotals[req.name] = {
name: req.name,
ips: 0,
ipspa: req.ipspa,
assembler_max_line: req.assembler_max_line,
cycle_time: req.cycle_time,
}
}
var sub = subtotals[req.name];
sub.ips += req.ips;
sub.assemblers = sub.ips / sub.ipspa;
sub.lines_required = sub.assemblers / Math.floor(sub.assembler_max_line);
if (req.inputs) {
for (var i = req.inputs.length - 1; i >= 0; i--) {
this.getSubtotals(req.inputs[i], subtotals);
};
};
return subtotals;
},
getGraph: function(result) {
return <Graph req={result} />;
},
showOptions: function(ev) {
ev.preventDefault();
this.setState({showOptions: true});
},
hideOptions: function(ev) {
ev.preventDefault();
this.setState({showOptions: false});
},
render: function() {
var result, subtotals, layout;
if (this.state.result) {
result = <Req req={this.state.result}/>;
subs = this.getSubtotals(this.state.result);
subtotals = [];
for( n in subs ) {
subtotals.push(<Req req={subs[n]} />);
}
layout = this.getGraph(this.state.result);
}
var options;
if (this.state.showOptions) {
options = (
<p>
<a onClick={this.hideOptions} href="">
<span className="glyphicon glyphicon-collapse-down"></span>
Hide options
</a>
<div id="options">
<label>Assembler level:
<select
value={this.state.opts.asslvl}
name="asslvl"
onChange={this.setOption}>
<option value="0.5">1 (0.5 modifier)</option>
<option value="0.75">2 (0.75 modifier)</option>
<option value="1.25">3 (1.25 modifier)</option>
</select>
</label>
<label>Smelter level:
<select
value={this.state.opts.smeltlvl}
name="smeltlvl"
onChange={this.setOption}>
<option value="1">Stone</option>
<option value="2">Steel / Electric</option>
</select>
</label>
<label>Belt speed:
<select
value={this.state.opts.beltlvl}
name="beltlvl"
onChange={this.setOption}>
<option value="3.8">Basic (slow corners)</option>
<option value="5.7">Basic (straight)</option>
<option value="6.3">Fast (slow corners)</option>
<option value="9.4">Fast (straight)</option>
<option value="8.3">Express (slow corners)</option>
<option value="14.2">Express (straight)</option>
</select>
</label>
<label>Recipe difficulty:
<select
value={this.state.opts.difficulty}
name="difficulty"
onChange={this.setOption}>
<option value="normal">Normal</option>
<option value="expensive">Expensive</option>
</select>
</label>
</div>
</p>
);
} else {
options = (
<p>
<a onClick={this.showOptions} href="">
<span className="glyphicon glyphicon-expand"></span>
Show options
</a>
</p>
);
}
return (
<div>
<header className="clearfix">
<span className="pull-right">
Data source:
<select
value={this.state.currentlib}
onChange={this.changeDataLib}>
{this.props.datalibs.map(function(lib){
return <option key={lib}>{lib}</option>;
})}
</select>
</span>
</header>
<p>
Calculate the requirements for
<input
id="recipe"
type="text"
list="recipes"
placeholder="recipe name"
value={this.state.recipe}
onChange={this.setRecipe}/>
<datalist id="recipes">
{Object.keys(this.props.recipes).map(function(recipe) {
return <option key={recipe}>{recipe}</option>;
})}
</datalist>
producing at a rate of
<input
id="ips"
type="number"
min="0"
step="any"
value={this.state.ips}
onChange={this.setIPS}/>
item(s) / second.
</p>
{options}
{result}
<h2>Sub-totals</h2>
{subtotals}
<h2>Layout</h2>
{layout}
</div>
);
}
});
var Req = React.createClass({
render: function() {
var inputs, details;
if (this.props.req.inputs && this.props.req.inputs.length) {
inputs = (
<div className="inputs">
{this.props.req.inputs.map(function(input){
return <Req req={input} />;
})}
</div>
);
}
if (this.props.req.assemblers) {
details = [
<div className="assemblers">
requires
<span className="val">{this.props.req.assemblers.toFixed(2)}</span>
assemblers
</div>,
<div className="lines_required">
on
<span className="val">{this.props.req.lines_required.toFixed(2)}</span>
assembly lines
</div>,
<div className="recipe-info">
(
<div className="assembler_max_line">
<span className="val">{this.props.req.assembler_max_line.toFixed(2)}</span>
max assemblers per line
</div>
-
<div className="cycle_time">
cycles every
<span className="val">{this.props.req.cycle_time.toFixed(2)}s</span>
</div>
<div className="ipspa">
@
<span className="val">{this.props.req.ipspa.toFixed(2)}</span>i/s
</div>
)
</div>
];
} else {
details = null;
}
return (
<div className="req">
<div className="name">{this.props.req.name}</div>
<div className="data">
<div className="ips">@
<span className="val ips-val">{this.props.req.ips.toFixed(2)}</span>
items/s
(or
<span className="val ips-val">{(1.0 / this.props.req.ips).toFixed(2)}</span>
s/item)
</div>
{details}
</div>
{inputs}
</div>
);
}
});
function renderCalc(recipeData) {
React.renderComponent(
<Calc
recipes={recipeData}
datalibs={window.DATALIBS}
currentlib={window.CURRENT_LIB}/>,
document.getElementById('calc')
);
}