Last active
November 9, 2018 19:37
-
-
Save DaniilVysotskiy/52f068e0d049794a33f3b2c96addaf04 to your computer and use it in GitHub Desktop.
Elevator Saga version 0.0.4
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
{ | |
queue: { | |
up: [], | |
down: [] | |
}, | |
jobs: { | |
elevators: [] | |
}, | |
init: function(elevators, floors) { | |
// Queues | |
var queueUp = this.queue.up; | |
var queueDown = this.queue.down; | |
// elevators with job | |
var elevatorsWithJob = this.jobs.elevators; | |
// Queue stats UI | |
var container = document.querySelector('.statscontainer'); | |
var queueUpStats = document.createElement('div'); | |
queueUpStats.setAttribute('id', 'queueUp'); | |
queueUpStats.style.top = 160 + 'px'; | |
var queueUpKey = document.createElement('span'); | |
queueUpKey.classList.add('key'); | |
queueUpKey.innerText = 'Queue Up'; | |
var queueUpValue = document.createElement('span'); | |
queueUpValue.classList.add('value'); | |
queueUpValue.innerText = ''; | |
queueUpStats.appendChild(queueUpKey); | |
queueUpStats.appendChild(queueUpValue); | |
container.appendChild(queueUpStats); | |
var queueDownStats = document.createElement('div'); | |
queueDownStats.setAttribute('id', 'queueDown'); | |
queueDownStats.style.top = 180 + 'px'; | |
var queueDownKey = document.createElement('span'); | |
queueDownKey.classList.add('key'); | |
queueDownKey.innerText = 'Queue Down'; | |
var queueDownValue = document.createElement('span'); | |
queueDownValue.classList.add('value'); | |
queueDownValue.innerText = ''; | |
queueDownStats.appendChild(queueDownKey); | |
queueDownStats.appendChild(queueDownValue); | |
container.appendChild(queueDownStats); | |
elevators.forEach((elevator, index) => { | |
// Queue stats UI | |
var lift = document.createElement('div'); | |
lift.setAttribute('id', 'lift' + (index + 1)); | |
lift.style.top = (200 + 20 * (index + 1)) + 'px'; | |
var liftKey = document.createElement('span'); | |
liftKey.classList.add('key'); | |
liftKey.innerText = 'Lift #' + (index + 1); | |
var liftValue = document.createElement('span'); | |
liftValue.classList.add('value'); | |
liftValue.innerText = ''; | |
lift.appendChild(liftKey); | |
lift.appendChild(liftValue); | |
container.appendChild(lift); | |
// if no jobs, go to ground floor | |
elevator.on("idle", function() { | |
if ((queueUp.length > queueDown.length)) { | |
elevator.destinationQueue = queueUp; | |
// queueUp = []; | |
elevator.goingUpIndicator(true); | |
elevator.checkDestinationQueue(); | |
} else if ((queueDown.length > queueUp.length)) { | |
elevator.destinationQueue = queueDown; | |
// queueDown = []; | |
elevator.goingDownIndicator(true); | |
elevator.checkDestinationQueue(); | |
} else { | |
console.log('no jobs, returning to base!') | |
if (elevator.currentFloor() !== 0) { | |
elevator.goingDownIndicator(true); | |
elevator.goToFloor(0); | |
} | |
} | |
}); | |
// handling buttons in elevator | |
elevator.on("floor_button_pressed", function(floorNum) { | |
if (elevator.destinationQueue.includes(floorNum) === false) { | |
elevator.destinationQueue.push(floorNum); | |
} | |
// on ground floor | |
if (elevator.currentFloor() === 0) { | |
elevator.destinationQueue.sort((a, b) => a - b); | |
} | |
// on last floor | |
if (elevator.currentFloor() === floors.length - 1) { | |
elevator.destinationQueue.sort((a, b) => b - a); | |
} | |
// if better go up | |
if (elevator.currentFloor() < elevator.destinationQueue[0]) { | |
elevator.destinationQueue.sort((a, b) => a - b); | |
} | |
// if better go down | |
if (elevator.currentFloor() > elevator.destinationQueue[0]) { | |
elevator.destinationQueue.sort((a, b) => b - a); | |
} | |
elevator.checkDestinationQueue(); | |
}); | |
// Passing by handler | |
elevator.on("passing_floor", function(floorNum, direction) { | |
if (queueUp.includes(floorNum) && direction === 'up') { | |
queueUp.splice(queueUp.indexOf(floorNum), 1); | |
elevator.destinationQueue.unshift(floorNum); | |
} else if (queueDown.includes(floorNum) && direction === 'down') { | |
queueDown.splice(queueDown.indexOf(floorNum), 1); | |
elevator.destinationQueue.unshift(floorNum); | |
} | |
elevator.checkDestinationQueue(); | |
}); | |
// stopped at floor by handler | |
elevator.on("stopped_at_floor", function(floorNum) { | |
if (queueUp.includes(floorNum)) { | |
elevator.goingUpIndicator(true); | |
queueUp.splice(queueUp.indexOf(floorNum), 1); | |
} else if (queueDown.includes(floorNum)) { | |
queueDown.splice(queueDown.indexOf(floorNum), 1); | |
elevator.goingDownIndicator(true); | |
} | |
elevator.checkDestinationQueue(); | |
}); | |
}); | |
// handling buttons on each floor | |
floors.forEach(function(floor) { | |
// add floor to queueUp array | |
floor.on('up_button_pressed', function() { | |
floorNum = floor.floorNum(); | |
// if button pressed on ground floor - ignore, otherwise add to queueUp array | |
// if (floorNum !== 0) { | |
queueUp.push(floorNum); | |
queueUp.sort((a, b) => a - b); | |
// } | |
}); | |
// add floor to queueDown array | |
floor.on('down_button_pressed', function() { | |
floorNum = floor.floorNum(); | |
// if (floorNum !== 0) { | |
queueDown.push(floorNum); | |
queueDown.sort((a, b) => b - a); | |
// } | |
}); | |
}); | |
}, | |
update: function(dt, elevators, floors) { | |
// Queues | |
var queueUp = this.queue.up; | |
var queueDown = this.queue.down; | |
// elevators with job | |
var elevatorsWithJob = this.jobs.elevators; | |
// Queue real-time UI update | |
var queueUpValue = document.querySelector('#queueUp > .value'); | |
queueUpValue.innerText = queueUp; | |
var queueDownValue = document.querySelector('#queueDown > .value'); | |
queueDownValue.innerText = queueDown; | |
elevators.forEach((elevator, index) => { | |
if (elevator.destinationQueue.length && !elevatorsWithJob.includes(index)) { | |
elevatorsWithJob.push(index); | |
} else { | |
elevatorsWithJob.splice(index, 1); | |
} | |
console.log('elevatorsWithJob', elevatorsWithJob); | |
if (elevator.destinationDirection() === 'stopped' && elevator.currentFloor() === 0 && (queueUp.length || queueDown.length)) { | |
if (queueDown.length) { | |
elevator.destinationQueue = queueDown; | |
queueDown = []; | |
} else if (queueUp.length) { | |
elevator.destinationQueue = queueUp; | |
queueUp = []; | |
}; | |
elevator.goingUpIndicator(true); | |
elevator.checkDestinationQueue(); | |
} | |
// if elevator on ground floor, flash up indicator | |
if (elevator.currentFloor() === 0) { | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} | |
// if elevator on last floor, flash down indicator | |
if (elevator.currentFloor() === floors.length - 1) { | |
elevator.goingDownIndicator(true); | |
elevator.goingUpIndicator(false); | |
} | |
if(elevator.goingDownIndicator()) { | |
elevator.goingUpIndicator(false); | |
} | |
if(elevator.goingUpIndicator()) { | |
elevator.goingDownIndicator(false); | |
} | |
var idx = '#lift' + (index + 1); | |
var liftValue = document.querySelector(idx + ' > .value'); | |
liftValue.innerText = elevator.destinationQueue; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment