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 { 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));
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<boolean> {
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 };

View File

@@ -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<void> {
async function hidrate(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) {
@@ -15,9 +16,9 @@ async function hidrate(msg: RedemptionMessage): Promise<void> {
msg.message = `@${msg.userDisplayName} ha invitado a una ronda`;
broadcast(JSON.stringify(msg));
await say(channel, "waterGang waterGang waterGang");
return msg;
}
export { hidrate };

View File

@@ -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<void> {
async function highlightMessage(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`);
@@ -35,7 +36,7 @@ async function highlightMessage(msg: RedemptionMessage): Promise<void> {
return;
}
broadcast(JSON.stringify(msg));
return msg;
}
export { highlightMessage };

View File

@@ -9,14 +9,18 @@ import { randomInt } from "crypto";
type GunsSafeShots = Record<string, number>;
const gunsSafeShots: GunsSafeShots = {};
const timeoutSeconds = 60;
const maxSafeShots = 5;
async function russianRoulette(msg: RedemptionMessage): Promise<void> {
async function russianRoulette(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
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<void> {
gunsSafeShots[channelId] = maxSafeShots;
}
if (win) {
msg.message = "";
} else {
msg.message = "got shot";
}
msg.message = win ? "" : "got shot";
broadcast(JSON.stringify(msg));
const promises: Array<Promise<unknown>> = [];
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,

View File

@@ -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<boolean> {
async function stealVip(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
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<boolean> {
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<boolean> {
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 };

View File

@@ -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<boolean> {
async function timeoutFriend(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
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<boolean> {
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<boolean> {
console.error(`${LOG_PREFIX} ${e.message}`);
}
return false;
return;
}
return true;
return msg;
}
export { timeoutFriend };

View File

@@ -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 };