Skip to content

Commit

Permalink
Respect broadcaster priority when choosing a campaign to work on (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoTheTaco committed Aug 11, 2022
1 parent cf87627 commit 607dfe3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/twitch_drops_bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions src/watchdog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

import {EventEmitter} from "events";
import {Client, DropCampaign} from "./twitch.js";

Expand Down

0 comments on commit 607dfe3

Please sign in to comment.