Skip to content

Commit

Permalink
Implement Tiltify API for donation total, fix MQ messages not updatin…
Browse files Browse the repository at this point in the history
…g donation total
  • Loading branch information
zoton2 committed Feb 17, 2024
1 parent 209ba35 commit caf813d
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/extension/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,35 @@ async function updateDonationTotalFromAPI(init = false): Promise<void> {
}
}

async function updateDonationTotalFromAPITiltify(init = false): Promise<void> {
try {
let total = 0;
// TODO: REMOVE ESAW24 AND URL HARDCODING!
const resp = await needle('get', 'https://app.esamarathon.com/tiltify/campaigns/team/esaw2024');
if (resp.statusCode === 200) {
const eventTotal = resp.body.total_amount_raised.value
? parseFloat(resp.body.total_amount_raised.value)
: 0;
// event.total = eventTotal; // I hope this isn't important?
total += eventTotal;
}
if (init || donationTotal.value < total) {
nodecg().log.info('[Tracker] API donation total changed: $%s', total);
donationTotal.value = total;
}
} catch (err) {
nodecg().log.warn('[Tracker] Error updating donation total from API');
nodecg().log.debug('[Tracker] Error updating donation total from API:', err);
}
}

// 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;
for (const event of eventInfo) {
// HARDCODED FOR NOW!
if (data.event === 'esaw2024') {
event.total = data.new_total;
}
total += event.total;
// HARDCODED FOR NOW!
if (data.event === 'esaw2024') {
total += data.new_total;
}
if (donationTotal.value < total) {
nodecg().log.debug('[Tracker] Updated donation total received: $%s', total.toFixed(2));
Expand Down Expand Up @@ -195,4 +214,9 @@ async function setup(): Promise<void> {

if (config.enabled) {
setup();
} else {
// FOR TILTIFY USE!
// Get initial total from API and set an interval as a fallback.
updateDonationTotalFromAPITiltify(true);
setInterval(updateDonationTotalFromAPITiltify, 60 * 1000);
}

0 comments on commit caf813d

Please sign in to comment.