Skip to content

Commit

Permalink
Add Twitch commercial stuff for Companion
Browse files Browse the repository at this point in the history
  • Loading branch information
zoton2 committed Feb 3, 2024
1 parent 93a0659 commit 6103181
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions companion-plugin/companion-module-esa/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
17 changes: 17 additions & 0 deletions companion-plugin/companion-module-esa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ModuleInstance extends InstanceBase<Config> {
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({
Expand All @@ -94,6 +95,22 @@ class ModuleInstance extends InstanceBase<Config> {
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,
});
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions companion-plugin/companion-module-esa/src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ function initVariables(instance: InstanceBase<Config>) {
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;
18 changes: 18 additions & 0 deletions src/extension/companion.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>('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.
Expand Down Expand Up @@ -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');
}
}
});

0 comments on commit 6103181

Please sign in to comment.