Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: streamelements sub bomb detection #709

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions samples/streamelements-events/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,52 @@ module.exports = function (nodecg: NodeCG) {
});

client.onSubscriber((data) => {
nodecg.log.info(`${data.data.displayName} just subscribed for ${data.data.amount} months (${formatSubTier(data.data.tier)}).`);
nodecg.log.info(
`${data.data.displayName} just subscribed for ${data.data.amount} months (${formatSubTier(
data.data.tier,
)}).`,
);
});

client.onTestSubscriber((data) => {
nodecg.log.info(`${data.event.displayName} just subscribed for ${data.event.amount} months (${formatSubTier(data.event.tier)}).`);
})
nodecg.log.info(
`${data.event.displayName} just subscribed for ${data.event.amount} months (${formatSubTier(
data.event.tier,
)}).`,
);
});

client.onSubscriberBomb((data) => {
nodecg.log.info(`${data.gifterUsername} just gifted ${data.subscribers.length} subs.`);
});

client.onTestSubscriberBomb((data) => {
nodecg.log.info(`${data.gifterUsername} just gifted ${data.subscribers.length} subs.`);
});

client.onGift((data) => {
nodecg.log.info(
`${data.data.displayName} just got a tier ${formatSubTier(data.data.tier)} subscription from ${data.data.sender ?? "anonymous"}! It's ${data.data.displayName}'s ${data.data.amount} month.`,
`${data.data.displayName} just got a tier ${formatSubTier(data.data.tier)} subscription from ${
data.data.sender ?? "anonymous"
}! It's ${data.data.displayName}'s ${data.data.amount} month.`,
);
});

client.onTestGift((data) => {
nodecg.log.info(
`${data.event.displayName} just got a tier ${formatSubTier(data.event.tier)} subscription from ${data.event.sender ?? "anonymous"}! It's ${data.event.displayName}'s ${data.event.amount} month.`,
`${data.event.displayName} just got a tier ${formatSubTier(data.event.tier)} subscription from ${
data.event.sender ?? "anonymous"
}! It's ${data.event.displayName}'s ${data.event.amount} month.`,
);
})
});

client.onHost((data) => {
nodecg.log.info(`${data.data.displayName} just hosted the stream for ${data.data.amount} viewer(s).`);
});

client.onTestHost((data) => {
nodecg.log.info(`${data.event.displayName} just hosted the stream for ${data.event.amount} viewer(s).`);
})
});

