Created
January 14, 2016 15:46
-
-
Save meetwudi/aabe51ef24b575589935 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Controller (elevator) { | |
var currentController = this; | |
this.elevator = elevator; | |
elevator.on('floor_button_pressed', function (floorNum) { | |
currentController.goToFloor(floorNum); | |
}); | |
} | |
/** | |
* Get floor priority of the request to this elevator at the moment. | |
* @param {[type]} floorRequest [description] | |
* @return {[type]} [description] | |
*/ | |
Controller.prototype.getRequestPriority = function (floorRequest) { | |
var priority = 1 - this.elevator.loadFactor(); | |
if (floorRequest.floorNum > this.elevator.currentFloor() && this.elevator.destinationDirection() === 'up') priority += 10 / Math.pow(Math.abs(floorRequest.floorNum - this.elevator.currentFloor()), 2); | |
if (floorRequest.floorNum < this.elevator.currentFloor() && this.elevator.destinationDirection() === 'down') priority -= 10 / Math.pow(Math.abs(floorRequest.floorNum - this.elevator.currentFloor()), 2); | |
return priority; | |
}; | |
Controller.prototype.appendFloorRequest = function (floorRequest) { | |
this.goToFloor(floorRequest.floorNum); | |
}; | |
Controller.prototype.goToFloor = function (floorNum) { | |
var currentController = this; | |
this.elevator.goToFloor(floorNum); | |
setTimeout(function () { | |
currentController.rearrangeDestination(); | |
}, 0); | |
}; | |
Controller.prototype.rearrangeDestination = function () { | |
var currentController = this; | |
var destinationsToSameDirection, destinationToOpposite; | |
if (currentController.elevator.destinationDirection() === 'up') { | |
destinationsToSameDirection = currentController.elevator.destinationQueue.filter(function (destination) { | |
return destination >= currentController.elevator.currentFloor(); | |
}).sort(function (lhs, rhs) { | |
return lhs > rhs; | |
}); | |
destinationToOpposite = currentController.elevator.destinationQueue.filter(function (destination) { | |
return destination < currentController.elevator.currentFloor(); | |
}); | |
} | |
else if (currentController.elevator.destinationDirection() === 'down') { | |
destinationsToSameDirection = currentController.elevator.destinationQueue.filter(function (destination) { | |
return destination <= currentController.elevator.currentFloor(); | |
}).sort(function (lhs, rhs) { | |
return lhs < rhs; | |
}); | |
destinationToOpposite = currentController.elevator.destinationQueue.filter(function (destination) { | |
return destination > currentController.elevator.currentFloor(); | |
}); | |
} | |
else { | |
return; | |
} | |
currentController.elevator.destinationQueue = destinationsToSameDirection.concat(destinationToOpposite); | |
currentController.elevator.checkDestinationQueue(); | |
} | |
var controllers = elevators.map(function (elevator) { | |
return new Controller(elevator); | |
}); | |
function makeFloorRequest (direction, floorNum) { | |
return { | |
direction: direction, | |
floorNum: floorNum | |
}; | |
} | |
function dispatchFloorRequest (floorRequest) { | |
var sortedControllers = controllers.map(function (controller) { | |
return { | |
controller: controller, | |
priority: controller.getRequestPriority(floorRequest) | |
}; | |
}).sort(function (lhs, rhs) { | |
return lhs.priority < rhs.priority; | |
}).map(function (_) { | |
return _.controller; | |
}); | |
sortedControllers[0].appendFloorRequest(floorRequest); | |
} | |
floors.forEach(function (floor) { | |
floor.on('up_button_pressed', function () { | |
dispatchFloorRequest(makeFloorRequest('up', floor.floorNum())); | |
}); | |
floor.on('down_button_pressed', function () { | |
dispatchFloorRequest(makeFloorRequest('down', floor.floorNum())); | |
}); | |
}); | |
}, | |
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