Files
discord-decomp/modules/dice_roll/DiceRollLifecycleManager.tsx
T
2026-11-01 12:12:55 +02:00

116 lines
4.1 KiB
TypeScript

// Metro module 16158 (f16159).
import ChannelStore from "../../stores/ChannelStore";
import SelectedChannelStore from "../../stores/SelectedChannelStore";
import DiceRollStore, { INITIAL_STATE } from "./DiceRollStore";
import { AFTER_ROLL_DELAY_MS, ALLOWED_DICE_SIDES_SET, DEFAULT_DICE_SIDES, DISMISS_DELAY_MS, MAX_DICE_COUNT, ROLL_DURATION_MS } from "./DiceRollConstants";
import { MessageSendLocation } from "../messages/MessageConstants";
import AutomaticLifecycleManager from "../../lib/AutomaticLifecycleManager";
import { intl, t } from "../../intl/index.native";
import MessageActionCreators from "../../actions/MessageActionCreators";
import MessageParser from "../messages/MessageParser";
class DiceRollLifecycleManager extends AutomaticLifecycleManager {
actions = {
CHANNEL_SELECT: (a0) => this.handleChannelSelect(a0),
DICE_ROLL_START: (a0) => this.handleStartRoll(a0)
};
rollTimer = null;
postRollDismissTimer = null;
collapseTimer = null;
handleChannelSelect = ({ channelId: channelId2 }) => {
const state = DiceRollStore.getState();
if (state.channelId != null && state.channelId !== channelId2) {
this.clearTimers();
this.dismiss();
}
};
handleStartRoll = ({ channelId, diceCount = 1, diceSides = DEFAULT_DICE_SIDES }) => {
let c28246_1, DEFAULT_DICE_SIDES2, c28246_3;
c28246_1 = void 0;
DEFAULT_DICE_SIDES2 = void 0;
c28246_3 = void 0;
if (SelectedChannelStore.getChannelId() === channelId && DiceRollStore.getState().channelId == null) {
const diceCount2 = diceCount !== 42 ? Math.min(Math.max(diceCount, 1), MAX_DICE_COUNT) : 42;
c28246_1 = diceCount2;
DEFAULT_DICE_SIDES2 = ALLOWED_DICE_SIDES_SET.has(diceSides) ? diceSides : DEFAULT_DICE_SIDES;
DiceRollStore.setState({
channelId,
rolling: true,
diceCount: diceCount2,
diceSides: DEFAULT_DICE_SIDES2,
results: null
});
const r34 = [];
c28246_3 = r34;
let r0 = 0;
while (r0 < diceCount2) {
r34.push(Math.floor(Math.random() * DEFAULT_DICE_SIDES2) + 1);
r0 = r0 + 1;
}
this.rollTimer = setTimeout(() => {
this.rollTimer = null;
this.finishRoll(channelId, c28246_1, DEFAULT_DICE_SIDES2, c28246_3);
}, ROLL_DURATION_MS);
}
};
finishRoll(a0, a1, a2, a3) {
DiceRollStore.setState({
rolling: false,
results: a3
});
this.postRollDismissTimer = setTimeout(() => {
this.postRollDismissTimer = null;
this.dismiss();
}, AFTER_ROLL_DELAY_MS);
this.sendMessage(a0, a1, a2, a3);
}
sendMessage(a0, a1, a2, a3) {
const channel = ChannelStore.getChannel(a0);
if (channel != null) {
let r17 = channel.getGuildId();
if (r17 == null) {
r17 = "@me";
}
const r35 = `${location.protocol}${window.GLOBAL_ENV.WEBAPP_ENDPOINT}/channels/${r17}/${a0}/roll-dice/${a1}d${a2}`;
const r50 = `[\`${intl.formatToPlainString(t.uV5JaG, {
count: a1,
sides: a2
})}\`](${r35})`;
const r62 = intl.formatToMarkdownString(t.tmSbYW, {
total: a3.reduce((acc, item) => acc + item, 0),
count: a1,
sides: a2
});
const r72 = `-# ${a3.map((item) => `:game_die: ${item.toString()}`).join(" ")}`;
MessageActionCreators.sendMessage(a0, MessageParser.parse(channel, a1 === 1 ? `### ${r62} ${r50}` : `### ${r62} ${r50}
${r72}`), true, {
location: MessageSendLocation.CHAT_INPUT
});
}
}
dismiss() {
DiceRollStore.setState({
dismissing: true
});
this.collapseTimer = setTimeout(() => {
this.collapseTimer = null;
DiceRollStore.setState(INITIAL_STATE);
}, DISMISS_DELAY_MS);
}
clearTimers() {
if (this.rollTimer != null) {
clearTimeout(this.rollTimer);
this.rollTimer = null;
}
if (this.postRollDismissTimer != null) {
clearTimeout(this.postRollDismissTimer);
this.postRollDismissTimer = null;
}
if (this.collapseTimer != null) {
clearTimeout(this.collapseTimer);
this.collapseTimer = null;
}
}
}
const diceRollLifecycleManager = new DiceRollLifecycleManager();
export default diceRollLifecycleManager;