Created
April 4, 2017 16:22
-
-
Save vibby/19dcc6192c04ed7839458f2924a8bb18 to your computer and use it in GitHub Desktop.
http://play.elevatorsaga.com Chalenge
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) { | |
Array.prototype.getMaxValue = function() { | |
return Math.max.apply(null, this); | |
}; | |
Array.prototype.getMinValue = function() { | |
return Math.min.apply(null, this); | |
}; | |
Array.prototype.removeValue = function(what) { | |
while ((ax = this.indexOf(what)) !== -1) { | |
this.splice(ax, 1); | |
} | |
return this; | |
}; | |
Array.prototype.addValue = function(what) { | |
if (!this.hasValue(what)) { | |
this.push(what); | |
} | |
}; | |
Array.prototype.hasValue = function(what) { | |
return this.indexOf(what) != -1; | |
}; | |
Array.prototype.sortByInteger = function() { | |
return this.sort(function (a, b) { | |
if (a < b) | |
return -1; | |
if (a > b) | |
return 1; | |
return 0; | |
}) | |
}; | |
var floorsThatWantUp = []; | |
var floorsThatWantDown = []; | |
elevators[0].goToFloor(100); | |
for (elevatorId in elevators) { | |
elevator = elevators[elevatorId]; | |
if (typeof elevator != 'object') { | |
continue; | |
} | |
elevator.reordQueue = function (floor) { | |
this.destinationQueue.sortByInteger(); | |
if (this.goingDownIndicator()) { | |
this.destinationQueue.reverse(); | |
} | |
this.checkDestinationQueue(); | |
} | |
elevator.addFloor = function(floor) { | |
this.goToFloor(floor); | |
this.reordQueue(); | |
} | |
elevator.removeFloor = function(floor) { | |
this.destinationQueue.removeValue(floor); | |
this.checkDestinationQueue(); | |
} | |
elevator.changeDirection = function (direction) { | |
direction = typeof direction != "undefined" ? direction : this.goingDownIndicator(); | |
this.goingDownIndicator(!direction); | |
this.goingUpIndicator(direction); | |
} | |
elevator.changeDirection(true); | |
elevator.on("floor_button_pressed", function(floor) { | |
this.addFloor(floor); | |
}); | |
elevator.on("passing_floor", function(floor) { | |
if (this.loadFactor() >= .8) { | |
return; | |
} | |
if (this.goingUpIndicator() && floorsThatWantUp.hasValue(floor)) { | |
this.goToFloor(floor, true); | |
floorsThatWantUp.removeValue(floor); | |
} | |
if (this.goingDownIndicator() && floorsThatWantDown.hasValue(floor)) { | |
this.goToFloor(floor, true); | |
floorsThatWantDown.removeValue(floor); | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floor) { | |
if (this.goingUpIndicator()) { | |
floorsThatWantUp.removeValue(floor); | |
} | |
if (this.goingDownIndicator()) { | |
floorsThatWantDown.removeValue(floor); | |
} | |
if (floor == 0) { | |
this.changeDirection(true); | |
} | |
}); | |
elevator.on("idle", function() { | |
this.changeDirection(false); | |
var highestWanted = floorsThatWantDown.getMaxValue(); | |
this.goToFloor(highestWanted ? highestWanted : 0); | |
floorsThatWantDown.removeValue(highestWanted); | |
}); | |
} | |
for (floorId in floors) { | |
floor = floors[floorId]; | |
if (typeof floor != 'object') { | |
continue; | |
} | |
floor.on("up_button_pressed", function() { | |
floorsThatWantUp.addValue(this.floorNum()); | |
}); | |
floor.on("down_button_pressed", function() { | |
floorsThatWantDown.addValue(this.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