1
0

♻️ Extract roussian roulette logic to server

This commit is contained in:
2022-01-05 19:01:15 +01:00
parent 05a93ba316
commit 2edb4481f8
4 changed files with 81 additions and 74 deletions

View File

@@ -0,0 +1,60 @@
import { say, timeout } from "../../chatClient/clientActions";
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { broadcast } from "../../helpers/webServer";
import { getUsernameFromId } from "../../helpers/twitch";
import { randomInt } from "crypto";
type GunsSafeShots = Record<string, number>;
const gunsSafeShots: GunsSafeShots = {};
const maxSafeShots = 5;
async function russianRoulette(msg: RedemptionMessage): Promise<void> {
const { channelId, userDisplayName } = msg;
const channel = await getUsernameFromId(parseInt(channelId));
if (!channel) {
console.log(`${LOG_PREFIX}No channel found`);
return;
}
if (!gunsSafeShots[channelId]) {
gunsSafeShots[channelId] = maxSafeShots;
}
const win =
gunsSafeShots[channelId] > 0 &&
randomInt(gunsSafeShots[channelId]-- + 1) !== 0;
if (gunsSafeShots[channelId] < 0 || !win) {
gunsSafeShots[channelId] = maxSafeShots;
}
if (win) {
msg.message = "";
} else {
msg.message = "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(
say(
channel,
`PepeHands ${userDisplayName} no ha sobrevivido para contarlo`
)
);
} else {
promises.push(say(channel, `rdCool Clap ${userDisplayName}`));
}
await Promise.all(promises);
}
export { russianRoulette };

View File

@@ -6,6 +6,7 @@ import { UserIdResolvable } from "twitch";
import { broadcast } from "../helpers/webServer";
import { getApiClient } from "../helpers/twitch";
import { getVip } from "./actions/getVip";
import { russianRoulette } from "./actions/russianRoulette";
import { stealVip } from "./actions/stealVip";
const LOG_PREFIX = "[PubSub] ";
@@ -43,12 +44,10 @@ async function onRedemption(message: PubSubRedemptionMessage) {
};
switch (msg.rewardId) {
case RedemptionIds.StealVip:
if (await stealVip(msg)) {
msg.message = `@${msg.userDisplayName} ha "tomado prestado" el VIP de @${msg.message}`;
broadcast(JSON.stringify(msg));
}
case RedemptionIds.RussianRoulette:
await russianRoulette(msg);
break;
case RedemptionIds.TimeoutFriend:
break;
case RedemptionIds.GetVip:
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`;
@@ -57,6 +56,15 @@ async function onRedemption(message: PubSubRedemptionMessage) {
broadcast(JSON.stringify(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));
}
break;
case RedemptionIds.Hidrate:
break;
default:
console.log(LOG_PREFIX, msg);