forked from darkon5/Plugbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugbot.js
More file actions
435 lines (386 loc) · 12.4 KB
/
plugbot.js
File metadata and controls
435 lines (386 loc) · 12.4 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* TERMS OF REPRODUCTION USE
*
* 1. Provide a link back to the original repository (this repository), as
* in, https://github.com/ConnerGDavis/Plugbot, that is well-visible
* wherever the source is being reproduced. For example, should you
* display it on a website, you should provide a link above/below that
* which the users use, titled something such as "ORIGINAL AUTHOR".
*
* 2. Retain these three comments: the GNU GPL license statement, this comment,
* and that below it, that details the author and purpose.
*/
/**
* NOTE: This is all procedural as hell because prototypes and any
* OOP techniques in Javascript are stupid and confusing.
*
* @author Conner Davis ([VIP] ♫Łŏġïç®)
* Harrison Schneidman ([VIDJ] EXƎ)
*/
/**
* Whether the user has currently enabled auto-woot.
*/
var autowoot = true;
/**
* Whether the user has currently enabled auto-queueing.
*/
var autoqueue = false;
/**
* Whether or not the user has enabled hiding this video.
*/
var hideVideo = false;
/**
* Whether or not the user has enabled the userlist.
*/
var userList = true;
/**
* Whenever a user chooses to apply custom username FX to a
* user, their username and chosen colour and saved here.
*/
var customUsernames = new Array();
/**
* Initialise all of the Plug.dj API listeners which we use
* to asynchronously intercept specific events and the data
* attached with them.
*/
function initAPIListeners()
{
/**
* This listens in for whenever a new DJ starts playing.
*/
API.addEventListener(API.DJ_ADVANCE, djAdvanced);
/**
* This listens for whenever a user in the room either WOOT!s
* or Mehs the current song.
*/
API.addEventListener(API.VOTE_UPDATE, function(obj) {
if (userList)
populateUserlist();
});
/**
* Whenever a user joins, this listener is called.
*/
API.addEventListener(API.USER_JOIN, function(user) {
if (userList)
populateUserlist();
});
/**
* Called upon a user exiting the room.
*/
API.addEventListener(API.USER_LEAVE, function(user) {
if (userList)
populateUserlist();
});
API.addEventListener(API.CHAT, checkCustomUsernames);
}
/**
* Periodically check the chat history to see if any of the messages
* match that of the user's chosen custom username FX. If so, then we
* need to stylise every instance of those.
*/
function checkCustomUsernames()
{
$('span[class*="chat-from"]').each(function() {
for (var custom in customUsernames)
{
var check = customUsernames[custom].split(":");
if (check[0] == $(this).text())
{
$(this).css({ color: "#" + check[1]});
break;
}
}
});
}
/**
* Renders all of the Plug.bot "UI" that is visible beneath the video
* player.
*/
function displayUI()
{
/*
* Be sure to remove any old instance of the UI, in case the user
* reloads the script without refreshing the page (updating.)
*/
$('#plugbot-ui').remove();
/*
* Generate the HTML code for the UI.
*/
$('#chat').prepend('<div id="plugbot-ui"></div>');
$('#plugbot-ui').append(
'<p id="plugbot-btn-woot" style="color:#3FFF00">auto-woot</p>'
+ '<p id="plugbot-btn-queue" style="color:#ED1C24">auto-queue</p>'
+ '<p id="plugbot-btn-hidevideo" style="color:#ED1C24">hide video</p>'
+ '<p id="plugbot-btn-userlist" style="color:#3FFF00">userlist</p>'
+ '<h2 title="This makes it so you can give a user in the room a special colour when they chat!">Custom Username FX: <br /><br id="space" /><span onclick="promptCustomUsername()" style="cursor:pointer">+ add new</span></h2>'
);
}
/**
* Prompt the user to provide a new custom username FX.
*/
function promptCustomUsername() {
var check = prompt("Format: username:color\n(For color codes, Google 'Hexadecimal color chart')");
customUsernames.push(check);
$('#space').after('<span id="' + check + '" onclick="removeCustomUsername(\'' + check + '\');$(this).next().remove();$(this).remove();" style="cursor:pointer;color:#' + check.split(":")[1] + '">- ' + check.split(":")[0]
+ '</span><br />');
checkCustomUsernames();
}
/**
* Remove an existing entry in the custom username FX.
*/
function removeCustomUsername(data) {
customUsernames.splice(data, 1);
}
/**
* For every button on the Plug.bot UI, we have listeners backing them
* that are built to intercept the user's clicking each button. Based
* on the button that they clicked, we can execute some logic that will
* in some way affect their experience.
*/
function initUIListeners()
{
$("#plugbot-btn-userlist").on("click", function() {
userList = !userList;
$(this).css("color", userList ? "#3FFF00" : "#ED1C24");
$("#plugbot-userlist").css("visibility", userList ? ("visible") : ("hidden"));
if (!userList) {
$("#plugbot-userlist").empty();
} else {
populateUserlist();
}
});
$("#plugbot-btn-woot").on("click", function() {
autowoot = !autowoot;
$(this).css("color", autowoot ? "#3FFF00" : "#ED1C24");
if (autowoot)
$("#button-vote-positive").click();
});
$("#plugbot-btn-hidevideo").on("click", function() {
hideVideo = !hideVideo;
$(this).css("color", hideVideo ? "#3FFF00" : "#ED1C24");
$("#yt-frame").animate({"height": (hideVideo ? "0px" : "271px")}, {duration: "fast"});
$("#playback .frame-background").animate({"opacity": (hideVideo ? "0" : "0.91")}, {duration: "medium"});
});
$("#plugbot-btn-queue").on("click", function() {
autoqueue = !autoqueue;
$(this).css("color", autoqueue ? "#3FFF00" : "#ED1C24");
$("#button-dj-waitlist-" + (autoqueue ? "join" : "leave")).click();
});
}
/**
* Called whenever a new DJ begins playing in the room.
*
* @param {Object} obj
* This contains the user (current DJ)'s data.
*/
function djAdvanced(obj)
{
/*
* If they want the video to be hidden, be sure to re-hide it.
*/
if (hideVideo)
{
$("#yt-frame").css("height", "0px");
$("#playback .frame-background").css("opacity", "0.0");
}
/*
* If auto-woot is enabled, WOOT! the song.
*/
if (autowoot)
$("#button-vote-positive").click();
/*
* If auto-queueing has been enabled, and they have just recently
* left the waitlist, join them back.
*/
if ($("#button-dj-waitlist-join").css("display") === "block" && autoqueue)
$("#button-dj-waitlist-join").click();
/*
* If the userlist is enabled, re-populate it.
*/
if (userList)
populateUserlist();
}
/**
* Generates every user in the room and their current vote as
* colour-coded text. Also, moderators get the star next to
* their name.
*/
function populateUserlist()
{
/*
* Destroy the old userlist DIV and replace it with a fresh
* empty one to work with.
*/
$("#plugbot-userlist").remove();
$('body').append('<div id="plugbot-userlist"></div>');
/*
* Update the current # of users in the room.
*/
$('#plugbot-userlist').append('<h1 style="text-indent:12px;color:#42A5DC;font-size:14px;font-variant:small-caps;">Users: ' + API.getUsers().length + '</h1>');
/*
* If the user is in the waitlist, show them their current spot.
*/
if ($('#button-dj-waitlist-view').attr('title') !== '') {
if ($('#button-dj-waitlist-leave').css('display') === 'block' && ($.inArray(API.getDJs(), API.getSelf()) == -1)) {
var spot = $('#button-dj-waitlist-view').attr('title').split('(')[1];
spot = spot.substring(0, spot.indexOf(')'));
$('#plugbot-userlist').append('<h1 id="plugbot-queuespot"><span style="font-variant:small-caps">Waitlist:</span> ' + spot + '</h3><br />');
}
}
/*
* An array of all of the room's users.
*/
var users = new Array();
/*
* Populate the users array with the next user
* in the room (this is stored alphabetically.)
*/
for (user in API.getUsers())
{
users.push(API.getUsers()[user]);
}
/*
* For every user, call the #appendUser(username, vote) method
* which will display their username with any colour coding that
* they match.
*/
for (user in users)
{
var user = users[user];
appendUser(user)
}
}
/**
* Appends another user's username to the userlist.
*
* @param {Object} username
* The username of this user.
* @param {Object} vote
* Their current 'vote', which may be:
* -1 : Meh
* 0 : 'undecided' (hasn't voted yet)
* 1 : WOOT!
*/
function appendUser(user)
{
var username = user.username;
var vote = user.vote;
/*
* Some variables that we'll either be setting as true/false
* (some conditionals that do major changes to their look in the userlist)
* or setting a value to.
*/
var colour;
var currentDj = false;
var moderator = user.moderator;
if (API.getSuperUsers() != null) var su = user.superuser;
if (API.getHost() != null) var host = user.owner;
var img;
/*
* Based on their vote, apply the colour coding.
*/
switch (vote)
{
case 1: // WOOT!
colour = "3FFF00";
if (moderator)
img = "http://i.imgur.com/T5G4f.png";
if (host)
img = "http://i.imgur.com/Lu1qo.png";
if (su)
img = "http://i.imgur.com/XA1DE.png";
break;
case 0: // Undecided
colour = "FFFFFF";
if (moderator)
img = "http://i.imgur.com/sRsU0.png";
if (host)
img = "http://i.imgur.com/6Bq5W.png";
if (su)
img = "http://i.imgur.com/veoVS.png";
break;
case -1: // Meh
colour = "ED1C24";
if (moderator)
img = "http://i.imgur.com/JPA1Q.png";
if (host)
img = "http://i.imgur.com/wVLR3.png";
if (su)
img = "http://i.imgur.com/5LcI3.png";
break;
}
/*
* If they're the current DJ, apply some more special
* changes.
*/
if (API.getDJs().length > 0 && API.getDJs()[0].username == username) {
currentDj = true;
colour = "42A5DC";
if (moderator)
img = "http://i.imgur.com/CsK3d.png";
}
/*
* Sometimes undecided mod star breaks. This fixes that.
*/
if (img == undefined && (moderator || host || su))
{
colour = "FFFFFF";
img = moderator ? "http://i.imgur.com/sRsU0.png" : "http://i.imgur.com/6Bq5W.png";
}
/*
* Apply the HTML code to the page that actually displays them
* inside the userlist.
*/
$('#plugbot-userlist').append(
((moderator || host || su) ? '<img src="' + img + '" align="left" style="margin-left:6px" alt="Moderator" />' : '')
+ '<p style="' + ((moderator || host || su) ? 'text-indent:6px !important;' : '')
+ 'color:#' + colour + ';' + (currentDj ? 'font-weight:bold;font-size:15px' : '') + '"'
+ (currentDj ? ('title="' + username + ' is the current DJ!"') : '') + '>'
+ username + '</p>'
);
}
///////////////////////////////////////////////////////////
////////// EVERYTHING FROM HERE ON OUT IS INIT ////////////
///////////////////////////////////////////////////////////
/*
* Clear the old code so we can properly update everything.
*/
$('#plugbot-css').remove();
$('#plugbot-js').remove();
/*
* Write the CSS rules that are used for components of the
* Plug.bot UI.
*/
$('body').prepend('<style type="text/css" id="plugbot-css">'
+ '#plugbot-ui { position: absolute; margin-left: 349px; }'
+ '#plugbot-ui p { background-color: #0b0b0b; height: 32px; padding-top: 8px; padding-left: 8px; cursor: pointer; font-variant: small-caps; width: 84px; font-size: 15px; margin: 0; }'
+ '#plugbot-ui h2 { background-color: #0b0b0b; height: 112px; width: 156px; margin: 0; color: #fff; font-size: 13px; font-variant: small-caps; padding: 8px 0 0 12px; border-top: 1px dotted #292929; }'
+ '#plugbot-userlist { border: 6px solid rgba(10, 10, 10, 0.8); border-left: 0 !important; background-color: #000000; padding: 8px 0px 20px 0px; width: 12%; }'
+ '#plugbot-userlist p { margin: 0; padding-top: 2px; text-indent: 24px; font-size: 10px; }'
+ '#plugbot-userlist p:first-child { padding-top: 0px !important; }'
+ '#plugbot-queuespot { color: #42A5DC; text-align: left; font-size: 15px; margin-left: 8px }');
/*
* Hit the woot button, since auto-woot is enabled by default.
*/
$("#button-vote-positive").click();
/*
* Call all init-related functions to start the software up.
*/
initAPIListeners();
populateUserlist();
displayUI();
initUIListeners();