Created
February 10, 2016 16:47
-
-
Save ejamesc/ed8f490537cbaa57862d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 waitingFloors = []; | |
| var goOnIdle = function(elevator, floor) { | |
| if (elevator.destinationDirection === "stopped") { | |
| elevator.goToFloor(floor.floorNum()); | |
| return true; | |
| } | |
| return false; | |
| }; | |
| var stopByUp = function(elevator, floor) { | |
| if (elevator.destinationDirection === "up" && elevator.currentFloor() < floor.floorNum()) { | |
| elevator.destinationQueue.push(floor.floorNum()); | |
| elevator.checkDestinationQueue(); | |
| return true; | |
| } | |
| return false; | |
| }; | |
| var stopByDown = function(elevator, floor) { | |
| if (elevator.destinationDirection === "down" && elevator.currentFloor() > floor.floorNum()) { | |
| elevator.destinationQueue.push(floor.floorNum()); | |
| elevator.destinationQueue.sort(sortDest(elevator)); | |
| elevator.checkDestinationQueue(); | |
| return true; | |
| } | |
| return false; | |
| }; | |
| var sortDest = function(elevator) { | |
| return function(a, b){ | |
| if (elevator.destinationDirection() === "up") { | |
| return a - b; | |
| } else if (elevator.destinationDirection() === "down") { | |
| return b - a; | |
| } | |
| }; | |
| }; | |
| elevators.forEach(function (elevator) { | |
| // Whenever the elevator is idle (has no more queued destinations) ... | |
| elevator.on("idle", function() { | |
| if (waitingFloors.length > 0) { | |
| elevator.goToFloor(waitingFloors.pop()); | |
| } | |
| }); | |
| elevator.on("floor_button_pressed", function(floorNum) { | |
| var qf = elevator.getPressedFloors(); | |
| if (qf.length > 0 && elevator.destinationQueue.indexOf(floorNum) > -1) { | |
| elevator.destinationQueue.push(floorNum); | |
| elevator.destinationQueue.sort(sortDest(elevator)); | |
| elevator.checkDestinationQueue(); | |
| } else { | |
| elevator.goToFloor(floorNum); | |
| } | |
| }); | |
| }); | |
| floors.forEach(function(floor) { | |
| floor.on("up_button_pressed", function() { | |
| var moveSucceed = false; | |
| elevators.forEach(function (el) { | |
| if (!moveSucceed) { | |
| moveSucceed = goOnIdle(el, floor); | |
| } | |
| if (!moveSucceed) { | |
| moveSucceed = stopByUp(el, floor); | |
| } | |
| waitingFloors.push(floor.floorNum()); | |
| }); | |
| }); | |
| floor.on("down_button_pressed", function() { | |
| var moveSucceed = false; | |
| elevators.forEach(function (el) { | |
| if (!moveSucceed) { | |
| moveSucceed = goOnIdle(el, floor); | |
| } | |
| if (!moveSucceed) { | |
| moveSucceed = stopByUp(el, floor); | |
| } | |
| waitingFloors.push(floor.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