Last active
August 29, 2015 14:14
-
-
Save passsy/8e29670a3166ef4cb047 to your computer and use it in GitHub Desktop.
elevatorsaga level 10 - single queue http://play.elevatorsaga.com/#challenge=10
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
/** | |
* elevatorsaga level 10 - single queue | |
* http://play.elevatorsaga.com/#challenge=10 | |
**/ | |
{ | |
init: function(elevators, floors) { | |
_.each(elevators, function(elevator) { | |
elevator.goToFloor(0); | |
}); | |
var globalWaitingQueue = []; | |
function moveClosestElevator(elevators, level) { | |
var availableElevators = _.filter(elevators, function(el) { | |
return el.loadFactor() < 0.7; | |
}); | |
availableElevators = _.shuffle(availableElevators); | |
var byDistance = _.sortBy(availableElevators, function(elevator) { | |
return elevator.level - level; | |
}); | |
var found; | |
if(availableElevators.length <= 2){ | |
found = _.find(byDistance, function(elevator) { | |
return Math.abs(elevator.level - level) <= 3; | |
}); | |
} else { | |
found = byDistance[0]; | |
} | |
if (found) { | |
found.goToFloor(level); | |
globalWaitingQueue = _.reject(globalWaitingQueue, function (lvl) {return lvl == level}); | |
return true; | |
} | |
return false; | |
} | |
function workQueue() { | |
globalWaitingQueue = _.uniq(globalWaitingQueue); | |
console.log(globalWaitingQueue); | |
if (globalWaitingQueue.length > 0) { | |
var level = globalWaitingQueue[0]; | |
var emptyElevators = _.filter(elevators, function (elevator) {return elevator.loadFactor() == 0}); | |
if (emptyElevators.length > 0) { | |
// TODO get closest | |
return moveClosestElevator(emptyElevators, level); | |
} else { | |
//nothing to to here. queue | |
return false; | |
} | |
} | |
} | |
_.each(elevators, function(elevator) { | |
elevator.on("idle", function() { | |
if (!workQueue()) { | |
elevator.goToFloor(0); | |
} | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.goToFloor(floorNum); | |
_.uniq(elevator.destinationQueue); | |
//elevator.destinationQueue.sort(); | |
elevator.checkDestinationQueue(); | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
if (floorNum == 0) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(true); | |
} | |
}) | |
elevator.on("passing_floor", function(floorNum, direction) { | |
if (globalWaitingQueue.length > 0) { | |
//console.log("floorNum: " + floorNum + " direction: " + direction + " elevator.currentFloor : " + elevator.currentFloor()); | |
var floor = floors[floorNum]; | |
if (elevator.loadFactor() < 1) { | |
if ("up") { | |
var above = _.filter(globalWaitingQueue, function(lvl) { | |
//return elevator.currentFloor() < lvl && lvl <= _.max(elevator.destinationQueue); | |
return elevator.currentFloor() < lvl; | |
}); | |
if (above.length > 0) { | |
var target = above[0]; | |
elevator.goToFloor(target, true); | |
globalWaitingQueue = _.reject(globalWaitingQueue, function (lvl) {return lvl == target}); | |
} | |
} else if ("down") { | |
var below = _.filter(globalWaitingQueue, function(lvl) { | |
//return elevator.currentFloor() > lvl && lvl >= _.min(elevator.destinationQueue); | |
return elevator.currentFloor() > lvl; | |
}); | |
if (below.length > 0) { | |
var target = below[0]; | |
elevator.goToFloor(target, true); | |
globalWaitingQueue = _.reject(globalWaitingQueue, function (lvl) {return lvl == target}); | |
} | |
} | |
} | |
} | |
}); | |
}); | |
_.each(floors, function(floor) { | |
_.each(['up_button_pressed', 'down_button_pressed'], function(event) { | |
floor.on(event, function() { | |
globalWaitingQueue.push(floor.level); | |
workQueue(); | |
//console.log(globalWaitingQueue); | |
}); | |
}); | |
}); | |
}, | |
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