Created
January 14, 2016 02:21
-
-
Save chadkouse/da94114e3c5950b0eec6 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 findLeastBusyElevator() { | |
var e = elevators[0]; | |
elevators.forEach(function (el) { | |
if (el.destinationQueue.length < e.destinationQueue.length) | |
e = el; | |
}); | |
return e; | |
} | |
function findClosestLeastBusyElevator(floorNum) { //only needed on a few levels | |
var e = elevators[0]; | |
var closest = elevators.slice(0).sort(function (a, b) { | |
return Math.abs(a.currentFloor() - floorNum) - Math.abs(b.currentFloor() - floorNum); | |
}); | |
console.log(floorNum, closest[0].currentFloor()); | |
var closestOne = closest[0].currentFloor(); | |
closest = closest.filter(function(c){return c.currentFloor == closestOne;}); | |
closest.forEach(function (el) { | |
if (el.destinationQueue.length < e.destinationQueue.length) | |
e = el; | |
}); | |
return e; | |
} | |
for (var i = 0; i < elevators.length; i++) { | |
elevators[i].elevatorNum = i; | |
} | |
elevators.forEach(function (elevator) { | |
elevator.on("passing_floor", function (floorNum, direction) { | |
if (this.destinationQueue.indexOf(floorNum) >= 0) { | |
this.destinationQueue = this.destinationQueue.filter(function (f) { | |
return f != floorNum; | |
}); | |
this.checkDestinationQueue(); | |
this.goToFloor(floorNum, true); | |
} | |
}); | |
elevator.on("floor_button_pressed", function (floorNum) { | |
if (this.destinationQueue.indexOf(floorNum) < 0) | |
this.goToFloor(floorNum); | |
}); | |
elevator.on("idle", function() { | |
console.log("Idle....."); | |
this.goToFloor(0); | |
}); | |
}); | |
floors.forEach(function (floor) { | |
floor.on("up_button_pressed", function () { | |
var already = elevators.filter(function (e) { | |
return e.destinationQueue.indexOf(floor.floorNum()) >= 0 | |
}); | |
if (already.length > 0) | |
return; | |
var elevator = findLeastBusyElevator(this.floorNum()); //change to closest least busy for some | |
if (elevator.destinationQueue.indexOf(this.floorNum()) < 0) | |
elevator.goToFloor(this.floorNum()); | |
}); | |
floor.on("down_button_pressed", function () { | |
var already = elevators.filter(function (e) { | |
return e.destinationQueue.indexOf(floor.floorNum()) >= 0 | |
}); | |
if (already.length > 0) | |
return; | |
var elevator = findLeastBusyElevator(this.floorNum()); //change to closest least busy for some | |
if (elevator.destinationQueue.indexOf(this.floorNum()) < 0) | |
elevator.goToFloor(this.floorNum()); | |
}); | |
}); | |
}, | |
update: function (dt, elevators, floors) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment