Skip to content

Instantly share code, notes, and snippets.

@megatolya
Created May 17, 2016 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megatolya/2f0da60b4336a012b2ddda033bf4ace9 to your computer and use it in GitHub Desktop.
Save megatolya/2f0da60b4336a012b2ddda033bf4ace9 to your computer and use it in GitHub Desktop.
//http://play.elevatorsaga.com/#challenge=4
{
init: function(elevators, floors) {
var floorsQueue = [];
floors.forEach(function (floor) {
floor.on("up_button_pressed", function() {
if (floorsQueue.indexOf(floor.floorNum()) === -1) {
floorsQueue.push(floor.floorNum());
}
});
floor.on("down_button_pressed", function() {
if (floorsQueue.indexOf(floor.floorNum()) === -1) {
floorsQueue.push(floor.floorNum());
}
});
});
var e = elevators[0]; // Let's use the first elevator
// Whenever the elevator is idle (has no more queued destinations) ...
e.on("idle", function() {
// let's go to all the floors (or did we forget one?)
console.log(e.destinationQueue);
console.log(e.getPressedFloors());
var pressed = e.getPressedFloors();
pressed.forEach(function (number) {
e.goToFloor(number);
});
floorsQueue.forEach(function (number) {
e.goToFloor(number);
});
floorsQueue = [];
// console.log(e.currentFloor());
});
},
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