Created
January 23, 2015 16:17
-
-
Save lakario/0b7f959e38fc342d2a76 to your computer and use it in GitHub Desktop.
Elevator Saga Challenges 1-6
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 midPoint = Math.floor(floors.length / 2); | |
var rotator = 0; | |
_.each(floors, function(floor) { | |
floor.on("up_button_pressed", function() { | |
var destination = floor.floorNum(); | |
goToFloor(pickElevator(destination), destination, "press up"); | |
}); | |
floor.on("down_button_pressed", function() { | |
var destination = floor.floorNum(); | |
goToFloor(pickElevator(destination), destination, "press down"); | |
}); | |
}); | |
_.each(elevators, function(elevator) { | |
elevator.on("idle", function() { | |
var increment = rotator++; | |
var destination = 0; | |
if (elevator.length > 1) { | |
destination = increment % 2 == 0 ? midPoint : increment % 3 == 0 ? floors.length - 1 : 0; | |
} | |
goToFloor(elevator, destination, "idle"); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
goToFloor(elevator, floorNum, "press floor"); | |
}); | |
}); | |
function goToFloor(elevator, floorNum, logMessage) { | |
var elevatorId = elevators.indexOf(elevator) + 1; | |
console.log('this elevator 4', elevator); | |
var currentFloor = elevator.currentFloor(); | |
var delta = getDelta(currentFloor, floorNum); | |
var elevatorDelta = getElevatorDelta(elevator); | |
console.log("Floor requested: ", floorNum); | |
if (delta > 0 && !isGoingToFloor(floorNum)) { | |
elevator.goToFloor(floorNum); | |
prioritizeFloors(elevator); | |
console.log( | |
("Elevator " + elevatorId + " (floor " + currentFloor + ")" | |
+ " going to floor: " + floorNum | |
+ " (" + logMessage + ")").trim(), elevator.destinationQueue); | |
} | |
else { | |
console.log ("Ignoring request (" + logMessage + ")", elevator.destinationQueue) | |
} | |
} | |
function prioritizeFloors(elevator) { | |
if (!elevator.destinationQueue) { | |
return; | |
} | |
var currentFloor = elevator.currentFloor(); | |
var direction = getElevatorDirection(elevator); | |
var firstHalf = [], secondHalf = []; | |
firstHalf = elevator.destinationQueue.filter(function(floorNum) { | |
return floorNum <= currentFloor; | |
}).sort(); // down sequence | |
secondHalf = elevator.destinationQueue.filter(function(floorNum) { | |
return firstHalf.indexOf(floorNum) == -1; | |
}).sort(); // up sequence | |
if (direction == 'up') { | |
firstHalf = firstHalf.reverse(); // up sequence | |
} | |
else { | |
secondHalf = secondHalf.reverse(); // down sequen | |
} | |
elevator.destinationQueue = _.unique(firstHalf.concat(secondHalf)); | |
elevator.checkDestinationQueue(); | |
} | |
function isGoingToFloor(elevator, floorNum) { | |
return elevator.destinationQueue && elevator.destinationQueue.indexOf(floorNum) > -1; | |
} | |
function getDelta(start, end) { | |
return Math.abs(start - end); | |
} | |
function getElevatorDelta(elevator) { | |
return elevator.destinationQueue.length > 0 | |
? getDelta(elevator.currentFloor(), elevator.getFirstPressedFloor()) | |
: 0; | |
} | |
function getDirection(start, end) { | |
return end ? (start < end ? "up" : "down") : "none"; | |
} | |
function getElevatorDirection(elevator) { | |
var destination = elevator.destinationQueue.length > 0 ? elevator.getFirstPressedFloor() : undefined; | |
return getDirection(elevator.currentFloor(), destination); | |
} | |
function pickElevator(destination) { | |
//elevators[rotator++ % elevators.length] | |
//var sameDirection = _.groupBy(elevators, function(elevator) { | |
// return getElevatorDirection(elevator); | |
//}); | |
var result; | |
var idleElevators = elevators.filter(function(elevator) { | |
return !elevator.destinationQueue || elevator.destinationQueue.length == 0; | |
}); | |
if (idleElevators.length > 0) { | |
return getClosestElevator(idleElevators, destination); | |
} | |
return getClosestElevator(elevators, destination);; | |
} | |
function getClosestElevator(availableElevators, destination) { | |
var minDelta = floors.length; | |
var result; | |
availableElevators = availableElevators || elevators; | |
_.each(availableElevators, function(elevator) { | |
var elevatorDelta = getDelta(elevator.currentFloor(), destination); | |
if ((minDelta = Math.min(elevatorDelta, minDelta)) == elevatorDelta) { | |
result = elevator; | |
} | |
}); | |
return result; | |
} | |
}, | |
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