Created
January 25, 2015 07:02
-
-
Save foxyblocks/2d56a5d7ce60c7dc2ff1 to your computer and use it in GitHub Desktop.
Elevator Saga (Train strategy)
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) { | |
var floorsWaiting = { | |
up: [], | |
down: [] | |
}; | |
var self = this; | |
var distance = function(num1, num2) { | |
return Math.abs(num1 - num2); | |
}; | |
var uniq = function(array) { | |
return array.filter( function(value, index, self) { | |
return self.indexOf(value) === index; | |
}); | |
}; | |
var closestNum = function(array, num) { | |
closest = 0; | |
array.forEach(function(candidate){ | |
var d = distance(candidate, num); | |
var currentBestDistance = distance(closest, num); | |
if (d < currentBestDistance){ | |
closest = candidate; | |
} | |
}); | |
return closest; | |
}; | |
var closestFloorNumWaiting = function (elevator) { | |
var allFloorsNumsWaiting = floorsWaiting['up'].concat(floorsWaiting['down']); | |
return closestNum(allFloorsNumsWaiting, elevator.currentFloor()); | |
}; | |
var closestIdleElevator = function (floorNum) { | |
var idles = elevators.filter(function(elevator){ | |
return elevator.destinationQueue.length == 0; | |
}); | |
return idles[0]; | |
} | |
var floorIsWaiting = function(floorNum, direction) { | |
return floorsWaiting[direction].indexOf(floorNum) > -1 | |
} | |
var addWaiting = function(floorNum, direction) { | |
if (!floorIsWaiting(floorNum, direction)) { | |
floorsWaiting[direction].push(floorNum); | |
} | |
} | |
var handleCallRequest = function(floor, direction) { | |
if (!floorIsWaiting(floor.level, direction)) { | |
addWaiting(floor.level, direction); | |
} | |
}; | |
var handleUpRequest = function() { | |
console.log('upRequest', this); | |
handleCallRequest(this, 'up') | |
}; | |
var handleDownRequest = function() { | |
console.log('downRequest', this); | |
handleCallRequest(this, 'down') | |
}; | |
// bind up/down requests to all floors | |
floors.forEach(function(floor){ | |
floor.on('up_button_pressed', handleUpRequest); | |
floor.on('down_button_pressed', handleDownRequest); | |
}) | |
var checkIdle = function() { | |
var num = closestFloorNumWaiting(this) | |
console.log('idle', this, num, floorsWaiting); | |
if(typeof num !== 'undefined') { | |
var goingUp = (num > this.currentFloor()); | |
this.goingUpIndicator(goingUp); | |
this.goingDownIndicator(!goingUp); | |
this.goToFloor(num); | |
} else { | |
this.goToFloor(0); | |
} | |
} | |
// bind elevator events | |
elevators.forEach(function(elevator){ | |
elevator.on("floor_button_pressed", function(floorNum) { | |
var goingUp = floorNum > this.currentFloor(); | |
if (this.destinationQueue.indexOf(floorNum) == -1) { | |
this.destinationQueue.push(floorNum); | |
this.destinationQueue = this.destinationQueue.sort(); | |
if(!goingUp){ | |
this.destinationQueue = this.destinationQueue.reverse() | |
} | |
} | |
this.checkDestinationQueue() | |
console.log('floor_button_pressed', floorNum, this.destinationQueue) | |
}) | |
elevator.on("idle", checkIdle); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
var goingUp = (direction == 'up'); | |
console.log('passing_floor: ' + floorNum, floorsWaiting, 'going up: ' + this.goingUpIndicator(), 'going down: ' + this.goingDownIndicator()); | |
var index = floorsWaiting[direction].indexOf(floorNum); | |
if(index > -1) { | |
this.goToFloor(floorNum, true) | |
floorsWaiting[direction].splice(index, 1); | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
console.log('stopped_at_floor', floorNum, this.destinationQueue); | |
// var index = this.destinationQueue.indexOf(floorNum); | |
// if (index > -1) { | |
// this.destinationQueue.splice(index, 1); | |
// } | |
// | |
// //at the bottom | |
if (floorNum == 0) { | |
this.goingUpIndicator(true); | |
this.goingDownIndicator(false); | |
this.goToFloor(floors.length - 1); | |
} | |
// //at the top | |
if (floorNum == floors.length - 1) { | |
this.goingDownIndicator(true); | |
this.goingUpIndicator(false); | |
this.goToFloor(0); | |
} | |
}); | |
elevator.goToFloor(floors.length - 1); | |
}); | |
}, | |
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