From 14584932ffadc1c1a266f512e005ade0340694fd Mon Sep 17 00:00:00 2001 From: alexbcberio Date: Wed, 5 Jan 2022 21:53:58 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20handling=20of=20redemp?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/pubSubClient/actions/getVip.ts | 12 ++++--- src/backend/pubSubClient/actions/hidrate.ts | 9 ++--- .../pubSubClient/actions/highlightMessage.ts | 7 ++-- .../pubSubClient/actions/russianRoulette.ts | 16 +++++---- src/backend/pubSubClient/actions/stealVip.ts | 33 ++++++++++++------- .../pubSubClient/actions/timeoutFriend.ts | 12 ++++--- src/backend/pubSubClient/index.ts | 30 ++++++++--------- 7 files changed, 68 insertions(+), 51 deletions(-) diff --git a/src/backend/pubSubClient/actions/getVip.ts b/src/backend/pubSubClient/actions/getVip.ts index f457dd8..b0cec71 100644 --- a/src/backend/pubSubClient/actions/getVip.ts +++ b/src/backend/pubSubClient/actions/getVip.ts @@ -4,13 +4,15 @@ import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; -async function getVip(msg: RedemptionMessage): Promise { +async function getVip( + msg: RedemptionMessage +): Promise { const channel = await getUsernameFromId(parseInt(msg.channelId)); if (!channel) { console.log(`${LOG_PREFIX}No channel found`); - return false; + return; } const addVipUser = msg.userDisplayName; @@ -18,12 +20,12 @@ async function getVip(msg: RedemptionMessage): Promise { if (await hasVip(channel, addVipUser)) { console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); - return false; + return; } - const addedVip = await addVip(channel, addVipUser, msg.message); + await addVip(channel, addVipUser); - return addedVip; + return msg; } export { getVip }; diff --git a/src/backend/pubSubClient/actions/hidrate.ts b/src/backend/pubSubClient/actions/hidrate.ts index 0028f70..b1edce4 100644 --- a/src/backend/pubSubClient/actions/hidrate.ts +++ b/src/backend/pubSubClient/actions/hidrate.ts @@ -1,10 +1,11 @@ import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; -import { broadcast } from "../../helpers/webServer"; import { getUsernameFromId } from "../../helpers/twitch"; import { say } from "../../chatClient"; -async function hidrate(msg: RedemptionMessage): Promise { +async function hidrate( + msg: RedemptionMessage +): Promise { const channel = await getUsernameFromId(parseInt(msg.channelId)); if (!channel) { @@ -15,9 +16,9 @@ async function hidrate(msg: RedemptionMessage): Promise { msg.message = `@${msg.userDisplayName} ha invitado a una ronda`; - broadcast(JSON.stringify(msg)); - await say(channel, "waterGang waterGang waterGang"); + + return msg; } export { hidrate }; diff --git a/src/backend/pubSubClient/actions/highlightMessage.ts b/src/backend/pubSubClient/actions/highlightMessage.ts index 58dfab7..722ab21 100644 --- a/src/backend/pubSubClient/actions/highlightMessage.ts +++ b/src/backend/pubSubClient/actions/highlightMessage.ts @@ -1,10 +1,11 @@ import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; -import { broadcast } from "../../helpers/webServer"; import { getUsernameFromId } from "../../helpers/twitch"; import { timeout } from "../../chatClient/clientActions"; -async function highlightMessage(msg: RedemptionMessage): Promise { +async function highlightMessage( + msg: RedemptionMessage +): Promise { if (!msg.message) { console.log(`${LOG_PREFIX}Redemption has no message`); @@ -35,7 +36,7 @@ async function highlightMessage(msg: RedemptionMessage): Promise { return; } - broadcast(JSON.stringify(msg)); + return msg; } export { highlightMessage }; diff --git a/src/backend/pubSubClient/actions/russianRoulette.ts b/src/backend/pubSubClient/actions/russianRoulette.ts index a98a41d..b95fe84 100644 --- a/src/backend/pubSubClient/actions/russianRoulette.ts +++ b/src/backend/pubSubClient/actions/russianRoulette.ts @@ -9,14 +9,18 @@ import { randomInt } from "crypto"; type GunsSafeShots = Record; const gunsSafeShots: GunsSafeShots = {}; +const timeoutSeconds = 60; const maxSafeShots = 5; -async function russianRoulette(msg: RedemptionMessage): Promise { +async function russianRoulette( + msg: RedemptionMessage +): Promise { const { channelId, userDisplayName } = msg; const channel = await getUsernameFromId(parseInt(channelId)); if (!channel) { console.log(`${LOG_PREFIX}No channel found`); + return; } @@ -32,18 +36,16 @@ async function russianRoulette(msg: RedemptionMessage): Promise { gunsSafeShots[channelId] = maxSafeShots; } - if (win) { - msg.message = ""; - } else { - msg.message = "got shot"; - } + msg.message = win ? "" : "got shot"; broadcast(JSON.stringify(msg)); const promises: Array> = []; if (!win) { - promises.push(timeout(channel, userDisplayName, 60, "F en la ruleta")); + promises.push( + timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta") + ); promises.push( say( channel, diff --git a/src/backend/pubSubClient/actions/stealVip.ts b/src/backend/pubSubClient/actions/stealVip.ts index 868d0f5..3f0e8f2 100644 --- a/src/backend/pubSubClient/actions/stealVip.ts +++ b/src/backend/pubSubClient/actions/stealVip.ts @@ -5,11 +5,13 @@ import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; // remove vip from a user to grant it to yourself -async function stealVip(msg: RedemptionMessage): Promise { +async function stealVip( + msg: RedemptionMessage +): Promise { if (!msg.message) { console.log(`${LOG_PREFIX}Redemption has no message`); - return false; + return; } const channel = await getUsernameFromId(parseInt(msg.channelId)); @@ -17,7 +19,7 @@ async function stealVip(msg: RedemptionMessage): Promise { if (!channel) { console.log(`${LOG_PREFIX}No channel found`); - return false; + return; } const addVipUser = msg.userDisplayName; @@ -26,23 +28,32 @@ async function stealVip(msg: RedemptionMessage): Promise { if (!(await hasVip(channel, removeVipUser))) { console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`); - return false; + return; } if (await hasVip(channel, addVipUser)) { console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); - return false; + return; } const removed = await removeVip(channel, removeVipUser); - const added = await addVip( - channel, - addVipUser, - `@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}` - ); - return removed && added; + if (!removed) { + return; + } + + const added = await addVip(channel, addVipUser); + + if (!added) { + await addVip(channel, removeVipUser); + + return; + } + + msg.message = `@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`; + + return msg; } export { stealVip }; diff --git a/src/backend/pubSubClient/actions/timeoutFriend.ts b/src/backend/pubSubClient/actions/timeoutFriend.ts index 4cdaec4..9e5c702 100644 --- a/src/backend/pubSubClient/actions/timeoutFriend.ts +++ b/src/backend/pubSubClient/actions/timeoutFriend.ts @@ -4,12 +4,14 @@ import { broadcast } from "../../helpers/webServer"; import { getUsernameFromId } from "../../helpers/twitch"; import { timeout } from "../../chatClient/clientActions"; -async function timeoutFriend(msg: RedemptionMessage): Promise { +async function timeoutFriend( + msg: RedemptionMessage +): Promise { const { message, channelId, userDisplayName } = msg; if (!msg.message) { console.log(`${LOG_PREFIX}Redemption has no message`); - return false; + return; } const channel = await getUsernameFromId(parseInt(channelId)); @@ -17,7 +19,7 @@ async function timeoutFriend(msg: RedemptionMessage): Promise { if (!channel) { console.log(`${LOG_PREFIX}No channel found`); - return false; + return; } const time = 60; @@ -35,10 +37,10 @@ async function timeoutFriend(msg: RedemptionMessage): Promise { console.error(`${LOG_PREFIX} ${e.message}`); } - return false; + return; } - return true; + return msg; } export { timeoutFriend }; diff --git a/src/backend/pubSubClient/index.ts b/src/backend/pubSubClient/index.ts index b093ca3..7eb4150 100644 --- a/src/backend/pubSubClient/index.ts +++ b/src/backend/pubSubClient/index.ts @@ -46,39 +46,37 @@ async function onRedemption(message: PubSubRedemptionMessage) { backgroundColor: reward.background_color }; + let handledMessage: RedemptionMessage | undefined; + switch (msg.rewardId) { case RedemptionIds.RussianRoulette: - await russianRoulette(msg); + handledMessage = await russianRoulette(msg); break; case RedemptionIds.TimeoutFriend: - await timeoutFriend(msg); + handledMessage = await timeoutFriend(msg); break; case RedemptionIds.HighlightMessage: - await highlightMessage(msg); + handledMessage = await highlightMessage(msg); break; case RedemptionIds.GetVip: - msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`; - - if (await getVip(msg)) { - broadcast(JSON.stringify(msg)); - } + handledMessage = await getVip(msg); break; case RedemptionIds.StealVip: - if (await stealVip(msg)) { - msg.message = `@${msg.userDisplayName} ha "tomado prestado" el VIP de @${msg.message}`; - - broadcast(JSON.stringify(msg)); - } + handledMessage = await stealVip(msg); break; case RedemptionIds.Hidrate: - await hidrate(msg); + handledMessage = await hidrate(msg); break; default: - console.log(LOG_PREFIX, msg); + console.log(`${LOG_PREFIX}Unhandled redemption ${msg.rewardId}`); - broadcast(JSON.stringify(msg)); + handledMessage = msg; break; } + + if (handledMessage) { + broadcast(JSON.stringify(handledMessage)); + } } export { registerUserListener, LOG_PREFIX };