-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-solution.js.js
76 lines (56 loc) · 2.41 KB
/
my-solution.js.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
{
init: function(elevators, floors) {
//NEXT IMPROVEMENT!
//PROBLEM: elevator goes to levels in sort of pushed buttons, which isn't verry efficient
//SOLUTION: clear goto queue and build new queue after a passenger has pushed a level button inside the elevator
var getElevatorWithSmallestLoad = function(){
var smallestLoad = {
elevatorId: 0,
load: 1
};
for(var i = 0; i < elevators.length; i++){
var currentElevator = elevators[i];
var currentElevatorLoadFactor = currentElevator.loadFactor();
if(currentElevatorLoadFactor < smallestLoad.load){
smallestLoad.elevatorId = i;
smallestLoad.load = currentElevatorLoadFactor;
}
}
return smallestLoad.elevatorId;
};
// Whenever the elevator is idle (has no more queued destinations) ...
var elevatorRegisterIdle = function(elevator){
elevator.on("idle", function() {
elevator.goToFloor(Math.floor(floors.length/2));
});
};
var elevatorRegisterButtons = function(elevator){
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
} );
};
var floorRegisterButtons = function(floor){
floor.on("up_button_pressed down_button_pressed", function(){
//find elevator with smallest load
var elevatorToSendId = getElevatorWithSmallestLoad();
var elevatorToSend = elevators[elevatorToSendId];
//send elevator to requested floor
elevatorToSend.goToFloor(this.level);
});
};
//AWAKE ELEVATORS
for(var elelvatorCounter = 0; elelvatorCounter < elevators.length ; elelvatorCounter ++){
var currentElevator = elevators[elelvatorCounter];
elevatorRegisterIdle(currentElevator);
elevatorRegisterButtons(currentElevator);
}
//AWAKE FLOOR BUTTONS
for(var i = 0 ; i < floors.length ; i++){
var currentFloor = floors[i];
floorRegisterButtons(currentFloor);
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}