From 607dfe32220b814975024271d629b6d06b5b576e Mon Sep 17 00:00:00 2001 From: Tycho Bellers Date: Wed, 10 Aug 2022 21:46:29 -0700 Subject: [PATCH] Respect broadcaster priority when choosing a campaign to work on (#172) --- src/twitch_drops_bot.ts | 26 ++++++++++++++++++++++++-- src/watchdog.ts | 2 -- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/twitch_drops_bot.ts b/src/twitch_drops_bot.ts index 53f4f69..2d4edba 100644 --- a/src/twitch_drops_bot.ts +++ b/src/twitch_drops_bot.ts @@ -271,8 +271,17 @@ export class TwitchDropsBot extends EventEmitter { } else if (indexA === indexB) { // Both games have the same priority. Give priority to the one that ends first. const endTimeA = Date.parse(campaignA.endAt); const endTimeB = Date.parse(campaignB.endAt); - if (endTimeA === endTimeB) { - return a < b ? -1 : 1; + if (endTimeA === endTimeB) { // Both campaigns end at the same time. Give priority to the one that is in the broadcasters list + const broadcasterIndexA = this.#getFirstBroadcasterIndex(campaignA); + const broadcasterIndexB = this.#getFirstBroadcasterIndex(campaignB); + if (broadcasterIndexA === -1 && broadcasterIndexB !== -1) { + return 1; + } else if (broadcasterIndexA !== -1 && broadcasterIndexB === -1) { + return -1; + } else if (broadcasterIndexA === broadcasterIndexB) { + return a < b ? -1 : 1; + } + return Math.sign(broadcasterIndexA - broadcasterIndexB); } return endTimeA < endTimeB ? -1 : 1; } @@ -549,6 +558,19 @@ export class TwitchDropsBot extends EventEmitter { }); } + #getFirstBroadcasterIndex(campaign: DropCampaign) { + for (let i = 0; i < this.#broadcasterIds.length; ++i) { + if (campaign.allow && campaign.allow.isEnabled) { + for (const channel of campaign.allow.channels) { + if (channel.displayName.toLowerCase() === this.#broadcasterIds[i].toLowerCase()) { + return i; + } + } + } + } + return -1; + } + #isCampaignCompleted(dropCampaignDetails: DropCampaign, inventory: Inventory): boolean { const timeBasedDrops = dropCampaignDetails.timeBasedDrops; if (timeBasedDrops != null) { diff --git a/src/watchdog.ts b/src/watchdog.ts index cc5b9c2..70408fe 100644 --- a/src/watchdog.ts +++ b/src/watchdog.ts @@ -1,5 +1,3 @@ -"use strict"; - import {EventEmitter} from "events"; import {Client, DropCampaign} from "./twitch.js";