From bb671927820d6c6e06a74aed53bb27aea744abe1 Mon Sep 17 00:00:00 2001 From: zoton2 Date: Sun, 18 Feb 2024 21:10:51 +0100 Subject: [PATCH] Change donation alerts to only play alerts on the appropriate stream --- shared | 2 +- src/extension/tracker/index.ts | 42 +++++++++++++---- src/extension/util/rabbitmq.ts | 7 ++- src/graphics/omnibar/components/Total.vue | 55 ++++++++++++++++------- 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/shared b/shared index f468e7bf..c7e7d58e 160000 --- a/shared +++ b/shared @@ -1 +1 @@ -Subproject commit f468e7bf6263f70dc76f7946eaca4e8f0f415b33 +Subproject commit c7e7d58e8336a6f5d2e8a4fdde0a83cbb2ce7db0 diff --git a/src/extension/tracker/index.ts b/src/extension/tracker/index.ts index a78d96d2..e747dd78 100644 --- a/src/extension/tracker/index.ts +++ b/src/extension/tracker/index.ts @@ -75,8 +75,7 @@ async function updateDonationTotalFromAPITiltify(init = false): Promise { 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 @@ -110,23 +109,48 @@ async function updateDonationTotalFromAPITiltify(init = false): Promise { // 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. diff --git a/src/extension/util/rabbitmq.ts b/src/extension/util/rabbitmq.ts index cdf90f87..9eea066e 100644 --- a/src/extension/util/rabbitmq.ts +++ b/src/extension/util/rabbitmq.ts @@ -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', diff --git a/src/graphics/omnibar/components/Total.vue b/src/graphics/omnibar/components/Total.vue index 8ac77e90..55dc62ed 100644 --- a/src/graphics/omnibar/components/Total.vue +++ b/src/graphics/omnibar/components/Total.vue @@ -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; @@ -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 { 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); + }); } }