-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
174 lines (148 loc) · 5.2 KB
/
script.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
const start = document.getElementById("start-button");
start.addEventListener("click", function () {
const floors = parseInt(document.querySelector(".floor-input").value);
const lifts = parseInt(document.querySelector(".lift-input").value);
const container = document.querySelector(".simulator-Container");
const headContainer = document.querySelector(".flexbox-container");
if (isNaN(floors) || isNaN(lifts) || floors < 0 || lifts < 0) {
alert("Please enter only valid positive numbers for floors and lifts.");
return;
}
if (floors === 0) {
alert("For floor 0, no lift is required.");
return;
}
if (lifts == 0){
alert("At least one lift is required.");
return;
}
headContainer.style.display = "none";
container.style.display = "block";
container.innerHTML = "";
initializeBuilding(floors, lifts, container);
});
let liftsPositionArray = [];
let liftsAvailableArray = [];
let requestedLiftArray = {
up: [],
down: [],
};
let floorServiced = {
up: [],
down: [],
};
function initializeBuilding(floors, lifts, container) {
const windowWidth = window.innerWidth;
const containerWidth = 120 * lifts;
for (let i = floors; i >= 0; i--) {
const floorDiv = document.createElement("div");
floorDiv.classList.add("floor");
floorDiv.dataset.floor = i;
floorDiv.style.width = windowWidth > containerWidth ? `${windowWidth - 10}px` : `${containerWidth * 1.2}px`;
const floorControls = document.createElement("div");
floorControls.classList.add("controls-container");
floorControls.innerHTML = `
<span>Floor ${i}</span>
${i < floors ? `<button class="upBtn" data-floor="${i}">↑</button>` : ""}
${i > 0 ? `<button class="downBtn" data-floor="${i}">↓</button>` : ""}
`;
floorDiv.appendChild(floorControls);
if (i === 0) {
const liftsContainer = document.createElement("div");
liftsContainer.classList.add("lifts-container");
for (let j = 0; j < lifts; j++) {
const liftDiv = document.createElement("div");
liftDiv.classList.add("lift");
liftDiv.dataset.lift = j + 1;
liftDiv.dataset.currentFloor = 0;
liftDiv.innerHTML = `
<div class="lift-door left-door"></div>
<div class="lift-door right-door"></div>
`;
liftsContainer.appendChild(liftDiv);
liftsPositionArray.push(0);
liftsAvailableArray.push(true);
}
floorDiv.appendChild(liftsContainer);
}
container.appendChild(floorDiv);
}
for (let i = 0; i <= floors; i++) {
floorServiced.up[i] = false;
floorServiced.down[i] = false;
}
document.querySelectorAll(".upBtn").forEach((button) => {
button.addEventListener("click", function () {
askForLift(parseInt(this.dataset.floor), "up");
});
});
document.querySelectorAll(".downBtn").forEach((button) => {
button.addEventListener("click", function () {
askForLift(parseInt(this.dataset.floor), "down");
});
});
}
function askForLift(requestedFloor, direction) {
if (!floorServiced[direction][requestedFloor]) {
requestedLiftArray[direction].push(requestedFloor);
getaLift(direction);
floorServiced[direction][requestedFloor] = true;
}
}
function getaLift(direction) {
while (requestedLiftArray[direction].length > 0) {
const requestedFloor = requestedLiftArray[direction][0];
const liftIndex = findNearestAvailableLift(requestedFloor);
if (liftIndex !== -1) {
moveLift(liftIndex, requestedFloor, direction);
requestedLiftArray[direction].shift();
} else {
break;
}
}
}
function findNearestAvailableLift(requestedFloor) {
let nearestLiftIndex = -1;
let minDistance = Infinity;
for (let index = 0; index < liftsPositionArray.length; index++) {
const currentFloor = liftsPositionArray[index];
if (liftsAvailableArray[index]) {
const distance = Math.abs(currentFloor - requestedFloor);
if (distance < minDistance) {
minDistance = distance;
nearestLiftIndex = index;
}
}
}
return nearestLiftIndex;
}
function moveLift(from, to, direction) {
const liftElement = document.querySelector(`.lift[data-lift="${from + 1}"]`);
const currentFloor = liftsPositionArray[from];
const floorHeight = document.querySelector(".floor").offsetHeight;
const distance = Math.abs(currentFloor - to);
const travelTime = distance * 2;
liftsAvailableArray[from] = false;
liftElement.style.transition = `transform ${travelTime}s linear`;
liftElement.style.transform = `translate3d(0, -${floorHeight * to}px, 0)`;
setTimeout(() => {
liftsPositionArray[from] = to;
openLiftDoors(liftElement);
setTimeout(() => {
closeLiftDoors(liftElement);
setTimeout(() => {
liftsAvailableArray[from] = true;
floorServiced[direction][to] = false;
getaLift(direction);
}, 2500);
}, 2500);
}, travelTime * 1000);
}
function openLiftDoors(liftElement) {
liftElement.querySelector(".left-door").classList.add("open");
liftElement.querySelector(".right-door").classList.add("open");
}
function closeLiftDoors(liftElement) {
liftElement.querySelector(".left-door").classList.remove("open");
liftElement.querySelector(".right-door").classList.remove("open");
}