-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacrodata.js
More file actions
82 lines (75 loc) · 1.83 KB
/
macrodata.js
File metadata and controls
82 lines (75 loc) · 1.83 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
// TODO: add more file names
const files = [
'Siena',
'Nanning',
'Narva',
'Ocula',
'Kingsport',
'Labrador',
'Le Mars',
'Longbranch',
'Moonbeam',
'Minsk',
'Dranesville',
];
const emptyBins = [
{ WO: 0,FC: 0,DR: 0,MA: 0 },
{ WO: 0,FC: 0,DR: 0,MA: 0 },
{ WO: 0,FC: 0,DR: 0,MA: 0 },
{ WO: 0,FC: 0,DR: 0,MA: 0 },
{ WO: 0,FC: 0,DR: 0,MA: 0 }
];
class MacrodataFile
{
constructor()
{
this.localStorageKey = 'macrodata';
const file = JSON.parse(localStorage.getItem(this.localStorageKey)) ?? this.assignFile();
this.fileName = file.fileName;
this.storedBins = file.storedBins;
this.coordinates = file.coordinates;
}
assignFile()
{
// quick fix to ensure you don't get the same filename twice in a row
const allFilesButPrevious = files.filter(file => file !== this.fileName);
const fileName = allFilesButPrevious[Math.floor(Math.random() * allFilesButPrevious.length)];
const coordinates = this.#generateCoordinates();
console.log('assigning',fileName);
const macrodata = {
fileName,
storedBins: emptyBins,
coordinates
}
localStorage.setItem(this.localStorageKey,JSON.stringify(macrodata));
return macrodata;
}
updateProgress(bins)
{
const updatedFile = {
fileName: this.fileName,
storedBins: bins,
coordinates: this.coordinates
}
localStorage.setItem(this.localStorageKey,JSON.stringify(updatedFile));
}
resetFile()
{
localStorage.removeItem(this.localStorageKey);
const file = this.assignFile();
this.fileName = file.fileName;
this.storedBins = file.storedBins;
this.coordinates = file.coordinates;
}
// private member fn to pick coordinates
#generateCoordinates()
{
function randHex()
{
return floor(random(0,256)).toString(16).toUpperCase();
}
let x = randHex() + randHex() + randHex();
let y = randHex() + randHex() + randHex();
return `0x${x} : 0x${y}`;
}
}