mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Copy extensions from extras project to main
This commit is contained in:
108
public/scripts/extensions/dice/droll.js
Normal file
108
public/scripts/extensions/dice/droll.js
Normal file
@@ -0,0 +1,108 @@
|
||||
// Borrowed from the Droll library by thebinarypenguin
|
||||
// https://github.com/thebinarypenguin/droll
|
||||
// Licensed under MIT license
|
||||
var droll = {};
|
||||
|
||||
// Define a "class" to represent a formula
|
||||
function DrollFormula() {
|
||||
this.numDice = 0;
|
||||
this.numSides = 0;
|
||||
this.modifier = 0;
|
||||
|
||||
this.minResult = 0;
|
||||
this.maxResult = 0;
|
||||
this.avgResult = 0;
|
||||
}
|
||||
|
||||
// Define a "class" to represent the results of the roll
|
||||
function DrollResult() {
|
||||
this.rolls = [];
|
||||
this.modifier = 0;
|
||||
this.total = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the roll result
|
||||
*/
|
||||
DrollResult.prototype.toString = function () {
|
||||
if (this.rolls.length === 1 && this.modifier === 0) {
|
||||
return this.rolls[0] + '';
|
||||
}
|
||||
|
||||
if (this.rolls.length > 1 && this.modifier === 0) {
|
||||
return this.rolls.join(' + ') + ' = ' + this.total;
|
||||
}
|
||||
|
||||
if (this.rolls.length === 1 && this.modifier > 0) {
|
||||
return this.rolls[0] + ' + ' + this.modifier + ' = ' + this.total;
|
||||
}
|
||||
|
||||
if (this.rolls.length > 1 && this.modifier > 0) {
|
||||
return this.rolls.join(' + ') + ' + ' + this.modifier + ' = ' + this.total;
|
||||
}
|
||||
|
||||
if (this.rolls.length === 1 && this.modifier < 0) {
|
||||
return this.rolls[0] + ' - ' + Math.abs(this.modifier) + ' = ' + this.total;
|
||||
}
|
||||
|
||||
if (this.rolls.length > 1 && this.modifier < 0) {
|
||||
return this.rolls.join(' + ') + ' - ' + Math.abs(this.modifier) + ' = ' + this.total;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse the formula into its component pieces.
|
||||
* Returns a DrollFormula object on success or false on failure.
|
||||
*/
|
||||
droll.parse = function (formula) {
|
||||
var pieces = null;
|
||||
var result = new DrollFormula();
|
||||
|
||||
pieces = formula.match(/^([1-9]\d*)?d([1-9]\d*)([+-]\d+)?$/i);
|
||||
if (!pieces) { return false; }
|
||||
|
||||
result.numDice = (pieces[1] - 0) || 1;
|
||||
result.numSides = (pieces[2] - 0);
|
||||
result.modifier = (pieces[3] - 0) || 0;
|
||||
|
||||
result.minResult = (result.numDice * 1) + result.modifier;
|
||||
result.maxResult = (result.numDice * result.numSides) + result.modifier;
|
||||
result.avgResult = (result.maxResult + result.minResult) / 2;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test the validity of the formula.
|
||||
* Returns true on success or false on failure.
|
||||
*/
|
||||
droll.validate = function (formula) {
|
||||
return (droll.parse(formula)) ? true : false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Roll the dice defined by the formula.
|
||||
* Returns a DrollResult object on success or false on failure.
|
||||
*/
|
||||
droll.roll = function (formula) {
|
||||
var pieces = null;
|
||||
var result = new DrollResult();
|
||||
|
||||
pieces = droll.parse(formula);
|
||||
if (!pieces) { return false; }
|
||||
|
||||
for (var a = 0; a < pieces.numDice; a++) {
|
||||
result.rolls[a] = (1 + Math.floor(Math.random() * pieces.numSides));
|
||||
}
|
||||
|
||||
result.modifier = pieces.modifier;
|
||||
|
||||
for (var b = 0; b < result.rolls.length; b++) {
|
||||
result.total += result.rolls[b];
|
||||
}
|
||||
result.total += result.modifier;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
// END OF DROLL CODE
|
95
public/scripts/extensions/dice/index.js
Normal file
95
public/scripts/extensions/dice/index.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import { getContext } from "../../extensions.js";
|
||||
export { MODULE_NAME };
|
||||
|
||||
const MODULE_NAME = 'dice';
|
||||
const UPDATE_INTERVAL = 1000;
|
||||
|
||||
function setDiceIcon() {
|
||||
const sendButton = document.getElementById('roll_dice');
|
||||
sendButton.style.backgroundImage = `url(/img/dice-solid.svg)`;
|
||||
sendButton.classList.remove('spin');
|
||||
}
|
||||
|
||||
function doDiceRoll() {
|
||||
const value = $(this).data('value');
|
||||
const isValid = droll.validate(value);
|
||||
|
||||
if (isValid) {
|
||||
const result = droll.roll(value);
|
||||
const context = getContext();
|
||||
context.sendSystemMessage('generic', `${context.name1} rolls the ${value}. The result is: ${result.total}`);
|
||||
}
|
||||
}
|
||||
|
||||
function addDiceRollButton() {
|
||||
const buttonHtml = `
|
||||
<input id="roll_dice" type="button" />
|
||||
<div id="dice_dropdown">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item" data-value="d4">d4</li>
|
||||
<li class="list-group-item" data-value="d6">d6</li>
|
||||
<li class="list-group-item" data-value="d8">d8</li>
|
||||
<li class="list-group-item" data-value="d10">d10</li>
|
||||
<li class="list-group-item" data-value="d12">d12</li>
|
||||
<li class="list-group-item" data-value="d20">d20</li>
|
||||
<li class="list-group-item" data-value="d100">d100</li>
|
||||
</ul>
|
||||
</div>
|
||||
`;
|
||||
|
||||
$('#send_but_sheld').prepend(buttonHtml);
|
||||
$('#dice_dropdown li').on('click', doDiceRoll);
|
||||
const button = $('#roll_dice');
|
||||
const dropdown = $('#dice_dropdown');
|
||||
dropdown.hide();
|
||||
button.hide();
|
||||
|
||||
let popper = Popper.createPopper(button.get(0), dropdown.get(0), {
|
||||
placement: 'top-start',
|
||||
});
|
||||
|
||||
$(document).on('click touchend', function (e) {
|
||||
const target = $(e.target);
|
||||
if (target.is(dropdown)) return;
|
||||
if (target.is(button) && !dropdown.is(":visible")) {
|
||||
e.preventDefault();
|
||||
|
||||
dropdown.show();
|
||||
popper.update();
|
||||
} else {
|
||||
dropdown.hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addDiceScript() {
|
||||
if (!window.droll) {
|
||||
const script = document.createElement('script');
|
||||
script.src = "/scripts/extensions/dice/droll.js";
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
}
|
||||
|
||||
function patchSendForm() {
|
||||
const columns = $('#send_form').css('grid-template-columns').split(' ');
|
||||
columns[columns.length - 1] = `${parseInt(columns[columns.length - 1]) + 40}px`;
|
||||
columns[1] = 'auto';
|
||||
$('#send_form').css('grid-template-columns', columns.join(' '));
|
||||
}
|
||||
|
||||
async function moduleWorker() {
|
||||
const context = getContext();
|
||||
|
||||
context.onlineStatus === 'no_connection'
|
||||
? $('#roll_dice').hide(200)
|
||||
: $('#roll_dice').show(200);
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
addDiceScript();
|
||||
addDiceRollButton();
|
||||
patchSendForm();
|
||||
setDiceIcon();
|
||||
moduleWorker();
|
||||
setInterval(moduleWorker, UPDATE_INTERVAL);
|
||||
});
|
7
public/scripts/extensions/dice/manifest.json
Normal file
7
public/scripts/extensions/dice/manifest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"display_name": "D&D Dice",
|
||||
"loading_order": 5,
|
||||
"requires": [],
|
||||
"js": "index.js",
|
||||
"css": "style.css"
|
||||
}
|
40
public/scripts/extensions/dice/style.css
Normal file
40
public/scripts/extensions/dice/style.css
Normal file
@@ -0,0 +1,40 @@
|
||||
#roll_dice {
|
||||
order: 100;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
margin: 0;
|
||||
padding: 1px;
|
||||
background: no-repeat;
|
||||
background-size: 26px auto;
|
||||
background-position: center center;
|
||||
outline: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: 0.3s;
|
||||
filter: invert(1);
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
#roll_dice:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.list-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding-left: 0;
|
||||
margin-top: 0;
|
||||
margin-bottom: 3px;
|
||||
}
|
||||
|
||||
.list-group-item {
|
||||
position: relative;
|
||||
display: block;
|
||||
padding: 0.75rem 1.25rem;
|
||||
margin-bottom: -1px;
|
||||
background-color: rgba(0,0,0,0.3);
|
||||
border: 1px solid rgba(0,0,0,0.7);
|
||||
box-sizing: border-box;
|
||||
user-select: none;
|
||||
cursor: pointer;
|
||||
}
|
Reference in New Issue
Block a user