Skip to content

Commit 607dfe3

Browse files
committed
Respect broadcaster priority when choosing a campaign to work on (#172)
1 parent cf87627 commit 607dfe3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/twitch_drops_bot.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,17 @@ export class TwitchDropsBot extends EventEmitter {
271271
} else if (indexA === indexB) { // Both games have the same priority. Give priority to the one that ends first.
272272
const endTimeA = Date.parse(campaignA.endAt);
273273
const endTimeB = Date.parse(campaignB.endAt);
274-
if (endTimeA === endTimeB) {
275-
return a < b ? -1 : 1;
274+
if (endTimeA === endTimeB) { // Both campaigns end at the same time. Give priority to the one that is in the broadcasters list
275+
const broadcasterIndexA = this.#getFirstBroadcasterIndex(campaignA);
276+
const broadcasterIndexB = this.#getFirstBroadcasterIndex(campaignB);
277+
if (broadcasterIndexA === -1 && broadcasterIndexB !== -1) {
278+
return 1;
279+
} else if (broadcasterIndexA !== -1 && broadcasterIndexB === -1) {
280+
return -1;
281+
} else if (broadcasterIndexA === broadcasterIndexB) {
282+
return a < b ? -1 : 1;
283+
}
284+
return Math.sign(broadcasterIndexA - broadcasterIndexB);
276285
}
277286
return endTimeA < endTimeB ? -1 : 1;
278287
}
@@ -549,6 +558,19 @@ export class TwitchDropsBot extends EventEmitter {
549558
});
550559
}
551560

561+
#getFirstBroadcasterIndex(campaign: DropCampaign) {
562+
for (let i = 0; i < this.#broadcasterIds.length; ++i) {
563+
if (campaign.allow && campaign.allow.isEnabled) {
564+
for (const channel of campaign.allow.channels) {
565+
if (channel.displayName.toLowerCase() === this.#broadcasterIds[i].toLowerCase()) {
566+
return i;
567+
}
568+
}
569+
}
570+
}
571+
return -1;
572+
}
573+
552574
#isCampaignCompleted(dropCampaignDetails: DropCampaign, inventory: Inventory): boolean {
553575
const timeBasedDrops = dropCampaignDetails.timeBasedDrops;
554576
if (timeBasedDrops != null) {

src/watchdog.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
import {EventEmitter} from "events";
42
import {Client, DropCampaign} from "./twitch.js";
53

0 commit comments

Comments
 (0)