Skip to content

Commit

Permalink
refactor: remove duplicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Dec 1, 2024
1 parent 189aa7c commit 59946ed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/interactions/add/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { editUserButtons, registerDmgButton } from "@utils/buttons";
import { getTemplateWithDB, getUserByEmbed, registerUser } from "@utils/db";
import { ensureEmbed, getEmbeds } from "@utils/parse";
import * as Djs from "discord.js";
import { logger } from "../../logger";
/**
* Interaction to add a new skill dice
* @param interaction {Djs.ButtonInteraction}
Expand Down Expand Up @@ -143,6 +144,7 @@ export async function registerDamageDice(
}
}
const user = getUserByEmbed(interaction.message, ul, first);
logger.trace("user", user);
if (!user) throw new Error(ul("error.user")); //mean that there is no embed
value = evalStatsDice(value, user.stats);

Expand Down Expand Up @@ -215,12 +217,10 @@ export async function registerDamageDice(
}

const components = registerDmgButton(ul);
//get all other embeds from the old messages
//remove the old dice embed
//add the new dice embed
const userEmbed = getEmbeds(ul, interaction.message ?? undefined, "user");
if (!userEmbed) throw new NoEmbed(); //mean that there is no embed
const statsEmbed = getEmbeds(ul, interaction.message ?? undefined, "stats");
logger.trace("stats", statsEmbed);
const allEmbeds = [userEmbed];
if (statsEmbed) allEmbeds.push(statsEmbed);
allEmbeds.push(diceEmbed);
Expand Down
65 changes: 3 additions & 62 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// noinspection SuspiciousTypeOfGuard

import { TUTORIAL_IMAGES } from "@interfaces/constant";
import type { UserData, UserRegistration } from "@interfaces/database";
import type { DiscordChannel, Settings, Translation } from "@interfaces/discord";
import { logger } from "@logger";
import { editUserButtons, selectEditMenu } from "@utils/buttons";
import { registerUser, setDefaultManagerId } from "@utils/db";
import { parseEmbedFields } from "@utils/parse";

import { TUTORIAL_IMAGES } from "@interfaces/constant";
import type { DiscordChannel, Settings, Translation } from "@interfaces/discord";
import { logger } from "@logger";
import * as Djs from "discord.js";
import { evaluate } from "mathjs";
import moment from "moment";

/**
Expand Down Expand Up @@ -212,53 +210,6 @@ export function isArrayEqual(array1: string[] | undefined, array2: string[] | un
);
}

/**
* Replace the {{}} in the dice string and evaluate the interior if any
* @param dice {string}
*/
export function replaceFormulaInDice(dice: string) {
// noinspection RegExpRedundantEscape
const formula = /(?<formula>\{{2}(.+?)\}{2})/gim;
const formulaMatch = formula.exec(dice);
if (formulaMatch?.groups?.formula) {
const formula = formulaMatch.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
try {
const result = evaluate(formula);
return cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));
} catch (error) {
throw new Error(
`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula} [From: replaceFormulaInDice]`
);
}
}
return cleanedDice(dice);
}

// noinspection JSUnusedGlobalSymbols
/**
* Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`
*/
export function generateStatsDice(
originalDice: string,
stats?: { [name: string]: number }
) {
let dice = originalDice;
if (stats && Object.keys(stats).length > 0) {
//damage field support adding statistic, like : 1d6 + strength
//check if the value contains a statistic & calculate if it's okay
//the dice will be converted before roll
const allStats = Object.keys(stats);
for (const stat of allStats) {
const regex = new RegExp(escapeRegex(stat.removeAccents()), "gi");
if (dice.match(regex)) {
const statValue = stats[stat];
dice = dice.replace(regex, statValue.toString());
}
}
}
return replaceFormulaInDice(dice);
}

/**
* Escape regex string
* @param string {string}
Expand All @@ -267,16 +218,6 @@ export function escapeRegex(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

/**
* Replace the ++ +- -- by their proper value:
* - `++` = `+`
* - `+-` = `-`
* - `--` = `+`
* @param dice {string}
*/
export function cleanedDice(dice: string) {
return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+");
}
/**
* filter the choices by removing the accents and check if it includes the removedAccents focused
* @param choices {string[]}
Expand Down

0 comments on commit 59946ed

Please sign in to comment.