Created
April 17, 2019 13:42
-
-
Save rbreve/b0e4a06dd73e7809e99c72cf549d6d0a to your computer and use it in GitHub Desktop.
Elevator Saga 1-12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
init: function(elevators, floors) { | |
var requests= []; | |
elevators.forEach(function(e) { | |
e.on("stopped_at_floor", function(floorNum) { | |
if(floorNum===0) { | |
e.destinationQueue.sort(function(a, b){return a-b}); | |
} | |
if(floorNum===floors.length-1) { | |
e.destinationQueue.sort(function(a, b){return b-a}); | |
} | |
var index = requests.indexOf(floorNum); | |
if (index >= 0) { | |
requests.splice(index, 1); | |
} | |
e.checkDestinationQueue(); | |
}); | |
e.on("passing_floor", function(floorNum, direction) { | |
if (requests.includes(floorNum) && e.loadFactor() < 0.5) { | |
e.goToFloor(floorNum, true); | |
var index = requests.indexOf(floorNum); | |
if (index >= 0) { | |
requests.splice(index, 1); | |
} | |
} | |
}); | |
e.on("floor_button_pressed", function(floorNum) { | |
e.goToFloor(floorNum); | |
} ); | |
e.on("idle", function() { | |
if(requests.length > 0) { | |
if(e.loadFactor() < 0.5){ | |
e.goToFloor(requests.shift()); | |
} | |
}else { | |
e.goToFloor(0); | |
} | |
}); | |
}) | |
floors.forEach(function(f) { | |
f.on("up_button_pressed", function() { | |
requests.push(f.floorNum()); | |
}) | |
f.on("down_button_pressed", function() { | |
requests.push(f.floorNum()); | |
}) | |
}); | |
}, | |
update: function(dt, elevators, floors) { | |
// We normally don't need to do anything here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment