fjas
This commit is contained in:
13
server/utils/dice.js
Normal file
13
server/utils/dice.js
Normal file
@@ -0,0 +1,13 @@
|
||||
export function withSides(sides) {
|
||||
const r = Math.random()
|
||||
return Math.floor(r * sides) + 1;
|
||||
}
|
||||
|
||||
export function d6() {
|
||||
return withSides(6);
|
||||
}
|
||||
|
||||
export function d8() {
|
||||
return withSides(8);
|
||||
}
|
||||
|
||||
12
server/utils/helpers.js
Executable file
12
server/utils/helpers.js
Executable file
@@ -0,0 +1,12 @@
|
||||
export function rollDice(sides) {
|
||||
const r = Math.random()
|
||||
return Math.floor(r * sides) + 1;
|
||||
}
|
||||
|
||||
export function d6() {
|
||||
return rollDice(6);
|
||||
}
|
||||
|
||||
export function d8() {
|
||||
return rollDice(8);
|
||||
}
|
||||
26
server/utils/id.js
Normal file
26
server/utils/id.js
Normal file
@@ -0,0 +1,26 @@
|
||||
export function cleanName(s) {
|
||||
if (typeof s !== "string") {
|
||||
throw new Error("String expected, but got a ", typeof s);
|
||||
}
|
||||
return s.toLowerCase().replace(" ", "_").replace(/[^a-zA-Z0-9_]/, "_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random number, convert it to base36, and return it as a string with 7-8 characters.
|
||||
*/
|
||||
export function miniUid() {
|
||||
// we use 12 digits, but we could go up to 16
|
||||
return Number(Math.random().toFixed(12).substring(2)).toString(36);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an id from a name
|
||||
*/
|
||||
export function fromName(...names) {
|
||||
let res = "";
|
||||
for (const name of names) {
|
||||
res += ":" + cleanName(name);
|
||||
}
|
||||
|
||||
return res + ":" + miniUid();
|
||||
}
|
||||
Reference in New Issue
Block a user