client.onRaid((data) => {
nodecg.log.info(`${data.data.displayName} just raided the stream with ${data.data.amount} viewers.`);
Expand Down Expand Up @@ -92,8 +112,7 @@ module.exports = function (nodecg: NodeCG) {
};

function formatSubTier(tier: "1000" | "2000" | "3000" | "prime"): string {
if (tier === "prime")
return "Twitch Prime";
if (tier === "prime") return "Twitch Prime";

// We want to display the tier as 1, 2, 3
// However StreamElements stores the sub tiers as 1000, 2000 and 3000.
Expand Down
30 changes: 19 additions & 11 deletions samples/streamelements-events/graphics/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,55 @@ <h1>streamelements-events sample bundle</h1>
<p>
Last subscriber:
<span v-if="streamElementsReplicant?.lastSubscriber">
{{ streamElementsReplicant?.lastSubscriber?.data.displayName }} subscribed for {{
streamElementsReplicant?.lastSubscriber?.data.amount }} months ({{ subTier }}).
{{ streamElementsReplicant?.lastSubscriber?.data.displayName }} subscribed for
{{ streamElementsReplicant?.lastSubscriber?.data.amount }} months ({{ subTier }}).
</span>
<span v-else>none</span>
</p>
<p>
Last subbomb:
<span v-if="streamElementsReplicant?.lastSubBomb">
{{ streamElementsReplicant?.lastSubBomb.gifterUsername }} gifted
{{ streamElementsReplicant?.subscribers.subs.length }}.
</span>
<span v-else>none</span>
</p>
<p>
Last tip:
<span v-if="streamElementsReplicant?.lastTip">
{{ streamElementsReplicant?.lastSubscriber?.data.displayName }} subscribed for {{
streamElementsReplicant?.lastSubscriber?.data.amount }} months.
{{ streamElementsReplicant?.lastSubscriber?.data.displayName }} subscribed for
{{ streamElementsReplicant?.lastSubscriber?.data.amount }} months.
</span>
<span v-else>none</span>
</p>
<p>
Last cheer:
<span v-if="streamElementsReplicant?.lastCheer">
{{ streamElementsReplicant?.lastCheer?.data.amount }} bits by {{
streamElementsReplicant?.lastCheer?.data.displayName }}
{{ streamElementsReplicant?.lastCheer?.data.amount }} bits by
{{ streamElementsReplicant?.lastCheer?.data.displayName }}.
</span>
<span v-else>none</span>
</p>
<p>
Last follow:
<span v-if="streamElementsReplicant?.lastFollow">
{{ streamElementsReplicant?.lastFollow?.data.displayName }}
{{ streamElementsReplicant?.lastFollow?.data.displayName }}.
</span>
<span v-else>none</span>
</p>
<p>
Last raid:
<span v-if="streamElementsReplicant?.lastRaid">
{{ streamElementsReplicant?.lastRaid?.data.displayName }} raided with {{
streamElementsReplicant?.lastRaid?.data.amount }} viewers
{{ streamElementsReplicant?.lastRaid?.data.displayName }} raided with
{{ streamElementsReplicant?.lastRaid?.data.amount }} viewers.
</span>
<span v-else>none</span>
</p>
<p>
Last host:
<span v-if="streamElementsReplicant?.lastHost">
{{ streamElementsReplicant?.lastHost?.data.displayName }} hosted with {{
streamElementsReplicant?.lastHost?.data.viewers }} viewers
{{ streamElementsReplicant?.lastHost?.data.displayName }} hosted with
{{ streamElementsReplicant?.lastHost?.data.viewers }} viewers.
</span>
<span v-else>none</span>
</p>
Expand Down
124 changes: 102 additions & 22 deletions services/nodecg-io-streamelements/extension/StreamElements.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import io = require("socket.io-client");
import { Result, emptySuccess, error } from "nodecg-io-core";
import {
StreamElementsCheerEvent, StreamElementsEvent,
StreamElementsCheerEvent,
StreamElementsEvent,
StreamElementsFollowEvent,
StreamElementsHostEvent,
StreamElementsRaidEvent,
StreamElementsSubBombEvent,
StreamElementsSubscriberEvent,
StreamElementsTestCheerEvent, StreamElementsTestEvent,
StreamElementsTestCheerEvent,
StreamElementsTestEvent,
StreamElementsTestFollowEvent,
StreamElementsTestHostEvent, StreamElementsTestRaidEvent,
StreamElementsTestSubscriberEvent, StreamElementsTestTipEvent,
StreamElementsTipEvent
StreamElementsTestHostEvent,
StreamElementsTestRaidEvent,
StreamElementsTestSubscriberEvent,
StreamElementsTestTipEvent,
StreamElementsTipEvent,
} from "./StreamElementsEvent";
import { EventEmitter } from "events";
import { Replicant } from "nodecg-types/types/server";

export interface StreamElementsReplicant {
lastSubscriber?: StreamElementsSubscriberEvent;
lastSubBomb?: StreamElementsSubBombEvent<StreamElementsSubscriberEvent>;
lastTip?: StreamElementsTipEvent;
lastCheer?: StreamElementsCheerEvent;
lastGift?: StreamElementsSubscriberEvent;
Expand All @@ -25,8 +31,17 @@ export interface StreamElementsReplicant {
lastHost?: StreamElementsHostEvent;
}

/**
* Internal utility interface for tracking sub-bombs.
*/
interface SubBomb {
timeout: NodeJS.Timeout;
subs: Array<StreamElementsSubscriberEvent | StreamElementsTestSubscriberEvent>;
}

export class StreamElementsServiceClient extends EventEmitter {
private socket: SocketIOClient.Socket;
private subBombDetectionMap: Map<string, SubBomb> = new Map();

constructor(private jwtToken: string, private handleTestEvents: boolean) {
super();
Expand All @@ -47,21 +62,73 @@ export class StreamElementsServiceClient extends EventEmitter {
this.onEvent((data: StreamElementsEvent) => {
if (data.type === "subscriber") {
if (data.data.gifted) {
this.emit("gift", data);
this.handleSubGift(
data.data.sender,
data,
(subBomb) => this.emit("subbomb", subBomb),
(gift) => this.emit("gift", gift),
);
}
}
this.emit(data.type, data);
});

if (this.handleTestEvents) {
this.onTestEvent((data: StreamElementsTestEvent) => {
if (data.listener) {
this.emit("test", data);
this.emit("test:" + data.listener, data);
}
});

this.onTestSubscriber((data) => {
if (data.event.gifted) {
this.handleSubGift(
data.event.sender,
data,
(subBomb) => this.emit("test:subbomb", subBomb),
(gift) => this.emit("test:gift", gift),
);
}
});
}
}

private handleSubGift<T extends StreamElementsSubscriberEvent | StreamElementsTestSubscriberEvent>(
subGifter: string | undefined,
gift: T,
handlerSubBomb: (data: StreamElementsSubBombEvent<T>) => void,
handlerGift: (data: T) => void,
) {
const gifter = subGifter ?? "anonymous";

const subBomb = this.subBombDetectionMap.get(gifter) ?? {
subs: [],
timeout: setTimeout(() => {
this.subBombDetectionMap.delete(gifter);

// Only fire sub bomb event if more than one sub were gifted.
// Otherwise, this is just a single gifted sub.
if (subBomb.subs.length > 1) {
const subBombEvent = {
gifterUsername: gifter,
subscribers: subBomb.subs as T[],
};
handlerSubBomb(subBombEvent);
}

subBomb.subs.forEach(handlerGift);
}, 1000),
};

subBomb.subs.push(gift);

// New subs in this sub bomb. Refresh timeout in case another one follows.
subBomb.timeout.refresh();

this.subBombDetectionMap.set(gifter, subBomb);
}

async connect(): Promise<void> {
return new Promise((resolve, _reject) => {
this.createSocket();
Expand Down Expand Up @@ -123,8 +190,15 @@ export class StreamElementsServiceClient extends EventEmitter {
});
}

public onSubscriber(handler: (data: StreamElementsSubscriberEvent) => void): void {
this.on("subscriber", handler);
public onSubscriber(handler: (data: StreamElementsSubscriberEvent) => void, includeSubGifts = true): void {
this.on("subscriber", (data) => {
if (data.data.gifted && !includeSubGifts) return;
handler(data);
});
}

public onSubscriberBomb(handler: (data: StreamElementsSubBombEvent<StreamElementsSubscriberEvent>) => void): void {
this.on("subbomb", handler);
}

public onTip(handler: (data: StreamElementsTipEvent) => void): void {
Expand Down Expand Up @@ -155,16 +229,21 @@ export class StreamElementsServiceClient extends EventEmitter {
this.on("test", handler);
}

public onTestSubscriber(handler: (data: StreamElementsTestSubscriberEvent) => void): void {
this.on("test:subscriber-latest", handler);
public onTestSubscriber(handler: (data: StreamElementsTestSubscriberEvent) => void, includeSubGifts = true): void {
this.on("test:subscriber-latest", (data) => {
if (data.event.gifted && !includeSubGifts) return;
handler(data);
});
}

public onTestSubscriberBomb(
handler: (data: StreamElementsSubBombEvent<StreamElementsTestSubscriberEvent>) => void,
): void {
this.on("test:subbomb", handler);
}

public onTestGift(handler: (data: StreamElementsTestSubscriberEvent) => void): void {
this.on("test:subscriber-latest", d => {
if(d.data.gifted) {
handler(d);
}
});
this.on("test:gift", handler);
}

public onTestCheer(handler: (data: StreamElementsTestCheerEvent) => void): void {
Expand Down Expand Up @@ -192,12 +271,13 @@ export class StreamElementsServiceClient extends EventEmitter {
rep.value = {};
}

this.on("subscriber", (data) => (rep.value.lastSubscriber = data));
this.on("tip", (data) => (rep.value.lastTip = data));
this.on("cheer", (data) => (rep.value.lastCheer = data));
this.on("gift", (data) => (rep.value.lastGift = data));
this.on("follow", (data) => (rep.value.lastFollow = data));
this.on("raid", (data) => (rep.value.lastRaid = data));
this.on("host", (data) => (rep.value.lastHost = data));
this.onSubscriber((data) => (rep.value.lastSubscriber = data));
this.onSubscriberBomb((data) => (rep.value.lastSubBomb = data));
this.onTip((data) => (rep.value.lastTip = data));
this.onCheer((data) => (rep.value.lastCheer = data));
this.onGift((data) => (rep.value.lastGift = data));
this.onFollow((data) => (rep.value.lastFollow = data));
this.onRaid((data) => (rep.value.lastRaid = data));
this.onHost((data) => (rep.value.lastHost = data));
}
}
Loading