From 67b35f7ed06c511158c6e1a58da3b8fa23bdffaa Mon Sep 17 00:00:00 2001 From: alexbcberio Date: Thu, 6 Jan 2022 00:38:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20definitive=20VIP=20obte?= =?UTF-8?q?ntion=20and=20steal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/pubSubClient/actions/getVip.ts | 21 +++++++++++-- src/backend/pubSubClient/actions/stealVip.ts | 31 +++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/backend/pubSubClient/actions/getVip.ts b/src/backend/pubSubClient/actions/getVip.ts index b0cec71..c421feb 100644 --- a/src/backend/pubSubClient/actions/getVip.ts +++ b/src/backend/pubSubClient/actions/getVip.ts @@ -1,13 +1,17 @@ -import { addVip, hasVip } from "../../chatClient/clientActions"; +import { addVip, hasVip, removeVip } from "../../chatClient/clientActions"; +import { save, vipUsers } from "../../helpers/miniDb"; import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; +const MAX_VIPS = 3; + async function getVip( msg: RedemptionMessage ): Promise { - const channel = await getUsernameFromId(parseInt(msg.channelId)); + const channelId = parseInt(msg.channelId); + const channel = await getUsernameFromId(channelId); if (!channel) { console.log(`${LOG_PREFIX}No channel found`); @@ -23,8 +27,21 @@ async function getVip( return; } + const users = vipUsers[channelId]; + + if (users.length >= MAX_VIPS) { + const user = users.shift(); + + if (user) { + await removeVip(channel, user); + } + } + await addVip(channel, addVipUser); + users.push(addVipUser); + save(); + return msg; } diff --git a/src/backend/pubSubClient/actions/stealVip.ts b/src/backend/pubSubClient/actions/stealVip.ts index 3f0e8f2..9ecc8b8 100644 --- a/src/backend/pubSubClient/actions/stealVip.ts +++ b/src/backend/pubSubClient/actions/stealVip.ts @@ -1,4 +1,5 @@ -import { addVip, hasVip, removeVip } from "../../chatClient/clientActions"; +import { addVip, hasVip, removeVip, say } from "../../chatClient/clientActions"; +import { save, vipUsers } from "../../helpers/miniDb"; import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; @@ -14,7 +15,8 @@ async function stealVip( return; } - const channel = await getUsernameFromId(parseInt(msg.channelId)); + const channelId = parseInt(msg.channelId); + const channel = await getUsernameFromId(channelId); if (!channel) { console.log(`${LOG_PREFIX}No channel found`); @@ -23,15 +25,20 @@ async function stealVip( } const addVipUser = msg.userDisplayName; - const removeVipUser = msg.message; + const removeVipUser = msg.message.toLowerCase(); + const channelVips = vipUsers[channelId]; - if (!(await hasVip(channel, removeVipUser))) { - console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`); + if (!channelVips.find(u => u.toLowerCase() === removeVipUser)) { + const message = + channelVips.length === 0 + ? "No hay nadie a quien puedas robar el VIP" + : `Solo puedes robar el VIP de: "${channelVips.sort().join('", "')}"`; + await say(channel, message); return; } - if (await hasVip(channel, addVipUser)) { + if (channelVips.includes(addVipUser) || (await hasVip(channel, addVipUser))) { console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); return; @@ -39,7 +46,8 @@ async function stealVip( const removed = await removeVip(channel, removeVipUser); - if (!removed) { + if (!removed && (await hasVip(channel, removeVipUser))) { + console.log(`${LOG_PREFIX}Could not remove VIP of @${removeVipUser}`); return; } @@ -51,7 +59,16 @@ async function stealVip( return; } + const removeIdx = channelVips.findIndex( + u => u.toLowerCase() === removeVipUser + ); + + channelVips.splice(removeIdx); + channelVips.push(addVipUser); + save(); + msg.message = `@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`; + await say(channel, msg.message); return msg; }