-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
241 lines (204 loc) · 7.2 KB
/
index.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
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
import State from './state.js';
const ELEMENTS = {
svg: document.getElementById('svg'),
summary: document.getElementById('summary'),
};
const STATE = new State();
const REQUIRED_STATIONS = {};
let HIDEOUT;
loadHideoutTree().then((res) => {
HIDEOUT = res;
// precalculate all required stations for each station, so we dont have
// to do it on every hover/click/etc, i.e. not only the direct
// predecessors but all up to tier 1 stations
for (const stationId in HIDEOUT) {
const reqStations = findRequiredStations(stationId);
REQUIRED_STATIONS[stationId] = reqStations;
}
attatchEventListeners();
if (STATE.highlightedStations.length) {
const stations = STATE.highlightedStations;
highlight(stations);
showSummary(stations);
}
});
async function loadHideoutTree() {
return (await fetch('./hideout.json')).json();
}
function attatchEventListeners() {
ELEMENTS.summary
.getElementsByTagName('h2')[0]
.addEventListener('mouseup', (e) => {
const reqsList =
ELEMENTS.summary.getElementsByClassName('summary-lists')[0];
if (reqsList.style.height === '0px') {
reqsList.style.height = '';
} else {
reqsList.style.height = '0px';
}
});
ELEMENTS.summary
.getElementsByTagName('span')[0]
.addEventListener('mouseup', (e) => {
if (ELEMENTS.summary.style.right === '16px') {
ELEMENTS.summary.style.left = '16px';
ELEMENTS.summary.style.right = 'initial';
} else {
ELEMENTS.summary.style.left = 'initial';
ELEMENTS.summary.style.right = '16px';
}
});
const stations = Array.from(ELEMENTS.svg.getElementsByClassName('station'));
stations.forEach((e) => {
e.addEventListener('mouseenter', onEnterStation);
e.addEventListener('mouseleave', onLeaveStation);
e.addEventListener('mouseup', onClickStation);
});
}
function onEnterStation(event) {
if (!STATE.highlightedStations.length) {
highlight(REQUIRED_STATIONS[event.currentTarget.id]);
}
}
function onLeaveStation() {
if (!STATE.highlightedStations.length) {
removeHighlights();
}
}
function onClickStation(event) {
// only lmb triggers an action
if (event.button) return;
clearSummary();
removeHighlights();
if (STATE.highlightedStations.includes(event.currentTarget.id)) {
// clicking a highlighted station again "unclicks" it, i.e. removes
// the static highlighting and clears the summary
STATE.clearHighlights();
// dispatch mouse enter event to keep the hover highlights; atm identical behavior
// as directly calling highlight(REQUIRED_STATIONS[event.currentTarget.id])
event.currentTarget.dispatchEvent(new Event('mouseenter'));
} else {
if (event.ctrlKey) {
STATE.addHighlights(REQUIRED_STATIONS[event.currentTarget.id]);
} else {
STATE.highlightedStations =
REQUIRED_STATIONS[event.currentTarget.id];
}
const stations = STATE.highlightedStations;
highlight(stations);
showSummary(stations);
}
}
function findRequiredStations(stationId) {
const queue = [stationId];
const requiredStations = [];
while (queue.length > 0) {
const currentStation = queue.pop();
requiredStations.push(currentStation);
for (const prereq of HIDEOUT[currentStation].prerequisites.stations) {
if (!queue.includes(prereq) && !requiredStations.includes(prereq)) {
queue.push(prereq);
}
}
}
return requiredStations;
}
function removeHighlights() {
for (const key in HIDEOUT) {
document.getElementById(HIDEOUT[key].id).classList.remove('fadeout');
for (const reqStation of HIDEOUT[key].prerequisites.stations) {
const edgeId = reqStation + '-' + key;
ELEMENTS.svg.getElementById(edgeId).classList.remove('fadeout');
}
}
}
function highlight(stations) {
for (const key in HIDEOUT) {
if (!stations.includes(key)) {
document.getElementById(HIDEOUT[key].id).classList.add('fadeout');
for (const reqStation of HIDEOUT[key].prerequisites.stations) {
const edgeId = reqStation + '-' + key;
ELEMENTS.svg.getElementById(edgeId).classList.add('fadeout');
}
}
}
}
function clearSummary() {
ELEMENTS.summary.classList.add('hidden');
while (ELEMENTS.summary.childElementCount > 1) {
ELEMENTS.summary.lastElementChild.remove();
}
}
function showSummary(stations) {
// get total requirements
const reqs = totalRequirements(stations);
// div that holds the sub-lists for items, skills and traders
let reqLists = document.createElement('div');
reqLists.classList.add('summary-lists');
// required items list
let list = document.createElement('ul');
list.classList.add('prereq-items');
// sort by total amount required
const itemValues = Object.values(reqs.items).sort((a, b) =>
a.amount < b.amount ? 1 : a.amount == b.amount ? 0 : -1
);
// add items to the list
for (const i of itemValues) {
const node = document.createElement('li');
node.innerText = `${i.amount.toLocaleString('en-US')} ${i.item}`;
list.appendChild(node);
}
reqLists.appendChild(list);
// required skills list
list = document.createElement('ul');
list.classList.add('prereq-skills');
for (const s in reqs.skills) {
const node = document.createElement('li');
node.innerText = `${reqs.skills[s].name} ${reqs.skills[s].lvl}`;
list.appendChild(node);
}
reqLists.appendChild(list);
// required traders list
list = document.createElement('ul');
list.classList.add('prereq-traders');
for (const t in reqs.traders) {
const node = document.createElement('li');
node.innerText = `${reqs.traders[t].name} ${reqs.traders[t].lvl}`;
list.appendChild(node);
}
reqLists.appendChild(list);
// show summary
ELEMENTS.summary.appendChild(reqLists);
ELEMENTS.summary.classList.remove('hidden');
}
function totalRequirements(stations) {
const items = {};
const skills = {};
const traders = {};
for (const stationId of stations) {
for (const i of HIDEOUT[stationId].prerequisites.items) {
if (items[i.item]) {
items[i.item].amount += i.amount;
} else {
items[i.item] = { ...i };
}
}
for (const s of HIDEOUT[stationId].prerequisites.skills) {
if (!skills[s.name]) {
skills[s.name] = { ...s };
}
if (skills[s.name].lvl < s.lvl) {
skills[s.name] = s;
}
}
for (const t of HIDEOUT[stationId].prerequisites.traders) {
if (!traders[t.name]) {
traders[t.name] = { ...t };
}
if (traders[t.name].lvl < t.lvl) {
traders[t.name] = t;
}
}
}
return { items, skills, traders };
}