-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split into view, viewmodel, and factorio-blueprint #48
Changes from 1 commit
154f9b7
0d58e35
4b1232b
00ae993
b5d96ff
55f1bf6
76fea93
e44cbb9
02d3637
2a6aef3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,30 @@ window.FBE = window.FBE || {}; | |
var events = new TinyEmitter(), | ||
bp = new Blueprint(), | ||
selectedItem, | ||
entityNamesThatDoNotRotate = /beacon|roboport|lab|heat_pipe|pipe$|furnace|chest|pole|substation|solar|accumulator|centrifuge|wall|rocket_silo|radar|turret|speaker|power_switch|reactor|lamp|land_mine/i | ||
entityNamesThatDoNotRotate = /beacon|roboport|lab|heat_pipe|pipe$|furnace|chest|pole|substation|solar|accumulator|centrifuge|wall|rocket_silo|radar|turret|speaker|power_switch|reactor|lamp|land_mine/i, | ||
defaultDirectionsByEntityName = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the variable name sounds like it will contain a map, with a EntityName being the key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do |
||
{ | ||
regex: /inserter|offshore_pump/i, | ||
direction: 6 | ||
}, | ||
{ | ||
regex: /splitter|burner_mining/i, | ||
direction: 4 | ||
}, | ||
{ | ||
regex: /electric_mining|pipe_to_ground|steam_engine|steam_turbine|pump|gate|arithmetic_combinator|decider_combinator/i, | ||
direction: 2 | ||
} | ||
], | ||
factorioBlueprintOverrides = { | ||
// fix bad data in factorio-blueprint | ||
'steam_turbine': { width: 5, height: 3 }, | ||
'boiler': { width: 3, height: 2 }, | ||
// fix spots where height/width are swapped | ||
'pump': { width: 2, height: 1 }, | ||
'decider_combinator': { width: 2, height: 1 }, | ||
'arithmetic_combinator': { width: 2, height: 1 } | ||
} | ||
; | ||
|
||
FBE.viewmodel = { | ||
|
@@ -156,13 +179,7 @@ window.FBE = window.FBE || {}; | |
name: name | ||
}, | ||
rawEntities[name], | ||
// fix bad data in factorio-blueprint | ||
name === 'steam_turbine' && { width: 5, height: 3 }, | ||
name === 'boiler' && { width: 3, height: 2 }, | ||
// fix spots where height/width are swapped | ||
name === 'pump' && { width: 2, height: 1 }, | ||
name === 'decider_combinator' && { width: 2, height: 1 }, | ||
name === 'arithmetic_combinator' && { width: 2, height: 1 } | ||
factorioBlueprintOverrides[name] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's definitely clearer to read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed |
||
); | ||
}) | ||
.filter(isPlaceable) | ||
|
@@ -212,10 +229,11 @@ window.FBE = window.FBE || {}; | |
function getDefaultDirection(entity) { | ||
if (entity.type === 'input') { return 2; } | ||
if (entity.type === 'output') { return 6; } | ||
if (/inserter|offshore_pump/.test(entity.name)) { return 6; } | ||
if (/splitter|burner_mining/.test(entity.name)) { return 4; } | ||
if (/electric_mining|pipe_to_ground|steam_engine|steam_turbine|pump|gate|arithmetic_combinator|decider_combinator/.test(entity.name)) { return 2; } | ||
return 0; | ||
|
||
var first = defaultDirectionsByEntityName | ||
.filter(function(x){ return x.regex.test(entity.name);})[0]; | ||
|
||
return (first && first.direction) || 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still doing this strange conditional that returns a non-boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the logical operators to for short-circuit evaluation is a pretty common idiom, and the simplest way to guard against nulls or provide default values. See also: http://eloquentjavascript.net/01_values.html#h_3jN0iK4yKW In this case, I think a ternary will be clearer, but it'll still be using non-booleans / truthiness |
||
} | ||
|
||
})(window.FBE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These regexes still feel wrong, as they are just a list of options. i think these would be better handled by an array.
Also doing a little bit of searching, i think this regex match performs a lot slower than using indexOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe Array.includes() is what you should be using to check the existence of a string in an array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex is called once at app startup and whenever we load a blueprint. I don't think it's a performance bottleneck.
Profiling on my aging laptop (i5, chrome, ubunut) it takes 5ms to call
getPlaceableItems
, and of that, 0.16ms is spent inentityNamesThatDoNotRotate.test
.Is that fast enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, but this still feels like an abuse of regex. It's being used like an array, instead of using an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will merge this in for now, but opened #54 to get this fixed