Created
January 30, 2015 09:38
-
-
Save bchoii/20324d364a7bfc8a60ed 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) { | |
function contains(array, item) { | |
var i = array.length; | |
while (i--) { | |
if (array[i] === item) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
function remove(array, item) { | |
var i = array.length; | |
while (i--) { | |
if (array[i] === item) { | |
array.splice(i, 1); | |
} | |
} | |
}; | |
var upQueue = []; | |
var downQueue = []; | |
var idleElevators = []; | |
function sortDestination(elevator) { | |
elevator.destinationQueue.sort(function(a, b){return a-b}); | |
if (elevator.goingUpIndicator()) { | |
for (var i = 0; i < elevator.destinationQueue.length; i++) { | |
if (elevator.destinationQueue[0] < elevator.currentFloor()) { | |
elevator.destinationQueue.push(elevator.destinationQueue.shift()); | |
} | |
} | |
} | |
if (elevator.goingDownIndicator()) { | |
elevator.destinationQueue.reverse(); | |
for (var i = 0; i < elevator.destinationQueue.length; i++) { | |
if (elevator.destinationQueue[0] > elevator.currentFloor()) { | |
elevator.destinationQueue.push(elevator.destinationQueue.shift()); | |
} | |
} | |
} | |
elevator.checkDestinationQueue(); | |
} | |
function updateIndicator(elevator) { | |
if (elevator.destinationQueue.length == 0) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(true); | |
} else { | |
if (elevator.destinationQueue[0] > elevator.currentFloor()) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} | |
if (elevator.destinationQueue[0] < elevator.currentFloor()) { | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} | |
} | |
} | |
function digest() { | |
downQueue.sort(function(a, b){return a-b}); | |
downQueue.reverse(); | |
upQueue.sort(function(a, b){return a-b}); | |
if (idleElevators.length > 0) { | |
if (downQueue.length > 0) { | |
var e = idleElevators.shift(); | |
var f = downQueue.shift(); | |
e.goToFloor(f); | |
} | |
} | |
if (idleElevators.length > 0) { | |
if (upQueue.length > 0) { | |
var e = idleElevators.shift(); | |
var f = upQueue.shift(); | |
e.goToFloor(f); | |
} | |
} | |
} | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function() { | |
upQueue.push(floor.floorNum()); | |
digest(); | |
}); | |
floor.on("down_button_pressed", function() { | |
downQueue.push(floor.floorNum()); | |
digest(); | |
}); | |
}); | |
elevators.forEach(function(elevator){ | |
elevator.on("idle", function() { | |
idleElevators.push(elevator); | |
updateIndicator(elevator); | |
digest(); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.goToFloor(floorNum); | |
updateIndicator(elevator); | |
sortDestination(elevator); | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
if (elevator.goingUpIndicator()) { | |
remove(upQueue, floorNum); | |
} | |
if (elevator.goingDownIndicator()) { | |
remove(downQueue, floorNum); | |
} | |
updateIndicator(elevator); | |
}); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
if (direction == 'down') { | |
if (contains(downQueue, floorNum)) { | |
if (elevator.loadFactor() < 0.5) { | |
elevator.goToFloor(floorNum, true); | |
} | |
} | |
} | |
}); | |
}); | |
}, | |
update: function(dt, elevators, floors) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment