Created
January 27, 2015 10:06
-
-
Save passsy/9daebd806436ffb6dc02 to your computer and use it in GitHub Desktop.
elevatorsaga two queues
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) { | |
_.each(elevators, function(elevator) { | |
elevator.goToFloor(0); | |
}); | |
var upQueue = []; | |
var downQueue = []; | |
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 runQueues() { | |
upQueue = _.uniq(upQueue); | |
downQueue = _.uniq(downQueue); | |
if (upQueue.length == 0 && downQueue.length == 0) { | |
return; | |
} | |
} | |
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 (Math.random() > 0.8) { | |
elevator.goToFloor(floors.length - 1); | |
} else { | |
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 (direction == "up") { | |
var above = _.filter(upQueue, function(level){ | |
return elevator.currentFloor() < level; | |
}); | |
var target = above[0]; | |
if (target){ | |
elevator.goToFloor(target, true); | |
upQueue = _.reject(upQueue, function(lvl){return lvl == target}); | |
} | |
} | |
}); | |
}); | |
_.each(floors, function(floor){ | |
floor.on('up_button_pressed', function(){ | |
upQueue.push(floor.level); | |
runQueues(); | |
}); | |
floor.on('down_button_pressed', function(){ | |
downQueue.push(floor.level); | |
runQueues(); | |
}); | |
}); | |
}, | |
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