Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loisaidasam/a3dd4ac53c878aecfc7a to your computer and use it in GitHub Desktop.
Save loisaidasam/a3dd4ac53c878aecfc7a to your computer and use it in GitHub Desktop.
{
// Docs: http://play.elevatorsaga.com/documentation.html#docs
init: function(elevators, floors) {
console.log("init() with " + elevators.length + " elevators and " + floors.length + " floors");
for (var i=0; i<elevators.length; i++) {
(function(elevator) {
elevator.myNum = i;
elevator.log = function(text) {
console.log("Elevator[" + elevator.myNum + "] " + text);
}
elevator.on("idle", function() {
//var idleFloor = floors.length / 2 - 1;
//if (idleFloor < 0) {
// idleFloor = 0;
//}
var idleFloor = 0;
console.log("idle, going to floor " + idleFloor);
elevator.goToFloor(idleFloor);
//console.log("done idling");
});
elevator.on("floor_button_pressed", function(floorNum) {
console.log("floor_button_pressed " + floorNum + " (current destinationQueue: " + elevator.destinationQueue + ")");
(function(elevator, floorNum) {
if (elevator.destinationQueue.indexOf(floorNum) != -1) {
elevator.log("floorNum is already in my queue");
return;
}
if (elevator.goingUpIndicator() && floorNum < elevator.currentFloor()) {
console.log("goingUpIndicator but floorNum (" + floorNum + ") < currentFloor (" + elevator.currentFloor() + ")");
console.log("adding to end of queue");
elevator.goToFloor(floorNum);
return;
}
if (elevator.goingDownIndicator() && floorNum > elevator.currentFloor()) {
console.log("goingDownIndicator but floorNum (" + floorNum + ") > currentFloor (" + elevator.currentFloor() + ")");
console.log("adding to end of queue");
elevator.goToFloor(floorNum);
return;
}
// Push floors in the queue that are on the way to this floor
var newQueue = new Array();
var currentFloor = elevator.currentFloor();
if (floorNum > currentFloor) {
// Going up!
for (var j = currentFloor; j < floorNum; j++) {
if (elevator.destinationQueue.indexOf(j) != -1) {
console.log("Stopping at " + j + " on the way to " + floorNum);
newQueue.push(j);
}
}
} else if (floorNum < currentFloor) {
// Going down!
for (var j = currentFloor; j > floorNum; j--) {
if (elevator.destinationQueue.indexOf(j) != -1) {
console.log("Stopping at " + j + " on the way to " + floorNum);
newQueue.push(j);
}
}
}
// Push floor
newQueue.push(floorNum);
// Push remaining queue items
for (var j = 0; j < elevator.destinationQueue.length; j++) {
if (newQueue.indexOf(elevator.destinationQueue[j]) == -1) {
newQueue.push(elevator.destinationQueue[j]);
}
}
// And set the queue
elevator.destinationQueue = newQueue;
elevator.checkDestinationQueue();
})(elevator, floorNum);
console.log("elevator.destinationQueue: " + elevator.destinationQueue);
});
})(elevators[i]);
}
function chooseElevator(goToFloorNum) {
// See which is already going in that direction
var minLoadFactor = 1.0;
var currentMinLoadElevator;
for (var i = 0; i < elevators.length; i++) {
var elevator = elevators[i];
if (elevator.loadFactor() == 1) {
elevator.log("Load factor is 1!");
continue;
}
if (elevator.destinationQueue.indexOf(goToFloorNum) != -1) {
elevator.log("floorNum is already in my queue, done");
return elevator;
}
if (goToFloorNum > elevator.currentFloor() && elevator.goingUpIndicator()) {
elevator.log("Already going up, using me");
return elevator;
}
if (goToFloorNum < elevator.currentFloor() && elevator.goingDownIndicator()) {
elevator.log("Already going down, using me");
return elevator;
}
if (elevator.loadFactor() < minLoadFactor) {
currentMinLoadElevator = elevator;
minLoadFactor = elevator.loadFactor();
}
}
if (currentMinLoadElevator) {
currentMinLoadElevator.log("Using min load elevator with load " + minLoadFactor);
return currentMinLoadElevator;
}
// Other ideas:
// - Check for shortest queue
// - Check for closest to elevator's current floor
// - Check for least full
console.log("Using a random elevator");
var elevatorNum = Math.floor(Math.random() * elevators.length);
return elevators[elevatorNum];
}
for (var i=0; i<floors.length; i++) {
(function(floor){
var floorNum = floor.floorNum();
floor.log = function(text) {
console.log("Floor[" + floorNum + "]" + text);
}
floor.on("up_button_pressed", function() {
var elevator = chooseElevator(floorNum);
elevator.log("up_button_pressed from floor " + floorNum + " - using me");
if (elevator.destinationQueue.indexOf(floorNum) == -1) {
elevator.goToFloor(floorNum);
} else {
elevator.log("floor " + floorNum + " is already in destinationQueue!");
}
console.log("elevator.destinationQueue: " + elevator.destinationQueue);
});
floor.on("down_button_pressed", function() {
var elevator = chooseElevator(floorNum);
elevator.log("down_button_pressed from floor " + floorNum + " - using me");
if (elevator.destinationQueue.indexOf(floorNum) == -1) {
elevator.goToFloor(floorNum);
} else {
elevator.log("floor " + floorNum + " is already in destinationQueue!");
}
elevator.goToFloor(floorNum);
elevator.log("elevator.destinationQueue: " + elevator.destinationQueue);
});
})(floors[i]);
}
},
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