#629 {{roll}} replacement macros

This commit is contained in:
Cohee
2023-07-18 19:51:20 +03:00
parent 623675d940
commit 9e21534fae
4 changed files with 23 additions and 12 deletions

View File

@@ -38,6 +38,7 @@
<script src="scripts/fuse.js"></script> <script src="scripts/fuse.js"></script>
<script src="scripts/select2.min.js"></script> <script src="scripts/select2.min.js"></script>
<script src="scripts/seedrandom.min.js"></script> <script src="scripts/seedrandom.min.js"></script>
<script src="scripts/droll.js"></script>
<script type="module" src="scripts/eventemitter.js"></script> <script type="module" src="scripts/eventemitter.js"></script>
<script type="module" src="scripts/power-user.js"></script> <script type="module" src="scripts/power-user.js"></script>
<script type="module" src="scripts/swiped-events.js"></script> <script type="module" src="scripts/swiped-events.js"></script>

View File

@@ -437,6 +437,7 @@ const system_messages = {
<li><tt>{{date}}</tt> - the current date</li> <li><tt>{{date}}</tt> - the current date</li>
<li><tt>{{idle_duration}}</tt> - the time since the last user message was sent</li> <li><tt>{{idle_duration}}</tt> - the time since the last user message was sent</li>
<li><tt>{{random:(args)}}</tt> - returns a random item from the list. (ex: {{random:1,2,3,4}} will return 1 of the 4 numbers at random. Works with text lists too.</li> <li><tt>{{random:(args)}}</tt> - returns a random item from the list. (ex: {{random:1,2,3,4}} will return 1 of the 4 numbers at random. Works with text lists too.</li>
<li><tt>{{roll:(formula)}}</tt> - rolls a dice. (ex: {{roll:1d6}} will roll a 6-sided dice and return a number between 1 and 6)</li>
</ul>` </ul>`
}, },
welcome: welcome:
@@ -1551,6 +1552,7 @@ function substituteParams(content, _name1, _name2, _original) {
return utcTime; return utcTime;
}); });
content = randomReplace(content); content = randomReplace(content);
content = diceRollReplace(content);
return content; return content;
} }
@@ -1604,6 +1606,23 @@ function randomReplace(input, emptyListPlaceholder = '') {
}); });
} }
function diceRollReplace(input, invalidRollPlaceholder = '') {
const randomPattern = /{{roll:([^}]+)}}/gi;
return input.replace(randomPattern, (match, matchValue) => {
const formula = matchValue.trim();
const isValid = droll.validate(formula);
if (!isValid) {
console.debug(`Invalid roll formula: ${formula}`);
return invalidRollPlaceholder;
}
const result = droll.roll(formula);
return new String(result.total);
});
}
function getStoppingStrings(isImpersonate, addSpace) { function getStoppingStrings(isImpersonate, addSpace) {
const charString = `\n${name2}:`; const charString = `\n${name2}:`;
const youString = `\nYou:`; const youString = `\nYou:`;
@@ -1710,7 +1729,7 @@ export function extractMessageBias(message) {
return null; return null;
} }
const forbiddenMatches = ['user', 'char', 'time', 'date', 'random', 'idle_duration']; const forbiddenMatches = ['user', 'char', 'time', 'date', 'random', 'idle_duration', 'roll'];
const found = []; const found = [];
const rxp = /\{\{([\s\S]+?)\}\}/gm; const rxp = /\{\{([\s\S]+?)\}\}/gm;
//const rxp = /{([^}]+)}/g; //const rxp = /{([^}]+)}/g;
@@ -1719,8 +1738,8 @@ export function extractMessageBias(message) {
while ((curMatch = rxp.exec(message))) { while ((curMatch = rxp.exec(message))) {
const match = curMatch[1].trim(); const match = curMatch[1].trim();
// Ignore random pattern matches // Ignore random/roll pattern matches
if (/^random:.+/i.test(match)) { if (/^random:.+/i.test(match) || /^roll:.+/i.test(match)) {
continue; continue;
} }

View File

@@ -76,20 +76,11 @@ function addDiceRollButton() {
}); });
} }
function addDiceScript() {
if (!window.droll) {
const script = document.createElement('script');
script.src = "/scripts/extensions/dice/droll.js";
document.body.appendChild(script);
}
}
async function moduleWorker() { async function moduleWorker() {
$('#roll_dice').toggle(getContext().onlineStatus !== 'no_connection'); $('#roll_dice').toggle(getContext().onlineStatus !== 'no_connection');
} }
jQuery(function () { jQuery(function () {
addDiceScript();
addDiceRollButton(); addDiceRollButton();
moduleWorker(); moduleWorker();
setInterval(moduleWorker, UPDATE_INTERVAL); setInterval(moduleWorker, UPDATE_INTERVAL);