Created
January 24, 2015 18:42
-
-
Save robo-corg/24fcabf1550901e0f640 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 optimizeDestinationQueue(elevator) { | |
queue = elevator.destinationQueue.sort(); | |
console.log("starting queue: " + queue); | |
queue = queue.reduce(function (a, b) { | |
console.log("a: " + a); | |
if (a.indexOf(b) !== -1) { | |
return a; | |
} | |
else { | |
return a.concat([b]); | |
} | |
}, []); | |
console.log("optimized queue: " + queue); | |
elevator.destinationQueue = queue; | |
elevator.checkDestinationQueue(); | |
} | |
function bestElevatorForFloor(floor) { | |
var elevatorsAlreadyVisiting = elevators.filter(function (elevator) { | |
return elevator.destinationQueue.indexOf(floor.floorNum()) !== -1; | |
}).sort(function (e1, e2) { | |
return e1.loadFactor() - e2.loadFactor(); | |
}); | |
console.log(elevatorsAlreadyVisiting.map(function (e) { return e.loadFactor();})); | |
if (elevatorsAlreadyVisiting.length > 0) { | |
return elevatorsAlreadyVisiting[0]; | |
} | |
function distToFloor(e) { | |
return Math.abs(e.currentFloor() - floor.floorNum()) * e.loadFactor(); | |
} | |
var sorted = elevators.sort(function (e1, e2) { | |
return distToFloor(e1) - distToFloor(e2); | |
}); | |
return sorted[0]; | |
} | |
floors.forEach(function (floor) { | |
var buttonHandler = function() { | |
var selectElevator = bestElevatorForFloor(floor); | |
selectElevator.goToFloor(floor.floorNum()); | |
// optimizeDestinationQueue(selectElevator); | |
}; | |
floor.on("down_button_pressed", buttonHandler); | |
floor.on("up_button_pressed", buttonHandler); | |
}); | |
var elevatorFloor = 0; | |
elevators.forEach(function (elevator) { | |
elevator.on("idle", function() { | |
// The elevator is idle, so let's go to all the floors (or did we forget one?) | |
//elevator.goToFloor(elevatorFloor); | |
elevatorFloor = (elevatorFloor + 1) % floors.length; | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.goToFloor(floorNum); | |
optimizeDestinationQueue(elevator); | |
}); | |
}); | |
}, | |
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