-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
248 lines (242 loc) · 8.8 KB
/
timer.js
File metadata and controls
248 lines (242 loc) · 8.8 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
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
242
243
244
245
246
247
248
const l = what => console.log(Object.keys(what)[0], what[Object.keys(what)[0]]);
class Timer {
constructor(options) {
this.commandPrefix = options.commandPrefix || '!';
this.googleFont = options.googleFont || 'https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&display=swap'
this.fontFamily = /family=([^&|:]*)/.exec(this.googleFont)[1].replace("+", " "); //might need to be adjusted based on font input uri
this.allowAny = options.allowAny || false; //change to make sure it defaults properly using ternary opreator instead
this.allowVIP = options.allowVIP || false;
this.allowMod = options.allowMod || true;
this.showUpDownArrow = options.showUpDownArrow || true;
this.countUpOrDown = options.countUpOrDown || 'down';
this.timerInt = ''; //interval id for the setInterval loop which handles the actual ticking of the clock
this.isRunning = false;
this.rootElement = document.querySelector(options.rootElement);
this.rootElement.style.opacity = 0;
this.ticks = 0;
this.totalTicks = 300000;
this.goal = new Date(300000).toISOString().substr(14, 5);
this.helpInfo = '';
this.showAscDesc = options.showAscDesc ? true : false;
this.countdownCompleteSoundFile = options.countdownCompleteSoundFile;
this.countdownSoundFile = options.countdownSoundFile;
this.rootElementOpacity = options.opacity || 1;
this.loadFont();
this.#domStuff();
// this.mainHelpInfo();
}
about() {
this.help(args['about']);
}
add(args) {
this.set(args, true);
}
clear() {
if (this.isRunning) return;
this.ticks = 0;
this.totalTicks = 0;
this.goal = '';
this.render();
}
#domStuff() {
this.labelElement = Object.assign(document.createElement('div'), { id: 'timerLabel', className: '', innerHTML: '' });
this.fa = Object.assign(document.createElement('i'), { id: 'stopwatch', className: 'fa-solid fa-stopwatch' });
this.info = Object.assign(document.createElement('div'), { id: 'info', style: `font-family: ${this.fontFamily}`, innerHTML: `${this.currentTime || ''}` });
this.ascdesc = Object.assign(document.createElement('i'), { id: 'ascdesc', className: 'ssmaller fa-solid ', style: this.showAscDesc ? '' : 'display:none', innerHTML: this.goal });
this.timerhelp = Object.assign(document.createElement('div'), { id: 'timerhelp', innerHTML: `${this.helpInfo}` });
this.rootElement.append(this.fa, this.info, this.ascdesc, this.timerhelp, this.labelElement);
this.rootElement.style.fontFamily = this.fontFamily; //set it "#app" wide1
var audio = document.createElement('audio');
audio.style.display = 'block';
audio.src = this.countdownCompleteSoundFile;
audio.autoplay = false;
this.audio = audio;
var a2 = document.createElement('audio');
a2.style.display = 'block';
a2.src = this.countdownSoundFile;
a2.autoplay = false;
this.a2 = a2;
document.body.appendChild(audio);
}
elapsed() {
this.countUpOrDown = 'up';
this.render();
}
#finished() {
this.isRunning = false;
clearInterval(this.timerInt);
this.ticks = 0;
let body = document.querySelector('body');
body.classList.remove('bodyflash');
body.classList.add('bodyflash');
setTimeout(function () {
body.classList.remove('bodyflash');
}, 5000);
}
help(args) {
switch (args[0]) {
case "start":
this.helpInfo = "starts the timer, accepts a time string or starts a default 5 minute if omitted";
break;
case "stop":
this.helpInfo = "this stops the timer, keeping the current count where it is";
break;
case "reset":
this.helpInfo = "restarts the timer, changing nothing else.";
break;
case "clear":
this.helpInfo = "stops the timer, clears the timer goal. full reset!";
break;
case "add":
this.helpInfo = `lets you extend the timer goal. <br/><br/>e.g., <span style='color: #0591e3'>${this.commandPrefix}</span> add 5m20s`;
break;
case "subtract":
this.helpInfo = `lets you decrease the timer goal. <br/><br/>e.g., <span style='color: #0591e3'>${this.commandPrefix}</span> subtract 5m20s`;
break;
case "remove":
this.helpInfo = `lets you decrease the timer goal. <br/><br/>e.g., <span style='color: #0591e3'>${this.commandPrefix}</span> subtract 5m20s`;
break;
case "about":
this.helpInfo = "timer overlay created by twitch.tv/<span style='color: #0591e3'>xmetrix</span>";
break;
default:
this.helpInfo = this.mainHelpInfo();
}
this.rootElement.style.opacity = 1;
this.timerhelp.classList.add('showHelp');
setTimeout(function () {
this.timerhelp.classList.remove('showHelp');
}, 10000)
console.log('help should have shown the shit');
this.render();
}
hide() {
this.hideRootElement = true;
this.render();
}
mainHelpInfo() {
return `<h3>Timer Commands</h3>
<p>The chat command prefix is: <span style='color: #0591e3'>${this.commandPrefix}</span></p>
<ul>
<li>start - starts a timer with default 5:00 or accepts time string 1h5m2s</li>
<li>stop - stops timer and keeps all values</li>
<li>reset - resets timer to keeping target time</li>
<li>clear - completely resets timer</li>
<li>add - allows you to add timestring 1h25m5s</li>
<li>subtract/remove - allows you to subtract timestring 70m</li>
<li>remaining - toggles the display to time remaining</li>
<li>elapsed - toggles the display to time elapsed</li>
</ul>
<p>Type <strong>help command</strong> for specific help with that command</p>`;
}
label(args) {
this.labelInfo = args.join(' ').replace("<", "<");
}
loadFont() {
let s = document.createElement('style');
let head = document.head || document.getElementsByTagName("head")[0];
head.appendChild(s);
s.type = 'text/css';
if (s.styleSheet) {
s.styleSheet.cssText = `@import url('${this.googleFont}');`;
} else {
s.appendChild(document.createTextNode(`@import url('${this.googleFont}');`));
}
}
prefix(args) {
document.dispatchEvent(new CustomEvent('prefixUpdated', { detail: args[0] }));
this.commandPrefix == args[0];
}
processCommand(chatObj){
}
refresh() {
//save current settings to localstorage, and retreive them on constructor
location.reload();
}
remaining() {
this.countUpOrDown = 'down';
this.render();
}
remove(args) {
this.set(args, false, true);
}
render() {
let { start, end } = (this.totalTicks - this.ticks >= 3600000) ? { start: 11, end: 8 } : { start: 14, end: 5 };
this.currentTime = new Date(this.countUpOrDown == "up" ? this.ticks : this.totalTicks - this.ticks).toISOString().substr(start, end);
if (this.isRunning) { this.fa.classList.add('timerG'); this.fa.classList.remove('timerR'); } else { this.fa.classList.remove('timerG'); this.fa.classList.add('timerR'); }
this.info.innerHTML = this.currentTime || '';
this.labelElement.innerHTML = this.labelInfo || '';
this.ascdesc.className = `smaller fa-solid fa-arrow-${this.countUpOrDown}`;
this.ascdesc.innerHTML = this.goal;
this.timerhelp.innerHTML = this.helpInfo;
if(this.hideRootElement){
this.rootElement.style.opacity = 0;
return;
}
this.rootElement.style.opacity = this.rootElementOpacity;
}
reset() {
this.ticks = 0;
this.render();
}
set(args, add = false, subtract = false) { //timer set 1m
let timestring = args.join('').toLowerCase();
let ms = { h: 3600000, m: 60000, s: 1000 };
let totalUserStringTicks = parseInt([...timestring.matchAll(/(\d{0,})(h|m|s)/g)].reduce((p, c) => p + c[1] * ms[c[2]], 0));
if (add) {
this.totalTicks += Math.min(86399000, totalUserStringTicks);
this.totalTicks = Math.min(86399000, this.totalTicks);
this.goal = new Date((this.totalTicks)).toISOString().substr(11, 8);
this.render();
return;
}
if (subtract) {
let ticksToRemove = totalUserStringTicks;
if (this.totalTicks >= ticksToRemove) {
this.totalTicks -= ticksToRemove;
}
this.goal = new Date((this.totalTicks)).toISOString().substr(11, 8);
this.render();
return;
}
this.totalTicks = Math.min(86399000, totalUserStringTicks);
this.goal = new Date((this.totalTicks)).toISOString().substr(11, 8);
console.log('this is args', args, args[0], this.totalTicks);
if (args[1] == 'up' || args[1] == 'u') { this.countUpOrDown = 'up'; }
if (args[1] == 'down' || args[1] == 'd') { this.countUpOrDown = 'down'; }
this.render();
}
show(args) {
if (args[0] && /[^0-9\.]/.test(args[0])) return;
this.rootElementOpacity = args[0] || 1;
this.hideRootElement = false;
}
start(args) {
console.log('start');
if (this.isRunning) return;
if (args.length) this.set(args);
clearInterval(this.timerInt);
this.render();
this.timerInt = setInterval(() => { this.tick(); }, 1000);
this.isRunning = true;
}
stop() {
clearInterval(this.timerInt);
this.isRunning = false;
this.render();
}
subtract(args) {
this.set(args, false, true);
}
tick() {
this.ticks += 1000;
this.render();
if (this.totalTicks - this.ticks == 5000) {
this.audio.play();
}
if (this.ticks == this.totalTicks) {
this.#finished();
this.a2.play();
}
}
}