Galène videoconferencing server discussion list archives
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@toke.dk>
To: Juliusz Chroboczek <jch@irif.fr>
Cc: "Toke Høiland-Jørgensen" <toke@toke.dk>, galene@lists.galene.org
Subject: [Galene] [PATCH] Add /roll command to roll dice in the chat
Date: Wed,  7 Apr 2021 23:12:27 +0200
Message-ID: <20210407211227.111091-1-toke@toke.dk> (raw)

This adds a /roll command to the chat that will take a dice specifier (like
'1d20' or '2d10+5'), generate some random numbers corresponding to the roll
and print the result in the chat. This is useful for role playing sessions,
and other situations where a ready source of random numbers is needed.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 static/galene.js | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/static/galene.js b/static/galene.js
index d79a56d2b20a..341ab3a39a7e 100644
--- a/static/galene.js
+++ b/static/galene.js
@@ -2279,6 +2279,51 @@ commands.msg = {
     }
 };
 
+commands.roll = {
+    parameters: 'dice',
+    description: 'roll dice and send results',
+    f: (c, r) => {
+        let p = parseCommand(r);
+        if(!p[0])
+            throw new Error('/roll requires dice');
+
+        let regxp = new RegExp(/^(\d*)d(\d+)([+-]\d+)?$/);
+        let m = regxp.exec(p[0]);
+        if (!m)
+            throw new Error(`invalid dice: ${p[0]} - format is XdY, e.g. "2d10" or "d100"`);
+        let reps = m[1] || 1;
+        let range = m[2];
+        let s = `rolls ${reps}d${range}`;
+
+        let sum = 0;
+        let mod = 0;
+
+        // If there's a space between the modifier and the dice, the modifier
+        // ends up in p[1] instead of in m[3]. Support either, but not both
+        if (m[3] && p[1]) {
+            throw new Error('only one modifier allowed')
+        } else if (m[3] || p[1]) {
+            mod = parseInt(m[3] || p[1]);
+            if (Number.isNaN(mod))
+                throw new Error(`invalid modifier: ${p[1]} - format is +X or -X`)
+            s = s + `${mod < 0 ? mod : '+' + mod}`;
+        }
+        s = s + ':';
+
+        for (let i = 0; i < reps; i++) {
+            let roll = Math.floor((Math.random() * range) + 1);
+            s = s + ` ${roll}`;
+            sum += roll;
+        }
+
+        sum += mod;
+        if (reps > 1 || mod)
+            s = s + ` = ${sum}`;
+
+        serverConnection.chat('me', '', s);
+    }
+};
+
 /**
    @param {string} c
    @param {string} r
-- 
2.31.1


                 reply	other threads:[~2021-04-07 21:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.galene.org/postorius/lists/galene.lists.galene.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210407211227.111091-1-toke@toke.dk \
    --to=toke@toke.dk \
    --cc=galene@lists.galene.org \
    --cc=jch@irif.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Galène videoconferencing server discussion list archives

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://lists.galene.org/galene

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 galene galene/ https://lists.galene.org/galene \
		galene@lists.galene.org
	public-inbox-index galene

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git