Repository navigation
Expand file tree
/
Copy pathInputHandler.js
More file actions
107 lines (82 loc) · 2.59 KB
/
Copy pathInputHandler.js
File metadata and controls
107 lines (82 loc) · 2.59 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
import { CONSTANTS } from '../../Utility/config.js';
import { Strategy } from '../Strategy/Strategy.js';
class InputHandler {
static inputArray = [];
/**
* @retuns {Integer[]}
*/
static get getGeneratedArray() {
return InputHandler.inputArray;
}
/**
*
* @param {Integer[]} newArray
*/
static set setInputArray(newArray) {
InputHandler.inputArray = newArray;
}
/**
*
* @returns {number}
*/
static getAllowedMaxInputSize() {
const MAX_BAR_HEIGHT = window.innerHeight - CONSTANTS.OFFSET;
const MARGIN = CONSTANTS.margin;
const BAR_MIN_WIDTH = 1;
const SCREEN_WIDTH = window.innerWidth;
const potentialNum = Math.floor(SCREEN_WIDTH / (BAR_MIN_WIDTH + MARGIN));
return potentialNum <= MAX_BAR_HEIGHT ? potentialNum : MAX_BAR_HEIGHT;
}
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
static shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex !== 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
/**
*
* @param inputSize
*/
// SG 07/11/2022 12:01 generates random numbers, not consecutive
static generateRandomIntArray(inputSize) {
const MAX_BAR_HEIGHT = window.innerHeight - CONSTANTS.OFFSET;
let arr = Array.from(new Array(MAX_BAR_HEIGHT), (x, i) => i + 5);
arr = InputHandler.shuffle(arr);
arr = arr.slice(0, inputSize);
InputHandler.setInputArray = arr;
Strategy.setTargetArray = arr;
}
// // SG 07/11/2022 12:01 generates random numbers, all consecutive
// static generateOptimizedArray(inputSize) {
// const MAX_BAR_HEIGHT = window.innerHeight - CONSTANTS.OFFSET;
// let div = Math.floor(window.innerHeight / window.innerWidth)
// let arr = [];
// for(let i = 1; i < MAX_BAR_HEIGHT; i++) {
// if(i % div === 0) {
// arr.push(i);
// }
// }
// arr = InputHandler.shuffle(arr);
// InputHandler.setInputArray = arr;
// console.log(InputHandler.getGeneratedArray)
// Strategy.setTargetArray = arr;
// }
/**
*
* @param inputSize
* @returns {[]}
*/
static handleInputRequest(inputSize) {
InputHandler.generateRandomIntArray(inputSize);
return InputHandler.getGeneratedArray;
}
}
export {InputHandler};