1
0

🎨 Improve handling of redemptions

This commit is contained in:
2022-01-05 21:53:58 +01:00
parent 5d20cce62f
commit 14584932ff
7 changed files with 68 additions and 51 deletions

View File

@@ -4,13 +4,15 @@ import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
async function getVip(msg: RedemptionMessage): Promise<boolean> { async function getVip(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
const channel = await getUsernameFromId(parseInt(msg.channelId)); const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); console.log(`${LOG_PREFIX}No channel found`);
return false; return;
} }
const addVipUser = msg.userDisplayName; const addVipUser = msg.userDisplayName;
@@ -18,12 +20,12 @@ async function getVip(msg: RedemptionMessage): Promise<boolean> {
if (await hasVip(channel, addVipUser)) { if (await hasVip(channel, addVipUser)) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); 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 }; export { getVip };

View File

@@ -1,10 +1,11 @@
import { LOG_PREFIX } from ".."; import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { broadcast } from "../../helpers/webServer";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { say } from "../../chatClient"; import { say } from "../../chatClient";
async function hidrate(msg: RedemptionMessage): Promise<void> { async function hidrate(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
const channel = await getUsernameFromId(parseInt(msg.channelId)); const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) { if (!channel) {
@@ -15,9 +16,9 @@ async function hidrate(msg: RedemptionMessage): Promise<void> {
msg.message = `@${msg.userDisplayName} ha invitado a una ronda`; msg.message = `@${msg.userDisplayName} ha invitado a una ronda`;
broadcast(JSON.stringify(msg));
await say(channel, "waterGang waterGang waterGang"); await say(channel, "waterGang waterGang waterGang");
return msg;
} }
export { hidrate }; export { hidrate };

View File

@@ -1,10 +1,11 @@
import { LOG_PREFIX } from ".."; import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { broadcast } from "../../helpers/webServer";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { timeout } from "../../chatClient/clientActions"; import { timeout } from "../../chatClient/clientActions";
async function highlightMessage(msg: RedemptionMessage): Promise<void> { async function highlightMessage(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); console.log(`${LOG_PREFIX}Redemption has no message`);
@@ -35,7 +36,7 @@ async function highlightMessage(msg: RedemptionMessage): Promise<void> {
return; return;
} }
broadcast(JSON.stringify(msg)); return msg;
} }
export { highlightMessage }; export { highlightMessage };

View File

