-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevator.js
330 lines (259 loc) · 10.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{
init: function(elevators, floors) {
//// Configuration
// Load Factor Config, above this the elevator goes directly
var maxLoad = 0.5;
var maxLargeLoad = 0.5;
// Turn on Elevator lights
var directionLights = true;
// Enable or disable console logging of elevator and queue activity
var logging = true;
//// Defaults
var floorCount = floors.length - 1;
// Queues for the up and down passengers.
var upQueue = [];
var downQueue = [];
// Combined queue of up and down passengers
var requestedQueue = [];
///////////////////////////////////////////////////////////
// Power on the elevators
function powerOnElevator(elevator, index) {
// Set the elevator to be not full by default.
var elevatorFull = false;
// Create a queue to register floor presses
var floorButtonsQueue = [];
// Set the next floor that the elevator should go to.
function nextFloor() {
// Update the unified up down queue
updatePickupQueue();
if (elevator.travelDirection == 'up' && floorButtonsQueue.length > 0) {
// Find people on other floors in the direction we are going.
pickupHitchkikers();
// Make sure the Queue is properly sorted.
sortQueue();
// Clean the current floor from the queue.
cleanGlobalQueues();
if (floorButtonsQueue.length > 0) {
elevator.goToFloor(floorButtonsQueue[0], checkLoad());
} else if (requestedQueue.length > 0) {
elevator.goToFloor(requestedQueue[0], checkLoad());
}
} else if (elevator.travelDirection == 'down' && floorButtonsQueue.length > 0) {
// Find people on other floors in the direction we are going.
pickupHitchkikers();
// Make sure the Queue is properly sorted.
sortQueue();
// Clean the current floor from the queue.
cleanGlobalQueues();
if (floorButtonsQueue.length > 0) {
elevator.goToFloor(floorButtonsQueue[0], checkLoad());
} else if (requestedQueue.length > 0) {
elevator.goToFloor(requestedQueue[0], checkLoad());
}
} else if (downQueue.length > 0) {
elevator.goToFloor(downQueue[0], checkLoad());
} else if (upQueue.length > 0) {
elevator.goToFloor(upQueue[0], checkLoad());
} else if (requestedQueue.length > 0) {
elevator.goToFloor(requestedQueue[0], checkLoad());
} else {
elevator.goToFloor(0);
}
}
// Sort the floor buttons queue
function sortQueue() {
if (elevator.travelDirection == "up") {
floorButtonsQueue = floorButtonsQueue.sort(function (a, b) { return a - b });
} else {
floorButtonsQueue = floorButtonsQueue.sort(function (a, b) { return b - a });
}
}
// Basic logging of individual elevator activity.
function logStatus() {
if (logging) {
console.log("Elevator " + index + " | Queue: " + floorButtonsQueue + " | Current Floor: " + elevator.currentFloor() + " | Direction: " + elevator.travelDirection + " | Current Load: " + elevator.loadFactor() + " | Capacity: " + elevator.maxPassengerCount());
}
}
// Check Load - See if both large and small elevators are full
function checkLoad() {
if (elevator.loadFactor() < maxLoad && elevator.maxPassengerCount() <= 5) {
var elevatorFull = false;
} else if (elevator.loadFactor() < maxLargeLoad && elevator.maxPassengerCount() > 5) {
var elevatorFull = false;
} else if (elevator.loadFactor() > maxLoad && elevator.maxPassengerCount() <= 5) {
var elevatorFull = true;
} else if (elevator.loadFactor() > maxLargeLoad && elevator.maxPassengerCount() >= 5) {
var elevatorFull = true;
} else {
var elevatorFull = false;
}
return elevatorFull;
};
// Find the next passengers to pick up, if idle
function findNextPickupArray() {
requestedQueue = _.union(upQueue, downQueue);
return (requestedQueue[0]);
}
// Find stops on the way and add them to the queue, if not full
function pickupHitchkikers() {
if (checkLoad() == false) {
if (elevator.travelDirection == 'up' && upQueue.length > 0) {
var floorsAbove = _.filter(upQueue, function (n) { return n > elevator.currentFloor() });
floorButtonsQueue = _.union(floorsAbove, floorButtonsQueue);
sortQueue();
upQueue = _.difference(floorButtonsQueue, upQueue);
}
else if (elevator.travelDirection == 'down' && downQueue.length > 0) {
var floorsBelow = _.filter(downQueue, function (n) { return n < elevator.currentFloor() });
floorButtonsQueue = _.union(floorsBelow, floorButtonsQueue);
sortQueue();
downQueue = _.difference(floorButtonsQueue, downQueue);
}
}
}
// Max and Min Floors
function getMaxFloor(numArray) {
if (numArray.length > 0) {
return _.max(numArray);
} else {
return floorCount;
}
}
function getMinFloor(numArray) {
if (numArray.length > 0) {
return _.min(numArray);
} else {
return 0;
}
}
// This sets the travel direction: up or down
function setTravelDirection() {
if (elevator.currentFloor() <= getMinFloor(upQueue)) {
elevator.travelDirection = "up";
// Set up the lights
if (directionLights) {
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(true);
}
}
else if (elevator.currentFloor() >= getMaxFloor(downQueue)) {
elevator.travelDirection = "down";
if (directionLights) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
}
}
else {
elevator.travelDirection = "down";
if (directionLights) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
}
}
};
// Clear the Queue when the elevator is at the requested floor
function cleanGlobalQueues() {
if (floorButtonsQueue.length > 0) {
if (elevator.currentFloor() == floorButtonsQueue[0]) {
floorButtonsQueue.shift();
return floorButtonsQueue;
} else {
return floorButtonsQueue;
}
}
// Clear the current floor from the downQueue, if elevator is at it
if (elevator.currentFloor() == downQueue[0]) {
downQueue.shift();
}
// Remove the current floor from the UpQueue, if elevator is at it
if (elevator.currentFloor() == upQueue[0]) {
upQueue.shift();
}
}
// Update the elevator queue stats area.
function updateStats() {
$(".elevator-" + index + " .value").text(floorButtonsQueue);
}
///////////////////////////////////////////////////////////
// Whenever the elevator is idle (has no more queued destinations) ...
elevator.on("idle", function () {
// Display logging if enabled
logStatus();
// Find people on other floors in the direction we are going
pickupHitchkikers();
// Set the direction of travel
setTravelDirection();
// Set the next floor
nextFloor();
});
///////////////////////////////////////////////////////////
// What to do When the elevator is stopped at a floor
elevator.on("stopped_at_floor", function (floorNum) {
// Display logging if enabled
logStatus();
// Display the queue in the stats area
updateStats();
// Update the unified up down queue
updatePickupQueue();
// Check the load of the elevators
checkLoad();
// Cleanup the global queues
cleanGlobalQueues();
// Sort the elevator queue
sortQueue()
// Set the direction of travel
setTravelDirection();
});
// What to do When the elevator is moving
elevator.on("passing_floor", function (floorNum, direction) {
// Find people on other floors in the direction we are going
pickupHitchkikers();
});
// When the button is pressed in the elevator
elevator.on("floor_button_pressed", function (floorNum) {
floorButtonsQueue.push(floorNum);
sortQueue();
});
} // End Main Elevator
// Elevator Dispatcher -
function requestDispatcher(direction){
// Coming soon
}
// Update the next passengers to pick up queue
function updatePickupQueue() {
requestedQueue = _.union(upQueue, downQueue);
if (logging) {
console.log("Requested: " + requestedQueue + " | Up: " + upQueue + " | Down: " + downQueue);
}
}
// Find who is waiting on each floor for the elevator
function floorPanels(floor) {
floor.on("up_button_pressed", function () {
upQueue.push(floor.level);
updatePickupQueue();
});
floor.on("down_button_pressed", function () {
downQueue.push(floor.level);
updatePickupQueue();
});
}
// Queue Stats. Append the individual elevator queue to the stats area.
var top = 140;
function setupStats() {
for (var i = 0, len = elevators.length; i < len; i++) {
$(".statscontainer").append('<div style="top:' + top + 'px" class="elevator-' + i + '"><span class="key">Elevator ' + i + ' Queue</span><span class="value">-</span>');
top = top + 20;
}
}
///////////////////////////////////////////////////////////
// --------- Power up the Elevator and Floor Panels -----------------------
// Get all the elevators moving
elevators.forEach(powerOnElevator);
// Turn on the panels at each floor
floors.forEach(floorPanels);
setupStats();
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}