From 61031814c549ee9887dd3132ce7fdb8731bbaf7b Mon Sep 17 00:00:00 2001 From: zoton2 Date: Sat, 3 Feb 2024 15:50:53 +0000 Subject: [PATCH] Add Twitch commercial stuff for Companion --- .../companion-module-esa/src/actions.ts | 9 +++++++++ .../companion-module-esa/src/index.ts | 17 +++++++++++++++++ .../companion-module-esa/src/variables.ts | 8 ++++++++ src/extension/companion.ts | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/companion-plugin/companion-module-esa/src/actions.ts b/companion-plugin/companion-module-esa/src/actions.ts index 7bffad52..bac22e48 100644 --- a/companion-plugin/companion-module-esa/src/actions.ts +++ b/companion-plugin/companion-module-esa/src/actions.ts @@ -53,6 +53,15 @@ function initActions(instance: ModuleInstance) { instance.wsSend({ name: 'player_hud_trigger_toggle', value: action.options.type }); }, }, + twitch_commercials_disable: { + name: 'Twitch Commercials Disable (esa-commercials)', + description: 'Disables Twitch commercials for the remainder of a run, if applicable.', + options: [], + callback: () => { + // We don't do any checks here in regards to if action is valid. + instance.wsSend({ name: 'twitch_commercials_disable' }); + }, + }, }); } export default initActions; diff --git a/companion-plugin/companion-module-esa/src/index.ts b/companion-plugin/companion-module-esa/src/index.ts index 3db0eaee..4a31c624 100644 --- a/companion-plugin/companion-module-esa/src/index.ts +++ b/companion-plugin/companion-module-esa/src/index.ts @@ -74,6 +74,7 @@ class ModuleInstance extends InstanceBase { const timer = msg.value as { time: string; state: 'stopped' | 'running' | 'paused' | 'finished'; + // Other unimportant (at the moment) types omitted. }; // TODO: Setup debounce to not hammer the function. this.setVariableValues({ @@ -94,6 +95,22 @@ class ModuleInstance extends InstanceBase { this.setVariableValues({ player_hud_trigger_type: value.playerHUDTriggerType, }); + } else if (msg.name === 'twitchCommercialTimer') { + // TODO: Reference type from another location? + const value = msg.value as { + secondsRemaining: number; + originalDuration: number; + timestamp: number; + }; + this.setVariableValues({ + twitch_commercial_timer_seconds_remaining: value.secondsRemaining, + }); + } else if (msg.name === 'twitchCommercialsDisabled') { + // TODO: Reference type from another location? + const value = msg.value as boolean; + this.setVariableValues({ + twitch_commercials_disabled: value, + }); } }); } diff --git a/companion-plugin/companion-module-esa/src/variables.ts b/companion-plugin/companion-module-esa/src/variables.ts index 3c89aade..0c677a7f 100644 --- a/companion-plugin/companion-module-esa/src/variables.ts +++ b/companion-plugin/companion-module-esa/src/variables.ts @@ -23,6 +23,14 @@ function initVariables(instance: InstanceBase) { variableId: 'player_hud_trigger_type', name: 'Player HUD Trigger Type', }, + { + variableId: 'twitch_commercial_timer_seconds_remaining', + name: 'Twitch Commercial Timer (nodecg-speedcontrol): Seconds Remaining', + }, + { + variableId: 'twitch_commercials_disabled', + name: 'Twitch Commercials Disabled (esa-commercials)', + }, ]); } export default initVariables; diff --git a/src/extension/companion.ts b/src/extension/companion.ts index 7ca7607c..60a5298a 100644 --- a/src/extension/companion.ts +++ b/src/extension/companion.ts @@ -1,18 +1,28 @@ import companion from './util/companion'; +import { get as nodecg } from './util/nodecg'; import { streamDeckData } from './util/replicants'; import { sc } from './util/speedcontrol'; +// Replicants only applicable to this file from another bundle. +const twitchCommercialsDisabled = nodecg().Replicant('disabled', 'esa-commercials'); + // Sending replicant data on any changes. sc.timer.on('change', (value) => companion.send({ name: 'timer', value })); sc.timerChangesDisabled.on('change', (value) => ( companion.send({ name: 'timerChangesDisabled', value }))); streamDeckData.on('change', (value) => companion.send({ name: 'streamDeckData', value })); +sc.twitchCommercialTimer.on('change', (value) => ( + companion.send({ name: 'twitchCommercialTimer', value }))); +twitchCommercialsDisabled.on('change', (value) => ( + companion.send({ name: 'twitchCommercialsDisabled', value }))); // Sending things on connection. companion.evt.on('open', (socket) => { companion.send({ name: 'timer', value: sc.timer.value }, socket); companion.send({ name: 'timerChangesDisabled', value: sc.timerChangesDisabled.value }, socket); companion.send({ name: 'streamDeckData', value: streamDeckData.value }); + companion.send({ name: 'twitchCommercialTimer', value: sc.twitchCommercialTimer.value }); + companion.send({ name: 'twitchCommercialsDisabled', value: twitchCommercialsDisabled.value }); }); // Listening for any actions triggered from Companion. @@ -50,5 +60,13 @@ companion.evt.on('action', async (name, value) => { } else { streamDeckData.value.playerHUDTriggerType = val; } + // Used to disable the Twitch commercials for the remainder of a run. + } else if (name === 'twitch_commercials_disable') { + if (!twitchCommercialsDisabled.value + && !['stopped', 'finished'].includes(sc.timer.value.state)) { + // Sends a message to the esa-commercials bundle. + // Because we are using server-to-server messages, no confirmation yet. + nodecg().sendMessageToBundle('disable', 'esa-commercials'); + } } });