-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.js
172 lines (144 loc) · 6.45 KB
/
elevator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{
init: function(elevators, floors) {
var elevatorCount = elevators.length;
var floorCount = floors.length;
var downQueue = [];
var upQueue = [];
var idleElevatorCount = []
// function to go a requested floor
var goToFloorRequested = function(floorNum, elevator) {
// set indicators based on where the elevator is relative to where its going
if (elevator.currentFloor() > floorNum) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
} else {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
};
elevator.goToFloor(floorNum);
};
// function to get an elevator to use
var callElevatorToFloor = function(queue, floorNum) {
// if there is an idle elevator, use it for the request
if (idleElevatorCount.length > 0) {
elevator = idleElevatorCount.shift();
goToFloorRequested(floorNum, elevator);
} else {
// otherwise push that floorNum to the corresponding queue and wait for an elevator going that direction
if (queue.indexOf(floorNum) < 0) {
queue.push(floorNum);
}
queue = downQueue.sort();
};
};
// for every single elevator:
for (var i = 0; i < elevatorCount; i++) {
elevators[i].on("idle", function() {
var elevator = this;
if (upQueue.length + downQueue.length === 0) {
if (elevator.goingUpIndicator()) {
// if we were going up, go to the highest floor with a person waiting
if (downQueue.length > 0) {
floorNum = downQueue.pop();
} else {
floorNum = upQueue.pop();
};
} else {
// shift takes the first index from an array
// pop takes the last index from an array
// otherwise, go to the lowest floor with a person waiting
if (upQueue.length > 0) {
floorNum = upQueue.shift();
} else {
floorNum = downQueue.shift();
};
};
} else {
// if there's nobody waiting for an elevator, set the elevator to idle
idleElevatorCount.push(elevator);
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(false);
};
});
// when someone goes into an elevator and presses a floor to go to, send the right elevator to that floor
elevators[i].on("floor_button_pressed", function(floorNum) {
var elevator = this;
// add the new floor to the queue if it wasn't already requested
if (elevator.destinationQueue.indexOf(floorNum) < 0 ) {
elevator.destinationQueue.push(floorNum);
// sort to make sure we visit floors in order
elevator.destinationQueue = elevator.destinationQueue.sort();
if (elevator.goingDownIndicator()) {
elevator.destinationQueue = elevator.destinationQueue.reverse();
};
// apply the changes
elevator.checkDestinationQueue();
};
});
elevators[i].on("stopped_at_floor", function(floorNum) {
var elevator = this;
// elevator.goingDownIndicator(true);
// elevator.goingUpIndicator(true);
if (floorNum == 0) {
// start moving up once we reach the bottom
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(true);
} else if (floorNum == floorCount) {
// start moving down once we reach the top
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
} else if (elevator.destinationQueue.length == 0) {
// if this was our final stop, pick up anybody here
// if the elevator ends up being idle, then these will both get set to false
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
};
// remove this floor from the corresponding queue if we picked someone up
if (elevator.goingUpIndicator()) {
index = upQueue.indexOf(floorNum);
if (index >= 0) {
upQueue.splice(index, 1);
};
} else if (elevator.goingDownIndicator()) {
index = downQueue.indexOf(floorNum);
if (index >= 0) {
downQueue.splice(index, 1);
};
};
});
elevators[i].on("passing_floor", function(floorNum, direction) {
var elevator = this;
if (direction == "up") {
queue = upQueue;
} else {
queue = downQueue;
};
// check if they're going to a floor already in the queue
index = queue.indexOf(floorNum);
if (index >= 0 && elevator.loadFactor() < 0.8) {
// if yes, and we have room, stop for them
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
elevator.goToFloor(floorNum, true);
};
});
};
// for every single floor
for (var j = 0; j < floors.length; j++) {
// floor object properties
//floor.floorNum(); // gets the floor number of the floor object
// when someone presses the up or down button at a floor, call an elevator to that floor
floors[j].on("up_button_pressed", function() {
var floor = this;
callElevatorToFloor(upQueue, floor.floorNum());
});
floors[j].on("down_button_pressed", function() {
var floor = this;
callElevatorToFloor(downQueue, floor.floorNum());
});
};
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}