106 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-02-05 17:45:49 +01:00
$(function () {
2022-02-06 21:53:03 +01:00
const webSocket = window.socket;
var lastFollow = '-',
lastSub = '-',
lastDonator = '-',
2022-02-07 03:50:03 +01:00
countFollow = 0,
countSub = 0,
2022-02-06 21:53:03 +01:00
timer = '-';
2022-02-05 17:45:49 +01:00
2022-02-06 19:14:55 +01:00
const getQueryMap = () => {
let queryString = window.location.search,
queryParts = queryString.substring(1).split('&'),
queryMap = new Map();
2022-02-05 17:45:49 +01:00
2022-02-07 03:50:03 +01:00
for (let part of queryParts) {
2022-02-06 19:14:55 +01:00
let key = part.substring(0, part.indexOf('=')),
value = part.substring(part.indexOf('=') + 1, part.length);
if (key.length > 0 && value.length > 0) queryMap.set(key, value);
2022-02-05 17:45:49 +01:00
}
return queryMap;
}
2022-02-06 21:53:03 +01:00
const queryMap = getQueryMap();
2022-02-07 03:50:03 +01:00
Map.prototype.getOrElse = (option, def) => queryMap.has(option) ? queryMap.get(option): def;
2022-02-05 17:45:49 +01:00
2022-02-06 21:53:03 +01:00
const refreshUI = () => {
2022-02-07 03:50:03 +01:00
if(queryMap.getOrElse('single',false)){
$('#all').show(); $('#timer').hide(); $('#goal').hide();
$('#follow').parent().closest('div').hide(); $('#subscribe').parent().closest('div').hide(); $('#donate').parent().closest('div').hide();
if(queryMap.getOrElse('follow',false)) $('#follow').parent().closest('div').show();
if(queryMap.getOrElse('subscribe',false)) $('#subscribe').parent().closest('div').show();
if(queryMap.getOrElse('donate',false)) $('#donate').parent().closest('div').show();
}else if(queryMap.getOrElse('goal',false)){
$('#all').hide(); $('#timer').hide(); $('#goal').show();
$('#cfollow').parent().closest('div').hide(); $('#csubscribe').parent().closest('div').hide();
if(queryMap.getOrElse('follow',false)) $('#cfollow').parent().closest('div').show();
if(queryMap.getOrElse('subscribe',false)) $('#csubscribe').parent().closest('div').show();
}
2022-02-06 21:53:03 +01:00
2022-02-06 22:02:11 +01:00
$('#follow').text(lastFollow);
$('#subscribe').text(lastSub);
2022-02-07 03:50:03 +01:00
$('#donate').text(lastDonator);
2022-02-07 03:55:22 +01:00
$('#cfollow').parent().closest('div').progressbar({ value : countFollow/parseInt(queryMap.getOrElse('follow',9999))*100 });
$('#csubscribe').parent().closest('div').progressbar({ value : countSub/parseInt(queryMap.getOrElse('subscribe',9999))*100});
2022-02-07 03:50:03 +01:00
$('#cfollow').text(`${countFollow} / ${queryMap.getOrElse('follow',9999)}`);
2022-02-07 03:50:54 +01:00
$('#csubscribe').text(`${countSub} / ${queryMap.getOrElse('subscribe',9999)}`);
2022-02-06 21:53:03 +01:00
}
2022-02-06 19:14:55 +01:00
const handleSocketMessage = (e)=>{
2022-02-05 17:45:49 +01:00
try {
let rawMessage = e.data,
2022-02-06 19:14:55 +01:00
message = JSON.parse(rawMessage);
2022-02-06 21:53:03 +01:00
if(!message.hasOwnProperty('eventFamily') || message.eventFamily != 'overlay' ||
!message.hasOwnProperty('eventType') || !message.hasOwnProperty('data'))
2022-02-06 19:14:55 +01:00
return;
2022-02-06 21:53:03 +01:00
console.log(message.eventType, message.data)
if(message.eventType == 'follow') {
lastFollow = message.data || lastFollow;
2022-02-07 03:50:03 +01:00
countFollow++;
2022-02-06 21:53:03 +01:00
} else if(message.eventType == 'subscribe') {
lastSub = message.data || lastSub;
2022-02-07 03:50:03 +01:00
countSub++;
2022-02-06 21:53:03 +01:00
} else if(message.eventType == 'donation') {
lastDonator = message.data || lastDonator;
} else if(message.eventType == 'timer') {
console.log("New timer !! ", message.data)
2022-02-05 17:45:49 +01:00
}
2022-02-06 21:53:03 +01:00
refreshUI();
2022-02-05 17:45:49 +01:00
} catch (ex) {
2022-02-06 19:14:55 +01:00
console.log(ex)
2022-02-05 17:45:49 +01:00
}
};
2022-02-06 19:14:55 +01:00
2022-02-07 17:19:38 +01:00
jQuery(async ()=>{
2022-02-07 03:50:03 +01:00
refreshUI();
try{
socket.addFamilyHandler("overlay", handleSocketMessage);
if(socket){
while(socket.getReadyState() === 0){
await new Promise(r => setTimeout(r, 500));
}
socket.getDBValue("get_current_stream_info_lastFollow", 'streamInfo', 'lastFollow', (response)=>{
if(response.streamInfo) lastFollow = response.streamInfo || lastFollow;
refreshUI();
})
socket.getDBValue("get_current_stream_info_lastSub", 'streamInfo', 'lastSub', (response)=>{
if(response.streamInfo) lastSub = response.streamInfo || lastSub;
refreshUI();
})
socket.getDBValue("get_current_stream_info_lastDonator", 'streamInfo', 'lastDonator', (response)=>{
if(response.streamInfo) lastDonator = response.streamInfo || lastDonator;
refreshUI();
})
socket.getDBValue('get_current_stream_info_counts', 'panelData', 'stream', (e)=>{
2022-02-07 03:55:22 +01:00
let data = JSON.parse(e.panelData);
2022-02-07 03:58:40 +01:00
countFollow = data.followers || countFollow;
2022-02-07 03:55:22 +01:00
countSub = 0;
2022-02-07 03:58:40 +01:00
refreshUI();
2022-02-07 03:50:03 +01:00
})
2022-02-06 21:03:37 +01:00
}
2022-02-07 03:50:03 +01:00
}catch(e) {console.log(e)}
2022-02-06 21:03:37 +01:00
})
2022-02-05 17:45:49 +01:00
});