Skip to content

Commit

Permalink
Change donation alerts to only play alerts on the appropriate stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zoton2 committed Feb 18, 2024
1 parent 4320feb commit bb67192
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion shared
Submodule shared updated 1 files
+2 −1 types/RabbitMQ.d.ts
42 changes: 33 additions & 9 deletions src/extension/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ async function updateDonationTotalFromAPITiltify(init = false): Promise<void> {
total += eventTotal;
}
if (init || donationTotal.value < total) {
const diff = total - donationTotal.value;
nodecg().sendMessage('donationTotalUpdated', { total, diff, showAlert: false });
nodecg().sendMessage('donationTotalUpdated', { total });
nodecg().log.info('[Tracker] API donation total changed: $%s', total);
donationTotal.value = total;
// If we checked the donation total on an interval and it was different, the MQ
Expand Down Expand Up @@ -110,23 +109,48 @@ async function updateDonationTotalFromAPITiltify(init = false): Promise<void> {
// Triggered when a donation total is updated in our tracker.
// THIS WORKS EVEN IF TRACKER CONFIG IS DISABLED! WHICH IS GOOD FOR TILTIFY!
mq.evt.on('donationTotalUpdated', (data) => {
let total = 0;
// HARDCODED FOR NOW!
if (data.event === 'esaw2024') {
clearInterval(tiltifyApiBackupTimeout);
total += data.new_total;
tiltifyApiBackupTimeout = setTimeout(
updateDonationTotalFromAPITiltify,
tiltifyApiBackupLength,
);
if (donationTotal.value < data.new_total) {
nodecg().sendMessage('donationTotalUpdated', { total: data.new_total });
nodecg().log.debug(
'[Tracker] Updated donation total received: $%s',
data.new_total.toFixed(2),
);
donationTotal.value = data.new_total;
}
}
if (donationTotal.value < total) {
const diff = total - donationTotal.value;
nodecg().sendMessage('donationTotalUpdated', { total, diff, showAlert: true });
nodecg().log.debug('[Tracker] Updated donation total received: $%s', total.toFixed(2));
donationTotal.value = total;
});

const seenDonationIds: number[] = [];
// Fully processed donations for donations targeted towards this stream.
mq.evt.on('donationFullyProcessedStream', (data) => {
// eslint-disable-next-line no-underscore-dangle
const id = data._id;
if (!seenDonationIds.includes(id)) {
seenDonationIds.push(id);
nodecg().log.debug('[Tracker] Received new donation with ID %s', id);
nodecg().sendMessage('newDonation', { amount: data.amount });
}
});
// Fully processed donations for donations targeted towards the main campaign.
// We only listen for this on stream 1.
if (eventConfig.thisEvent === 1) {
mq.evt.on('donationFullyProcessedTeam', (data) => {
// eslint-disable-next-line no-underscore-dangle
const id = data._id;
if (!seenDonationIds.includes(id)) {
seenDonationIds.push(id);
nodecg().log.debug('[Tracker] Received new donation with ID %s', id);
nodecg().sendMessage('newDonation', { amount: data.amount });
}
});
}

// DISABLED FOR NOW (ESAW24)
// Triggered when a new donation is fully processed on the tracker.
Expand Down
7 changes: 6 additions & 1 deletion src/extension/util/rabbitmq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ export const mq = new RabbitMQ(nodecg(), useTestData, {
key: '*.donation_total.updated',
},
{
name: 'donationFullyProcessed',
name: 'donationFullyProcessedStream',
exchange: 'tracker',
key: `${event}.donation.*.fully_processed`,
},
{
name: 'donationFullyProcessedTeam',
exchange: 'tracker',
key: 'esaw2024.donation.*.fully_processed', // HARDCODED! (ESAW24)
},
{
name: 'newScreenedTweet',
exchange: 'moderation',
Expand Down
55 changes: 38 additions & 17 deletions src/graphics/omnibar/components/Total.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export default class extends Vue {
playingAlerts = false;
showAlert = false;
alertText = '$0';
alertList: { total: number, amount: number, showAlert: boolean }[] = [];
alertList: { total?: number, amount?: number, showAlert: boolean }[] = [];
donationTotalTimeout: number | undefined;
get rawTotal(): number {
return replicantModule.repsTyped.donationTotal;
Expand All @@ -166,37 +167,57 @@ export default class extends Vue {
this.playingAlerts = true;
if (!start) await new Promise((res) => { setTimeout(res, 500); });
// Only show alerts for positive values and if the alert should be "shown".
if (this.alertList[0].amount > 0 && this.alertList[0].showAlert) {
nodecg.sendMessage('omnibarPlaySound', { amount: this.alertList[0].amount });
const { amount, total, showAlert } = this.alertList[0];
if (amount && amount > 0 && showAlert) {
nodecg.sendMessage('omnibarPlaySound', { amount });
// await this.sfx.play();
await new Promise((res) => { setTimeout(res, 500); });
this.showAlert = true;
this.alertText = formatUSD(this.alertList[0].amount);
this.alertText = formatUSD(amount);
}
gsap.to(this, {
total: this.alertList[0].total,
total: total ?? (this.total + (amount ?? 0)),
duration: 5,
});
await new Promise((res) => { setTimeout(res, 6000); });
this.alertList.shift();
this.showAlert = false;
if (this.alertList.length) this.playNextAlert();
else this.playingAlerts = false;
// Checks the currently set total against the raw replicant total.
// If they don't line up, just queue up another "alert" to adjust it.
else if (this.total !== this.rawTotal) {
this.alertList.push({
total: this.rawTotal,
showAlert: false,
});
} else {
this.playingAlerts = false;
}
}
async created(): Promise<void> {
this.total = this.rawTotal;
nodecg.listenFor(
'donationTotalUpdated',
(data: { total: number, diff: number, showAlert: boolean }) => {
this.alertList.push({
total: data.total,
amount: data.diff,
showAlert: data.showAlert,
});
if (!this.playingAlerts) this.playNextAlert(true);
},
);
nodecg.listenFor('donationTotalUpdated', (data: { total: number }) => {
// If after 10s this hasn't been cleared by a new donation, update the total with it.
this.donationTotalTimeout = window.setTimeout(() => {
// Double check if the total really needs updating.
if (data.total !== this.total) {
this.alertList.push({
total: data.total,
showAlert: false,
});
}
}, 10 * 1000);
if (!this.playingAlerts) this.playNextAlert(true);
});
nodecg.listenFor('newDonation', (data: { amount: number }) => {
clearTimeout(this.donationTotalTimeout);
this.alertList.push({
amount: data.amount,
showAlert: true,
});
if (!this.playingAlerts) this.playNextAlert(true);
});
}
}
</script>
Expand Down

0 comments on commit bb67192

Please sign in to comment.