Created
January 24, 2015 04:07
-
-
Save pdamer/59a9e65fa737e5c5756b to your computer and use it in GitHub Desktop.
elevator
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) { | |
elevators.dispatchElevator = function(floorNum, direction) { | |
if (elevator = elevators.already_going_to(floorNum, direction)) { | |
if (elevator.destinationQueue.indexOf(floorNum) < 2) { | |
return true; | |
} | |
} | |
var queues = elevators.map(function(elevator) { return elevator.destinationQueue.length }); | |
var min = queues.slice().sort()[0] | |
var elevator = elevators[queues.indexOf(min)] | |
if (elevator.destinationQueue.indexOf(floorNum) < 0) { | |
elevator.goToFloor(floorNum); | |
} | |
console.log('dispatching elevator' + elevator.number + ' to ' + floorNum + ' q: ' + elevator.destinationQueue); | |
} | |
elevators.already_going_to = function(floorNum, direction) { | |
return elevators.find(function(elevator) { | |
if (elevator.destinationQueue.indexOf(floorNum) == -1) { | |
return false; | |
} | |
if (floorNum == floors.length - 1 || floorNum == 0) { | |
return true | |
} | |
if (direction == 'up' && floorNum >= elevator.currentFloor()) { | |
return true; | |
} | |
if (direction == 'down' && floorNum <= elevator.currentFloor()) { | |
return true; | |
} | |
return false; | |
}); | |
} | |
elevators.forEach(function(elevator) { | |
elevator.going = function() { | |
if (this.goingDownIndicator()) { | |
return('down'); | |
} | |
if (this.goingUpIndicator()) { | |
return('up'); | |
} | |
console.log(this.number + 'just going!'); | |
return 'idle'; | |
} | |
elevator.number = elevators.indexOf(elevator); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
console.log(elevator.number + 'passing ' + floorNum + ' with load ' + elevator.loadFactor()); | |
if (elevator.loadFactor() < .8) { | |
if (floors[floorNum].waiting(direction) && ! elevators.already_going_to(floorNum, direction)) { | |
console.log(elevator.number + " helpful stopping at " + floorNum + " going " + direction); | |
elevator.goToFloor(floorNum, true); | |
} | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
console.log("elevator" + elevator.number + "stopping at " + floorNum + " going" + elevator.goingUpIndicator() + elevator.goingDownIndicator() + 'next '+ elevator.destinationQueue); | |
elevator.floor_buttons[floorNum] = false; | |
if (elevator.destinationQueue[0]) { | |
if (!elevator.floor_buttons[elevator.destinationQueue[0]]) { | |
console.log(elevator.number + "should still go to " + elevator.destinationQueue[0]); | |
while (elevator.destinationQueue[0] && !floors[elevator.destinationQueue[0]].up_waiting && !floors[elevator.destinationQueue[0]].down_waiting) { | |
console.log('skipping not waiting from '+floorNum+' to ' + elevator.destinationQueue[0]); | |
elevator.destinationQueue.shift(); | |
} | |
elevator.checkDestinationQueue(); | |
if (elevator.destinationQueue[0] > elevator.currentFloor()) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} else { | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} | |
} | |
} else { | |
if (floorNum == 0) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} else if (floorNum == floors.length -1) { | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} else if (elevator.floor_buttons.indexOf(true) == -1) { | |
if (floors[floorNum].up_waiting) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} else if (floors[floorNum].down_waiting) { | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} | |
} | |
} | |
if (elevator.goingUpIndicator()) { | |
floors[floorNum].up_waiting = false; | |
} else if (elevator.goingDownIndicator()) { | |
floors[floorNum].down_waiting = false; | |
} | |
}); | |
elevator.on("idle", function() { | |
// The elevator is idle, so let's go to all the floors (or did we forget one?) | |
elevator.goToFloor(0); | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(false); | |
}); | |
elevator.floor_buttons = []; | |
elevator.go_in_order_to_floor = function(floorNum) { | |
if (elevator.destinationQueue.indexOf(floorNum) < 0) { | |
elevator.goToFloor(floorNum); | |
elevator.destinationQueue.sort(); | |
if (elevator.goingUpIndicator()) { | |
while (elevator.destinationQueue[0] < elevator.currentFloor() && elevator.destinationQueue) { | |
console.log('reordering up ' + elevator.currentFloor() + '-' + elevator.destinationQueue); | |
elevator.destinationQueue.push(elevator.destinationQueue.shift()); | |
} | |
} else if (elevator.goingDownIndicator()) { | |
elevator.destinationQueue.reverse() | |
while (elevator.destinationQueue[0] > elevator.currentFloor()) { | |
console.log('reordering down ' + elevator.currentFloor() + '-' + elevator.destinationQueue); | |
elevator.destinationQueue.push(elevator.destinationQueue.shift()); | |
} | |
} | |
elevator.checkDestinationQueue(); | |
} | |
} | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.floor_buttons[floorNum] = true; | |
elevator.go_in_order_to_floor(floorNum); | |
}); | |
}); | |
floors.forEach(function(floor) { | |
floor.waiting = function(direction) { | |
return((this.up_waiting && direction == 'up') || (this.down_waiting && direction == 'down')); | |
} | |
floor.on("up_button_pressed", function(event) { | |
this.up_waiting = true; | |
elevators.dispatchElevator(floor.floorNum(), 'up'); | |
} ); | |
floor.on("down_button_pressed", function(event) { | |
this.down_waiting = true; | |
elevators.dispatchElevator(floor.floorNum(), 'down'); | |
} ); | |
}); | |
}, | |
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