WIP
This commit is contained in:
parent
232971098b
commit
1b0878bdc5
46
twitch/customOverlay.js
Normal file
46
twitch/customOverlay.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
(function() {
|
||||||
|
|
||||||
|
|
||||||
|
function sendData(tpe, data) {
|
||||||
|
$.panelsocketserver.sendJSONToAll(JSON.stringify({
|
||||||
|
'eventFamily': 'overlay',
|
||||||
|
'eventType': tpe,
|
||||||
|
'data': data
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
$.bind('command', function(event) {
|
||||||
|
|
||||||
|
const sender = "" + event.getSender().toLowerCase(),
|
||||||
|
command = event.getCommand(),
|
||||||
|
argsString = "" + event.getArguments().trim(),
|
||||||
|
args = event.getArgs(),
|
||||||
|
action = args[0],
|
||||||
|
value = args[1];
|
||||||
|
|
||||||
|
if (command.equalsIgnoreCase('overlay')) {
|
||||||
|
if (!action) {
|
||||||
|
$.say($.whisperPrefix(sender) + $.lang.get('customOverlay.help', ' Use "!overlay [follow | subscribe | donation | timer] value" to set overlay data.'));
|
||||||
|
} else if (action.equalsIgnoreCase('follow')) {
|
||||||
|
sendData(action, value);
|
||||||
|
} else if (action.equalsIgnoreCase('subscribe')) {
|
||||||
|
sendData(action, value);
|
||||||
|
} else if (action.equalsIgnoreCase('donation')) {
|
||||||
|
sendData(action, value);
|
||||||
|
} else if (action.equalsIgnoreCase('timer')) {
|
||||||
|
sendData(action, new Date(Date.now().getTime()+value*1000*60));
|
||||||
|
} else {
|
||||||
|
$.say($.whisperPrefix(sender) + $.lang.get('customOverlay.help'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$.bind('initReady', function() {
|
||||||
|
$.registerChatSubcommand('overlay', 'follow', 2);
|
||||||
|
$.registerChatSubcommand('overlay', 'subscribe', 2);
|
||||||
|
$.registerChatSubcommand('overlay', 'donation', 2);
|
||||||
|
$.registerChatSubcommand('overlay', 'timer', 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
})();
|
@ -3,7 +3,7 @@
|
|||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>PlampBot Song Requests</title>
|
<title>Overlay</title>
|
||||||
|
|
||||||
<!-- Load CSS for Chart.js -->
|
<!-- Load CSS for Chart.js -->
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.css" />
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.css" />
|
||||||
@ -13,23 +13,14 @@
|
|||||||
|
|
||||||
<!-- Main body -->
|
<!-- Main body -->
|
||||||
<body>
|
<body>
|
||||||
<div class="main-alert">
|
<div class="main">
|
||||||
<!-- Where the gif gets played. -->
|
|
||||||
<div id="alert"></div>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="main-text-alert">
|
|
||||||
<!-- Where the custom gif text is displayed. -->
|
|
||||||
<div id="alert-text"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- jQuery -->
|
<!-- jQuery -->
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||||
<!-- Load jQuery UI -->
|
<!-- Load jQuery UI -->
|
||||||
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
|
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.min.js"></script>
|
||||||
<!-- Load Chart.js -->
|
<!-- Load Chart.js -->
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
|
||||||
<!-- Load chartjs-plugin-datalabels -->
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.6.0/dist/chartjs-plugin-datalabels.min.js"></script>
|
|
||||||
<!-- Load Reconnecting socket -->
|
<!-- Load Reconnecting socket -->
|
||||||
<script src="/common/reconnecting-websocket/reconnectingWS.min.js"></script>
|
<script src="/common/reconnecting-websocket/reconnectingWS.min.js"></script>
|
||||||
<!-- Load Bot config file -->
|
<!-- Load Bot config file -->
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
$(function () {
|
$(function () {
|
||||||
var webSocket = getWebSocket(),
|
var webSocket = getWebSocket(),
|
||||||
queryMap = getQueryMap(),
|
queryMap = getQueryMap(),
|
||||||
isDebug = localStorage.getItem('phantombot_follow_debug') === 'true' || false;
|
isDebug = localStorage.getItem('phantombot_overlay_debug') === 'true' || false;
|
||||||
queue = [];
|
queue = [];
|
||||||
|
|
||||||
/*
|
const getWebSocket = () => {
|
||||||
* @function Gets a new instance of the websocket.
|
let socketUri = ((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/ws/overlay'), // URI of the socket.
|
||||||
*
|
|
||||||
* @return {ReconnectingWebSocket}
|
|
||||||
*/
|
|
||||||
function getWebSocket() {
|
|
||||||
let socketUri = ((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/ws/followpolls'), // URI of the socket.
|
|
||||||
reconnectInterval = 5000; // How often in milliseconds we should try reconnecting.
|
reconnectInterval = 5000; // How often in milliseconds we should try reconnecting.
|
||||||
|
|
||||||
return new ReconnectingWebSocket(socketUri, null, {
|
return new ReconnectingWebSocket(socketUri, null, {
|
||||||
@ -18,141 +13,54 @@ $(function () {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
const getQueryMap = () => {
|
||||||
* @function Parses the query params in the URL and puts them into a map.
|
let queryString = window.location.search,
|
||||||
*
|
queryParts = queryString.substring(1).split('&'),
|
||||||
* @return {Map}
|
queryMap = new Map();
|
||||||
*/
|
|
||||||
function getQueryMap() {
|
|
||||||
let queryString = window.location.search, // Query string that starts with ?
|
|
||||||
queryParts = queryString.substr(1).split('&'), // Split at each &, which is a new query.
|
|
||||||
queryMap = new Map(); // Create a new map for save our keys and values.
|
|
||||||
|
|
||||||
for (let i = 0; i < queryParts.length; i++) {
|
for (let part in queryParts) {
|
||||||
let key = queryParts[i].substr(0, queryParts[i].indexOf('=')),
|
let key = part.substring(0, part.indexOf('=')),
|
||||||
value = queryParts[i].substr(queryParts[i].indexOf('=') + 1, queryParts[i].length);
|
value = part.substring(part.indexOf('=') + 1, part.length);
|
||||||
|
if (key.length > 0 && value.length > 0) queryMap.set(key, value);
|
||||||
if (key.length > 0 && value.length > 0) {
|
|
||||||
queryMap.set(key, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return queryMap;
|
return queryMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
const printDebug = (message, force) => {
|
||||||
* @function Prints debug logs.
|
if (isDebug || force) console.log('%c[PhantomBot Log]', 'color: #6441a5; font-weight: 900;', message);
|
||||||
*
|
|
||||||
* @param {String} message
|
|
||||||
*/
|
|
||||||
function printDebug(message, force) {
|
|
||||||
if (isDebug || force) {
|
|
||||||
console.log('%c[PhantomBot Log]', 'color: #6441a5; font-weight: 900;', message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
window.toggleDebug = (toggle) => {
|
||||||
/*
|
localStorage.setItem('phantombot_overlay_debug', toggle.toString());
|
||||||
* @function Toggles the debug mode.
|
|
||||||
*
|
|
||||||
* @param {String} toggle
|
|
||||||
*/
|
|
||||||
window.toggleDebug = function (toggle) {
|
|
||||||
localStorage.setItem('phantombot_follow_debug', toggle.toString());
|
|
||||||
|
|
||||||
// Refresh the page.
|
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
const getOrElse = (option, def) => queryMap.has(option) ? queryMap.get(option): def;
|
||||||
* @function Checks if the query map has the option, if not, returns default.
|
|
||||||
*
|
|
||||||
* @param {String} option
|
|
||||||
* @param {String} def
|
|
||||||
* @return {String}
|
|
||||||
*/
|
|
||||||
function getOptionSetting(option, def) {
|
|
||||||
if (queryMap.has(option)) {
|
|
||||||
return queryMap.get(option);
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
const handleSocketMessage = (e)=>{
|
||||||
* @function Sends a message to the socket
|
|
||||||
*
|
|
||||||
* @param {String} message
|
|
||||||
*/
|
|
||||||
function sendToSocket(message) {
|
|
||||||
try {
|
|
||||||
webSocket.send(JSON.stringify(message));
|
|
||||||
} catch (ex) {
|
|
||||||
printDebug('Failed to send a message to the socket: ' + ex.stack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function sleep(ms) {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @event Called once the socket opens.
|
|
||||||
*/
|
|
||||||
webSocket.onopen = function () {
|
|
||||||
printDebug('Successfully connected to the socket.', true);
|
|
||||||
// Authenticate with the socket.
|
|
||||||
sendToSocket({
|
|
||||||
authenticate: getAuth()
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @event Called when the socket closes.
|
|
||||||
*/
|
|
||||||
webSocket.onclose = function () {
|
|
||||||
printDebug('Disconnected from the socket.', true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @event Called when we get a message.
|
|
||||||
*
|
|
||||||
* @param {Object} e
|
|
||||||
*/
|
|
||||||
webSocket.onmessage = function (e) {
|
|
||||||
try {
|
try {
|
||||||
let rawMessage = e.data,
|
let rawMessage = e.data,
|
||||||
message = JSON.parse(rawMessage);
|
message = JSON.parse(rawMessage);
|
||||||
|
|
||||||
printDebug('[MESSAGE] ' + rawMessage);
|
if(!message.hasOwnProperty('eventFamily') || message['eventFamily'] != 'overlay' ||
|
||||||
|
!message.hasOwnProperty('eventType') || !message.hasOwnProperty['data'])
|
||||||
|
return;
|
||||||
|
|
||||||
if (message.query_id === undefined) {
|
if(message['eventType'] == 'follow') {
|
||||||
// Check for our auth result.
|
console.log("New Follow !! ", message['data'])
|
||||||
if (message.authresult !== undefined) {
|
} else if(message['eventType'] == 'subscribe') {
|
||||||
if (message.authresult === 'true') {
|
console.log("New Sub !! ", message['data'])
|
||||||
printDebug('Successfully authenticated with the socket.', true);
|
} else if(message['eventType'] == 'donation') {
|
||||||
// Handle this.
|
console.log("New dono !! ", message['data'])
|
||||||
handleBrowserInteraction()
|
} else if(message['eventType'] == 'timer') {
|
||||||
} else {
|
console.log("New timer !! ", message['data'])
|
||||||
printDebug('Failed to authenticate with the socket.', true);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
|
|
||||||
// Queue all events and process them one at-a-time.
|
|
||||||
if (message.alert_image !== undefined || message.audio_panel_hook !== undefined) {
|
|
||||||
queue.push(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message cannot be handled error.
|
|
||||||
else {
|
|
||||||
printDebug('Failed to process message from socket: ' + rawMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
printDebug('Failed to parse socket message [' + e.data + ']: ' + e.stack);
|
console.log(ex)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle processing the queue.
|
socket.addFamilyHandler("overlay", handleSocketMessage);
|
||||||
setInterval(handleQueue, 5e2);
|
|
||||||
|
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user