Created
January 25, 2015 00:26
-
-
Save finder39/dc77e2693d71faf9f691 to your computer and use it in GitHub Desktop.
http://play.elevatorsaga.com/ - getting there
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) { | |
var getByFloor = function (value) { | |
var result = floorsWaiting.filter(function(o){return o.floorNum == value;} ); | |
return result? result[0] : null; // or undefined | |
} | |
var getElevatorById = function (arr, value) { | |
var result = arr.filter(function(o){return o.iden == value;} ); | |
return result? result[0] : null; // or undefined | |
} | |
var floorsWaiting = [] | |
var floorsVisited = [] | |
var moveFloorToFront = function(floorNum) { | |
floorsVisited = floorsVisited.filter(function(o){return o != floorNum;} ); | |
floorsVisited.unshift(floorNum) | |
} | |
var floorHasHigherPriority = function(newFloor, oldFloor) { | |
return floorsVisited.indexOf(newFloor) > floorsVisited.indexOf(oldFloor) | |
} | |
var sendElevatorToAFloor = function() { | |
var evToUse | |
elevators.forEach(function (elevator, index) { | |
if (elevator.idleInterval != null) { | |
evToUse = elevator | |
} | |
}); | |
if (evToUse == null) { | |
evToUse = elevators[Math.floor(Math.random()*elevators.length)]; | |
} | |
evToUse.findFloorToGoTo() | |
} | |
elevators.forEach(function (elevator, index) { | |
elevator.iden = index | |
elevator.findFloorToGoTo = function() { | |
var floorToGoTo | |
floorsWaiting.forEach(function (floorWaiting, index) { | |
if (getElevatorById(floorWaiting.vads, elevator.iden).inside == true) { | |
floorToGoTo = floorWaiting.floorNum | |
} else { | |
if (floorWaiting.up == true) { | |
if (floorToGoTo == null) { floorToGoTo = floorWaiting.floorNum;} | |
} | |
if (floorWaiting.down == true) { | |
if (!floorToGoTo == null) { floorToGoTo = floorWaiting.floorNum;} | |
} | |
} | |
}); | |
if (floorToGoTo != null && (elevator.destinationQueue == null || elevator.destinationQueue.length == 0)) { | |
elevator.destinationQueue = [floorToGoTo] | |
elevator.checkDestinationQueue() | |
if (elevator.idleInterval != null) { | |
clearInterval(elevator.idleInterval) | |
elevator.idleInterval = null | |
} | |
} | |
return floorToGoTo | |
} | |
elevator.on("idle", function() { | |
if (elevator.findFloorToGoTo() == null) { | |
elevator.idleInterval = setInterval(elevator.findFloorToGoTo, 100); | |
} | |
}); | |
elevator.on("stopped_at_floor ", function(floorNum) { | |
getElevatorById(getByFloor(floorNum).vads, elevator.iden).inside = false | |
getByFloor(floorNum).up = false | |
getByFloor(floorNum).down = false | |
moveFloorToFront(floorNum) | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
getElevatorById(getByFloor(floorNum).vads, elevator.iden).inside = true | |
}); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
//console.log(elevator.loadFactor()) | |
var floor = getByFloor(floorNum) | |
if (floor.inside == true) { | |
elevator.destinationQueue.unshift(floorNum) | |
elevator.checkDestinationQueue() | |
} else { | |
if (elevator.loadFactor() > 0.65) { | |
return | |
} | |
if (floor.up == true && direction == "up") { | |
if (floorNum != elevator.destinationQueue[0]) { | |
elevator.destinationQueue.unshift(floorNum) | |
elevator.checkDestinationQueue() | |
} | |
} | |
if (floor.down == true && direction == "down") { | |
if (floorNum != null && floorNum != elevator.destinationQueue[0]) { | |
elevator.destinationQueue.unshift(floorNum) | |
elevator.checkDestinationQueue() | |
} | |
} | |
} | |
}); | |
}); | |
floors.forEach(function (floor, index) { | |
// init each floor | |
var floorObj = { | |
floorNum: floor.floorNum(), | |
up: false, | |
down: false, | |
vads: [] | |
} | |
elevators.forEach(function (elevator, index) { | |
floorObj.vads.push({ | |
iden: elevator.iden, | |
inside: false | |
}) | |
}); | |
floorsWaiting.push(floorObj); | |
floor.on("up_button_pressed", function() { | |
getByFloor(floor.floorNum()).up = true | |
sendElevatorToAFloor() | |
}); | |
floor.on("down_button_pressed", function() { | |
getByFloor(floor.floorNum()).down = true | |
sendElevatorToAFloor() | |
}); | |
}); | |
}, | |
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