Created
January 23, 2015 22:37
-
-
Save dannyfritz/057f35bbe1e6118b9014 to your computer and use it in GitHub Desktop.
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 requestQueue = []; | |
var request = function (floor, direction) | |
{ | |
requestQueue.push({ | |
floor: floor, | |
direction: direction | |
}); | |
requestQueue = _.sortBy(requestQueue, 'floor'); | |
} | |
var getRequest = function () | |
{ | |
var nextRequest = _.last(requestQueue); | |
_.remove(requestQueue, nextRequest); | |
return nextRequest; | |
} | |
var findRequest = function (floor, direction) | |
{ | |
var validRequests = _.remove(requestQueue, { | |
floor: floor, | |
direction: direction | |
}); | |
return _.size(validRequests) > 0; | |
} | |
var setIndicator = function (elevator, direction) | |
{ | |
if (_.isUndefined(direction)) | |
{ | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(true); | |
} | |
else if (direction === 'up') | |
{ | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} | |
else if (direction === 'down') | |
{ | |
elevator.goingDownIndicator(true); | |
elevator.goingUpIndicator(false); | |
} | |
} | |
_.each(elevators, | |
function (elevator) | |
{ | |
elevator.on('idle', | |
function () | |
{ | |
var nextRequest = getRequest(); | |
if (_.isUndefined(nextRequest)) | |
{ | |
if (elevator.currentFloor() === 0) | |
{ | |
setIndicator(elevator, 'up'); | |
elevator.goToFloor(0); | |
} | |
else | |
{ | |
setIndicator(elevator, 'down'); | |
elevator.goToFloor(0); | |
} | |
return; | |
} | |
elevator.goToFloor(nextRequest.floor); | |
} | |
); | |
elevator.on('stopped_at_floor', | |
function (floorNum) | |
{ | |
if (_.isEmpty(elevator.destinationQueue)) | |
{ | |
setIndicator(elevator); | |
} | |
} | |
); | |
elevator.on('passing_floor', | |
function (floorNum, direction) | |
{ | |
if (elevator.loadFactor() >= 1) | |
{ | |
return; | |
} | |
var floorHasRequest = findRequest(floorNum, direction); | |
if (!floorHasRequest) | |
{ | |
return; | |
} | |
elevator.destinationQueue.unshift(floorNum); | |
elevator.checkDestinationQueue(); | |
} | |
); | |
elevator.on('floor_button_pressed', | |
function (floorNum) | |
{ | |
elevator.goToFloor(floorNum); | |
if (floorNum > elevator.currentFloor()) | |
{ | |
setIndicator(elevator, 'up'); | |
elevator.destinationQueue = _(elevator.destinationQueue) | |
.sortBy( | |
function (num) { | |
return num; | |
} | |
) | |
.uniq() | |
.value(); | |
} | |
else | |
{ | |
setIndicator(elevator, 'down'); | |
elevator.destinationQueue = _(elevator.destinationQueue) | |
.sortBy( | |
function (num) { | |
return -num; | |
} | |
) | |
.uniq() | |
.value(); | |
} | |
elevator.checkDestinationQueue(); | |
} | |
); | |
} | |
); | |
_.each(floors, | |
function (floor) | |
{ | |
floor.on('up_button_pressed', | |
function () | |
{ | |
request(floor.floorNum(), 'up'); | |
} | |
); | |
floor.on('down_button_pressed', | |
function () | |
{ | |
request(floor.floorNum(), 'down'); | |
} | |
); | |
} | |
); | |
}, | |
update: _.noop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment