Created
May 18, 2016 09:22
-
-
Save megatolya/d6a21c88957a2888f0174eb97f476e1d to your computer and use it in GitHub Desktop.
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 floorsQueue = []; | |
var UP = 'up'; | |
var DOWN = 'down'; | |
function removeFromQueue(floor) { | |
if (floorsQueue.indexOf(floor) !== -1) { | |
floorsQueue.splice(floor, 1); | |
} | |
} | |
function getNextQueuedFloor(elevator) { | |
var currentFloor = elevator.currentFloor(); | |
var prevFloor = currentFloor - 1; | |
var nextFloor = currentFloor + 1; | |
var reallyNext = true; | |
var reallyPrev = true; | |
if (prevFloor === -1) { | |
prevFloor = 2; | |
reallyPrev = false; | |
} | |
if (nextFloor === floors.length) { | |
nextFloor = floors.length - 2; | |
reallyNext = false; | |
} | |
var nearestNext = 0; | |
var nearestPrev = 0; | |
var nearest = -1; | |
floorsQueue.forEach(function (floor) { | |
if (nearest < Math.abs(floor.num - currentFloor)) { | |
nearest = floor.num; | |
} | |
if (floor.num === nextFloor && reallyNext) { | |
nearestNext++; | |
} | |
if (floor.num === prevFloor && reallyPrev) { | |
nearestPrev++; | |
} | |
}); | |
if (nearestNext && nearestNext > nearestPrev) { | |
return nearestNext; | |
} | |
if (nearestPrev && nearestPrev > nearestNext) { | |
return nearestPrev; | |
} | |
return nearest; | |
} | |
floors.forEach(function (floor) { | |
floor.on("up_button_pressed", function() { | |
floorsQueue.push({num: floor.floorNum(), direction: UP}); | |
}); | |
floor.on("down_button_pressed", function() { | |
floorsQueue.push({num: floor.floorNum(), direction: DOWN}); | |
}); | |
}); | |
var elevatorsPassengers = {}; | |
function getNextPressedFloor(elevator, id) { | |
var floors = elevatorsPassengers[id] || {}; | |
var max = 0; | |
var maxFloorNum = -1; | |
Object.keys(floors).forEach(function (floorNum) { | |
var count = floors[floorNum]; | |
if (count >= max) { | |
max = count; | |
maxFloorNum = floorNum; | |
} | |
}); | |
return maxFloorNum; | |
} | |
elevators.forEach(function (elevator, id) { | |
function onStoppedAtFloor() { | |
var floorNum = elevator.currentFloor(); | |
elevatorsPassengers[id][floorNum] = 0; | |
var pressed = getNextPressedFloor(elevator, id); | |
var queued = getNextQueuedFloor(elevator); | |
if (pressed !== -1) { | |
elevator.goToFloor(pressed); | |
} else if (queued !== -1) { | |
elevator.goToFloor(queued); | |
} | |
} | |
elevatorsPassengers[id] = {}; | |
elevator.on('floor_button_pressed', function (floorNum) { | |
removeFromQueue(floorNum); | |
elevatorsPassengers[id][floorNum] = elevatorsPassengers[id][floorNum] || 0; | |
elevatorsPassengers[id][floorNum]++; | |
var next = getNextPressedFloor(elevator, id); | |
elevator.goToFloor(next); | |
}); | |
elevator.on("passing_floor", function(floor) { | |
if (elevator.loadFactor() < 0.6) { | |
if (floorsQueue.map(function (floor) { | |
return floor.num; | |
}).indexOf(floor.num) !== -1) { | |
elevator.stop(); | |
} | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
onStoppedAtFloor(); | |
}); | |
onStoppedAtFloor(); | |
}); | |
}, update: function(dt, elevators, floors) { } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment