Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (130 loc) · 4.72 KB
/
Copy pathscript.js
File metadata and controls
160 lines (130 loc) · 4.72 KB
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
// User input for no of lifts
const lift_num = document.querySelector('#lifts');
// User input for no of floors
const floor_num = document.querySelector('#floors');
// adding the floors dynamically into the floors_space
const floors_space = document.querySelector('#floors_lifts');
// Button for submitting the user input
const sbmit_lift_floor_num = document.querySelector('#sbmit');
// for storing all the floors reference nodelist
let allFloor;
// for storing all the lifts reference nodelist
let allLifts;
// Keeps track of which lift is in which floor currently
let curLiftPos = [];
// whether the ith lift is busy
let isBusy = [];
let num_floors = 2;
let first_floor;
// Initializing with 2 floors and 1 lifts
refresh(2, 1);
sbmit_lift_floor_num.addEventListener('click', (e) => {
const floorVal = floor_num.value;
const liftVal = lift_num.value;
num_floors = floorVal;
refresh(floorVal, liftVal);
e.preventDefault();
});
// Everytime the function gets called whenever the user enters
// the no of floors and no of lifts
function refresh(floorVal, liftVal) {
// Emptying the previous no of floors to add
// the user entered no of floors
floors_space.innerHTML = "";
// Adding floors dynamically after user enters
for(let i = floorVal; i > 0; i--) {
let floorNo = 'Floor '+i;
let newFloor = document.createElement('div');
newFloor.classList.add('floor');
newFloor.innerHTML = `
<button id="up" class="button">UP</button>
<button id="dn" class="button">DN</button>
<p>${floorNo}</p>
`;
floors_space.appendChild(newFloor);
first_floor = newFloor;
}
// Every time the no of floors changes it gets chnaged
// So updating the no of buttons
allFloorButtons = document.querySelectorAll('.floor .button');
const floor_height = document.querySelector('.floor').offsetHeight;
// It adds the no of lifts entered by the user
// in the floors_space
for(let i = 1; i <= liftVal; ++i) {
const newLift = document.createElement('div');
newLift.classList.add('lifts');
newLift.innerHTML = `
<div class="gate" id="left_gate"></div>
<div class="gate" id="right_gate"></div>
`;
first_floor.appendChild(newLift);
newLift.style.left = i*15+"%";
// newLift.style.top = ((floorVal - 1)*floor_height)+'px';
// It tells ith lift is present in which floor currently
curLiftPos[i - 1] = ((floorVal - 1)*floor_height);
// whether this lift is busy or free
// intially every lift is free
isBusy[i - 1] = false;
}
// Everytime the no of lifts gets changed
// It is updated in the allLifts var
allLifts = document.querySelectorAll('.lifts');
allFloorButtons.forEach(btn => {
btn.addEventListener('click', () => {
var intc = setInterval(() => {
let freeLift = findFreeLift(btn);
if(freeLift !== -1) {
clearInterval(intc);
moveLift(btn, freeLift);
}
}, 5);
});
});
}
// returns which lift is free
function findFreeLift(btn) {
let prevDiff = Number.MAX_SAFE_INTEGER;
let free = -1;
const parBtnPos = btn.parentElement.offsetTop;
for(let i = 0;i<isBusy.length;++i) {
if(!isBusy[i]) {
if(Math.abs(parBtnPos - curLiftPos[i]) < prevDiff) {
free = i;
prevDiff = Math.abs(parBtnPos - curLiftPos[i]);
}
}
}
if(free !== -1) {
// Making the lift busy
isBusy[free] = true;
return free;
}
return -1;
}
function moveLift(btn, freeLift) {
// previous position
let prevPos = curLiftPos[freeLift];
let floor_height = btn.parentElement.offsetHeight;
let to = btn.parentElement.offsetTop;
curLiftPos[freeLift] = to;
let time = Math.abs(preCurGap(btn, prevPos));
// taking the lift to the specified floor
allLifts[freeLift].style.transitionDuration = ` ${time}s`;
allLifts[freeLift].classList.toggle('running');
allLifts[freeLift].style.transform = `translateY(${-(((num_floors - 1)
- to / floor_height) * 100)}%)`;
setTimeout(() => {
// Lift stopped
allLifts[freeLift].classList.toggle('running');
// scheduling the lift for gate open and close
allLifts[freeLift].classList.toggle('stopped');
setTimeout(() => {
allLifts[freeLift].classList.toggle('stopped');
isBusy[freeLift] = false;
}, 5000);
}, time*1000);
}
function preCurGap(btn, pre) {
let gap = (btn.parentElement.offsetTop - pre) / btn.parentElement.offsetHeight;
return gap;
}