From fd070bc03b17fa86edd773e9194c2ef221f1e637 Mon Sep 17 00:00:00 2001 From: nicnacnic Date: Wed, 8 Jun 2022 19:49:55 -0400 Subject: [PATCH] Fixed ad player --- extension/bot.js | 41 ++++++- extension/index.js | 152 +++++++++++++++++------- graphics/discordAudio/discordAudio.html | 123 ++++++++++++------- 3 files changed, 229 insertions(+), 87 deletions(-) diff --git a/extension/bot.js b/extension/bot.js index 194f67b..0010e03 100644 --- a/extension/bot.js +++ b/extension/bot.js @@ -29,10 +29,49 @@ module.exports.start = (nodecg) => { }) // Stream audio to browser. + // const app = nodecg.Router(); + // app.get('/bundles/nodecg-marathon-control/bot-audio', (req, res) => mixer.pipe(res)) + // nodecg.mount(app) + + //const app = nodecg.Router(); + const wss = new WebSocket.WebSocketServer({ noServer: true }); + + //const app = express(); + // let server; + // app.use((req, res, next) => { + // if (!server) { + // server = req.connection.server; + // //do fancy staffs with http-server + // } + // next(); + // }) + const app = nodecg.Router(); - app.get('/bundles/nodecg-marathon-control/bot-audio', (req, res) => mixer.pipe(res)) + + app.get('/bundles/nodecg-marathon-control/websocket/start', (req, res) => { + upgradeServer(req.connection.server) + }) + nodecg.mount(app) + mixer.on('data', (chunk) => { + wss.clients.forEach(function each(client) { + if (client.readyState === WebSocket.OPEN) { + client.send(chunk, { binary: true }); + } + }); + }); + + function upgradeServer(server) { + server.on('upgrade', (req, socket, head) => { + if (req.url === '/bundles/nodecg-marathon-control/websocket/data') { + wss.handleUpgrade(req, socket, head, function done(ws) { + wss.emit('connection', ws, req); + }); + } + }) + } + botData.value.users = {}; nodecg.log.info('Bot has been started!') diff --git a/extension/index.js b/extension/index.js index 4cdcc7c..f173fc0 100644 --- a/extension/index.js +++ b/extension/index.js @@ -305,66 +305,134 @@ module.exports = (nodecg) => { } // Ad player. - function playAds(newVal) { + async function playAds(newVal) { nodecg.log.info('Ad requested on ' + Date() + '.') - let secondsLeft = 0; let transitionTime = 0; let videoSource = null; - obs.send('GetTransitionDuration').then(result => transitionTime = result['transition-duration']); - if (newVal.videoAds && newVal.twitchAds) { - videoSource = newVal.videoSources[Math.floor(Math.random() * newVal.videoSources.length)]; - secondsLeft = Math.ceil((videoSource.duration + newVal.twitchAdLength + (transitionTime * 2)) / 1000); - } - else if (newVal.videoAds) { - videoSource = newVal.videoSources[Math.floor(Math.random() * newVal.videoSources.length)]; - secondsLeft = Math.ceil((videoSource.duration + (transitionTime * 2)) / 1000); + let result = await obs.send('GetTransitionDuration'); + transitionTime = result['transition-duration']; + + adPlayer.value.adPlaying = true; + + // Set button timer. + switch (true) { + case (newVal.videoAds && newVal.twitchAds): videoSource = newVal.videoSources[Math.floor(Math.random() * newVal.videoSources.length)]; secondsLeft = Math.ceil((videoSource.duration + (transitionTime * 2)) / 1000) + parseFloat(newVal.twitchAdLength); break; + case (newVal.videoAds): secondsLeft = Math.ceil((videoSource.duration + (transitionTime * 2)) / 1000); break; + case (newVal.twitchAds): secondsLeft = parseFloat(newVal.twitchAdLength); break; } - else if (newVal.twitchAds) newVal.twitchAdLength; + + adPlayer.value.secondsLeft = secondsLeft; + secondsLeft--; const timerInterval = setInterval(() => { adPlayer.value.secondsLeft = secondsLeft; secondsLeft--; - if (secondsLeft < 0) { - clearInterval(timerInterval); - adPlayer.value.secondsLeft = 0; - adPlayer.value.adPlaying = false; - checklist.value.playAd = true; - } + if (secondsLeft < 0) clearInterval(timerInterval); }, 1000); - if (newVal.videoAds && newVal.videoScene !== null) { - playVideo(newVal); - setTimeout(() => { - if (newVal.twitchAds) playTwitch(newVal); - }, Math.ceil(videoSource.duration + transitionTime)) - } - else if (newVal.playTwitch) playTwitch(newVal) + if (newVal.videoAds) await playVideo(newVal); + if (newVal.twitchAds) await playTwitch(newVal) - function playVideo(newVal) { - let previewScene = currentScene.value.preview; - obs.send('SetCurrentScene', { "scene-name": newVal.videoScene }).then(() => { + try { clearInterval(timerInterval) } catch { }; + + adPlayer.value.secondsLeft = 0; + adPlayer.value.adPlaying = false; + checklist.value.playAd = true; + + async function playVideo(newVal) { + return new Promise(async (resolve) => { + let previewScene = currentScene.value.preview; + obs.send('SetCurrentScene', { "scene-name": newVal.videoScene }); obs.once('TransitionEnd', () => { obs.send('RestartMedia', { sourceName: videoSource.source }) - setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000) - setTimeout(() => { + obs.send('SetPreviewScene', { "scene-name": previewScene }) + setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 500); + setTimeout(async () => { + resolve(); previewScene = currentScene.value.preview; - obs.send('SetCurrentScene', { "scene-name": settings.value.intermissionScene }).then(() => { - obs.once('TransitionEnd', () => setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000)) - }) - }, videoSource.duration + 1000); + obs.send('SetCurrentScene', { "scene-name": settings.value.intermissionScene }) + obs.once('TransitionEnd', () => setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 500)) + }, videoSource.duration + 1000) }); }); } - function playTwitch(newVal) { - nodecg.sendMessageToBundle('twitchStartCommercial', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength, fromDashboard: false }) - .then(() => nodecg.sendMessageToBundle('twitchStartCommercialTimer', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength })) - .catch(() => { - nodecg.log.error('Error playing Twitch ads. Are you sure your channel is an affiliate or partner?') - clearInterval(timerInterval); - adPlayer.value.adPlaying = false; - checklist.value.playAd = true; - }); + async function playTwitch(newVal) { + return new Promise(async (resolve) => { + nodecg.sendMessageToBundle('twitchStartCommercial', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength, fromDashboard: false }) + nodecg.sendMessageToBundle('twitchStartCommercialTimer', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength }) + setTimeout(() => resolve(), newVal.twitchAdLength * 1000); + }) } + + + // let previewScene = currentScene.value.preview; + // obs.send('SetCurrentScene', { "scene-name": newVal.videoScene }).then(() => { + // obs.once('TransitionEnd', () => { + // obs.send('RestartMedia', { sourceName: videoSource.source }) + // setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000) + // setTimeout(() => { + // previewScene = currentScene.value.preview; + // obs.send('SetCurrentScene', { "scene-name": settings.value.intermissionScene }).then(() => { + // obs.once('TransitionEnd', () => setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000)) + // }) + // }, videoSource.duration + 1000); + // }); + // }); + // if (newVal.videoAds && newVal.twitchAds) { + // videoSource = newVal.videoSources[Math.floor(Math.random() * newVal.videoSources.length)]; + // secondsLeft = Math.ceil((videoSource.duration + newVal.twitchAdLength + (transitionTime * 2)) / 1000); + // } + // else if (newVal.videoAds) { + // videoSource = newVal.videoSources[Math.floor(Math.random() * newVal.videoSources.length)]; + // secondsLeft = Math.ceil((videoSource.duration + (transitionTime * 2)) / 1000); + // } + // else if (newVal.twitchAds) secondsLeft = newVal.twitchAdLength; + // const timerInterval = setInterval(() => { + // adPlayer.value.secondsLeft = secondsLeft; + // secondsLeft--; + // if (secondsLeft < 0) { + // clearInterval(timerInterval); + // adPlayer.value.secondsLeft = 0; + // adPlayer.value.adPlaying = false; + // checklist.value.playAd = true; + // } + // }, 1000); + + // if (newVal.videoAds && newVal.videoScene !== null) { + // playVideo(newVal); + // setTimeout(() => { + // if (newVal.twitchAds) playTwitch(newVal); + // }, Math.ceil(videoSource.duration + transitionTime)) + // } + // else if (newVal.playTwitch) playTwitch(newVal) + + // function playVideo(newVal) { + // let previewScene = currentScene.value.preview; + // obs.send('SetCurrentScene', { "scene-name": newVal.videoScene }).then(() => { + // obs.once('TransitionEnd', () => { + // obs.send('RestartMedia', { sourceName: videoSource.source }) + // setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000) + // setTimeout(() => { + // previewScene = currentScene.value.preview; + // obs.send('SetCurrentScene', { "scene-name": settings.value.intermissionScene }).then(() => { + // obs.once('TransitionEnd', () => setTimeout(() => obs.send('SetPreviewScene', { "scene-name": previewScene }), 1000)) + // }) + // }, videoSource.duration + 1000); + // }); + // }); + // } + + // function playTwitch(newVal) { + // console.log('playing Twitch ads') + // nodecg.sendMessageToBundle('twitchStartCommercial', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength, fromDashboard: false }) + // .then(() => nodecg.sendMessageToBundle('twitchStartCommercialTimer', 'nodecg-speedcontrol', { duration: newVal.twitchAdLength })) + // .catch(() => { + // nodecg.log.error('Error playing Twitch ads. Are you sure your channel is an affiliate or partner?') + // clearInterval(timerInterval); + // adPlayer.value.adPlaying = false; + // checklist.value.playAd = true; + // }); + // } } // Get ad player sources. diff --git a/graphics/discordAudio/discordAudio.html b/graphics/discordAudio/discordAudio.html index b30f609..9cd40f2 100644 --- a/graphics/discordAudio/discordAudio.html +++ b/graphics/discordAudio/discordAudio.html @@ -8,59 +8,94 @@ +