-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
74 lines (64 loc) · 2.09 KB
/
solution.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
{
init: function(elevators, floors) {
var maxFloor = floors.length - 1;
var num = 0;
var q = [];
function sortQueue(elevator) {
elevator.destinationQueue.sort(function(a, b){return a-b});
if (elevator.currentFloor() > elevator.destinationQueue[0]) {
elevator.destinationQueue.sort(function(a, b){return b-a});
}
}
function assignFloor(elevator, floorNum, force) {
if (elevator.destinationQueue.indexOf(floorNum) < 0) {
if (force) {
elevator.destinationQueue.unshift(floorNum);
} else {
elevator.destinationQueue.push(floorNum);
sortQueue(elevator);
}
}
elevator.checkDestinationQueue();
}
function buttonPressed(floorNum, direction) {
if (!q.some(function(f) {return f === floorNum})) {
q.push(floorNum);
}
}
_.each(floors, function(floor) {
floor.on("up_button_pressed down_button_pressed", function(event) {
var direction = event.indexOf('up') == 0 ? 'up' : 'down';
buttonPressed(floor.level, direction);
});
});
_.each(elevators, function(elevator) {
elevator.num = num++;
elevator.on("floor_button_pressed", function(floorNum) {
assignFloor(elevator, floorNum);
});
elevator.on("passing_floor", function(floorNum, direction) {
console.log('passing_floor ' + floorNum + ' by ' + elevator.num + ' going ' + direction);
if (elevator.loadFactor() <= 0.6) {
if ((floors[floorNum].buttonStates.down && direction == "down") || (floors[floorNum].buttonStates.up && direction == "up")) {
assignFloor(elevator, floorNum, true);
}
}
});
elevator.on("stopped_at_floor", function(floorNum) {
var s = q.indexOf(floorNum);
if (s >= 0) {
q.splice(s, 1);
}
});
elevator.on("idle", function() {
if (q.length) {
assignFloor(elevator, q.shift());
} else {
assignFloor(elevator, 0);
}
});
});
},
update: function(dt, elevators, floors) {
}
}