forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisher-ui.js
199 lines (163 loc) · 5.38 KB
/
fisher-ui.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
import TimerBar from '../../resources/timerbar';
class FisherBar extends TimerBar {
stop() {
cancelAnimationFrame(this._animationFrame);
this._animationFrame = null;
}
}
if (window.customElements) {
// Preferred method but old CEF doesn't have this.
window.customElements.define('fisher-bar', FisherBar);
} else {
document.registerElement('fisher-bar', {
prototype: Object.create(FisherBar.prototype),
});
}
export default class FisherUI {
constructor(options, element) {
this.element = element;
this.options = options;
this.baitEl = element.querySelector('#bait-name');
this.placeEl = element.querySelector('#place-name');
this.timeEl = element.querySelector('#cast-duration');
this.arrowEl = element.querySelector('#fisher-arrow');
this.tugNames = ['unknown', 'light', 'medium', 'heavy'];
this.castStart = null;
this.fishing = false;
this.animationFrame = false;
this.bars = [];
}
draw() {
const timeMs = new Date() - this.castStart;
const time = (timeMs / 1000).toFixed(1);
this.timeEl.innerHTML = time;
this.arrowEl.style.top = timeMs / 600 + '%';
this.animationFrame = requestAnimationFrame(this.draw.bind(this));
}
setBait(baitName) {
this.baitEl.innerHTML = baitName;
}
setPlace(place) {
const oldPlace = this.placeEl.innerHTML;
if (!place) {
if (oldPlace && oldPlace[0] !== '(')
this.placeEl.innerHTML = '(' + oldPlace + ')';
else
this.placeEl.innerHTML = '------------';
} else {
this.placeEl.innerHTML = place;
}
}
startTimers() {
const barData = {};
const rows = this.element.querySelectorAll('.table-row');
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const min = row.getAttribute('data-min');
const max = row.getAttribute('data-max');
const bar = document.createElement('fisher-bar');
let timeouts = [];
bar.centertext = row.getAttribute('data-fish');
// Step one: fill until the minimum time
if (min && min !== 'undefined' && max && max !== 'undefined') {
row.opacity = 0.8;
bar.duration = min / 1000;
bar.stylefill = 'fill';
// Step two: empty until the maximum time
timeouts.push(window.setTimeout(() => {
row.style.opacity = 1;
bar.stylefill = 'empty';
bar.value = 0;
bar.duration = (max - min) / 1000;
timeouts.push(window.setTimeout(() => {
row.style.opacity = 0.5;
}, max - min));
}, min));
} else {
bar.duration = 0;
timeouts = [];
}
if (row.getAttribute('data-tug'))
bar.fg = this.options.Colors[this.tugNames[row.getAttribute('data-tug')]];
while (row.lastChild)
row.removeChild(row.lastChild);
row.appendChild(bar);
this.bars.push({
'row': row,
'bar': bar,
'timeouts': timeouts,
});
}
this.draw();
}
stopTimers() {
// Stops cast time timer and arrow
cancelAnimationFrame(this.animationFrame);
this.animationFrame = null;
this.bars.forEach((bar) => {
// Stops the timed events
bar.timeouts.forEach((timeout) => {
clearTimeout(timeout);
});
// Stops the bar
bar.bar.stop();
});
}
redrawFish(hookTimes, tugTypes) {
// Sort hook times by minimum time, with undefineds being at the end
const sortedKeys = Object.keys(hookTimes).sort((a, b) => {
const t = hookTimes;
if ((!t[a] || !t[a].min) && (!t[b] || !t[b].min))
return 0;
else if (!t[a] || !t[a].min)
return 1;
else if (!t[b] || !t[b].min)
return -1;
return t[a].min - t[b].min;
});
// Remove current values from all wells
Array.prototype.forEach.call(
this.element.querySelectorAll('.well-entry, .table-row'),
(node) => {
node.parentNode.removeChild(node);
},
);
for (let i = 0; i < sortedKeys.length; i++) {
// First, draw on the well
const fish = sortedKeys[i];
if (tugTypes[fish] && hookTimes[fish].min && hookTimes[fish].max) {
const tug = tugTypes[fish];
// Create the element with fish-specific styles
const el = document.createElement('div');
el.classList.add('well-entry');
el.setAttribute('data-fish', fish);
el.style.top = (hookTimes[fish].min / 600).toString() + '%';
el.style.height = ((hookTimes[fish].max - hookTimes[fish].min) / 600).toString() + '%';
el.style.backgroundColor = this.options.Colors[this.tugNames[tug]];
// Put the element in the well
const well = this.element.querySelector('#fisher-well-' + this.tugNames[tug]);
well.appendChild(el);
}
// Next, make the row for the table
const row = document.createElement('div');
row.classList.add('table-row');
row.setAttribute('data-fish', fish);
row.setAttribute('data-tug', tugTypes[fish]);
row.setAttribute('data-min', hookTimes[fish].min);
row.setAttribute('data-max', hookTimes[fish].max);
// Add the row to the table
const table = this.element.querySelector('#fisher-table');
table.appendChild(row);
}
}
startFishing() {
this.fishing = true;
this.castStart = new Date();
this.startTimers();
}
stopFishing() {
this.stopTimers();
this.fishing = false;
this.animationFrame = null;
}
}