Skip to content

Two sorted lists for up and down

joseljmz edited this page Jan 23, 2015 · 1 revision

I am using two arrays to store the up and down button pressed events.

{
    init: function(elevators, floors) {
        var between = function(i, a, b) {
            return (a < i && i < b) || (a > i && i > b);
        };
        var ups = [];
        var downs = [];
        for (var j in elevators) {
            var elevator = elevators[j];
            elevator.on("idle", function() {
                this.goingUpIndicator(true);
                this.goingDownIndicator(true);
                if (ups.length)
                    this.goToFloor(ups.shift());
                else if (downs.length)
                    this.goToFloor(downs.pop());
                else
                    this.goToFloor(0);
            });
            elevator.on("floor_button_pressed", function(floorNum) {
                if (this.destinationQueue.length === 0) {
                    this.goToFloor(floorNum);
                } else if (this.destinationQueue.indexOf(floorNum) < 0) {
                    for (var i = 0; i < this.destinationQueue.length; i++) {
                        if (between(floorNum, this.currentFloor(), this.destinationQueue[i])) {
                            this.destinationQueue.splice(i, 0, floorNum);
                            this.checkDestinationQueue();
                            return;
                        }
                    }
                    this.goToFloor(floorNum);
                }
            });
            elevator.on("stopped_at_floor", function(floorNum) {
                if (this.destinationQueue.length === 0) {
                    this.goingUpIndicator(true);
                    this.goingDownIndicator(true);
                } else {
                    var destinationFloor = this.destinationQueue.slice(0, 1);
                    var currentFloor = this.currentFloor();
                    if (destinationFloor > currentFloor) {
                        // going up
                        this.goingUpIndicator(true);
                        this.goingDownIndicator(false);
                    } else {
                        // going down
                        this.goingUpIndicator(false);
                        this.goingDownIndicator(true);
                    }
                }
            });
        }
        for (var j in floors) {
            var floor = floors[j];
            floor.on("up_button_pressed", function() {
                if (ups.indexOf(this.floorNum()) < 0) {
                    ups.push(this.floorNum());
                    ups.sort();
                }
            });
            floor.on("down_button_pressed", function() {
                if (downs.indexOf(this.floorNum()) < 0) {
                    downs.push(this.floorNum());
                    downs.sort();
                }
            });
        }
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
        //console.log('update', arguments);
    }
}
Clone this wiki locally