Last active
August 29, 2015 14:18
-
-
Save futjikato/329643e2e0d6b1e30c2f to your computer and use it in GitHub Desktop.
Elevator.js OOP
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 MAX_FLOOR = 0, | |
ELEVATOR_COUNT = elevators.length; | |
/** | |
* Setup classes | |
*/ | |
function InQueue() { | |
this.queue = []; | |
this.elevators = []; | |
this.verifyProcess = function(entry) { | |
var ack = false; | |
this.elevators.forEach(function(elev) { | |
if(elev.reachOnTour(entry)) { | |
ack = true; | |
return false; // break | |
} | |
}); | |
if(!ack) { | |
this.elevators[0].nudge(entry); | |
} | |
}; | |
} | |
InQueue.prototype.add = function(atFloor, direction) { | |
var found = false; | |
this.queue.forEach(function(inEntry) { | |
if(inEntry.floor === atFloor && inEntry.direction === direction) { | |
found = true; | |
inEntry.cnt++; | |
return false; // break | |
} | |
}); | |
if(!found) { | |
var entry = { | |
floor: atFloor, | |
direction: direction, | |
cnt: 1, | |
mark: false | |
}; | |
this.queue.push(entry); | |
this.verifyProcess(entry); | |
} | |
}; | |
InQueue.prototype.addElevator = function(elevator) { | |
this.elevators.push(elevator); | |
}; | |
InQueue.prototype.isWaiting = function(atFloor, direction) { | |
var found = false; | |
this.queue.forEach(function(inEntry) { | |
if(!inEntry.mark && inEntry.floor === atFloor && inEntry.direction === direction) { | |
found = inEntry; | |
return false; // break | |
} | |
}); | |
return found; | |
}; | |
InQueue.prototype.clear = function(atFloor, direction) { | |
var newQ = []; | |
this.queue.forEach(function(inEntry) { | |
if(inEntry.floor !== atFloor || inEntry.direction !== direction) { | |
newQ.push(inEntry); | |
} | |
}); | |
this.queue = newQ; | |
}; | |
InQueue.prototype.getHighestWait = function() { | |
var max = false; | |
this.queue.forEach(function(inEntry) { | |
if(!max || (inEntry.mark == false && inEntry.direction === 'up' && inEntry.floor > max.floor)) { | |
max = inEntry; | |
} | |
}); | |
return max; | |
}; | |
/** | |
* Custom base class. | |
*/ | |
function MyElevator(base, queue) { | |
this.base = base; | |
this.direction = 'idle'; | |
this.queue = queue; | |
this.position = 0; | |
var that = this; | |
base.on("idle", function() { | |
that.direction = 'idle'; | |
console.log('idle'); | |
if(that.position !== 0) { | |
that.startDownTour(); | |
} | |
}); | |
base.on("passing_floor", function(floor, direction) { | |
if(direction !== that.direction) { | |
console.error('Direction is messed up?!'); | |
} | |
that.position = floor; | |
if(base.getPressedFloors().indexOf(floor) >= 0) { | |
base.goToFloor(floor, true); | |
} else if(base.loadFactor() < 0.8 && q.isWaiting(floor, direction)) { | |
q.isWaiting(floor, direction).mark = that; | |
base.goToFloor(floor, true); | |
q.clear(floor, that.direction); | |
} | |
}); | |
base.on("stopped_at_floor", function(floor) { | |
q.clear(floor, that.direction); | |
if(that.direction === 'up') { | |
if(base.destinationQueue.length == 0 && that.position > 0) { | |
that.startDownTour(); | |
} | |
if(base.getPressedFloors().length === 0) { | |
base.destinationQueue = []; | |
var qTarget = q.getHighestWait(); | |
qTarget.mark = that; | |
base.goToFloor(qTarget.floor); | |
base.goingDownIndicator(true); | |
base.goingUpIndicator(false); | |
that.direction = 'down'; | |
base.checkDestinationQueue(); | |
} | |
} | |
if(floor === 0 && that.direction === 'down') { | |
var qTarget = q.getHighestWait(); | |
if(qTarget.floor > 0) { | |
qTarget.mark = that; | |
that.startUpTour(qTarget.floor); | |
} else { | |
base.goingUpIndicator(true); | |
base.goingDownIndicator(true); | |
that.direction ='idle'; | |
} | |
} | |
}); | |
base.on("floor_button_pressed", function() { | |
if(that.direction === 'idle') { | |
that.startUpTour(); | |
} | |
}); | |
} | |
/** | |
* Start a tour up. | |
* Check | |
*/ | |
MyElevator.prototype.startUpTour = function(floor) { | |
if(typeof floor === 'undefined') { | |
floor = MAX_FLOOR; | |
} | |
this.direction = 'up'; | |
this.base.goingUpIndicator(true); | |
this.base.goingDownIndicator(false); | |
this.base.goToFloor(floor); | |
}; | |
MyElevator.prototype.startDownTour = function() { | |
this.direction = 'down'; | |
this.base.goingUpIndicator(false); | |
this.base.goingDownIndicator(true); | |
this.base.goToFloor(0); | |
}; | |
MyElevator.prototype.nudge = function(qTarget) { | |
if(this.direction === 'idle') { | |
qTarget.worker = this; | |
this.startUpTour(qTarget.marked); | |
} | |
}; | |
MyElevator.prototype.reachOnTour = function(qTarget) { | |
switch(this.direction) { | |
case 'up': | |
return this.position < qTarget.floor && qTarget.direction === 'up'; | |
case 'down': | |
return this.position > qTarget.floor && qTarget.direction === 'down'; | |
default: | |
return false; | |
} | |
}; | |
/** +++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
* Init Code | |
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
*/ | |
var q = new InQueue(); | |
elevators.forEach(function(elev, i) { | |
var elevObj = new MyElevator(elev, q); | |
q.addElevator(elevObj); | |
}); | |
floors.forEach(function(floor) { | |
floor.on('up_button_pressed', function() { | |
q.add(floor.floorNum(), 'up'); | |
}); | |
floor.on('down_button_pressed', function() { | |
q.add(floor.floorNum(), 'down'); | |
}); | |
if(floor.floorNum() > MAX_FLOOR) { | |
MAX_FLOOR = floor.floorNum(); | |
} | |
}); | |
}, | |
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