♻️ Extract roussian roulette logic to server
This commit is contained in:
@@ -64,11 +64,6 @@ async function checkEvent(e) {
|
|||||||
case "a26c0d9e-fd2c-4943-bc94-c5c2f2c974e4":
|
case "a26c0d9e-fd2c-4943-bc94-c5c2f2c974e4":
|
||||||
await highlightMessage(data);
|
await highlightMessage(data);
|
||||||
break;
|
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
|
// robar el vip
|
||||||
case "ac750bd6-fb4c-4259-b06d-56953601243b":
|
case "ac750bd6-fb4c-4259-b06d-56953601243b":
|
||||||
await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage);
|
await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage);
|
||||||
@@ -151,10 +146,9 @@ function karaokeTime(username, message) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let rrAttemps = 0;
|
function russianRoulette({ userDisplayName, message }) {
|
||||||
function russianRoulette({ channelId, userId, userDisplayName }) {
|
|
||||||
return new Promise(res => {
|
return new Promise(res => {
|
||||||
const win = rando(5 - rrAttemps) !== 0;
|
const gotShot = Boolean(message);
|
||||||
|
|
||||||
const div = document.createElement("div");
|
const div = document.createElement("div");
|
||||||
div.classList.add("alert");
|
div.classList.add("alert");
|
||||||
@@ -164,59 +158,22 @@ function russianRoulette({ channelId, userId, userDisplayName }) {
|
|||||||
|
|
||||||
const p = createText();
|
const p = createText();
|
||||||
|
|
||||||
if (win) {
|
if (gotShot) {
|
||||||
p.innerText = `${userDisplayName} ha sido afortunado y aún sigue entre nosotros`;
|
|
||||||
rrAttemps = 0;
|
|
||||||
} else {
|
|
||||||
p.innerText = `${userDisplayName} se ha ido a un mundo mejor, siempre te recordaremos`;
|
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(img);
|
||||||
div.appendChild(p);
|
div.appendChild(p);
|
||||||
|
|
||||||
img.onload = () => {
|
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);
|
document.body.appendChild(div);
|
||||||
|
|
||||||
const actions = [];
|
if (gotShot) {
|
||||||
if (!win) {
|
|
||||||
img.classList.add("shoot");
|
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 = () => {
|
audio.onended = () => {
|
||||||
@@ -270,19 +227,6 @@ async function highlightMessage(data) {
|
|||||||
await createCard(data.rewardName, data.message, data.backgroundColor, data.rewardImage);
|
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
|
// send actions to be performed by the server
|
||||||
function sendWsActions(actions) {
|
function sendWsActions(actions) {
|
||||||
if (!Array.isArray(actions)) {
|
if (!Array.isArray(actions)) {
|
||||||
|
@@ -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;p<f.length;p++){var t=4E9>f[p]?f[p].toString().slice(1):"";0<t.length&&(q[q.length]=t)}}return Number(r)}catch(v){return Math.random()}};try{if(null!==a&&null!==b&&null!==
|
|
||||||
e){if(g(a))return c();if(window.jQuery&&a instanceof jQuery&&g(b)){if(0==a.length)return!1;var n=rando(0,a.length-1);return{index:n,value:a.eq(n)}}if(k(a)&&k(b)&&"string"===typeof e&&"float"==e.toLowerCase().trim()){if(a>b){var m=b;b=a;a=m}return c()*(b-a)+a}if(d(a)&&0<a.length&&g(b)){var l=c()*a.length<<0;return{index:l,value:a[l]}}if("object"===typeof a&&g(b)){l=a;var h=Object.keys(l);if(0<h.length){var u=h[h.length*c()<<0];return{key:u,value:l[u]}}}if((!0===a&&!1===b||!1===a&&!0===b)&&g(e))return.5>
|
|
||||||
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&&0<a.length&&g(b))return a.charAt(rando(0,a.length-1))}return!1}catch(f){return!1}}
|
|
||||||
function randoSequence(a,b){var e=function(h){return"undefined"===typeof h},g=function(h){return"number"===typeof h&&!isNaN(h)},k=function(h){return!e(h)&&null!==h&&h.constructor===Array};try{if(e(a)||null===a||null===b)return!1;var d=[];if(window.jQuery&&a instanceof jQuery&&e(b)){if(0<a.length){d=randoSequence(0,a.length-1);for(var c=0;c<d.length;c++)d[c]={index:d[c],value:a.eq(d[c])}}return d}if(e(b))if(k(a)&&e(b))for(c=0;c<a.length;c++)d[d.length]={index:c,value:a[c]};else if("object"===typeof a&&
|
|
||||||
e(b))for(var n in a)Object.prototype.hasOwnProperty.call(a,n)&&(d[d.length]={key:n,value:a[n]});else if("string"===typeof a&&e(b))for(c=0;c<a.length;c++)d[d.length]=a.charAt(c);else return g(a)&&e(b)?0<=a?randoSequence(0,a):randoSequence(a,0):!1;else{if(!g(a)||!g(b)||0<a%1||0<b%1)return!1;if(a>b){var m=b;b=a;a=m}for(c=a;c<=b;c++)d[d.length]=c}for(c=d.length-1;0<c;c--){var l=rando(c);m=d[c];d[c]=d[l];d[l]=m}return d}catch(h){return!1}};
|
|
60
src/backend/pubSubClient/actions/russianRoulette.ts
Normal file
60
src/backend/pubSubClient/actions/russianRoulette.ts
Normal 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 };
|
@@ -6,6 +6,7 @@ import { UserIdResolvable } from "twitch";
|
|||||||
import { broadcast } from "../helpers/webServer";
|
import { broadcast } from "../helpers/webServer";
|
||||||
import { getApiClient } from "../helpers/twitch";
|
import { getApiClient } from "../helpers/twitch";
|
||||||
import { getVip } from "./actions/getVip";
|
import { getVip } from "./actions/getVip";
|
||||||
|
import { russianRoulette } from "./actions/russianRoulette";
|
||||||
import { stealVip } from "./actions/stealVip";
|
import { stealVip } from "./actions/stealVip";
|
||||||
|
|
||||||
const LOG_PREFIX = "[PubSub] ";
|
const LOG_PREFIX = "[PubSub] ";
|
||||||
@@ -43,12 +44,10 @@ async function onRedemption(message: PubSubRedemptionMessage) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
switch (msg.rewardId) {
|
switch (msg.rewardId) {
|
||||||
case RedemptionIds.StealVip:
|
case RedemptionIds.RussianRoulette:
|
||||||
if (await stealVip(msg)) {
|
await russianRoulette(msg);
|
||||||
msg.message = `@${msg.userDisplayName} ha "tomado prestado" el VIP de @${msg.message}`;
|
break;
|
||||||
|
case RedemptionIds.TimeoutFriend:
|
||||||
broadcast(JSON.stringify(msg));
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case RedemptionIds.GetVip:
|
case RedemptionIds.GetVip:
|
||||||
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`;
|
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`;
|
||||||
@@ -57,6 +56,15 @@ async function onRedemption(message: PubSubRedemptionMessage) {
|
|||||||
broadcast(JSON.stringify(msg));
|
broadcast(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
break;
|
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:
|
default:
|
||||||
console.log(LOG_PREFIX, msg);
|
console.log(LOG_PREFIX, msg);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user