-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
282 lines (231 loc) · 9.18 KB
/
index.html
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Subtrain stars made by 715209</title>
<style>
.inactive {
opacity: 0.5;
}
img {
width: 80px;
}
</style>
</head>
<body>
<div id="stars" style="font-size: 100px;"></div>
<script>
class Subtrain {
constructor(stars, increasesAfter, timeout, warnTimer) {
this.stars = stars;
this.increasesAfter = increasesAfter;
this.timeout = timeout;
this.warnTimer = warnTimer;
this.subs = 0;
this.loc = 0;
this.update = increasesAfter;
this.timer = null;
// this.panicIcon = "☆";
// this.icons = ["★", "☆"];
this.panicIcon = null;
this.icon1 = "http://img.icons8.com/offices/344/bright-moon.png";
this.icon2 = "http://img.icons8.com/ios-glyphs/344/bright-moon--v1.png";
this.div = document.getElementById("stars");
// this.draw();
this.updateStars();
}
draw() {
// console.log(`Drawing ${this.stars} stars`);
for (let i = 0; i < this.stars; i++) {
let node = document.createElement("span", "");
// let star = document.createTextNode(this.icons[0]);
let star = document.createElement("img");
node.className = "inactive";
star.src = this.icon1;
node.appendChild(star);
this.div.appendChild(node);
}
// this.updateStars();
}
// update stars (to fill it)
// TODO: maybe find a better way to do this
updateStars() {
setInterval(() => {
if (this.update <= this.subs) {
if (!this.div.hasChildNodes()) {
this.draw();
}
if (this.loc < this.stars) {
// this.div.childNodes[this.loc].innerHTML = this.icons[1];
this.div.childNodes[this.loc].firstChild.src = this.icon2;
this.div.childNodes[this.loc].className = "active";
this.loc += 1;
} else {
// console.log("No more stars left D:");
}
this.update += this.increasesAfter;
if (this.timer == null) {
this.startTimer();
} else {
// console.log("resetting timer");
clearTimeout(this.timer);
clearTimeout(this.startFreakingOut);
this.startTimer();
}
}
}, 1000);
}
startTimer() {
// console.log("Starting timer");
this.timer = setTimeout(() => {
// console.log("Timer stopped you lost :D");
clearTimeout(this.startFreakingOut);
this.div.innerHTML = "";
this.timer = null;
this.subs = 0;
this.loc = 0;
this.update = this.increasesAfter;
}, this.timeout * 1000);
this.panicMode();
}
// TODO: find a better way?
panicMode(time) {
if (time == null) time = (this.timeout * 1000) - (this.warnTimer * 1000);
this.startFreakingOut = setTimeout(() => {
if (!this.loc == 0) {
// console.log("It's time to freakout")
if (this.panicIcon == this.icon1) {
this.panicIcon = this.icon2;
} else {
this.panicIcon = this.icon1;
}
for (let j = 0; j < this.loc; j++) {
// this.div.childNodes[j].innerHTML = this.panicIcon;
this.div.childNodes[j].firstChild.src = this.panicIcon;
}
this.panicMode(1000);
}
}, time);
}
}
class Twitch {
constructor(channel, giveMeTheStars) {
this.channel = channel;
this.subtrain = giveMeTheStars;
// Start
this.open();
}
open() {
this.ws = new WebSocket("wss://irc-ws.chat.twitch.tv:443");
this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this);
}
keepAlive() {
this.interval = setInterval(() => {
this.ws.send("PING :tmi.twitch.tv\r\n");
// really should check if i receive a pong back.
}, 4000);
}
onOpen() {
if (this.ws !== null && this.ws.readyState === 1) {
console.log("Successfully Connected");
console.log(`Authenticating and joining channel ${this.channel}`);
this.ws.send("CAP REQ :twitch.tv/tags twitch.tv/commands");
// this.ws.send(`PASS ${this.password}`);
this.ws.send(`NICK justinfan715209`);
this.ws.send(`JOIN #${this.channel}`);
this.keepAlive();
}
}
onClose() {
console.log("Disconnected from twitch server");
clearInterval(this.interval);
this.reconnect();
}
close() {
if (this.ws) {
this.ws.close();
}
}
reconnect() {
console.log(`Trying to reconnect in 5 seconds`);
setTimeout(() => {
console.log("Reconnecting...");
this.open();
}, 5000);
}
onError(e) {
console.log(new Error(e));
}
onMessage(message) {
if (message !== null) {
const parsed = this.parse(message.data);
switch (parsed.command) {
// case "PRIVMSG":
// console.log("Got message");
// break;
case "USERNOTICE":
this.handleSubs(parsed);
break;
case "PING":
this.ws.send(`PONG ${parsed.channel}`);
break;
default:
break;
}
}
}
parse(message) {
let parsedMessage = {
tags: {},
channel: null,
command: null,
username: null,
message: null,
raw: message
};
// tags
if (message.startsWith("@")) {
var space = message.indexOf(" ");
const tagsRaw = message.slice(1, space);
const tagsSplit = tagsRaw.split(";");
tagsSplit.map(d => {
const tagSplit = d.split("=");
parsedMessage.tags[tagSplit[0]] = tagSplit[1];
});
}
message = message.slice(space + 1).trim().split(" ");
let pos = 0;
// there's a username
if (message[0].startsWith(":")) {
parsedMessage.username = message[0];
pos += 1;
}
parsedMessage.command = message[pos];
parsedMessage.channel = message[pos + 1];
if (!message[pos + 2] == "") parsedMessage.message = message.slice(3).join(" ").slice(1);
return parsedMessage;
}
handleSubs(msg) {
switch (msg.tags["msg-id"]) {
case "sub":
case "resub":
case "subgift":
case "anonsubgift":
this.subtrain.subs += 1;
break;
default:
// console.log("unhandled usermsg", msg);
break;
}
}
}
let subtrain = new Subtrain(5, 1, 60, 10);
let twitch = new Twitch("715209", subtrain);
</script>
</body>
</html>