-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
430 lines (392 loc) · 13 KB
/
index.html
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Lift Simulation</title>
<!-- <link href="/src/main.css" rel="stylesheet" type="text/css" /> -->
</head>
<body>
<main>
<h1 class="title">Lift Simulation</h1>
<div id="simulation-container">
<form onsubmit="startSimulation(event)">
<div>
<h3>Total Floors</h3>
<input type="number" id="floor-nums" min="1" class="simulate-inputs" required />
</div>
<div>
<h3>Total Lifts</h3>
<input type="number" id="lift-nums" min="1" class="simulate-inputs" required />
</div>
<button type="submit" class="simulate-button">Start Simulation</button>
</form>
</div>
</main>
<style>
body {
font-family: monospace;
}
.title {
text-align: center;
}
.simulate-button {
padding: 10px 40px;
margin-top: 30px;
cursor: pointer;
}
.simulate-inputs {
padding: 10px 5px;
}
#simulation-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
.floor-container {
display: flex;
flex-direction: row;
align-items: center;
justify-items: flex-start;
gap: 20px;
padding: 10px;
min-height: 130px;
border: 2px solid black;
}
.lift-buttons {
display: flex;
flex-direction: column;
justify-content: center;
gap: 10px;
button {
font-size: 20px;
padding: 8px 10px 10px 10px;
cursor: pointer;
}
}
.floors {
display: flex;
flex-direction: column;
gap: 20px;
}
.lift {
display: flex;
flex-direction: row;
width: 5rem;
height: 6.5rem;
border: 2px solid black;
}
.left-door {
width: 50%;
height: 6.5rem;
border-right: 1px solid black;
background-color: aliceblue;
}
.right-door {
width: 50%;
height: 6.5rem;
border-left: 0.2px solid black;
background-color: aliceblue;
}
.animate-left-door {
animation-name: openCloseLeft;
animation-duration: 5s;
animation-timing-function: ease-in-out;
}
.animate-right-door {
animation-name: openCloseRight;
animation-duration: 5s;
animation-timing-function: ease-in-out;
}
@keyframes openCloseLeft {
0% {
margin-right: 0%;
}
50% {
margin-right: 50%;
}
100% {
margin-right: 0%;
}
}
@keyframes openCloseRight {
0% {
margin-left: 0%;
}
50% {
margin-left: 50%;
}
100% {
margin-left: 0%;
}
}
</style>
<script>
var Lift = /** @class */ (function () {
function Lift(currentFloor,
// direction: 'up' | 'down' | 'idle',
name, cb) {
this.currentFloor = currentFloor;
// this.direction = direction;
this._isRunning = false;
this.name = name;
this.cb = cb;
}
Lift.prototype.requestToMove = function (floor) {
// if (floor > this.currentFloor) {
// this.direction = 'up'
// } else if (floor < this.currentFloor) {
// this.direction = 'down'
// } else {
// this.direction = 'idle'
// }
this._isRunning = true;
this.sendLiftToFloor(floor);
};
Lift.prototype.isRunning = function () {
return this._isRunning;
};
Lift.prototype.getCurrentFloor = function () {
return this.currentFloor;
};
Lift.prototype.sendLiftToFloor = function (floor) {
var _this = this;
var liftEl = document.getElementById(this.name);
var targetFloorEl = document.getElementById("floor-".concat(floor));
if (liftEl && targetFloorEl) {
const floorsToMove = Math.abs(floor - this.currentFloor);
const travelTime = floorsToMove * 2000; // 2 seconds per floor
const handleAnimationEnd_1 = function () {
_this._isRunning = false;
_this.postCompletion(_this.cb);
};
const liftAnimationPromise = liftEl.animate([
{ transform: `translateY(${-(floor - 1) * (targetFloorEl.offsetHeight + 20)}px)` }
], {
duration: travelTime, // Total time based on number of floors
easing: 'linear',
fill: 'forwards'
});
const onFinishAnimation = function () {
_this.currentFloor = floor;
_this.animateLift(handleAnimationEnd_1.bind(_this), liftEl);
};
liftAnimationPromise.onfinish = onFinishAnimation.bind(this);
}
};
Lift.prototype.animateLift = function (animationEndCB, liftEl) {
var leftDoor = liftEl === null || liftEl === void 0 ? void 0 : liftEl.children[0];
var rightDoor = liftEl === null || liftEl === void 0 ? void 0 : liftEl.children[1];
var leftDoorKeyframes = [
{ marginRight: '0%' },
{ marginRight: '50%' },
{ marginRight: '0%' }
];
var rightDoorKeyframes = [
{ marginLeft: '0%' },
{ marginLeft: '50%' },
{ marginLeft: '0%' }
];
var animationPromiseLeft = leftDoor.animate(leftDoorKeyframes, {
duration: 5000,
easing: 'ease-in-out',
fill: 'forwards'
});
rightDoor.animate(rightDoorKeyframes, {
duration: 5000,
easing: 'ease-in-out',
fill: 'forwards'
});
animationPromiseLeft.onfinish = function () {
animationEndCB();
};
// setTimeout(() => {
// animationPromiseLeft.finish();
// animationPromiseRight.finish();
// }, 5000)
};
Lift.prototype.postCompletion = function (cb) {
cb();
};
Lift.prototype.createLift = function () {
var liftName = this.name;
var liftEl = document.createElement('div');
liftEl.setAttribute('class', 'lift');
liftEl.setAttribute('id', liftName);
liftEl.innerHTML = "\n <div class=\"left-door\"></div>\n <div class=\"right-door\"></div>\n ";
return liftEl;
};
return Lift;
}());
var Floor = /** @class */ (function () {
function Floor(isTop, isGround, floorNumber, assignTask) {
this.isTop = isTop;
this.isGround = isGround;
this.floorNumber = floorNumber;
this.assignTask = assignTask;
this.floorEl = this.getFloorElement();
}
Floor.prototype.getFloorElement = function () {
var floorEl = document.createElement("div");
floorEl.setAttribute("class", "floor-container");
floorEl.setAttribute("id", "floor-".concat(this.floorNumber));
var floorTitleEl = document.createElement("div");
floorTitleEl.innerHTML = "<h2>Floor ".concat(this.floorNumber, "</h2>");
floorEl.appendChild(floorTitleEl);
var liftButtonsEl = document.createElement("div");
liftButtonsEl.setAttribute("class", "lift-buttons");
liftButtonsEl.setAttribute("id", "floor-button-".concat(this.floorNumber));
if (!this.isTop) {
var liftCallBtn = this.createButton("up");
liftButtonsEl.appendChild(liftCallBtn);
}
if (!this.isGround) {
var liftCallBtn = this.createButton("down");
liftButtonsEl.appendChild(liftCallBtn);
}
floorEl.appendChild(liftButtonsEl);
return floorEl;
};
Floor.prototype.createButton = function (direction) {
var _this = this;
var button = document.createElement('button');
button.onclick = function () { return _this.assignTask({ floor: _this.floorNumber, direction: direction }); };
button.setAttribute('type', 'button');
button.innerText = direction === 'up' ? '🔼' : '🔽';
return button;
};
Floor.prototype.getFloor = function () {
return { floorEl: this.floorEl, floorNumber: this.floorNumber };
};
return Floor;
}());
var Building = /** @class */ (function () {
function Building(numberOfFloors, numberOfLifts) {
this.floors = [];
this.lifts = [];
this.taskQueue = [];
this.floorRequests = {};
this.init(numberOfFloors, numberOfLifts);
}
Building.prototype.init = function (numberOfFloors, numberOfLifts) {
this.createFloors(numberOfFloors);
this.createLifts(numberOfLifts);
};
Building.prototype.createFloors = function (numberOfFloors) {
for (var i = numberOfFloors; i > 0; i--) {
var isTop = i === numberOfFloors;
var isGround = i === 1;
var floor = new Floor(isTop, isGround, i, this.assignTask.bind(this));
this.floors.push(floor);
}
};
Building.prototype.createLifts = function (numberOfLifts) {
for (var i = 1; i <= numberOfLifts; i++) {
var lift = new Lift(1, "lift-".concat(i), this.processTask.bind(this));
this.lifts.push(lift);
}
};
// public requestLift(floor: number) {
// this.lift.requestToMove(floor);
// this.animateLift();
// }
// private animateLift() {
// const lift = document.querySelector('.lift');
// if (lift) {
// lift.style.transform = `translateY(${(this.lift.getCurrentFloor() - 1) * 100}px)`;
// }
// this.lift.postCompletion(() => {
// this.lift.sendLiftToFloor(this.lift.getCurrentFloor());
// });
// }
Building.prototype.renderBuilding = function () {
var _this = this;
var building = document.createElement('div');
building.classList.add('building');
var floorContainerEl = document.createElement('div');
floorContainerEl.setAttribute("class", "floors");
this.floors.forEach(function (floor, idx) {
var floorEl = floor.getFloor().floorEl;
_this.lifts.forEach(function (lift) {
if (idx === _this.floors.length - 1) {
floorEl.appendChild(lift.createLift());
}
});
floorContainerEl.appendChild(floorEl);
});
building.appendChild(floorContainerEl);
var simulationContainer = document.getElementById("simulation-container");
simulationContainer.style.width = "max-content";
simulationContainer.innerHTML = "";
simulationContainer.appendChild(building);
};
Building.prototype.findNearestLift = function (targetFloor) {
var nearestLiftIndex = -1;
var minDistance = Infinity;
var liftInCurrentFloor = false;
this.lifts.forEach(function (lift, index) {
var distance = Math.abs(lift.getCurrentFloor() - targetFloor);
if (distance === 0) {
liftInCurrentFloor = true;
}
console.log({ distance: distance, lift: lift, isRunning: lift.isRunning() });
if (distance < minDistance && !lift.isRunning()) {
minDistance = distance;
nearestLiftIndex = index;
}
});
if (liftInCurrentFloor && minDistance !== 0)
return -1;
return nearestLiftIndex;
};
Building.prototype.assignTask = function (task) {
if (this.floorRequests[task.floor]) {
console.log('Request already in progress for this floor');
return;
}
const eligibleLifts = this.lifts.filter(lift => !lift.isRunning());
if (eligibleLifts.length === 0) {
this.taskQueue.unshift(task);
console.log('No lift available');
return;
}
const nearestLift = eligibleLifts.reduce((prevLift, currLift) => {
const prevDistance = Math.abs(prevLift.getCurrentFloor() - task.floor);
const currDistance = Math.abs(currLift.getCurrentFloor() - task.floor);
return currDistance < prevDistance ? currLift : prevLift;
});
this.floorRequests[task.floor] = true;
nearestLift.requestToMove(task.floor);
nearestLift.cb = () => {
delete this.floorRequests[task.floor];
this.processTask();
};
};
Building.prototype.processTask = function () {
console.log({ tq: this.taskQueue.length });
var dequedTask = this.taskQueue.shift();
if (dequedTask) {
this.assignTask(dequedTask);
}
else {
console.log('No tasks in the queue');
}
};
return Building;
}());
//ignore typescript for the below function
//@ts-ignore
function startSimulation(event) {
event.preventDefault();
var floorsInput = document.getElementById("floor-nums");
var liftsInput = document.getElementById("lift-nums");
var floors = floorsInput.value;
var lifts = liftsInput.value;
var building = new Building(parseInt(floors), parseInt(lifts));
building.renderBuilding();
}
</script>
</body>
</html>