@@ -9,14 +9,18 @@ import { randomInt } from "crypto";
type GunsSafeShots = Record<string, number>; type GunsSafeShots = Record<string, number>;
const gunsSafeShots: GunsSafeShots = {}; const gunsSafeShots: GunsSafeShots = {};
const timeoutSeconds = 60;
const maxSafeShots = 5; const maxSafeShots = 5;
async function russianRoulette(msg: RedemptionMessage): Promise<void> { async function russianRoulette(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
const { channelId, userDisplayName } = msg; const { channelId, userDisplayName } = msg;
const channel = await getUsernameFromId(parseInt(channelId)); const channel = await getUsernameFromId(parseInt(channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); console.log(`${LOG_PREFIX}No channel found`);
return; return;
} }
@@ -32,18 +36,16 @@ async function russianRoulette(msg: RedemptionMessage): Promise<void> {
gunsSafeShots[channelId] = maxSafeShots; gunsSafeShots[channelId] = maxSafeShots;
} }
if (win) { msg.message = win ? "" : "got shot";
msg.message = "";
} else {
msg.message = "got shot";
}
broadcast(JSON.stringify(msg)); broadcast(JSON.stringify(msg));
const promises: Array<Promise<unknown>> = []; const promises: Array<Promise<unknown>> = [];
if (!win) { if (!win) {
promises.push(timeout(channel, userDisplayName, 60, "F en la ruleta")); promises.push(
timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta")
);
promises.push( promises.push(
say( say(
channel, channel,

View File

@@ -5,11 +5,13 @@ import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
// remove vip from a user to grant it to yourself // remove vip from a user to grant it to yourself
async function stealVip(msg: RedemptionMessage): Promise<boolean> { async function stealVip(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); console.log(`${LOG_PREFIX}Redemption has no message`);
return false; return;
} }
const channel = await getUsernameFromId(parseInt(msg.channelId)); const channel = await getUsernameFromId(parseInt(msg.channelId));
@@ -17,7 +19,7 @@ async function stealVip(msg: RedemptionMessage): Promise<boolean> {
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); console.log(`${LOG_PREFIX}No channel found`);
return false; return;
} }
const addVipUser = msg.userDisplayName; const addVipUser = msg.userDisplayName;
@@ -26,23 +28,32 @@ async function stealVip(msg: RedemptionMessage): Promise<boolean> {
if (!(await hasVip(channel, removeVipUser))) { if (!(await hasVip(channel, removeVipUser))) {
console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`); console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`);
return false; return;
} }
if (await hasVip(channel, addVipUser)) { if (await hasVip(channel, addVipUser)) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
return false; return;
} }
const removed = await removeVip(channel, removeVipUser); 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 }; export { stealVip };

View File

@@ -4,12 +4,14 @@ import { broadcast } from "../../helpers/webServer";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { timeout } from "../../chatClient/clientActions"; import { timeout } from "../../chatClient/clientActions";
async function timeoutFriend(msg: RedemptionMessage): Promise<boolean> { async function timeoutFriend(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
const { message, channelId, userDisplayName } = msg; const { message, channelId, userDisplayName } = msg;
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); console.log(`${LOG_PREFIX}Redemption has no message`);
return false; return;
} }
const channel = await getUsernameFromId(parseInt(channelId)); const channel = await getUsernameFromId(parseInt(channelId));
@@ -17,7 +19,7 @@ async function timeoutFriend(msg: RedemptionMessage): Promise<boolean> {
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); console.log(`${LOG_PREFIX}No channel found`);
return false; return;
} }
const time = 60; const time = 60;
@@ -35,10 +37,10 @@ async function timeoutFriend(msg: RedemptionMessage): Promise<boolean> {
console.error(`${LOG_PREFIX} ${e.message}`); console.error(`${LOG_PREFIX} ${e.message}`);
} }
return false; return;
} }
return true; return msg;
} }
export { timeoutFriend }; export { timeoutFriend };

View File

@@ -46,39 +46,37 @@ async function onRedemption(message: PubSubRedemptionMessage) {
backgroundColor: reward.background_color backgroundColor: reward.background_color
}; };
let handledMessage: RedemptionMessage | undefined;
switch (msg.rewardId) { switch (msg.rewardId) {
case RedemptionIds.RussianRoulette: case RedemptionIds.RussianRoulette:
await russianRoulette(msg); handledMessage = await russianRoulette(msg);
break; break;
case RedemptionIds.TimeoutFriend: case RedemptionIds.TimeoutFriend:
await timeoutFriend(msg); handledMessage = await timeoutFriend(msg);
break; break;
case RedemptionIds.HighlightMessage: case RedemptionIds.HighlightMessage:
await highlightMessage(msg); handledMessage = await highlightMessage(msg);
break; break;
case RedemptionIds.GetVip: case RedemptionIds.GetVip:
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`; handledMessage = await getVip(msg);
if (await getVip(msg)) {
broadcast(JSON.stringify(msg));
}
break; break;
case RedemptionIds.StealVip: case RedemptionIds.StealVip:
if (await stealVip(msg)) { handledMessage = await stealVip(msg);
msg.message = `@${msg.userDisplayName} ha "tomado prestado" el VIP de @${msg.message}`;
broadcast(JSON.stringify(msg));
}
break; break;
case RedemptionIds.Hidrate: case RedemptionIds.Hidrate:
await hidrate(msg); handledMessage = await hidrate(msg);
break; break;
default: default:
console.log(LOG_PREFIX, msg); console.log(`${LOG_PREFIX}Unhandled redemption ${msg.rewardId}`);
broadcast(JSON.stringify(msg)); handledMessage = msg;
break; break;
} }
if (handledMessage) {
broadcast(JSON.stringify(handledMessage));
}
} }
export { registerUserListener, LOG_PREFIX }; export { registerUserListener, LOG_PREFIX };