From 2edb4481f83a71229edda14108ca59a63834a6ff Mon Sep 17 00:00:00 2001 From: alexbcberio Date: Wed, 5 Jan 2022 19:01:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20roussian=20roule?= =?UTF-8?q?tte=20logic=20to=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/app.js | 70 ++----------------- client/libs/js/randojs.js | 5 -- .../pubSubClient/actions/russianRoulette.ts | 60 ++++++++++++++++ src/backend/pubSubClient/index.ts | 20 ++++-- 4 files changed, 81 insertions(+), 74 deletions(-) delete mode 100644 client/libs/js/randojs.js create mode 100644 src/backend/pubSubClient/actions/russianRoulette.ts diff --git a/client/app.js b/client/app.js index abb45c1..1717793 100644 --- a/client/app.js +++ b/client/app.js @@ -64,11 +64,6 @@ async function checkEvent(e) { case "a26c0d9e-fd2c-4943-bc94-c5c2f2c974e4": await highlightMessage(data); break; - case "a215d6a0-2c11-4503-bb29-1ca98ef046ac": - await giveTempVip(data); - data.message = `@${data.userDisplayName} ha encontrado diamantes!`; - await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage); - break; // robar el vip case "ac750bd6-fb4c-4259-b06d-56953601243b": await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage); @@ -151,10 +146,9 @@ function karaokeTime(username, message) { } -let rrAttemps = 0; -function russianRoulette({ channelId, userId, userDisplayName }) { +function russianRoulette({ userDisplayName, message }) { return new Promise(res => { - const win = rando(5 - rrAttemps) !== 0; + const gotShot = Boolean(message); const div = document.createElement("div"); div.classList.add("alert"); @@ -164,59 +158,22 @@ function russianRoulette({ channelId, userId, userDisplayName }) { const p = createText(); - if (win) { - p.innerText = `${userDisplayName} ha sido afortunado y aún sigue entre nosotros`; - rrAttemps = 0; - } else { + if (gotShot) { p.innerText = `${userDisplayName} se ha ido a un mundo mejor, siempre te recordaremos`; - rrAttemps++; + } else { + p.innerText = `${userDisplayName} ha sido afortunado y aún sigue entre nosotros`; } div.appendChild(img); div.appendChild(p); img.onload = () => { - const audio = createAudio(`/sfx/toy-gun/${win ? 'stuck' : 'shot'}.mp3`); + const audio = createAudio(`/sfx/toy-gun/${gotShot ? 'shot' : 'stuck'}.mp3`); document.body.appendChild(div); - const actions = []; - if (!win) { + if (gotShot) { img.classList.add("shoot"); - - actions.push({ - type: "timeout", - userId, - channelId, - data: { - username: userDisplayName, - time: "60", - reason: "F en la ruleta." - } - }); - - actions.push({ - type: "say", - userId, - channelId, - data: { - message: `PepeHands ${userDisplayName} no ha sobrevivido para contarlo.` - } - }); - - } else { - actions.push({ - type: "say", - userId, - channelId, - data: { - message: `rdCool Clap ${userDisplayName}` - } - }); - } - - if (actions.length > 0) { - sendWsActions(actions); } audio.onended = () => { @@ -270,19 +227,6 @@ async function highlightMessage(data) { await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage); } -async function giveTempVip(data) { - sendWsActions([{ - type: "addVip", - userId: data.userId, - channelId: data.channelId - }, { - scheduledAt: Date.now() + 1000 * 60 * 60 * 24 * 7, - type: "removeVip", - userId: data.userId, - channelId: data.channelId - }]); -} - // send actions to be performed by the server function sendWsActions(actions) { if (!Array.isArray(actions)) { diff --git a/client/libs/js/randojs.js b/client/libs/js/randojs.js deleted file mode 100644 index e44c009..0000000 --- a/client/libs/js/randojs.js +++ /dev/null @@ -1,5 +0,0 @@ -function rando(a,b,e){var g=function(f){return"undefined"===typeof f},k=function(f){return"number"===typeof f&&!isNaN(f)},d=function(f){return!g(f)&&null!==f&&f.constructor===Array},c=function(){try{for(var f,q=[],r;30>(r="."+q.join("")).length;){f=(window.crypto||window.msCrypto).getRandomValues(new Uint32Array(5));for(var p=0;pf[p]?f[p].toString().slice(1):"";0b){var m=b;b=a;a=m}return c()*(b-a)+a}if(d(a)&&0 -rando();if(k(a)&&g(b))return 0<=a?rando(0,a):rando(a,0);if(k(a)&&"string"===typeof b&&"float"==b.toLowerCase().trim()&&g(e))return 0<=a?rando(0,a,"float"):rando(a,0,"float");if(k(a)&&k(b)&&g(e))return a>b&&(m=b,b=a,a=m),a=Math.floor(a),b=Math.floor(b),Math.floor(c()*(b-a+1)+a);if("string"===typeof a&&0b){var m=b;b=a;a=m}for(c=a;c<=b;c++)d[d.length]=c}for(c=d.length-1;0; +const gunsSafeShots: GunsSafeShots = {}; + +const maxSafeShots = 5; + +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; + } + + 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> = []; + + 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 }; diff --git a/src/backend/pubSubClient/index.ts b/src/backend/pubSubClient/index.ts index 1010cf3..d6c7f67 100644 --- a/src/backend/pubSubClient/index.ts +++ b/src/backend/pubSubClient/index.ts @@ -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);