-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
137 lines (126 loc) · 5.06 KB
/
main.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
var itemcounter = null;
const connect_file_img = (imageid, fileinputid) => {
let imgElement = document.getElementById(imageid);
let inputElement = document.getElementById(fileinputid);
inputElement.addEventListener('change', (e) => {
imgElement.src = URL.createObjectURL(e.target.files[0]);
document.getElementById('imageSrc').removeAttribute("hidden");
}, false);
}
const printCSV = async (findings) => {
// TODO this order is not strong enough and prone to reodering by the interpreter
let sortedItems = getItems().sort((a, b) => {
if (typeof a.supplyPyramid === 'undefined') {
return 1;
}
if (typeof b.supplyPyramid === 'undefined') {
return -1;
}
return a.supplyPyramid.priority - b.supplyPyramid.priority;
});
let names = "";
let crates = "";
let pyramid = "";
let pyramidPrio = "";
let limit = "";
for (const item of sortedItems) {
let found = findings.items.find((finding) => { return item.itemName === finding.name; });
if (typeof found === 'undefined') {
continue;
}
names += "" + found.name + "\n";
crates += "" + found.count + "\n";
if (typeof item.supplyPyramid === 'undefined') {
pyramid += "none\n";
pyramidPrio += "none\n";
} else {
pyramid += "" + item.supplyPyramid.cratesIdeal + "\n";
pyramidPrio += "" + item.supplyPyramid.priority + "\n";
}
if (typeof item.stockpileLimitPrivate === 'undefined') {
limit += "none\n";
} else {
limit += "" + item.stockpileLimitPrivate + "\n";
}
}
document.getElementById('preformattedNames').textContent = names;
document.getElementById('preformattedCrates').textContent = crates;
document.getElementById('preformattedPyramid').textContent = pyramid;
document.getElementById('preformattedPyramidPriority').textContent = pyramidPrio;
document.getElementById('preformattedLimit').textContent = limit;
}
const removeAllChildNodes = (parent) => {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
const getFilter = async () => {
return {
'colonial': document.getElementById('colonialButton').checked,
'warden': document.getElementById('wardenButton').checked,
'shippables': true
};
}
const run = async () => {
console.log("run");
removeAllChildNodes(document.getElementById('itemlist'));
removeAllChildNodes(document.getElementById('preformattedNames'));
removeAllChildNodes(document.getElementById('preformattedCrates'));
removeAllChildNodes(document.getElementById('preformattedPyramid'));
removeAllChildNodes(document.getElementById('preformattedPyramidPriority'));
removeAllChildNodes(document.getElementById('preformattedLimit'));
await clearCanvas(document.getElementById('canvasImgmatch'));
let tmpCanvas = document.getElementById('canvasTmp');
let progressCb = (progress) => {
let roundedPercent = Math.ceil(progress.percent * 100);
if(roundedPercent > 0 && roundedPercent < 100) {
document.getElementById('progressBar').parentElement.hidden = false;
document.getElementById('resultCard').hidden = false;
}
document.getElementById('progressBar').setAttribute("style", "width: " + roundedPercent + "%;");
document.getElementById('progressBar').setAttribute("ariaValueNow", roundedPercent);
document.getElementById('progressBar').innerHTML = roundedPercent + "%";
if(roundedPercent === 100) {
document.getElementById('progressBar').parentElement.hidden = true;
document.getElementById('resultTable').hidden = false;
}
};
let currentTemplate = document.getElementById('canvasItem');
let visualizationCanvas = document.getElementById('canvasImgmatch');
let list = document.getElementById("itemlist");
if (itemcounter === null) {
itemcounter = new ItemCounter(tmpCanvas, progressCb, window.alert, "iconpacks", currentTemplate, visualizationCanvas, list);
await itemcounter.init();
}
itemcounter.setFilter(await getFilter());
let iconpack = document.getElementById("iconpack-select").selectedOptions[0].value;
itemcounter.setIconpack(iconpack);
let fileselector = document.getElementById('fileInputSrc');
let screenshotUrl = URL.createObjectURL(fileselector.files[0]);
let screenshot = await loadImage(screenshotUrl);
let findings = await itemcounter.count(screenshot); // takes long
if (findings === null) {
window.alert('No stockpile found on screenshot.');
return;
}
if (findings.stockpileType === null) {
window.alert('Stockpile type unknown. Assuming a Seaport or Storage Depot...');
}
if (!findings.stockpileType.crateBased) {
window.alert('Stockpile is not crate based. Some Table columns are wrong.');
}
await printCSV(findings);
}
const abort = () => {
itemcounter.abort = true;
}
const loaded = async () => {
connect_file_img('imageSrc', 'fileInputSrc');
for (let pack of known_iconpacks) {
let option = document.createElement('option');
option.setAttribute('value', pack.name);
let label = document.createTextNode(pack.label);
option.appendChild(label);
document.getElementById('iconpack-select').appendChild(option);
}
}