Compare commits
231 Commits
Author | SHA1 | Date |
---|---|---|
projectmoon | 86df3c5d1f | |
projectmoon | 38a7e50c5c | |
projectmoon | e309fd1fc6 | |
projectmoon | 9262fe2cac | |
projectmoon | 724a781e7c | |
projectmoon | ef074beb96 | |
projectmoon | 81a69f329a | |
projectmoon | c9e7efa61d | |
projectmoon | f295f2b7b6 | |
projectmoon | 090ce9be45 | |
projectmoon | 2a6dff3e07 | |
projectmoon | 952f35d53a | |
projectmoon | 552daa4746 | |
projectmoon | c514b85510 | |
projectmoon | 6eb81f43d5 | |
projectmoon | 44b1e0f649 | |
projectmoon | a8ccdc9cce | |
projectmoon | 13ce7b3ee6 | |
projectmoon | 6f09a11586 | |
projectmoon | ee3ec18e06 | |
projectmoon | 126548d868 | |
Matthew Sparks | 7e7e9e534e | |
Matthew Sparks | 2d9853fbf0 | |
Matthew Sparks | 3d6210b32d | |
Matthew Sparks | 8b5973475f | |
Matthew Sparks | 1992ef4e08 | |
Matthew Sparks | f904e3a948 | |
Matthew Sparks | 8317f40f61 | |
Matthew Sparks | 069ee47364 | |
Matthew Sparks | dc242182f4 | |
Matthew Sparks | 15163ac11d | |
Matthew Sparks | 1860eaf378 | |
Matthew Sparks | 2654887d8c | |
projectmoon | 125f3d0cee | |
projectmoon | a4c3d34a97 | |
projectmoon | 86fbb05e54 | |
projectmoon | 661a943672 | |
projectmoon | d65715dee6 | |
projectmoon | 55a3bfb861 | |
projectmoon | 0050810182 | |
projectmoon | 3ba546d4a4 | |
projectmoon | ffded7b572 | |
projectmoon | cf93d14913 | |
projectmoon | cf6dd96b34 | |
projectmoon | c8c6f4d6f0 | |
projectmoon | 2488429edb | |
projectmoon | f68d5ffcc1 | |
projectmoon | 473e899275 | |
projectmoon | 1f03837bfe | |
projectmoon | 0059e3d133 | |
matthew | 915b82d0aa | |
projectmoon | cab856241d | |
projectmoon | 764426382a | |
projectmoon | b4321721c4 | |
projectmoon | 494d28486e | |
projectmoon | b7393c1907 | |
projectmoon | 3d2eb14cd3 | |
projectmoon | 53339282e0 | |
projectmoon | 7050cf037a | |
projectmoon | 0c0ddafd03 | |
projectmoon | 7f0bdc1e82 | |
projectmoon | 0ca7ad4db0 | |
projectmoon | 59be127430 | |
projectmoon | e9c0a184bd | |
projectmoon | 589d0e0dbf | |
projectmoon | 892ccf73e3 | |
projectmoon | 896acee5ba | |
projectmoon | d70df44d2a | |
projectmoon | 5f15e62c6d | |
projectmoon | ed3b582aad | |
projectmoon | 49db0062a3 | |
projectmoon | 4ae871224a | |
projectmoon | 1ebd13e912 | |
projectmoon | 8f5b6f0636 | |
projectmoon | 5b3d174edc | |
projectmoon | 495df13fe6 | |
projectmoon | de92fc8488 | |
projectmoon | b05129ad9f | |
projectmoon | 5d002e5063 | |
projectmoon | 849a1b6a14 | |
projectmoon | 97be5d5ccb | |
projectmoon | 395753e8a9 | |
projectmoon | df0248d99a | |
projectmoon | 76214bc790 | |
projectmoon | 921c4cd644 | |
projectmoon | 8c2a90e86b | |
projectmoon | 926dae57fb | |
projectmoon | 4557498ac6 | |
projectmoon | ca34841d86 | |
projectmoon | c1ec7366e4 | |
projectmoon | a84d4fd869 | |
projectmoon | 34ee2c6e5d | |
projectmoon | 9de74d05a9 | |
projectmoon | 5643677627 | |
projectmoon | de63fd914e | |
projectmoon | e73ad118b2 | |
projectmoon | 3d5cda39c8 | |
projectmoon | 402f236ba7 | |
projectmoon | 059538b95d | |
projectmoon | 4de273db4a | |
projectmoon | a33367fada | |
projectmoon | 5630b4ed20 | |
projectmoon | 6b6be06c89 | |
projectmoon | a3b39ee42c | |
projectmoon | 7eee16961e | |
projectmoon | 43d8f9574f | |
projectmoon | 1c4cd3d139 | |
projectmoon | d1c04b8817 | |
projectmoon | 5e899cd962 | |
projectmoon | 257f3a066c | |
projectmoon | e539dcac1f | |
projectmoon | 9f97a6cb43 | |
projectmoon | a665293268 | |
projectmoon | 66fb6e7cf8 | |
projectmoon | bfc5609ab6 | |
projectmoon | 9798821b7b | |
projectmoon | cf9ce63892 | |
projectmoon | 6b6e59da2e | |
projectmoon | b1972e2850 | |
projectmoon | 49d4ae3037 | |
projectmoon | ffda24833e | |
projectmoon | 9446435843 | |
projectmoon | b894fb83db | |
projectmoon | 30f800eb4a | |
projectmoon | e553472b7a | |
projectmoon | 2096af2512 | |
projectmoon | 490d17790a | |
projectmoon | 38fbef4101 | |
projectmoon | 0396911c56 | |
projectmoon | c4e0393d99 | |
projectmoon | d67328ac6b | |
projectmoon | ec66bfa3d6 | |
projectmoon | 8939d6debd | |
projectmoon | 0f35c1932b | |
projectmoon | 7a506bdc4f | |
projectmoon | b62359c172 | |
projectmoon | 8d1a1a5ca6 | |
projectmoon | 119321e01a | |
projectmoon | ba6a4c9679 | |
projectmoon | e5431a587d | |
projectmoon | 0821cf2bf5 | |
projectmoon | 85557c0382 | |
projectmoon | d30d39ff1e | |
projectmoon | 8e4eb574d2 | |
projectmoon | 67291e6deb | |
projectmoon | 7e23f80e42 | |
projectmoon | d813198cb0 | |
projectmoon | 693167a581 | |
projectmoon | 9ab65b8943 | |
projectmoon | 8817cae9da | |
projectmoon | ef4f1ef02f | |
projectmoon | d42e075c5c | |
projectmoon | b0707dff05 | |
projectmoon | 304c91c69d | |
projectmoon | 94be4d2578 | |
projectmoon | 14f8bc8b39 | |
projectmoon | 7db639f16c | |
projectmoon | b3cd7266e4 | |
projectmoon | 12b7d355d2 | |
projectmoon | 3c177dc304 | |
projectmoon | f6099c657e | |
projectmoon | 932e06ad91 | |
projectmoon | b7ccd4e7ad | |
projectmoon | b32b761f82 | |
projectmoon | 9a5a18268c | |
projectmoon | f5a8e16ce0 | |
projectmoon | 7512ca0694 | |
projectmoon | 3faca6a2df | |
projectmoon | 042ecc40e0 | |
projectmoon | df54e6555a | |
projectmoon | b3c4d8a38c | |
projectmoon | a4e66a0ca6 | |
projectmoon | d0c6ca3de8 | |
projectmoon | 055bad3a46 | |
projectmoon | 16eb87e50f | |
projectmoon | 1b0003ff1b | |
projectmoon | a4cdad4936 | |
projectmoon | 297a8454f6 | |
projectmoon | c9c80b974c | |
projectmoon | eb42704380 | |
projectmoon | f355fad06b | |
projectmoon | 23cf9e6ba4 | |
projectmoon | aa28d8bec7 | |
projectmoon | 62203edce8 | |
projectmoon | e177da9c25 | |
projectmoon | a65084e04a | |
projectmoon | 979dc8ea34 | |
projectmoon | 0b2246cbd5 | |
projectmoon | 7e1abab66d | |
projectmoon | 569ba4f2e0 | |
projectmoon | 118e4b00c7 | |
projectmoon | c8c38ac1d4 | |
projectmoon | 91cfc52e5b | |
projectmoon | 224f8cd0f1 | |
projectmoon | 68db038336 | |
projectmoon | 18352c8c19 | |
projectmoon | dda0d74f45 | |
projectmoon | f46b914239 | |
projectmoon | f352c90b6b | |
projectmoon | e251294b5f | |
projectmoon | 0e04e67f6e | |
projectmoon | 551f21a49a | |
projectmoon | 9ed2a81dd3 | |
projectmoon | 0939feee84 | |
projectmoon | 9349dd5f00 | |
projectmoon | 74d0b88e80 | |
projectmoon | fb24090952 | |
projectmoon | d7aaed9e00 | |
projectmoon | b5c78bcee5 | |
projectmoon | 51ba3e3f42 | |
projectmoon | 8e2f34819e | |
projectmoon | 5ce79a3c05 | |
projectmoon | a5dde18899 | |
projectmoon | 09278a80b5 | |
projectmoon | 0d63f7ebcb | |
projectmoon | d2a4d76ab2 | |
projectmoon | ff34a93e40 | |
projectmoon | b3c258e279 | |
projectmoon | fcec37afb2 | |
projectmoon | 472f02d153 | |
projectmoon | 3154f36dca | |
projectmoon | bc89088dd1 | |
projectmoon | 53840bff9f | |
projectmoon | d456320e66 | |
projectmoon | e7520f6206 | |
projectmoon | cdc4254783 | |
projectmoon | 6a93194cbb | |
projectmoon | 66f9bc6013 | |
projectmoon | d2642d1fd3 | |
projectmoon | 39e6eb9b46 | |
projectmoon | b142b87d65 |
|
@ -0,0 +1,27 @@
|
||||||
|
kind: pipeline
|
||||||
|
name: build-and-test
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: rust:1.80
|
||||||
|
commands:
|
||||||
|
- apt-get update
|
||||||
|
- apt-get install -y cmake
|
||||||
|
- rustup component add rustfmt
|
||||||
|
- cargo build --verbose --all
|
||||||
|
- cargo test --verbose --all
|
||||||
|
|
||||||
|
- name: docker
|
||||||
|
image: plugins/docker
|
||||||
|
when:
|
||||||
|
ref:
|
||||||
|
- refs/tags/v*
|
||||||
|
- refs/heads/master
|
||||||
|
settings:
|
||||||
|
auto_tag: true
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
repo: ghcr.io/projectmoon/chronicle-dicebot
|
||||||
|
registry: ghcr.io
|
|
@ -0,0 +1,2 @@
|
||||||
|
DATABASE_URL="sqlite://test-db/dicebot.sqlite"
|
||||||
|
SQLX_OFFLINE="true"
|
|
@ -26,43 +26,43 @@ jobs:
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test --verbose
|
run: cargo test --verbose
|
||||||
|
|
||||||
# Push image to GitHub Packages.
|
# # Push image to GitHub Packages.
|
||||||
# See also https://docs.docker.com/docker-hub/builds/
|
# # See also https://docs.docker.com/docker-hub/builds/
|
||||||
push:
|
# push:
|
||||||
# Ensure test job passes before pushing image.
|
# # Ensure test job passes before pushing image.
|
||||||
needs: build_and_test
|
# needs: build_and_test
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
# runs-on: ubuntu-latest
|
||||||
if: github.event_name == 'push'
|
# if: github.event_name == 'push'
|
||||||
|
|
||||||
steps:
|
# steps:
|
||||||
- uses: actions/checkout@v2
|
# - uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Build image
|
# - name: Build image
|
||||||
run: docker build . --file Dockerfile --tag $IMAGE_NAME
|
# run: docker build . --file Dockerfile --tag $IMAGE_NAME
|
||||||
|
|
||||||
- name: Log into GitHub Container Registry
|
# - name: Log into GitHub Container Registry
|
||||||
# TODO: Create a PAT with `read:packages` and `write:packages` scopes and save it as an Actions secret `CR_PAT`
|
# # TODO: Create a PAT with `read:packages` and `write:packages` scopes and save it as an Actions secret `CR_PAT`
|
||||||
run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin
|
# run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin
|
||||||
|
|
||||||
- name: Push image to GitHub Container Registry
|
# - name: Push image to GitHub Container Registry
|
||||||
run: |
|
# run: |
|
||||||
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
|
# IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
|
||||||
|
|
||||||
# Change all uppercase to lowercase
|
# # Change all uppercase to lowercase
|
||||||
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
|
# IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
|
||||||
|
|
||||||
# Strip git ref prefix from version
|
# # Strip git ref prefix from version
|
||||||
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
|
# VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
|
||||||
|
|
||||||
# Strip "v" prefix from tag name
|
# # Strip "v" prefix from tag name
|
||||||
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
|
# [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
|
||||||
|
|
||||||
# Use Docker `latest` tag convention
|
# # Use Docker `latest` tag convention
|
||||||
[ "$VERSION" == "master" ] && VERSION=latest
|
# [ "$VERSION" == "master" ] && VERSION=latest
|
||||||
|
|
||||||
echo IMAGE_ID=$IMAGE_ID
|
# echo IMAGE_ID=$IMAGE_ID
|
||||||
echo VERSION=$VERSION
|
# echo VERSION=$VERSION
|
||||||
|
|
||||||
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
|
# docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
|
||||||
docker push $IMAGE_ID:$VERSION
|
# docker push $IMAGE_ID:$VERSION
|
||||||
|
|
|
@ -9,3 +9,6 @@ test-db/
|
||||||
bot-db*
|
bot-db*
|
||||||
# We store a disabled async test in this file
|
# We store a disabled async test in this file
|
||||||
bigboy
|
bigboy
|
||||||
|
.#*
|
||||||
|
*.sqlite
|
||||||
|
.tmp*
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
46
Cargo.toml
46
Cargo.toml
|
@ -1,42 +1,6 @@
|
||||||
[package]
|
[workspace]
|
||||||
name = "chronicle-dicebot"
|
|
||||||
version = "0.7.0"
|
|
||||||
authors = ["Taylor C. Richberger <taywee@gmx.com>", "projectmoon <projectmoon@agnos.is>"]
|
|
||||||
edition = "2018"
|
|
||||||
license = 'MIT'
|
|
||||||
description = 'A simple async Matrix dicebot'
|
|
||||||
readme = 'README.md'
|
|
||||||
repository = 'https://github.com/ProjectMoon/matrix-dicebot'
|
|
||||||
keywords = ["games", "dice", "matrix", "bot"]
|
|
||||||
categories = ["games"]
|
|
||||||
|
|
||||||
[dependencies]
|
members = [
|
||||||
log = "0.4"
|
"dicebot",
|
||||||
env_logger = "0.7"
|
"rpc"
|
||||||
toml = "0.5"
|
]
|
||||||
nom = "5"
|
|
||||||
rand = "0.7"
|
|
||||||
thiserror = "1.0"
|
|
||||||
itertools = "0.9"
|
|
||||||
async-trait = "0.1"
|
|
||||||
url = "2.1"
|
|
||||||
dirs = "3.0"
|
|
||||||
indoc = "1.0"
|
|
||||||
combine = "4.3"
|
|
||||||
sled = "0.34"
|
|
||||||
zerocopy = "0.3"
|
|
||||||
byteorder = "1.3"
|
|
||||||
futures = "0.3"
|
|
||||||
memmem = "0.1"
|
|
||||||
bincode = "1.3"
|
|
||||||
phf = { version = "0.7", features = ["macros"] }
|
|
||||||
olm-sys = "1.0"
|
|
||||||
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" }
|
|
||||||
|
|
||||||
[dependencies.serde]
|
|
||||||
version = "1"
|
|
||||||
features = ['derive']
|
|
||||||
|
|
||||||
[dependencies.tokio]
|
|
||||||
version = "0.2"
|
|
||||||
features = [ "full" ]
|
|
||||||
|
|
19
Dockerfile
19
Dockerfile
|
@ -1,25 +1,28 @@
|
||||||
# Builder image with development dependencies.
|
# Builder image with development dependencies.
|
||||||
FROM bougyman/voidlinux:glibc as builder
|
FROM ghcr.io/void-linux/void-linux:latest-mini-x86_64 as builder
|
||||||
|
RUN xbps-install -S
|
||||||
|
RUN xbps-install -yu xbps
|
||||||
RUN xbps-install -Syu
|
RUN xbps-install -Syu
|
||||||
RUN xbps-install -Sy base-devel rust cargo cmake wget gnupg
|
RUN xbps-install -Sy base-devel rustup cmake wget gnupg
|
||||||
RUN xbps-install -Sy libressl-devel libstdc++-devel
|
RUN xbps-install -Sy openssl-devel libstdc++-devel
|
||||||
|
RUN rustup-init -qy
|
||||||
|
|
||||||
# Install tini for signal processing and zombie killing
|
# Install tini for signal processing and zombie killing
|
||||||
ENV TINI_VERSION v0.19.0
|
ENV TINI_VERSION v0.19.0
|
||||||
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
|
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
|
||||||
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini.asc /tini.asc
|
|
||||||
RUN gpg --batch --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 595E85A6B1B4779EA4DAAEC70B588DFF0527A9B7 \
|
|
||||||
&& gpg --batch --verify /tini.asc /usr/local/bin/tini
|
|
||||||
RUN chmod +x /usr/local/bin/tini
|
RUN chmod +x /usr/local/bin/tini
|
||||||
|
|
||||||
# Build dicebot
|
# Build dicebot
|
||||||
RUN mkdir -p /root/src
|
RUN mkdir -p /root/src
|
||||||
WORKDIR /root/src
|
WORKDIR /root/src
|
||||||
ADD . ./
|
ADD . ./
|
||||||
RUN cargo build --release
|
RUN . /root/.cargo/env && cargo build --release
|
||||||
|
|
||||||
# Final image
|
# Final image
|
||||||
FROM bougyman/voidlinux:latest
|
FROM ghcr.io/void-linux/void-linux:latest-mini-x86_64
|
||||||
|
RUN xbps-install -S
|
||||||
|
RUN xbps-install -yu xbps
|
||||||
|
RUN xbps-install -Syu
|
||||||
RUN xbps-install -Sy ca-certificates libstdc++
|
RUN xbps-install -Sy ca-certificates libstdc++
|
||||||
COPY --from=builder \
|
COPY --from=builder \
|
||||||
/root/src/target/release/dicebot \
|
/root/src/target/release/dicebot \
|
||||||
|
|
679
LICENSE
679
LICENSE
|
@ -1,7 +1,19 @@
|
||||||
|
This software is governed by the terms of the Affero GNU General
|
||||||
|
Public License. Portions of the code come from the original
|
||||||
|
MIT-licensed project, and the terms of the MIT license also apply to
|
||||||
|
those portions. In files that are partially or wholly subject to the
|
||||||
|
MIT license in addition to the Affero GNU General Public License, this
|
||||||
|
is noted with a header at the top of the file.
|
||||||
|
|
||||||
|
Original upstream project: https://gitlab.com/Taywee/axfive-matrix-dicebot
|
||||||
|
|
||||||
|
For code from the original project that is governed by the MIT license
|
||||||
|
in addition to the Affero GNU General Public License, the following
|
||||||
|
terms apply:
|
||||||
|
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2020 Taylor C. Richberger
|
Copyright (c) 2020 Taylor C. Richberger
|
||||||
Modified work Copyright (c) 2020 projectmoon
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -20,3 +32,668 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
|
The project as a whole is governed by the terms of the Affero GNU
|
||||||
|
General Public License:
|
||||||
|
|
||||||
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU Affero General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
our General Public Licenses are intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing under
|
||||||
|
this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your version
|
||||||
|
supports such interaction) an opportunity to receive the Corresponding
|
||||||
|
Source of your version by providing access to the Corresponding Source
|
||||||
|
from a network server at no charge, through some standard or customary
|
||||||
|
means of facilitating copying of software. This Corresponding Source
|
||||||
|
shall include the Corresponding Source for any work covered by version 3
|
||||||
|
of the GNU General Public License that is incorporated pursuant to the
|
||||||
|
following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU Affero General Public License from time to time. Such new versions
|
||||||
|
will be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU Affero General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU Affero General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU Affero General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for the
|
||||||
|
specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
129
README.md
129
README.md
|
@ -1,28 +1,54 @@
|
||||||
# matrix-dicebot
|
# Tenebrous Dicebot
|
||||||
|
|
||||||
This is a fork of the
|
[![Build Status](https://drone.agnos.is/api/badges/projectmoon/tenebrous-dicebot/status.svg)](https://drone.agnos.is/projectmoon/tenebrous-dicebot)
|
||||||
[axfive-matrix-dicebot](https://gitlab.com/Taywee/axfive-matrix-dicebot)
|
[![Matrix Chat](https://img.shields.io/matrix/tenebrous:agnos.is?label=matrix&server_fqdn=matrix.org)][matrix-room]
|
||||||
with basic support for the Chronicles of Darkness 2E Storytelling
|
|
||||||
System, with future plans to extend the codebase further to support
|
_This repository is hosted on [Agnos.is Git][main-repo] and mirrored
|
||||||
variables and perhaps character sheet management.
|
to [GitHub][github-repo]._
|
||||||
|
|
||||||
|
This is a dice rolling bot for facilitating roleplaying games on the
|
||||||
|
Matrix messaging platform. It currently has basic support for the
|
||||||
|
Chronicles of Darkness 2E Storytelling System and Call of Cthulhu,
|
||||||
|
with plans to extend the codebase further to support other systems and
|
||||||
|
character sheet management.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
`matrix-dicebot` is a basic dice rolling bot. It currently has the
|
`tenebrous-dicebot` is a dice rolling bot for facilitating
|
||||||
following features:
|
role-playing games over Matrix (and anything that Matrix can bridge
|
||||||
|
to, like Discord). It currently has the following features:
|
||||||
|
|
||||||
* Rolling arbitrary dice expressions (e.g. 1d4, 1d20+5, 1d8+1d6, etc).
|
* Rolling arbitrary dice expressions (e.g. 1d4, 1d20+5, 1d8+1d6, etc).
|
||||||
* Rolling dice pools for the Chronicles of Darkness 2E Storytelling
|
* Rolling dice pools for the Chronicles of Darkness 2E Storytelling
|
||||||
System.
|
System.
|
||||||
|
* Rolling dice for the Call of Cthulhu system.
|
||||||
* Works in encrypted or unencrypted Matrix rooms.
|
* Works in encrypted or unencrypted Matrix rooms.
|
||||||
|
* Storing variables created by the user.
|
||||||
|
|
||||||
|
## Support and Community
|
||||||
|
|
||||||
|
The project has a Matrix room at [#tenebrous:agnos.is][matrix-room].
|
||||||
|
It is also possible to make a post in [GitHub
|
||||||
|
Discussions][github-discussions].
|
||||||
|
|
||||||
|
For reporting bugs, we prefer that you open an issue on
|
||||||
|
[git.agnos.is][agnosis-git-issues]. However, you may also open an
|
||||||
|
issue on [GitHub][github-issues].
|
||||||
|
|
||||||
|
### Development and Contributions
|
||||||
|
|
||||||
|
All development occurs on [git.agnos.is][main-repo]. If you wish to
|
||||||
|
contribute, please open a pull request there. In some cases, pull
|
||||||
|
requests from GitHub may be accepted. All contributions must be
|
||||||
|
licensed under [AGPL 3.0 or later][agpl] to be accepted.
|
||||||
|
|
||||||
## Building and Installation
|
## Building and Installation
|
||||||
|
|
||||||
### Docker Image
|
### Docker Image
|
||||||
|
|
||||||
The easiest way to run the dice bot is to use the official Docker
|
The easiest way to run the dice bot is to use the [official Docker
|
||||||
image. It is distributed on GitHub Container Registry by a CI
|
image][docker-image]. It is distributed on GitHub Container Registry
|
||||||
pipeline.
|
by a CI pipeline.
|
||||||
|
|
||||||
The `latest` tag always points to the most recent successfully built
|
The `latest` tag always points to the most recent successfully built
|
||||||
master commit and is considered unstable, while individual tags are
|
master commit and is considered unstable, while individual tags are
|
||||||
|
@ -38,10 +64,21 @@ root of the repository.
|
||||||
After pulling or building the image, see [instructions on how to use
|
After pulling or building the image, see [instructions on how to use
|
||||||
the Docker image](#running-the-bot).
|
the Docker image](#running-the-bot).
|
||||||
|
|
||||||
|
### Install from crates.io
|
||||||
|
|
||||||
|
The project can be from [crates.io][crates-io]. To install it, execute
|
||||||
|
`cargo install tenebrous-dicebot`. This will make the following
|
||||||
|
executables available on your system:
|
||||||
|
|
||||||
|
* `dicebot`: Main dicebot executable.
|
||||||
|
* `dicebot-cmd`: Run dicebot commands from the command line.
|
||||||
|
* `dicebot_migrate`: Standalone database migrator (not required).
|
||||||
|
* `tonic_client`: Test client for the gRPC connection (not required).
|
||||||
|
|
||||||
### Build from Source
|
### Build from Source
|
||||||
|
|
||||||
Precompiled executables are not yet available. Clone this repository
|
Precompiled executables are not yet available. Clone this repository
|
||||||
and run `OLM_LINK_VARIANT=dylib cargo install`.
|
and run `cargo install`.
|
||||||
|
|
||||||
Building the project requires:
|
Building the project requires:
|
||||||
|
|
||||||
|
@ -49,13 +86,9 @@ Building the project requires:
|
||||||
on Void and Arch, etc).
|
on Void and Arch, etc).
|
||||||
* Rust 1.45.0 or higher.
|
* Rust 1.45.0 or higher.
|
||||||
* OpenSSL/LibreSSL development headers installed.
|
* OpenSSL/LibreSSL development headers installed.
|
||||||
* `olm-sys` crate dependencies: cmake, libstdc++, libolm and its
|
* `olm-sys` crate dependencies: cmake, libstdc++.
|
||||||
development headers.
|
|
||||||
* glibc.
|
* glibc.
|
||||||
|
|
||||||
Note: The `olm-sys` crate must be built in dynamic linking mode until
|
|
||||||
a [bug][gnome-bug] in its build process is fixed.
|
|
||||||
|
|
||||||
#### Why doesn't it build on musl libc?
|
#### Why doesn't it build on musl libc?
|
||||||
|
|
||||||
As far as I can tell, the project doesn't build on musl libc. It
|
As far as I can tell, the project doesn't build on musl libc. It
|
||||||
|
@ -85,8 +118,16 @@ expressions.
|
||||||
!r 3d12 - 5d2 + 3 - 7d3 + 20d20
|
!r 3d12 - 5d2 + 3 - 7d3 + 20d20
|
||||||
```
|
```
|
||||||
|
|
||||||
This system does not yet have the capability to handle things like D&D
|
#### Keep/Drop Dice
|
||||||
5e advantage or disadvantage.
|
The bot supports either keeping the highest dice in a roll, or
|
||||||
|
dropping the highest dice in a roll. This allows the bot to handle
|
||||||
|
things like D&D 5e advantage or disadvantage.
|
||||||
|
|
||||||
|
```
|
||||||
|
!roll 2d20k1
|
||||||
|
!r 2d20dh1 + 5
|
||||||
|
!r 10d10k5 + 10d10dh5 - 2
|
||||||
|
```
|
||||||
|
|
||||||
### Storytelling System
|
### Storytelling System
|
||||||
|
|
||||||
|
@ -105,12 +146,29 @@ colon.
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
```
|
```
|
||||||
!pool 8 //regular pool of 8 dice
|
!pool 8 //regular pool of 8 dice
|
||||||
!pool n:8 //roll 8 dice, 9-again
|
!pool n:8 //roll 8 dice, 9-again
|
||||||
!pool ns3:8 //roll 8 dice, 9-again with only 3 successes for exceptional
|
!pool ns3:8 //roll 8 dice, 9-again with only 3 successes for exceptional
|
||||||
!pool rs2:5 //5 dice, rote quality, 2 successes for exceptional
|
!pool rs2:5 //5 dice, rote quality, 2 successes for exceptional
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Call of Cthulhu System
|
||||||
|
|
||||||
|
The commands `!cthRoll`, `!cthroll`, `!cthARoll` and `!cthadv` are for
|
||||||
|
the Call of Cthulhu system. `!cthRoll` and `!cthroll` are for rolling
|
||||||
|
percentile dice against a target number. A `b:` or `bb:` can be
|
||||||
|
prepended to get one or two bonus dice.
|
||||||
|
|
||||||
|
`!cthARoll` and `!cthadv` are for skill advancement.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
```
|
||||||
|
!cthRoll 50 //roll against a target of 50
|
||||||
|
!cthRoll bb:60 //roll against a target of 60 with 2 bonus dice
|
||||||
|
!cthARoll 30 //advancement roll against a target of 30
|
||||||
|
```
|
||||||
|
|
||||||
### User Variables
|
### User Variables
|
||||||
|
|
||||||
Users can store variables for use with the Storytelling dice pool
|
Users can store variables for use with the Storytelling dice pool
|
||||||
|
@ -125,8 +183,11 @@ Examples:
|
||||||
!get myvar //will print 5
|
!get myvar //will print 5
|
||||||
```
|
```
|
||||||
|
|
||||||
Variables can be referenced in dice pool rolling expressions, for
|
Variables can be referenced in dice pool and Call of Cthulhu rolling
|
||||||
example `!pool myvar` or `!pool myvar+3`.
|
expressions, for example `!pool myvar` or `!pool myvar+3` or `!cthroll
|
||||||
|
myvar`. The Call of Cthulhu advancement roll also accepts variables,
|
||||||
|
and if a variable is used, and the roll is successful, it will update
|
||||||
|
the variable with the new skill.
|
||||||
|
|
||||||
## Running the Bot
|
## Running the Bot
|
||||||
|
|
||||||
|
@ -162,7 +223,7 @@ home_server = 'https://example.com'
|
||||||
username = 'thisismyusername'
|
username = 'thisismyusername'
|
||||||
password = 'thisismypassword'
|
password = 'thisismypassword'
|
||||||
|
|
||||||
[datbase]
|
[database]
|
||||||
path = '/path/to/database/directory/'
|
path = '/path/to/database/directory/'
|
||||||
|
|
||||||
[bot]
|
[bot]
|
||||||
|
@ -212,14 +273,28 @@ commands locally.
|
||||||
|
|
||||||
The most basic plans are:
|
The most basic plans are:
|
||||||
|
|
||||||
* To add support for simple per-user variable management, e.g. setting
|
* Resource counting: creation of custom counters that can go up and
|
||||||
a name to a value (`gnosis = 3`) and then using those in dice rolls.
|
down.
|
||||||
This lays the foundation for character sheet integration.
|
|
||||||
* Perhaps some sort of character sheet integration. But for that, we
|
* Perhaps some sort of character sheet integration. But for that, we
|
||||||
would need a sheet service.
|
would need a sheet service.
|
||||||
* Use environment variables instead of config file in Docker image.
|
* Use environment variables instead of config file in Docker image.
|
||||||
|
* Per-system game rules.
|
||||||
|
|
||||||
[gnome-bug]: https://gitlab.gnome.org/BrainBlasted/olm-sys/-/issues/6
|
## Credits
|
||||||
|
|
||||||
|
This was orignally a fork of the [axfive-matrix-dicebot][axfive], with
|
||||||
|
support added for Chronicles of Darkness and Call of Cthulhu.
|
||||||
|
|
||||||
|
[axfive]: https://gitlab.com/Taywee/axfive-matrix-dicebot
|
||||||
[config-file]: #Configuration-File
|
[config-file]: #Configuration-File
|
||||||
[docker-image]: https://github.com/users/ProjectMoon/packages/container/package/chronicle-dicebot
|
[docker-image]: https://github.com/users/ProjectMoon/packages/container/package/chronicle-dicebot
|
||||||
[dirs]: https://docs.rs/dirs/2.0.2/dirs/
|
[dirs]: https://docs.rs/dirs/2.0.2/dirs/
|
||||||
|
[main-repo]: https://git.agnos.is/projectmoon/tenebrous-dicebot
|
||||||
|
[github-repo]: https://github.com/ProjectMoon/matrix-dicebot
|
||||||
|
[roadmap]: https://git.agnos.is/projectmoon/tenebrous-dicebot/wiki/Roadmap
|
||||||
|
[crates-io]: https://crates.io/crates/tenebrous-dicebot
|
||||||
|
[matrix-room]: https://matrix.to/#/#tenebrous:agnos.is
|
||||||
|
[agnosis-git-issues]: https://git.agnos.is/projectmoon/tenebrous-dicebot/issues
|
||||||
|
[github-discussions]: https://github.com/ProjectMoon/matrix-dicebot/discussions
|
||||||
|
[github-issues]: https://github.com/ProjectMoon/matrix-dicebot/issues
|
||||||
|
[agpl]: https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
[package]
|
||||||
|
name = "tenebrous-dicebot"
|
||||||
|
version = "0.13.2"
|
||||||
|
rust-version = "1.68"
|
||||||
|
authors = ["projectmoon <projectmoon@agnos.is>", "Taylor C. Richberger <taywee@gmx.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
license = 'AGPL-3.0-or-later'
|
||||||
|
description = 'An async Matrix dice bot for role-playing games'
|
||||||
|
readme = '../README.md'
|
||||||
|
repository = 'https://git.agnos.is/projectmoon/matrix-dicebot'
|
||||||
|
keywords = ["games", "dice", "matrix", "bot"]
|
||||||
|
categories = ["games"]
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tonic-build = "0.4"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# indexmap version locked fixes a dependency cycle.
|
||||||
|
# indexmap = "=1.6.2"
|
||||||
|
log = "0.4"
|
||||||
|
tracing-subscriber = "0.2"
|
||||||
|
toml = "0.5"
|
||||||
|
nom = "5"
|
||||||
|
rand = "0.8"
|
||||||
|
rust-argon2 = "0.8"
|
||||||
|
thiserror = "1.0"
|
||||||
|
itertools = "0.10"
|
||||||
|
async-trait = "0.1"
|
||||||
|
url = "2.1"
|
||||||
|
dirs = "3.0"
|
||||||
|
indoc = "1.0"
|
||||||
|
combine = "4.5"
|
||||||
|
futures = "0.3"
|
||||||
|
html2text = "0.2"
|
||||||
|
phf = { version = "0.8", features = ["macros"] }
|
||||||
|
matrix-sdk = { version = "0.6" }
|
||||||
|
refinery = { version = "0.8", features = ["rusqlite"]}
|
||||||
|
barrel = { version = "0.7", features = ["sqlite3"] }
|
||||||
|
strum = { version = "0.22", features = ["derive"] }
|
||||||
|
tempfile = "3"
|
||||||
|
substring = "1.4"
|
||||||
|
fuse-rust = "0.2"
|
||||||
|
tonic = "0.4"
|
||||||
|
prost = "0.7"
|
||||||
|
tenebrous-rpc = { path = "../rpc", version = "0.1.0" }
|
||||||
|
|
||||||
|
[dependencies.sqlx]
|
||||||
|
version = "0.6"
|
||||||
|
features = [ "offline", "sqlite", "runtime-tokio-native-tls" ]
|
||||||
|
|
||||||
|
[dependencies.serde]
|
||||||
|
version = "1"
|
||||||
|
features = ['derive']
|
||||||
|
|
||||||
|
[dependencies.tokio]
|
||||||
|
version = "1"
|
||||||
|
features = [ "full" ]
|
|
@ -0,0 +1,139 @@
|
||||||
|
{
|
||||||
|
"db": "SQLite",
|
||||||
|
"19d89370cac05c1bc4de0eb3508712da9ca133b1cf9445b5407d238f89c3ab0c": {
|
||||||
|
"query": "SELECT device_id FROM bot_state limit 1",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "device_id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 0
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"26903a92a7de34df3e227fe599e41ae1bb61612eb80befad398383af36df0ce4": {
|
||||||
|
"query": "DELETE FROM accounts WHERE user_id = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"2d4a32735da04509c2e3c4f99bef79ef699964f58ae332b0611f3de088596e1e": {
|
||||||
|
"query": "INSERT INTO accounts (user_id, password, account_status)\n VALUES (?, ?, ?)\n ON CONFLICT(user_id) DO\n UPDATE SET password = ?, account_status = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 5
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"59313c67900a1a9399389720b522e572f181ae503559cd2b49d6305acb9e2207": {
|
||||||
|
"query": "SELECT key, value as \"value: i32\" FROM user_variables\n WHERE room_id = ? AND user_id = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "key",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "value: i32",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Int64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 2
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"636b1b868eaf04cd234fbf17747d94a66e81f7bc1b060ba14151dbfaf40eeefc": {
|
||||||
|
"query": "SELECT value as \"value: i32\" FROM user_variables\n WHERE user_id = ? AND room_id = ? AND key = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "value: i32",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Int64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 3
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"667b26343ce44e1c48ac689ce887ef6a0558a2ce199f7372a5dce58672499c5a": {
|
||||||
|
"query": "INSERT INTO user_state (user_id, active_room)\n VALUES (?, ?)\n ON CONFLICT(user_id) DO\n UPDATE SET active_room = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 3
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"711d222911c1258365a6a0de1fe00eeec4686fd3589e976e225ad599e7cfc75d": {
|
||||||
|
"query": "SELECT count(*) as \"count: i32\" FROM user_variables\n WHERE room_id = ? and user_id = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "count: i32",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Int"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 2
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"bba0fc255e7c30d1d2d9468c68ba38db6e8a13be035aa1152933ba9247b14f8c": {
|
||||||
|
"query": "SELECT event_id FROM room_events\n WHERE room_id = ? AND event_id = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "event_id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 2
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dce9bb45cf954054a920ee8b53852c6d562e3588d76bbfaa1433d8309d4e4921": {
|
||||||
|
"query": "DELETE FROM user_state WHERE user_id = ?",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,57 @@
|
||||||
pub mod parser;
|
/**
|
||||||
|
* In addition to the terms of the AGPL, this file is governed by the
|
||||||
|
* terms of the MIT license, from the original axfive-matrix-dicebot
|
||||||
|
* project.
|
||||||
|
*/
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
/// A basic dice roll, in XdY notation, like "1d4" or "3d6".
|
||||||
|
/// Optionally supports D&D advantage/disadvantge keep-or-drop
|
||||||
|
/// functionality.
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub struct Dice {
|
pub struct Dice {
|
||||||
pub(crate) count: u32,
|
pub(crate) count: u32,
|
||||||
pub(crate) sides: u32,
|
pub(crate) sides: u32,
|
||||||
|
pub(crate) keep_drop: KeepOrDrop,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enum indicating how to handle bonuses or penalties using extra
|
||||||
|
/// dice. If set to Keep, the roll will keep the highest X number of
|
||||||
|
/// dice in the roll, and add those together. If set to Drop, the
|
||||||
|
/// opposite is performed, and the lowest X number of dice are added
|
||||||
|
/// instead. If set to None, then all dice in the roll are added up as
|
||||||
|
/// normal.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
pub enum KeepOrDrop {
|
||||||
|
/// Keep only the X highest dice for adding up to the total.
|
||||||
|
Keep(u32),
|
||||||
|
|
||||||
|
/// Keep only the X lowest dice (i.e. drop the highest) for adding
|
||||||
|
/// up to the total.
|
||||||
|
Drop(u32),
|
||||||
|
|
||||||
|
/// Add up all dice in the roll for the total.
|
||||||
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Dice {
|
impl fmt::Display for Dice {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}d{}", self.count, self.sides)
|
match self.keep_drop {
|
||||||
|
KeepOrDrop::Keep(keep) => write!(f, "{}d{}k{}", self.count, self.sides, keep),
|
||||||
|
KeepOrDrop::Drop(drop) => write!(f, "{}d{}dh{}", self.count, self.sides, drop),
|
||||||
|
KeepOrDrop::None => write!(f, "{}d{}", self.count, self.sides),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dice {
|
impl Dice {
|
||||||
fn new(count: u32, sides: u32) -> Dice {
|
pub fn new(count: u32, sides: u32, keep_drop: KeepOrDrop) -> Dice {
|
||||||
Dice { count, sides }
|
Dice {
|
||||||
|
count,
|
||||||
|
sides,
|
||||||
|
keep_drop,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +86,7 @@ impl fmt::Display for SignedElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct ElementExpression(Vec<SignedElement>);
|
pub struct ElementExpression(pub Vec<SignedElement>);
|
||||||
|
|
||||||
impl Deref for ElementExpression {
|
impl Deref for ElementExpression {
|
||||||
type Target = Vec<SignedElement>;
|
type Target = Vec<SignedElement>;
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod dice;
|
||||||
|
pub mod parser;
|
||||||
|
pub mod roll;
|
|
@ -0,0 +1,360 @@
|
||||||
|
/**
|
||||||
|
* In addition to the terms of the AGPL, this file is governed by the
|
||||||
|
* terms of the MIT license, from the original axfive-matrix-dicebot
|
||||||
|
* project.
|
||||||
|
*/
|
||||||
|
use nom::bytes::complete::take_while;
|
||||||
|
use nom::error::ErrorKind as NomErrorKind;
|
||||||
|
use nom::Err as NomErr;
|
||||||
|
use nom::{
|
||||||
|
alt, bytes::complete::tag, character::complete::digit1, complete, many0, named,
|
||||||
|
sequence::tuple, tag, IResult,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::dice::*;
|
||||||
|
|
||||||
|
//******************************
|
||||||
|
//Legacy Code
|
||||||
|
//******************************
|
||||||
|
|
||||||
|
fn is_whitespace(input: char) -> bool {
|
||||||
|
input == ' ' || input == '\n' || input == '\t' || input == '\r'
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Eat whitespace, returning it
|
||||||
|
pub fn eat_whitespace(input: &str) -> IResult<&str, &str> {
|
||||||
|
let (input, whitespace) = take_while(is_whitespace)(input)?;
|
||||||
|
Ok((input, whitespace))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
enum Sign {
|
||||||
|
Plus,
|
||||||
|
Minus,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Intermediate parsed value for a keep-drop expression to indicate
|
||||||
|
/// which one it is.
|
||||||
|
enum ParsedKeepOrDrop<'a> {
|
||||||
|
Keep(&'a str),
|
||||||
|
Drop(&'a str),
|
||||||
|
NotPresent,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! too_big {
|
||||||
|
($input: expr) => {
|
||||||
|
NomErr::Error(($input, NomErrorKind::TooLarge))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a dice expression. Does not eat whitespace
|
||||||
|
fn parse_dice(input: &str) -> IResult<&str, Dice> {
|
||||||
|
let (input, (count, _, sides)) = tuple((digit1, tag("d"), digit1))(input)?;
|
||||||
|
let count: u32 = count.parse().map_err(|_| too_big!(count))?;
|
||||||
|
let sides = sides.parse().map_err(|_| too_big!(sides))?;
|
||||||
|
let (input, keep_drop) = parse_keep_or_drop(input, count)?;
|
||||||
|
Ok((input, Dice::new(count, sides, keep_drop)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract keep/drop number as a string. Fails if the value is not a
|
||||||
|
/// string.
|
||||||
|
fn parse_keep_or_drop_text<'a>(
|
||||||
|
symbol: &'a str,
|
||||||
|
input: &'a str,
|
||||||
|
) -> IResult<&'a str, ParsedKeepOrDrop<'a>> {
|
||||||
|
let (parsed_kd, input) = match tuple::<&str, _, (_, _), _>((tag(symbol), digit1))(input) {
|
||||||
|
// if ok, one of the expressions is present
|
||||||
|
Ok((rest, (_, kd_expr))) => match symbol {
|
||||||
|
"k" => (ParsedKeepOrDrop::Keep(kd_expr), rest),
|
||||||
|
"dh" => (ParsedKeepOrDrop::Drop(kd_expr), rest),
|
||||||
|
_ => panic!("Unrecogized keep-drop symbol: {}", symbol),
|
||||||
|
},
|
||||||
|
// otherwise absent (attempt to keep all dice)
|
||||||
|
Err(_) => (ParsedKeepOrDrop::NotPresent, input),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((input, parsed_kd))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse keep/drop expression, which consits of "k" or "dh" following
|
||||||
|
/// a dice expression. For example, "1d4h3" or "1d4dh2".
|
||||||
|
fn parse_keep_or_drop<'a>(input: &'a str, count: u32) -> IResult<&'a str, KeepOrDrop> {
|
||||||
|
let (input, keep) = parse_keep_or_drop_text("k", input)?;
|
||||||
|
let (input, drop) = parse_keep_or_drop_text("dh", input)?;
|
||||||
|
|
||||||
|
use ParsedKeepOrDrop::*;
|
||||||
|
let keep_drop: KeepOrDrop = match (keep, drop) {
|
||||||
|
//Potential valid Keep expression.
|
||||||
|
(Keep(keep), NotPresent) => match keep.parse().map_err(|_| too_big!(input))? {
|
||||||
|
_i if _i > count || _i == 0 => Ok(KeepOrDrop::None),
|
||||||
|
i => Ok(KeepOrDrop::Keep(i)),
|
||||||
|
},
|
||||||
|
//Potential valid Drop expression.
|
||||||
|
(NotPresent, Drop(drop)) => match drop.parse().map_err(|_| too_big!(input))? {
|
||||||
|
_i if _i >= count => Ok(KeepOrDrop::None),
|
||||||
|
i => Ok(KeepOrDrop::Drop(i)),
|
||||||
|
},
|
||||||
|
//No Keep or Drop specified; regular behavior.
|
||||||
|
(NotPresent, NotPresent) => Ok(KeepOrDrop::None),
|
||||||
|
//Anything else is an error.
|
||||||
|
_ => Err(NomErr::Error((input, NomErrorKind::Many1))),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
Ok((input, keep_drop))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a single digit expression. Does not eat whitespace
|
||||||
|
fn parse_bonus(input: &str) -> IResult<&str, u32> {
|
||||||
|
let (input, bonus) = digit1(input)?;
|
||||||
|
Ok((input, bonus.parse().unwrap()))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a sign expression. Eats whitespace.
|
||||||
|
fn parse_sign(input: &str) -> IResult<&str, Sign> {
|
||||||
|
let (input, _) = eat_whitespace(input)?;
|
||||||
|
named!(sign(&str) -> Sign, alt!(
|
||||||
|
complete!(tag!("+")) => { |_| Sign::Plus } |
|
||||||
|
complete!(tag!("-")) => { |_| Sign::Minus }
|
||||||
|
));
|
||||||
|
|
||||||
|
let (input, sign) = sign(input)?;
|
||||||
|
Ok((input, sign))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse an element expression. Eats whitespace.
|
||||||
|
fn parse_element(input: &str) -> IResult<&str, Element> {
|
||||||
|
let (input, _) = eat_whitespace(input)?;
|
||||||
|
named!(element(&str) -> Element, alt!(
|
||||||
|
parse_dice => { |d| Element::Dice(d) } |
|
||||||
|
parse_bonus => { |b| Element::Bonus(b) }
|
||||||
|
));
|
||||||
|
|
||||||
|
let (input, element) = element(input)?;
|
||||||
|
Ok((input, element))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a signed element expression. Eats whitespace.
|
||||||
|
fn parse_signed_element(input: &str) -> IResult<&str, SignedElement> {
|
||||||
|
let (input, _) = eat_whitespace(input)?;
|
||||||
|
let (input, sign) = parse_sign(input)?;
|
||||||
|
let (input, _) = eat_whitespace(input)?;
|
||||||
|
|
||||||
|
let (input, element) = parse_element(input)?;
|
||||||
|
let element = match sign {
|
||||||
|
Sign::Plus => SignedElement::Positive(element),
|
||||||
|
Sign::Minus => SignedElement::Negative(element),
|
||||||
|
};
|
||||||
|
Ok((input, element))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse a full element expression. Eats whitespace.
|
||||||
|
pub fn parse_element_expression(input: &str) -> IResult<&str, ElementExpression> {
|
||||||
|
named!(first_element(&str) -> SignedElement, alt!(
|
||||||
|
parse_signed_element => { |e| e } |
|
||||||
|
parse_element => { |e| SignedElement::Positive(e) }
|
||||||
|
));
|
||||||
|
let (input, first) = first_element(input)?;
|
||||||
|
let (input, rest) = if input.trim().is_empty() {
|
||||||
|
(input, vec![first])
|
||||||
|
} else {
|
||||||
|
named!(rest_elements(&str) -> Vec<SignedElement>, many0!(parse_signed_element));
|
||||||
|
let (input, mut rest) = rest_elements(input)?;
|
||||||
|
rest.insert(0, first);
|
||||||
|
(input, rest)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((input, ElementExpression(rest)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn dice_test() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("2d4"),
|
||||||
|
Ok(("", Dice::new(2, 4, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("20d40"),
|
||||||
|
Ok(("", Dice::new(20, 40, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("8d7"),
|
||||||
|
Ok(("", Dice::new(8, 7, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("2d20k1"),
|
||||||
|
Ok(("", Dice::new(2, 20, KeepOrDrop::Keep(1))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("100d10k90"),
|
||||||
|
Ok(("", Dice::new(100, 10, KeepOrDrop::Keep(90))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("11d10k10"),
|
||||||
|
Ok(("", Dice::new(11, 10, KeepOrDrop::Keep(10))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("12d10k11"),
|
||||||
|
Ok(("", Dice::new(12, 10, KeepOrDrop::Keep(11))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("12d10k13"),
|
||||||
|
Ok(("", Dice::new(12, 10, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("12d10k0"),
|
||||||
|
Ok(("", Dice::new(12, 10, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("20d40dh5"),
|
||||||
|
Ok(("", Dice::new(20, 40, KeepOrDrop::Drop(5))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("8d7dh9"),
|
||||||
|
Ok(("", Dice::new(8, 7, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_dice("8d7dh8"),
|
||||||
|
Ok(("", Dice::new(8, 7, KeepOrDrop::None)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cant_have_both_keep_and_drop_test() {
|
||||||
|
let res = parse_dice("1d4k3dh2");
|
||||||
|
assert!(res.is_err());
|
||||||
|
match res {
|
||||||
|
Err(NomErr::Error((_, kind))) => {
|
||||||
|
assert_eq!(kind, NomErrorKind::Many1);
|
||||||
|
}
|
||||||
|
_ => panic!("Got success, expected error"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn big_number_of_dice_doesnt_crash_test() {
|
||||||
|
let res = parse_dice("64378631476346123874527551481376547657868536d4");
|
||||||
|
assert!(res.is_err());
|
||||||
|
match res {
|
||||||
|
Err(NomErr::Error((input, kind))) => {
|
||||||
|
assert_eq!(kind, NomErrorKind::TooLarge);
|
||||||
|
assert_eq!(input, "64378631476346123874527551481376547657868536");
|
||||||
|
}
|
||||||
|
_ => panic!("Got success, expected error"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn big_number_of_sides_doesnt_crash_test() {
|
||||||
|
let res = parse_dice("1d423562312587425472658956278456298376234876");
|
||||||
|
assert!(res.is_err());
|
||||||
|
match res {
|
||||||
|
Err(NomErr::Error((input, kind))) => {
|
||||||
|
assert_eq!(kind, NomErrorKind::TooLarge);
|
||||||
|
assert_eq!(input, "423562312587425472658956278456298376234876");
|
||||||
|
}
|
||||||
|
_ => panic!("Got success, expected error"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn element_test() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_element(" \t\n\r\n 8d7 \n"),
|
||||||
|
Ok((" \n", Element::Dice(Dice::new(8, 7, KeepOrDrop::None))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_element(" \t\n\r\n 3d20k2 \n"),
|
||||||
|
Ok((" \n", Element::Dice(Dice::new(3, 20, KeepOrDrop::Keep(2)))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_element(" \t\n\r\n 8 \n"),
|
||||||
|
Ok((" \n", Element::Bonus(8)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn signed_element_test() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_signed_element("+ 7"),
|
||||||
|
Ok(("", SignedElement::Positive(Element::Bonus(7))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_signed_element(" \t\n\r\n- 8 \n"),
|
||||||
|
Ok((" \n", SignedElement::Negative(Element::Bonus(8))))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_signed_element(" \t\n\r\n- 8d4 \n"),
|
||||||
|
Ok((
|
||||||
|
" \n",
|
||||||
|
SignedElement::Negative(Element::Dice(Dice::new(8, 4, KeepOrDrop::None)))
|
||||||
|
))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_signed_element(" \t\n\r\n- 8d4k4 \n"),
|
||||||
|
Ok((
|
||||||
|
" \n",
|
||||||
|
SignedElement::Negative(Element::Dice(Dice::new(8, 4, KeepOrDrop::Keep(4))))
|
||||||
|
))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_signed_element(" \t\n\r\n+ 8d4 \n"),
|
||||||
|
Ok((
|
||||||
|
" \n",
|
||||||
|
SignedElement::Positive(Element::Dice(Dice::new(8, 4, KeepOrDrop::None)))
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn element_expression_test() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_element_expression("8d4"),
|
||||||
|
Ok((
|
||||||
|
"",
|
||||||
|
ElementExpression(vec![SignedElement::Positive(Element::Dice(Dice::new(
|
||||||
|
8,
|
||||||
|
4,
|
||||||
|
KeepOrDrop::None
|
||||||
|
)))])
|
||||||
|
))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_element_expression("\t2d20k1 + 5"),
|
||||||
|
Ok((
|
||||||
|
"",
|
||||||
|
ElementExpression(vec![
|
||||||
|
SignedElement::Positive(Element::Dice(Dice::new(2, 20, KeepOrDrop::Keep(1)))),
|
||||||
|
SignedElement::Positive(Element::Bonus(5)),
|
||||||
|
])
|
||||||
|
))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_element_expression(" - 8d4 \n "),
|
||||||
|
Ok((
|
||||||
|
" \n ",
|
||||||
|
ElementExpression(vec![SignedElement::Negative(Element::Dice(Dice::new(
|
||||||
|
8,
|
||||||
|
4,
|
||||||
|
KeepOrDrop::None
|
||||||
|
)))])
|
||||||
|
))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_element_expression("\t3d4k2 + 7 - 5 - 6d12dh3 + 1d1 + 53 1d5 "),
|
||||||
|
Ok((
|
||||||
|
" 1d5 ",
|
||||||
|
ElementExpression(vec![
|
||||||
|
SignedElement::Positive(Element::Dice(Dice::new(3, 4, KeepOrDrop::Keep(2)))),
|
||||||
|
SignedElement::Positive(Element::Bonus(7)),
|
||||||
|
SignedElement::Negative(Element::Bonus(5)),
|
||||||
|
SignedElement::Negative(Element::Dice(Dice::new(6, 12, KeepOrDrop::Drop(3)))),
|
||||||
|
SignedElement::Positive(Element::Dice(Dice::new(1, 1, KeepOrDrop::None))),
|
||||||
|
SignedElement::Positive(Element::Bonus(53)),
|
||||||
|
])
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,10 @@
|
||||||
use crate::dice;
|
/**
|
||||||
|
* In addition to the terms of the AGPL, this file is governed by the
|
||||||
|
* terms of the MIT license, from the original axfive-matrix-dicebot
|
||||||
|
* project.
|
||||||
|
*/
|
||||||
|
use crate::basic::dice;
|
||||||
|
use crate::basic::dice::KeepOrDrop;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
@ -14,15 +20,27 @@ pub trait Rolled {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct DiceRoll(pub Vec<u32>);
|
/// array of rolls in order, how many dice to keep, and how many to drop
|
||||||
|
/// keep indicates how many of the highest dice to keep
|
||||||
|
/// drop indicates how many of the highest dice to drop
|
||||||
|
pub struct DiceRoll (pub Vec<u32>, usize, usize);
|
||||||
|
|
||||||
impl DiceRoll {
|
impl DiceRoll {
|
||||||
pub fn rolls(&self) -> &[u32] {
|
pub fn rolls(&self) -> &[u32] {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn keep(&self) -> usize {
|
||||||
|
self.1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drop(&self) -> usize {
|
||||||
|
self.2
|
||||||
|
}
|
||||||
|
|
||||||
|
// only count kept dice in total
|
||||||
pub fn total(&self) -> u32 {
|
pub fn total(&self) -> u32 {
|
||||||
self.0.iter().sum()
|
self.0[self.2..self.1].iter().sum()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,11 +54,21 @@ impl fmt::Display for DiceRoll {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", self.rolled_value())?;
|
write!(f, "{}", self.rolled_value())?;
|
||||||
let rolls = self.rolls();
|
let rolls = self.rolls();
|
||||||
let mut iter = rolls.iter();
|
let keep = self.keep();
|
||||||
|
let drop = self.drop();
|
||||||
|
let mut iter = rolls.iter().enumerate();
|
||||||
if let Some(first) = iter.next() {
|
if let Some(first) = iter.next() {
|
||||||
write!(f, " ({}", first)?;
|
if drop != 0 {
|
||||||
|
write!(f, " ([{}]", first.1)?;
|
||||||
|
} else {
|
||||||
|
write!(f, " ({}", first.1)?;
|
||||||
|
}
|
||||||
for roll in iter {
|
for roll in iter {
|
||||||
write!(f, " + {}", roll)?;
|
if roll.0 >= keep || roll.0 < drop {
|
||||||
|
write!(f, " + [{}]", roll.1)?;
|
||||||
|
} else {
|
||||||
|
write!(f, " + {}", roll.1)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
write!(f, ")")?;
|
write!(f, ")")?;
|
||||||
}
|
}
|
||||||
|
@ -53,11 +81,17 @@ impl Roll for dice::Dice {
|
||||||
|
|
||||||
fn roll(&self) -> DiceRoll {
|
fn roll(&self) -> DiceRoll {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let rolls: Vec<_> = (0..self.count)
|
let mut rolls: Vec<_> = (0..self.count)
|
||||||
.map(|_| rng.gen_range(1, self.sides + 1))
|
.map(|_| rng.gen_range(1..=self.sides))
|
||||||
.collect();
|
.collect();
|
||||||
|
// sort rolls in descending order
|
||||||
|
rolls.sort_by(|a, b| b.cmp(a));
|
||||||
|
|
||||||
DiceRoll(rolls)
|
match self.keep_drop {
|
||||||
|
KeepOrDrop::Keep(k) => DiceRoll(rolls,k as usize, 0),
|
||||||
|
KeepOrDrop::Drop(dh) => DiceRoll(rolls,self.count as usize, dh as usize),
|
||||||
|
KeepOrDrop::None => DiceRoll(rolls,self.count as usize, 0),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,18 +227,26 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn dice_roll_display_test() {
|
fn dice_roll_display_test() {
|
||||||
assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)");
|
assert_eq!(DiceRoll(vec![1, 3, 4], 3, 0).to_string(), "8 (1 + 3 + 4)");
|
||||||
assert_eq!(DiceRoll(vec![]).to_string(), "0");
|
assert_eq!(DiceRoll(vec![], 0, 0).to_string(), "0");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
DiceRoll(vec![4, 7, 2, 10]).to_string(),
|
DiceRoll(vec![4, 7, 2, 10], 4, 0).to_string(),
|
||||||
"23 (4 + 7 + 2 + 10)"
|
"23 (4 + 7 + 2 + 10)"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll(vec![20, 13, 11, 10], 3, 0).to_string(),
|
||||||
|
"44 (20 + 13 + 11 + [10])"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll(vec![20, 13, 11, 10], 4, 1).to_string(),
|
||||||
|
"34 ([20] + 13 + 11 + 10)"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn element_roll_display_test() {
|
fn element_roll_display_test() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(),
|
ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3, 0)).to_string(),
|
||||||
"8 (1 + 3 + 4)"
|
"8 (1 + 3 + 4)"
|
||||||
);
|
);
|
||||||
assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
|
assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
|
||||||
|
@ -213,11 +255,11 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn signed_element_roll_display_test() {
|
fn signed_element_roll_display_test() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(),
|
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3, 0))).to_string(),
|
||||||
"8 (1 + 3 + 4)"
|
"8 (1 + 3 + 4)"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(),
|
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3, 0))).to_string(),
|
||||||
"-8 (1 + 3 + 4)"
|
"-8 (1 + 3 + 4)"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -234,14 +276,14 @@ mod tests {
|
||||||
fn element_expression_roll_display_test() {
|
fn element_expression_roll_display_test() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
|
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
|
||||||
DiceRoll(vec![1, 3, 4])
|
DiceRoll(vec![1, 3, 4], 3, 0)
|
||||||
)),])
|
)),])
|
||||||
.to_string(),
|
.to_string(),
|
||||||
"8 (1 + 3 + 4)"
|
"8 (1 + 3 + 4)"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
|
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
|
||||||
DiceRoll(vec![1, 3, 4])
|
DiceRoll(vec![1, 3, 4], 3, 0)
|
||||||
)),])
|
)),])
|
||||||
.to_string(),
|
.to_string(),
|
||||||
"-8 (1 + 3 + 4)"
|
"-8 (1 + 3 + 4)"
|
||||||
|
@ -258,8 +300,8 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ElementExpressionRoll(vec![
|
ElementExpressionRoll(vec![
|
||||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3, 0))),
|
||||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2], 2, 0))),
|
||||||
SignedElementRoll::Positive(ElementRoll::Bonus(4)),
|
SignedElementRoll::Positive(ElementRoll::Bonus(4)),
|
||||||
SignedElementRoll::Negative(ElementRoll::Bonus(7)),
|
SignedElementRoll::Negative(ElementRoll::Bonus(7)),
|
||||||
])
|
])
|
||||||
|
@ -268,13 +310,33 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ElementExpressionRoll(vec![
|
ElementExpressionRoll(vec![
|
||||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3, 0))),
|
||||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2], 2, 0))),
|
||||||
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
||||||
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||||
])
|
])
|
||||||
.to_string(),
|
.to_string(),
|
||||||
"-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)"
|
"-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
ElementExpressionRoll(vec![
|
||||||
|
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![4, 3, 1], 3, 0))),
|
||||||
|
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![12, 2], 1, 0))),
|
||||||
|
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
||||||
|
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||||
|
])
|
||||||
|
.to_string(),
|
||||||
|
"7 (-8 (4 + 3 + 1) + 12 (12 + [2]) - 4 + 7)"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
ElementExpressionRoll(vec![
|
||||||
|
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![4, 3, 1], 3, 1))),
|
||||||
|
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![12, 2], 2, 0))),
|
||||||
|
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
||||||
|
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||||
|
])
|
||||||
|
.to_string(),
|
||||||
|
"13 (-4 ([4] + 3 + 1) + 14 (12 + 2) - 4 + 7)"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
use matrix_sdk::ruma::room_id;
|
||||||
|
use matrix_sdk::Client;
|
||||||
|
use tenebrous_dicebot::commands;
|
||||||
|
use tenebrous_dicebot::commands::ResponseExtractor;
|
||||||
|
use tenebrous_dicebot::context::{Context, RoomContext};
|
||||||
|
use tenebrous_dicebot::db::sqlite::Database;
|
||||||
|
use tenebrous_dicebot::error::BotError;
|
||||||
|
use tenebrous_dicebot::models::Account;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), BotError> {
|
||||||
|
let input = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
||||||
|
let command = match commands::parser::parse_command(&input) {
|
||||||
|
Ok(command) => command,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com")?;
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = Database::new(
|
||||||
|
db_path
|
||||||
|
.path()
|
||||||
|
.to_str()
|
||||||
|
.expect("Could not get path to temporary db"),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let context = Context {
|
||||||
|
db,
|
||||||
|
account: Account::default(),
|
||||||
|
matrix_client: Client::new(homeserver).await.expect("Could not create matrix client"),
|
||||||
|
origin_room: RoomContext {
|
||||||
|
id: &room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "fake room".to_owned(),
|
||||||
|
secure: false,
|
||||||
|
},
|
||||||
|
active_room: RoomContext {
|
||||||
|
id: &room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "fake room".to_owned(),
|
||||||
|
secure: false,
|
||||||
|
},
|
||||||
|
username: "@localuser:example.com",
|
||||||
|
message_body: &input,
|
||||||
|
};
|
||||||
|
|
||||||
|
let message = command.execute(&context).await.message_html("fakeuser");
|
||||||
|
let message = html2text::from_read(message.as_bytes(), 80);
|
||||||
|
println!("{}", message.trim());
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
//Needed for nested Result handling from tokio. Probably can go away after 1.47.0.
|
||||||
|
#![type_length_limit = "7605144"]
|
||||||
|
use futures::try_join;
|
||||||
|
use log::error;
|
||||||
|
use matrix_sdk::Client;
|
||||||
|
use std::env;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
use tenebrous_dicebot::bot::DiceBot;
|
||||||
|
use tenebrous_dicebot::config::*;
|
||||||
|
use tenebrous_dicebot::db::sqlite::Database;
|
||||||
|
use tenebrous_dicebot::error::BotError;
|
||||||
|
use tenebrous_dicebot::rpc;
|
||||||
|
use tenebrous_dicebot::state::DiceBotState;
|
||||||
|
use tracing_subscriber::filter::EnvFilter;
|
||||||
|
|
||||||
|
/// Attempt to create config object and ddatabase connection pool from
|
||||||
|
/// the given config path. An error is returned if config creation or
|
||||||
|
/// database pool creation fails for some reason.
|
||||||
|
async fn init(config_path: &str) -> Result<(Arc<Config>, Database, Client), BotError> {
|
||||||
|
let cfg = read_config(config_path)?;
|
||||||
|
let cfg = Arc::new(cfg);
|
||||||
|
let sqlite_path = format!("{}/dicebot.sqlite", cfg.database_path());
|
||||||
|
let db = Database::new(&sqlite_path).await?;
|
||||||
|
let client = tenebrous_dicebot::matrix::create_client(&cfg).await?;
|
||||||
|
Ok((cfg, db, client))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), BotError> {
|
||||||
|
let filter = if env::var("RUST_LOG").is_ok() {
|
||||||
|
EnvFilter::from_default_env()
|
||||||
|
} else {
|
||||||
|
EnvFilter::new("tonic=info,tenebrous_dicebot=info,dicebot=info,refinery=info")
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing_subscriber::fmt().with_env_filter(filter).init();
|
||||||
|
|
||||||
|
match run().await {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => error!("Error: {}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run() -> Result<(), BotError> {
|
||||||
|
let config_path = std::env::args()
|
||||||
|
.skip(1)
|
||||||
|
.next()
|
||||||
|
.expect("Need a config as an argument");
|
||||||
|
|
||||||
|
let (cfg, db, client) = init(&config_path).await?;
|
||||||
|
let grpc = rpc::serve_grpc(&cfg, &db, &client);
|
||||||
|
let bot = run_bot(&cfg, &db, &client);
|
||||||
|
|
||||||
|
match try_join!(bot, grpc) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => error!("Error: {:?}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_bot(cfg: &Arc<Config>, db: &Database, client: &Client) -> Result<(), BotError> {
|
||||||
|
let state = Arc::new(RwLock::new(DiceBotState::new(&cfg)));
|
||||||
|
|
||||||
|
match DiceBot::new(cfg, &state, db, client) {
|
||||||
|
Ok(bot) => bot.run().await?,
|
||||||
|
Err(e) => println!("Error connecting: {:?}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
use std::env;
|
||||||
|
use tenebrous_dicebot::db::sqlite::migrator;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), migrator::MigrationError> {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
let db_path: &str = match &args[..] {
|
||||||
|
[_, path] => path.as_ref(),
|
||||||
|
[_, _, ..] => panic!("Expected exactly 0 or 1 argument"),
|
||||||
|
_ => "dicebot.sqlite",
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Using database: {}", db_path);
|
||||||
|
|
||||||
|
migrator::migrate(db_path).await
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
use tenebrous_rpc::protos::dicebot::UserIdRequest;
|
||||||
|
use tenebrous_rpc::protos::dicebot::{dicebot_client::DicebotClient};
|
||||||
|
use tonic::{metadata::MetadataValue, transport::Channel, Request};
|
||||||
|
|
||||||
|
async fn create_client(
|
||||||
|
shared_secret: &str,
|
||||||
|
) -> Result<DicebotClient<Channel>, Box<dyn std::error::Error>> {
|
||||||
|
let channel = Channel::from_static("http://0.0.0.0:9090")
|
||||||
|
.connect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let bearer = MetadataValue::from_str(&format!("Bearer {}", shared_secret))?;
|
||||||
|
|
||||||
|
let client = DicebotClient::with_interceptor(channel, move |mut req: Request<()>| {
|
||||||
|
req.metadata_mut().insert("authorization", bearer.clone());
|
||||||
|
Ok(req)
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(client)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let mut client = create_client("example-key").await?;
|
||||||
|
|
||||||
|
let request = tonic::Request::new(UserIdRequest {
|
||||||
|
user_id: "@projectmoon:agnos.is".into(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let response = client.rooms_for_user(request).await?.into_inner();
|
||||||
|
println!("Rooms: {:?}", response.rooms);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,173 @@
|
||||||
|
use crate::context::{Context, RoomContext};
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::logic;
|
||||||
|
use crate::matrix;
|
||||||
|
use crate::{
|
||||||
|
commands::{execute_command, ExecutionResult, ResponseExtractor},
|
||||||
|
models::Account,
|
||||||
|
};
|
||||||
|
use futures::stream::{self, StreamExt};
|
||||||
|
use matrix_sdk::ruma::{OwnedEventId, RoomId};
|
||||||
|
use matrix_sdk::{self, room::Joined, Client};
|
||||||
|
use std::clone::Clone;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
/// Handle responding to a single command being executed. Wil print
|
||||||
|
/// out the full result of that command.
|
||||||
|
pub(super) async fn handle_single_result(
|
||||||
|
client: &Client,
|
||||||
|
cmd_result: &ExecutionResult,
|
||||||
|
respond_to: &str,
|
||||||
|
room: &Joined,
|
||||||
|
event_id: OwnedEventId,
|
||||||
|
) {
|
||||||
|
let html = cmd_result.message_html(respond_to);
|
||||||
|
let plain = cmd_result.message_plain(respond_to);
|
||||||
|
matrix::send_message(client, room.room_id(), (&html, &plain), Some(event_id)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format failure messages nicely in either HTML or plain text. If
|
||||||
|
/// plain is true, plain-text will be returned. Otherwise, formatted
|
||||||
|
/// HTML.
|
||||||
|
fn format_failures(
|
||||||
|
errors: &[(&str, &BotError)],
|
||||||
|
commands_executed: usize,
|
||||||
|
respond_to: &str,
|
||||||
|
plain: bool,
|
||||||
|
) -> String {
|
||||||
|
let respond_to = match plain {
|
||||||
|
true => respond_to.to_owned(),
|
||||||
|
false => format!(
|
||||||
|
"<a href=\"https://matrix.to/#/{}\">{}</a>",
|
||||||
|
respond_to, respond_to
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
let failures: Vec<String> = errors
|
||||||
|
.iter()
|
||||||
|
.map(|&(cmd, err)| format!("<strong>{}:</strong> {}", cmd, err))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let message = format!(
|
||||||
|
"{}: Executed {} commands ({} failed)\n\nFailures:\n{}",
|
||||||
|
respond_to,
|
||||||
|
commands_executed,
|
||||||
|
errors.len(),
|
||||||
|
failures.join("\n")
|
||||||
|
)
|
||||||
|
.replace("\n", "<br/>");
|
||||||
|
|
||||||
|
match plain {
|
||||||
|
true => html2text::from_read(message.as_bytes(), message.len()),
|
||||||
|
false => message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handle responding to multiple commands being executed. Will print
|
||||||
|
/// out how many commands succeeded and failed (if any failed).
|
||||||
|
pub(super) async fn handle_multiple_results(
|
||||||
|
client: &Client,
|
||||||
|
results: &[(String, ExecutionResult)],
|
||||||
|
respond_to: &str,
|
||||||
|
room: &Joined,
|
||||||
|
) {
|
||||||
|
let user_pill = format!(
|
||||||
|
"<a href=\"https://matrix.to/#/{}\">{}</a>",
|
||||||
|
respond_to, respond_to
|
||||||
|
);
|
||||||
|
|
||||||
|
let errors: Vec<(&str, &BotError)> = results
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(cmd, result)| match result {
|
||||||
|
Err(e) => Some((cmd.as_ref(), e)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let (message, plain) = if errors.len() == 0 {
|
||||||
|
(
|
||||||
|
format!("{}: Executed {} commands", user_pill, results.len()),
|
||||||
|
format!("{}: Executed {} commands", respond_to, results.len()),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
format_failures(&errors, results.len(), respond_to, false),
|
||||||
|
format_failures(&errors, results.len(), respond_to, true),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
matrix::send_message(client, room.room_id(), (&message, &plain), None).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Map an account's active room value to an actual matrix room, if
|
||||||
|
/// the account has an active room. This only retrieves the
|
||||||
|
/// user-specified active room, and doesn't perform any further
|
||||||
|
/// filtering.
|
||||||
|
fn get_account_active_room(client: &Client, account: &Account) -> Result<Option<Joined>, BotError> {
|
||||||
|
let active_room = account
|
||||||
|
.registered_user()
|
||||||
|
.and_then(|u| u.active_room.as_deref())
|
||||||
|
.map(|room_id| <&RoomId>::try_from(room_id))
|
||||||
|
.transpose()?
|
||||||
|
.and_then(|active_room_id| client.get_joined_room(active_room_id));
|
||||||
|
|
||||||
|
Ok(active_room)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute a single command in the list of commands. Can fail if the
|
||||||
|
/// Account value cannot be created/fetched from the database, or if
|
||||||
|
/// room display names cannot be calculated. Otherwise, the success or
|
||||||
|
/// error of command execution itself is returned.
|
||||||
|
async fn execute_single_command(
|
||||||
|
command: &str,
|
||||||
|
db: &Database,
|
||||||
|
client: &Client,
|
||||||
|
origin_room: &Joined,
|
||||||
|
sender: &str,
|
||||||
|
) -> ExecutionResult {
|
||||||
|
let origin_ctx = RoomContext::new(origin_room, sender).await?;
|
||||||
|
let account = logic::get_account(db, sender).await?;
|
||||||
|
let active_room = get_account_active_room(client, &account)?;
|
||||||
|
|
||||||
|
// Active room is used in secure command-issuing rooms. In
|
||||||
|
// "public" rooms, where other users are, treat origin as the
|
||||||
|
// active room.
|
||||||
|
let active_room = active_room
|
||||||
|
.as_ref()
|
||||||
|
.filter(|_| origin_ctx.secure)
|
||||||
|
.unwrap_or(origin_room);
|
||||||
|
|
||||||
|
let active_ctx = RoomContext::new(active_room, sender).await?;
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account,
|
||||||
|
db: db.clone(),
|
||||||
|
matrix_client: client.clone(),
|
||||||
|
origin_room: origin_ctx,
|
||||||
|
username: &sender,
|
||||||
|
active_room: active_ctx,
|
||||||
|
message_body: &command,
|
||||||
|
};
|
||||||
|
|
||||||
|
execute_command(&ctx).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempt to execute all commands sent to the bot in a message. This
|
||||||
|
/// asynchronously executes all commands given to it. A Vec of all
|
||||||
|
/// commands and their execution results are returned.
|
||||||
|
pub(super) async fn execute(
|
||||||
|
commands: Vec<&str>,
|
||||||
|
db: &Database,
|
||||||
|
client: &Client,
|
||||||
|
room: &Joined,
|
||||||
|
sender: &str,
|
||||||
|
) -> Vec<(String, ExecutionResult)> {
|
||||||
|
stream::iter(commands)
|
||||||
|
.then(|command| async move {
|
||||||
|
let result = execute_single_command(command, db, client, room, sender).await;
|
||||||
|
(command.to_owned(), result)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
.await
|
||||||
|
}
|
|
@ -0,0 +1,163 @@
|
||||||
|
use super::DiceBot;
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::Rooms;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use log::{debug, error, info, warn};
|
||||||
|
use matrix_sdk::ruma::events::room::member::RoomMemberEventContent;
|
||||||
|
use matrix_sdk::ruma::events::{StrippedStateEvent, SyncMessageLikeEvent};
|
||||||
|
use matrix_sdk::{self, room::Room, ruma::events::room::message::RoomMessageEventContent};
|
||||||
|
use matrix_sdk::{Client, DisplayName};
|
||||||
|
use std::ops::Sub;
|
||||||
|
use std::time::UNIX_EPOCH;
|
||||||
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
|
/// Check if a message is recent enough to actually process. If the
|
||||||
|
/// message is within "oldest_message_age" seconds, this function
|
||||||
|
/// returns true. If it's older than that, it returns false and logs a
|
||||||
|
/// debug message.
|
||||||
|
fn check_message_age(
|
||||||
|
event: &SyncMessageLikeEvent<RoomMessageEventContent>,
|
||||||
|
oldest_message_age: u64,
|
||||||
|
) -> bool {
|
||||||
|
let sending_time = event
|
||||||
|
.origin_server_ts()
|
||||||
|
.to_system_time()
|
||||||
|
.unwrap_or(UNIX_EPOCH);
|
||||||
|
|
||||||
|
let oldest_timestamp = SystemTime::now().sub(Duration::from_secs(oldest_message_age));
|
||||||
|
|
||||||
|
if sending_time > oldest_timestamp {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
let age = match oldest_timestamp.duration_since(sending_time) {
|
||||||
|
Ok(n) => format!("{} seconds too old", n.as_secs()),
|
||||||
|
Err(_) => "before the UNIX epoch".to_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!("Ignoring message because it is {}: {:?}", age, event);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determine whether or not to process a received message. This check
|
||||||
|
/// is necessary in addition to the event processing check because we
|
||||||
|
/// may receive message events when entering a room for the first
|
||||||
|
/// time, and we don't want to respond to things before the bot was in
|
||||||
|
/// the channel, but we do want to respond to things that were sent if
|
||||||
|
/// the bot left and rejoined quickly.
|
||||||
|
async fn should_process_message<'a>(
|
||||||
|
bot: &DiceBot,
|
||||||
|
event: &SyncMessageLikeEvent<RoomMessageEventContent>,
|
||||||
|
) -> Result<(String, String), BotError> {
|
||||||
|
//Ignore messages that are older than configured duration.
|
||||||
|
if !check_message_age(event, bot.config.oldest_message_age()) {
|
||||||
|
let state_check = bot.state.read().unwrap();
|
||||||
|
if !((*state_check).logged_skipped_old_messages()) {
|
||||||
|
drop(state_check);
|
||||||
|
let mut state = bot.state.write().unwrap();
|
||||||
|
(*state).skipped_old_messages();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Err(BotError::ShouldNotProcessError);
|
||||||
|
}
|
||||||
|
|
||||||
|
let msg_body: String = event
|
||||||
|
.as_original()
|
||||||
|
.map(|e| e.content.body())
|
||||||
|
.map(str::to_string)
|
||||||
|
.unwrap_or_else(|| String::new());
|
||||||
|
|
||||||
|
let sender_username: String = format!(
|
||||||
|
"@{}:{}",
|
||||||
|
event.sender().localpart(),
|
||||||
|
event.sender().server_name()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Do not process messages from the bot itself. Otherwise it might
|
||||||
|
// try to execute its own commands.
|
||||||
|
let bot_username = bot
|
||||||
|
.client
|
||||||
|
.user_id()
|
||||||
|
.map(|u| format!("@{}:{}", u.localpart(), u.server_name()))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
if sender_username == bot_username {
|
||||||
|
return Err(BotError::ShouldNotProcessError);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((msg_body, sender_username))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> bool {
|
||||||
|
db.should_process(room_id, event_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
error!(
|
||||||
|
"Database error when checking if we should process an event: {}",
|
||||||
|
e.to_string()
|
||||||
|
);
|
||||||
|
false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn on_stripped_state_member(
|
||||||
|
event: StrippedStateEvent<RoomMemberEventContent>,
|
||||||
|
client: Client,
|
||||||
|
room: Room,
|
||||||
|
) {
|
||||||
|
let room = match room {
|
||||||
|
Room::Invited(invited_room) => invited_room,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if room.own_user_id().as_str() != event.state_key {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Autojoining room {}",
|
||||||
|
room.display_name()
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.unwrap_or_else(|| DisplayName::Named("[error]".to_string()))
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = client.join_room_by_id(&room.room_id()).await {
|
||||||
|
warn!("Could not join room: {}", e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn on_room_message(
|
||||||
|
event: SyncMessageLikeEvent<RoomMessageEventContent>,
|
||||||
|
room: Room,
|
||||||
|
bot: DiceBot,
|
||||||
|
) {
|
||||||
|
let room = match room {
|
||||||
|
Room::Joined(joined_room) => joined_room,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
let room_id = room.room_id().as_str();
|
||||||
|
if !should_process_event(&bot.db, room_id, event.event_id().as_str()).await {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (msg_body, sender_username) =
|
||||||
|
if let Ok((msg_body, sender_username)) = should_process_message(&bot, &event).await {
|
||||||
|
(msg_body, sender_username)
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let results = bot
|
||||||
|
.execute_commands(&room, &sender_username, &msg_body)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
bot.handle_results(
|
||||||
|
&room,
|
||||||
|
&sender_username,
|
||||||
|
event.event_id().to_owned(),
|
||||||
|
results,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
|
@ -0,0 +1,172 @@
|
||||||
|
use crate::commands::ExecutionResult;
|
||||||
|
use crate::config::*;
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::DbState;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::state::DiceBotState;
|
||||||
|
use log::info;
|
||||||
|
use matrix_sdk::room::Room;
|
||||||
|
use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
|
||||||
|
use matrix_sdk::ruma::events::SyncMessageLikeEvent;
|
||||||
|
use matrix_sdk::ruma::OwnedEventId;
|
||||||
|
use matrix_sdk::{self, room::Joined, Client};
|
||||||
|
use matrix_sdk::config::SyncSettings;
|
||||||
|
use std::clone::Clone;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
mod command_execution;
|
||||||
|
pub mod event_handlers;
|
||||||
|
|
||||||
|
/// How many commands can be in one message. If the amount is higher
|
||||||
|
/// than this, we reject execution.
|
||||||
|
const MAX_COMMANDS_PER_MESSAGE: usize = 50;
|
||||||
|
|
||||||
|
/// The DiceBot struct represents an active dice bot. The bot is not
|
||||||
|
/// connected to Matrix until its run() function is called.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct DiceBot {
|
||||||
|
/// A reference to the configuration read in on application start.
|
||||||
|
config: Arc<Config>,
|
||||||
|
|
||||||
|
/// The matrix client.
|
||||||
|
client: Client,
|
||||||
|
|
||||||
|
/// State of the dicebot
|
||||||
|
state: Arc<RwLock<DiceBotState>>,
|
||||||
|
|
||||||
|
/// Active database layer
|
||||||
|
db: Database,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DiceBot {
|
||||||
|
/// Create a new dicebot with the given configuration and state
|
||||||
|
/// actor. This function returns a Result because it is possible
|
||||||
|
/// for client creation to fail for some reason (e.g. invalid
|
||||||
|
/// homeserver URL).
|
||||||
|
pub fn new(
|
||||||
|
config: &Arc<Config>,
|
||||||
|
state: &Arc<RwLock<DiceBotState>>,
|
||||||
|
db: &Database,
|
||||||
|
client: &Client,
|
||||||
|
) -> Result<Self, BotError> {
|
||||||
|
Ok(DiceBot {
|
||||||
|
client: client.clone(),
|
||||||
|
config: config.clone(),
|
||||||
|
state: state.clone(),
|
||||||
|
db: db.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Logs in to matrix and potentially records a new device ID. If
|
||||||
|
/// no device ID is found in the database, a new one will be
|
||||||
|
/// generated by the matrix SDK, and we will store it.
|
||||||
|
async fn login(&self, client: &Client) -> Result<(), BotError> {
|
||||||
|
let username = self.config.matrix_username();
|
||||||
|
let password = self.config.matrix_password();
|
||||||
|
|
||||||
|
// Pull device ID from database, if it exists. Then write it
|
||||||
|
// to DB if the library generated one for us.
|
||||||
|
let device_id: Option<String> = self.db.get_device_id().await?;
|
||||||
|
let device_id: Option<&str> = device_id.as_deref();
|
||||||
|
|
||||||
|
let no_device_ld_login = || client.login_username(username, password);
|
||||||
|
let device_id_login = |id| client.login_username(username, password).device_id(id);
|
||||||
|
let login = device_id.map_or_else(no_device_ld_login, device_id_login);
|
||||||
|
|
||||||
|
login.send().await?;
|
||||||
|
|
||||||
|
if device_id.is_none() {
|
||||||
|
let device_id = client.device_id().ok_or(BotError::NoDeviceIdFound)?;
|
||||||
|
self.db.set_device_id(device_id.as_str()).await?;
|
||||||
|
info!("Recorded new device ID: {}", device_id.as_str());
|
||||||
|
} else {
|
||||||
|
info!("Using existing device ID: {}", device_id.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Logged in as {}", username);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn bind_events(&self) {
|
||||||
|
//on room message: need closure to pass bot ref in.
|
||||||
|
self.client
|
||||||
|
.add_event_handler({
|
||||||
|
let bot: DiceBot = self.clone();
|
||||||
|
move |event: SyncMessageLikeEvent<RoomMessageEventContent>, room: Room| {
|
||||||
|
let bot = bot.clone();
|
||||||
|
async move { event_handlers::on_room_message(event, room, bot).await }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//auto-join handler
|
||||||
|
self.client
|
||||||
|
.add_event_handler(event_handlers::on_stripped_state_member);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Logs the bot in to Matrix and listens for events until program
|
||||||
|
/// terminated, or a panic occurs. Originally adapted from the
|
||||||
|
/// matrix-rust-sdk command bot example.
|
||||||
|
pub async fn run(self) -> Result<(), BotError> {
|
||||||
|
let client = self.client.clone();
|
||||||
|
self.login(&client).await?;
|
||||||
|
self.bind_events().await;
|
||||||
|
|
||||||
|
info!("Listening for commands");
|
||||||
|
|
||||||
|
// TODO replace with sync_with_callback for cleaner shutdown
|
||||||
|
// process.
|
||||||
|
client.sync(SyncSettings::default()).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute_commands(
|
||||||
|
&self,
|
||||||
|
room: &Joined,
|
||||||
|
sender: &str,
|
||||||
|
msg_body: &str,
|
||||||
|
) -> Vec<(String, ExecutionResult)> {
|
||||||
|
let commands: Vec<&str> = msg_body
|
||||||
|
.lines()
|
||||||
|
.filter(|line| line.starts_with("!"))
|
||||||
|
.take(MAX_COMMANDS_PER_MESSAGE + 1)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
//Up to 50 commands allowed, otherwise we send back an error.
|
||||||
|
let results: Vec<(String, ExecutionResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE {
|
||||||
|
command_execution::execute(commands, &self.db, &self.client, room, sender).await
|
||||||
|
} else {
|
||||||
|
vec![("".to_owned(), Err(BotError::MessageTooLarge))]
|
||||||
|
};
|
||||||
|
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_results(
|
||||||
|
&self,
|
||||||
|
room: &Joined,
|
||||||
|
sender_username: &str,
|
||||||
|
event_id: OwnedEventId,
|
||||||
|
results: Vec<(String, ExecutionResult)>,
|
||||||
|
) {
|
||||||
|
if results.len() >= 1 {
|
||||||
|
if results.len() == 1 {
|
||||||
|
command_execution::handle_single_result(
|
||||||
|
&self.client,
|
||||||
|
&results[0].1,
|
||||||
|
sender_username,
|
||||||
|
&room,
|
||||||
|
event_id,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
} else if results.len() > 1 {
|
||||||
|
command_execution::handle_multiple_results(
|
||||||
|
&self.client,
|
||||||
|
&results,
|
||||||
|
sender_username,
|
||||||
|
&room,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +1,9 @@
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::db::variables::UserAndRoom;
|
use crate::error::{BotError, DiceRollingError};
|
||||||
use crate::error::BotError;
|
use crate::parser::dice::{Amount, Element, Operator};
|
||||||
use crate::parser::{Amount, Element, Operator};
|
|
||||||
use crate::roll::Rolled;
|
|
||||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum DiceRollingError {
|
|
||||||
#[error("variable not found: {0}")]
|
|
||||||
VariableNotFound(String),
|
|
||||||
|
|
||||||
#[error("dice pool expression too large")]
|
|
||||||
ExpressionTooLarge,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub enum DicePoolQuality {
|
pub enum DicePoolQuality {
|
||||||
|
@ -94,29 +81,6 @@ pub struct DicePool {
|
||||||
pub(crate) modifiers: DicePoolModifiers,
|
pub(crate) modifiers: DicePoolModifiers,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn calculate_dice_amount(pool: &DicePoolWithContext<'_>) -> Result<i32, BotError> {
|
|
||||||
let stream = stream::iter(&pool.0.amounts);
|
|
||||||
let key = UserAndRoom(&pool.1.username, &pool.1.room_id);
|
|
||||||
let variables = &pool.1.db.variables.get_user_variables(&key)?;
|
|
||||||
|
|
||||||
use DiceRollingError::VariableNotFound;
|
|
||||||
let dice_amount: Result<i32, BotError> = stream
|
|
||||||
.then(|amount| async move {
|
|
||||||
match &amount.element {
|
|
||||||
Element::Number(num_dice) => Ok(*num_dice * amount.operator.mult()),
|
|
||||||
Element::Variable(variable) => variables
|
|
||||||
.get(variable)
|
|
||||||
.ok_or(VariableNotFound(variable.clone().to_string()))
|
|
||||||
.map(|i| *i)
|
|
||||||
.map_err(|e| e.into()),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.try_fold(0, |total, num_dice| async move { Ok(total + num_dice) })
|
|
||||||
.await;
|
|
||||||
|
|
||||||
dice_amount
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DicePool {
|
impl DicePool {
|
||||||
pub fn easy_pool(dice_amount: i32, quality: DicePoolQuality) -> DicePool {
|
pub fn easy_pool(dice_amount: i32, quality: DicePoolQuality) -> DicePool {
|
||||||
DicePool {
|
DicePool {
|
||||||
|
@ -177,10 +141,11 @@ impl RolledDicePool {
|
||||||
|
|
||||||
impl fmt::Display for RolledDicePool {
|
impl fmt::Display for RolledDicePool {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let dice_plural = if self.num_dice == 1 { "die" } else { "dice" };
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{} dice ({}, exceptional on {} successes)",
|
"{} {} ({}, exceptional on {} successes)",
|
||||||
self.num_dice, self.modifiers.quality, self.modifiers.exceptional_on
|
self.num_dice, dice_plural, self.modifiers.quality, self.modifiers.exceptional_on
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,18 +157,21 @@ pub struct DicePoolRoll {
|
||||||
rolls: Vec<i32>,
|
rolls: Vec<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Amount of dice to display before cutting off and showing "and X
|
||||||
|
/// more", so we don't spam the room with huge messages.
|
||||||
|
const MAX_DISPLAYED_ROLLS: usize = 15;
|
||||||
|
|
||||||
fn fmt_rolls(pool: &DicePoolRoll) -> String {
|
fn fmt_rolls(pool: &DicePoolRoll) -> String {
|
||||||
let max_displayed_rolls = 15;
|
|
||||||
let rolls = pool.rolls();
|
let rolls = pool.rolls();
|
||||||
if rolls.len() > max_displayed_rolls {
|
if rolls.len() > MAX_DISPLAYED_ROLLS {
|
||||||
let first_ten = rolls.iter().take(max_displayed_rolls).join(", ");
|
let shown_amount = rolls.into_iter().take(MAX_DISPLAYED_ROLLS).join(", ");
|
||||||
format!(
|
format!(
|
||||||
"{}, and {} more",
|
"{}, and {} more",
|
||||||
first_ten,
|
shown_amount,
|
||||||
rolls.len() - max_displayed_rolls
|
rolls.len() - MAX_DISPLAYED_ROLLS
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
rolls.iter().take(10).join(", ")
|
rolls.into_iter().join(", ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,12 +191,12 @@ impl DicePoolRoll {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn successes(&self) -> i32 {
|
pub fn successes(&self) -> i32 {
|
||||||
let successes = self
|
let successes: usize = self
|
||||||
.rolls
|
.rolls
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.filter(|&roll| *roll >= self.modifiers.success_on)
|
||||||
.filter(|&roll| roll >= self.modifiers.success_on)
|
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
i32::try_from(successes).unwrap_or(0)
|
i32::try_from(successes).unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,12 +208,6 @@ impl DicePoolRoll {
|
||||||
/// Attach a Context to a dice pool. Needed for database access.
|
/// Attach a Context to a dice pool. Needed for database access.
|
||||||
pub struct DicePoolWithContext<'a>(pub &'a DicePool, pub &'a Context<'a>);
|
pub struct DicePoolWithContext<'a>(pub &'a DicePool, pub &'a Context<'a>);
|
||||||
|
|
||||||
impl Rolled for DicePoolRoll {
|
|
||||||
fn rolled_value(&self) -> i32 {
|
|
||||||
self.successes()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for DicePoolRoll {
|
impl fmt::Display for DicePoolRoll {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let successes = self.successes();
|
let successes = self.successes();
|
||||||
|
@ -274,7 +236,7 @@ struct RngDieRoller<R: rand::Rng>(R);
|
||||||
|
|
||||||
impl<R: rand::Rng> DieRoller for RngDieRoller<R> {
|
impl<R: rand::Rng> DieRoller for RngDieRoller<R> {
|
||||||
fn roll_number(&mut self, sides: i32) -> i32 {
|
fn roll_number(&mut self, sides: i32) -> i32 {
|
||||||
self.0.gen_range(1, sides + 1)
|
self.0.gen_range(1..=sides)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,7 +308,7 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result<RolledDicePool,
|
||||||
return Err(DiceRollingError::ExpressionTooLarge.into());
|
return Err(DiceRollingError::ExpressionTooLarge.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let num_dice = calculate_dice_amount(&pool).await?;
|
let num_dice = crate::logic::calculate_dice_amount(&pool.0.amounts, &pool.1).await?;
|
||||||
let mut roller = RngDieRoller(rand::thread_rng());
|
let mut roller = RngDieRoller(rand::thread_rng());
|
||||||
|
|
||||||
if num_dice > 0 {
|
if num_dice > 0 {
|
||||||
|
@ -363,7 +325,19 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result<RolledDicePool,
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::db::Database;
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::Variables;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
macro_rules! dummy_room {
|
||||||
|
() => {
|
||||||
|
crate::context::RoomContext {
|
||||||
|
id: &matrix_sdk::ruma::room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "displayname".to_owned(),
|
||||||
|
secure: false,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
///Instead of being random, generate a series of numbers we have complete
|
///Instead of being random, generate a series of numbers we have complete
|
||||||
///control over.
|
///control over.
|
||||||
|
@ -491,8 +465,8 @@ mod tests {
|
||||||
assert_eq!(vec![10], roll);
|
assert_eq!(vec![10], roll);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn number_of_dice_equality_test() {
|
fn number_of_dice_equality_test() {
|
||||||
let num_dice = 5;
|
let num_dice = 5;
|
||||||
let rolls = vec![1, 2, 3, 4, 5];
|
let rolls = vec![1, 2, 3, 4, 5];
|
||||||
let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain);
|
let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain);
|
||||||
|
@ -500,10 +474,23 @@ mod tests {
|
||||||
assert_eq!(5, rolled_pool.num_dice);
|
assert_eq!(5, rolled_pool.num_dice);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
async fn rejects_large_expression_test() {
|
async fn rejects_large_expression_test() {
|
||||||
let db = Database::new_temp().unwrap();
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
let mut amounts = vec![];
|
let mut amounts = vec![];
|
||||||
|
|
||||||
|
@ -525,10 +512,27 @@ mod tests {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
async fn converts_to_chance_die_test() {
|
async fn converts_to_chance_die_test() {
|
||||||
let db = Database::new_temp().unwrap();
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
let mut amounts = vec![];
|
let mut amounts = vec![];
|
||||||
|
|
||||||
|
@ -547,15 +551,36 @@ mod tests {
|
||||||
assert_eq!(1, roll.num_dice);
|
assert_eq!(1, roll.num_dice);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
async fn can_resolve_variables_test() {
|
async fn can_resolve_variables_test() {
|
||||||
let db = Database::new_temp().unwrap();
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
let user_and_room = crate::db::variables::UserAndRoom(&ctx.username, &ctx.room_id);
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
db.variables
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
.set_user_variable(&user_and_room, "myvariable", 10)
|
.await
|
||||||
.expect("could not set myvariable to 10");
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db.clone(),
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
|
db.set_user_variable(
|
||||||
|
&ctx.username,
|
||||||
|
&ctx.origin_room.id.as_str(),
|
||||||
|
"myvariable",
|
||||||
|
10,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("could not set myvariable to 10");
|
||||||
|
|
||||||
let amounts = vec![Amount {
|
let amounts = vec![Amount {
|
||||||
operator: Operator::Plus,
|
operator: Operator::Plus,
|
||||||
|
@ -563,9 +588,13 @@ mod tests {
|
||||||
}];
|
}];
|
||||||
|
|
||||||
let pool = DicePool::new(amounts, DicePoolModifiers::default());
|
let pool = DicePool::new(amounts, DicePoolModifiers::default());
|
||||||
let pool_with_ctx = DicePoolWithContext(&pool, &ctx);
|
|
||||||
|
|
||||||
assert_eq!(calculate_dice_amount(&pool_with_ctx).await.unwrap(), 10);
|
assert_eq!(
|
||||||
|
crate::logic::calculate_dice_amount(&pool.amounts, &ctx)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
10
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//DicePool tests
|
//DicePool tests
|
||||||
|
@ -659,6 +688,30 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Format tests
|
//Format tests
|
||||||
|
#[test]
|
||||||
|
fn formats_rolled_dice_pool_single_die() {
|
||||||
|
let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain);
|
||||||
|
let rolled_pool = RolledDicePool::from(&pool, 1, vec![1]);
|
||||||
|
let message = format!("{}", rolled_pool);
|
||||||
|
assert!(message.starts_with("1 die"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn formats_rolled_dice_pool_multiple_dice() {
|
||||||
|
let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain);
|
||||||
|
let rolled_pool = RolledDicePool::from(&pool, 2, vec![1, 2]);
|
||||||
|
let message = format!("{}", rolled_pool);
|
||||||
|
assert!(message.starts_with("2 dice"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn formats_rolled_dice_pool_zero_dice() {
|
||||||
|
let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain);
|
||||||
|
let rolled_pool = RolledDicePool::from(&pool, 0, vec![]);
|
||||||
|
let message = format!("{}", rolled_pool);
|
||||||
|
assert!(message.starts_with("0 dice"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn formats_dramatic_failure_test() {
|
fn formats_dramatic_failure_test() {
|
||||||
let result = DicePoolRoll {
|
let result = DicePoolRoll {
|
||||||
|
@ -695,4 +748,32 @@ mod tests {
|
||||||
fmt_rolls(&result)
|
fmt_rolls(&result)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shows_more_than_10_dice_test() {
|
||||||
|
//Make sure we display more than 10 dice when below the display limit (15).
|
||||||
|
let result = DicePoolRoll {
|
||||||
|
modifiers: DicePoolModifiers::default(),
|
||||||
|
rolls: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14",
|
||||||
|
fmt_rolls(&result)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shows_exactly_15_dice_test() {
|
||||||
|
//If we are at format limit (15), make sure all are shown
|
||||||
|
let result = DicePoolRoll {
|
||||||
|
modifiers: DicePoolModifiers::default(),
|
||||||
|
rolls: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15",
|
||||||
|
fmt_rolls(&result)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::cofd::dice::{DicePool, DicePoolModifiers, DicePoolQuality};
|
use crate::cofd::dice::{DicePool, DicePoolModifiers, DicePoolQuality};
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::parser::{parse_amounts, DiceParsingError};
|
use crate::parser::dice::{parse_amounts, DiceParsingError};
|
||||||
use combine::parser::char::{digit, spaces, string};
|
use combine::parser::char::{digit, spaces, string};
|
||||||
use combine::{choice, count, many1, one_of, Parser};
|
use combine::{choice, count, many1, one_of, Parser};
|
||||||
|
|
||||||
|
@ -45,13 +45,13 @@ pub fn parse_modifiers(input: &str) -> Result<DicePoolModifiers, DiceParsingErro
|
||||||
let (result, rest) = parser.parse(input)?;
|
let (result, rest) = parser.parse(input)?;
|
||||||
|
|
||||||
if rest.len() == 0 {
|
if rest.len() == 0 {
|
||||||
convert_to_info(&result)
|
convert_to_modifiers(&result)
|
||||||
} else {
|
} else {
|
||||||
Err(DiceParsingError::UnconsumedInput)
|
Err(DiceParsingError::UnconsumedInput)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_to_info(parsed: &Vec<ParsedInfo>) -> Result<DicePoolModifiers, DiceParsingError> {
|
fn convert_to_modifiers(parsed: &Vec<ParsedInfo>) -> Result<DicePoolModifiers, DiceParsingError> {
|
||||||
use ParsedInfo::*;
|
use ParsedInfo::*;
|
||||||
if parsed.len() == 0 {
|
if parsed.len() == 0 {
|
||||||
Ok(DicePoolModifiers::default())
|
Ok(DicePoolModifiers::default())
|
||||||
|
@ -79,19 +79,8 @@ fn convert_to_info(parsed: &Vec<ParsedInfo>) -> Result<DicePoolModifiers, DicePa
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_dice_pool(input: &str) -> Result<DicePool, BotError> {
|
pub fn parse_dice_pool(input: &str) -> Result<DicePool, BotError> {
|
||||||
//The "modifiers:" part is optional. Assume amounts if no modifier
|
let (amounts, modifiers_str) = parse_amounts(input)?;
|
||||||
//section found.
|
|
||||||
let split = input.split(":").collect::<Vec<_>>();
|
|
||||||
let (modifiers_str, amounts_str) = (match split[..] {
|
|
||||||
[amounts] => Ok(("", amounts)),
|
|
||||||
[modifiers, amounts] => Ok((modifiers, amounts)),
|
|
||||||
_ => Err(BotError::DiceParsingError(
|
|
||||||
DiceParsingError::UnconsumedInput,
|
|
||||||
)),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let modifiers = parse_modifiers(modifiers_str)?;
|
let modifiers = parse_modifiers(modifiers_str)?;
|
||||||
let amounts = parse_amounts(&amounts_str)?;
|
|
||||||
Ok(DicePool::new(amounts, modifiers))
|
Ok(DicePool::new(amounts, modifiers))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +164,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dice_pool_number_with_quality() {
|
fn dice_pool_number_with_quality() {
|
||||||
let result = parse_dice_pool("n:8");
|
let result = parse_dice_pool("8 n");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result.unwrap(),
|
result.unwrap(),
|
||||||
|
@ -186,7 +175,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn dice_pool_number_with_success_change() {
|
fn dice_pool_number_with_success_change() {
|
||||||
let modifiers = DicePoolModifiers::custom_exceptional_on(3);
|
let modifiers = DicePoolModifiers::custom_exceptional_on(3);
|
||||||
let result = parse_dice_pool("s3:8");
|
let result = parse_dice_pool("8 s3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers));
|
assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers));
|
||||||
}
|
}
|
||||||
|
@ -194,14 +183,14 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn dice_pool_with_quality_and_success_change() {
|
fn dice_pool_with_quality_and_success_change() {
|
||||||
let modifiers = DicePoolModifiers::custom(DicePoolQuality::Rote, 3);
|
let modifiers = DicePoolModifiers::custom(DicePoolQuality::Rote, 3);
|
||||||
let result = parse_dice_pool("rs3:8");
|
let result = parse_dice_pool("8 rs3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers));
|
assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dice_pool_complex_expression_test() {
|
fn dice_pool_complex_expression_test() {
|
||||||
use crate::parser::*;
|
use crate::parser::dice::*;
|
||||||
let modifiers = DicePoolModifiers::custom(DicePoolQuality::Rote, 3);
|
let modifiers = DicePoolModifiers::custom(DicePoolQuality::Rote, 3);
|
||||||
let amounts = vec![
|
let amounts = vec![
|
||||||
Amount {
|
Amount {
|
||||||
|
@ -224,20 +213,20 @@ mod tests {
|
||||||
|
|
||||||
let expected = DicePool::new(amounts, modifiers);
|
let expected = DicePool::new(amounts, modifiers);
|
||||||
|
|
||||||
let result = parse_dice_pool("rs3:8+10-2+varname");
|
let result = parse_dice_pool("8+10-2+varname rs3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), expected);
|
assert_eq!(result.unwrap(), expected);
|
||||||
|
|
||||||
let result = parse_dice_pool("rs3:8+10- 2 + varname");
|
let result = parse_dice_pool("8+10- 2 + varname rs3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), expected);
|
assert_eq!(result.unwrap(), expected);
|
||||||
|
|
||||||
let result = parse_dice_pool("rs3 : 8+ 10 -2 + varname");
|
let result = parse_dice_pool("8+ 10 -2 + varname rs3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), expected);
|
assert_eq!(result.unwrap(), expected);
|
||||||
|
|
||||||
//This one has tabs in it.
|
//This one has tabs in it.
|
||||||
let result = parse_dice_pool(" r s3 : 8 + 10 -2 + varname");
|
let result = parse_dice_pool(" 8 + 10 -2 + varname r s3");
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), expected);
|
assert_eq!(result.unwrap(), expected);
|
||||||
}
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::basic::dice::ElementExpression;
|
||||||
|
use crate::basic::parser::parse_element_expression;
|
||||||
|
use crate::basic::roll::Roll;
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use nom::Err as NomErr;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
pub struct RollCommand(pub ElementExpression);
|
||||||
|
|
||||||
|
impl TryFrom<String> for RollCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let result = parse_element_expression(&input);
|
||||||
|
match result {
|
||||||
|
Ok((rest, expression)) if rest.len() == 0 => Ok(RollCommand(expression)),
|
||||||
|
//"Legacy code boundary": translates Nom errors into BotErrors.
|
||||||
|
Ok(_) => Err(BotError::NomParserIncomplete),
|
||||||
|
Err(NomErr::Error(e)) => Err(BotError::NomParserError(e.1)),
|
||||||
|
Err(NomErr::Failure(e)) => Err(BotError::NomParserError(e.1)),
|
||||||
|
Err(NomErr::Incomplete(_)) => Err(BotError::NomParserIncomplete),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for RollCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"roll regular dice"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let roll = self.0.roll();
|
||||||
|
let html = format!(
|
||||||
|
"<strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}",
|
||||||
|
self.0, roll
|
||||||
|
);
|
||||||
|
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::cofd::dice::{roll_pool, DicePool, DicePoolWithContext};
|
||||||
|
use crate::cofd::parser::{create_chance_die, parse_dice_pool};
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
pub struct PoolRollCommand(pub DicePool);
|
||||||
|
|
||||||
|
impl PoolRollCommand {
|
||||||
|
pub fn chance_die() -> Result<PoolRollCommand, BotError> {
|
||||||
|
let pool = create_chance_die()?;
|
||||||
|
Ok(PoolRollCommand(pool))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<String> for PoolRollCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let pool = parse_dice_pool(&input)?;
|
||||||
|
Ok(PoolRollCommand(pool))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for PoolRollCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"roll dice pool"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let pool_with_ctx = DicePoolWithContext(&self.0, ctx);
|
||||||
|
let rolled_pool = roll_pool(&pool_with_ctx).await?;
|
||||||
|
|
||||||
|
let html = format!(
|
||||||
|
"<strong>Pool:</strong> {}</p><p><strong>Result</strong>: {}",
|
||||||
|
rolled_pool, rolled_pool.roll
|
||||||
|
);
|
||||||
|
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::cthulhu::dice::{
|
||||||
|
advancement_roll, regular_roll, AdvancementRoll, AdvancementRollWithContext, DiceRoll,
|
||||||
|
DiceRollWithContext,
|
||||||
|
};
|
||||||
|
use crate::cthulhu::parser::{parse_advancement_roll, parse_regular_roll};
|
||||||
|
use crate::error::BotError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
pub struct CthRoll(pub DiceRoll);
|
||||||
|
|
||||||
|
impl TryFrom<String> for CthRoll {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let roll = parse_regular_roll(&input)?;
|
||||||
|
Ok(CthRoll(roll))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for CthRoll {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"roll percentile dice"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let roll_with_ctx = DiceRollWithContext(&self.0, ctx);
|
||||||
|
let executed_roll = regular_roll(&roll_with_ctx).await?;
|
||||||
|
|
||||||
|
let html = format!(
|
||||||
|
"<strong>Roll:</strong> {}</p><p><strong>Result</strong>: {}",
|
||||||
|
executed_roll, executed_roll.roll
|
||||||
|
);
|
||||||
|
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CthAdvanceRoll(pub AdvancementRoll);
|
||||||
|
|
||||||
|
impl TryFrom<String> for CthAdvanceRoll {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let roll = parse_advancement_roll(&input)?;
|
||||||
|
Ok(CthAdvanceRoll(roll))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for CthAdvanceRoll {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"roll skill advancement dice"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let roll_with_ctx = AdvancementRollWithContext(&self.0, ctx);
|
||||||
|
let executed_roll = advancement_roll(&roll_with_ctx).await?;
|
||||||
|
let html = format!(
|
||||||
|
"<strong>Roll:</strong> {}</p><p><strong>Result</strong>: {}",
|
||||||
|
executed_roll, executed_roll.roll
|
||||||
|
);
|
||||||
|
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,200 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::db::Users;
|
||||||
|
use crate::error::BotError::{AccountDoesNotExist, PasswordCreationError};
|
||||||
|
use crate::logic::hash_password;
|
||||||
|
use crate::models::{AccountStatus, User};
|
||||||
|
use crate::{context::Context, error::BotError};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::convert::{Into, TryFrom};
|
||||||
|
|
||||||
|
pub struct RegisterCommand;
|
||||||
|
|
||||||
|
impl TryFrom<String> for RegisterCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(_: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(RegisterCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for RegisterCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"register user account"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
if ctx.account.is_registered() {
|
||||||
|
return Err(BotError::AccountAlreadyExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = User {
|
||||||
|
username: ctx.username.to_owned(),
|
||||||
|
password: None,
|
||||||
|
account_status: AccountStatus::Registered,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.db.upsert_user(&user).await?;
|
||||||
|
|
||||||
|
Execution::success(format!(
|
||||||
|
"User account {} registered for bot commands.",
|
||||||
|
ctx.username
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UnlinkCommand(pub String);
|
||||||
|
|
||||||
|
impl TryFrom<String> for UnlinkCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(UnlinkCommand(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for UnlinkCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"unlink user accountx from external applications"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let mut user = ctx
|
||||||
|
.db
|
||||||
|
.get_user(&ctx.username)
|
||||||
|
.await?
|
||||||
|
.ok_or(BotError::AccountDoesNotExist)?;
|
||||||
|
|
||||||
|
user.password = None;
|
||||||
|
ctx.db.upsert_user(&user).await?;
|
||||||
|
|
||||||
|
Execution::success(format!(
|
||||||
|
"Accounted {} is now inaccessible to external applications.",
|
||||||
|
ctx.username
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LinkCommand(pub String);
|
||||||
|
|
||||||
|
impl TryFrom<String> for LinkCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(LinkCommand(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for LinkCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"link user account to external applications"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let mut user = ctx
|
||||||
|
.db
|
||||||
|
.get_user(&ctx.username)
|
||||||
|
.await?
|
||||||
|
.ok_or(BotError::AccountDoesNotExist)?;
|
||||||
|
|
||||||
|
let pw_hash = hash_password(&self.0).map_err(|e| PasswordCreationError(e))?;
|
||||||
|
user.password = Some(pw_hash);
|
||||||
|
ctx.db.upsert_user(&user).await?;
|
||||||
|
|
||||||
|
Execution::success(format!(
|
||||||
|
"Accounted now available for external use. Please log in to \
|
||||||
|
external applications with username {} and the password you set.",
|
||||||
|
ctx.username
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CheckCommand;
|
||||||
|
|
||||||
|
impl TryFrom<String> for CheckCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(_: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(CheckCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for CheckCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"check user account status"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let user = ctx.db.get_user(&ctx.username).await?;
|
||||||
|
|
||||||
|
match user {
|
||||||
|
Some(user) => match user.password {
|
||||||
|
Some(_) => Execution::success(
|
||||||
|
"Account exists, and is available to external applications with a password. \
|
||||||
|
If you forgot your password, change it with !link."
|
||||||
|
.to_string(),
|
||||||
|
),
|
||||||
|
None => Execution::success(
|
||||||
|
"Account exists, but is not available to external applications.".to_string(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
None => Execution::success(
|
||||||
|
"No account registered. Only simple commands in public rooms are available."
|
||||||
|
.to_string(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UnregisterCommand;
|
||||||
|
|
||||||
|
impl TryFrom<String> for UnregisterCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(_: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(UnregisterCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for UnregisterCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"unregister user account"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let user = ctx.db.get_user(&ctx.username).await?;
|
||||||
|
|
||||||
|
match user {
|
||||||
|
Some(_) => {
|
||||||
|
ctx.db.delete_user(&ctx.username).await?;
|
||||||
|
Execution::success("Your user account has been removed.".to_string())
|
||||||
|
}
|
||||||
|
None => Err(AccountDoesNotExist.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::help::{parse_help_topic, HelpTopic};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
pub struct HelpCommand(pub Option<HelpTopic>);
|
||||||
|
|
||||||
|
impl TryFrom<String> for HelpCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let topic = parse_help_topic(&input);
|
||||||
|
Ok(HelpCommand(topic))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for HelpCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"help information"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let help = match &self.0 {
|
||||||
|
Some(topic) => topic.message(),
|
||||||
|
_ => "There is no help for this topic",
|
||||||
|
};
|
||||||
|
|
||||||
|
let html = format!("<strong>Help:</strong> {}", help.replace("\n", "<br/>"));
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,301 @@
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use log::{error, info};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
pub mod basic_rolling;
|
||||||
|
pub mod cofd;
|
||||||
|
pub mod cthulhu;
|
||||||
|
pub mod management;
|
||||||
|
pub mod misc;
|
||||||
|
pub mod parser;
|
||||||
|
pub mod rooms;
|
||||||
|
pub mod variables;
|
||||||
|
|
||||||
|
/// A custom error type specifically related to parsing command text.
|
||||||
|
/// Does not wrap an execution failure.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum CommandError {
|
||||||
|
#[error("invalid command: {0}")]
|
||||||
|
InvalidCommand(String),
|
||||||
|
|
||||||
|
#[error("command can only be executed from encrypted direct message")]
|
||||||
|
InsecureExecution,
|
||||||
|
|
||||||
|
#[error("ignored command")]
|
||||||
|
IgnoredCommand,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A successfully executed command returns a message to be sent back
|
||||||
|
/// to the user in HTML (plain text used as a fallback by message
|
||||||
|
/// formatter).
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Execution {
|
||||||
|
html: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Execution {
|
||||||
|
pub fn success(html: String) -> ExecutionResult {
|
||||||
|
Ok(Execution { html })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Response message in HTML.
|
||||||
|
pub fn html(&self) -> String {
|
||||||
|
self.html.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wraps either a successful command execution response, or an error
|
||||||
|
/// that occurred.
|
||||||
|
pub type ExecutionResult = Result<Execution, BotError>;
|
||||||
|
|
||||||
|
/// Extract response messages out of a type, whether it is success or
|
||||||
|
/// failure.
|
||||||
|
pub trait ResponseExtractor {
|
||||||
|
/// HTML representation of the message, directly mentioning the
|
||||||
|
/// username.
|
||||||
|
fn message_html(&self, username: &str) -> String;
|
||||||
|
|
||||||
|
fn message_plain(&self, username: &str) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResponseExtractor for ExecutionResult {
|
||||||
|
/// Error message in bolded HTML.
|
||||||
|
fn message_html(&self, username: &str) -> String {
|
||||||
|
// TODO use user display name too (element seems to render this
|
||||||
|
// without display name)
|
||||||
|
let username = format!(
|
||||||
|
"<a href=\"https://matrix.to/#/{}\">{}</a>",
|
||||||
|
username, username
|
||||||
|
);
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Ok(resp) => format!("<p>{}</p>", resp.html).replace("\n", "<br/>"),
|
||||||
|
Err(e) => format!("<p>{}: <strong>{}</strong></p>", username, e).replace("\n", "<br/>"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn message_plain(&self, username: &str) -> String {
|
||||||
|
let message = match self {
|
||||||
|
Ok(resp) => format!("{}", resp.html),
|
||||||
|
Err(e) => format!("{}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
format!(
|
||||||
|
"{}:\n{}",
|
||||||
|
username,
|
||||||
|
html2text::from_read(message.as_bytes(), message.len())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The trait that any command that can be executed must implement.
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Command: Send + Sync {
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult;
|
||||||
|
fn name(&self) -> &'static str;
|
||||||
|
fn is_secure(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determine if we are allowed to execute this command. Currently the
|
||||||
|
/// rules are that secure commands must be executed in secure rooms
|
||||||
|
/// (encrypted + direct), and anything else can be executed where
|
||||||
|
/// ever. Later, we can add stuff like admin/regular user power
|
||||||
|
/// separation, etc.
|
||||||
|
fn execution_allowed(cmd: &(impl Command + ?Sized), ctx: &Context<'_>) -> Result<(), CommandError> {
|
||||||
|
match cmd {
|
||||||
|
cmd if cmd.is_secure() && ctx.is_secure() => Ok(()),
|
||||||
|
cmd if cmd.is_secure() && !ctx.is_secure() => Err(CommandError::InsecureExecution),
|
||||||
|
_ => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempt to execute a command, and return the content that should
|
||||||
|
/// go back to Matrix, if the command was executed, whether or not the
|
||||||
|
/// command was successful.
|
||||||
|
pub async fn execute_command(ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let cmd = parser::parse_command(&ctx.message_body)?;
|
||||||
|
|
||||||
|
let result = match execution_allowed(cmd.as_ref(), ctx) {
|
||||||
|
Ok(_) => cmd.execute(ctx).await,
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
log_command(cmd.as_ref(), ctx, &result);
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Log result of an executed command.
|
||||||
|
fn log_command(cmd: &(impl Command + ?Sized), ctx: &Context, result: &ExecutionResult) {
|
||||||
|
use substring::Substring;
|
||||||
|
let command = match cmd.is_secure() {
|
||||||
|
true => cmd.name(),
|
||||||
|
false => ctx.message_body,
|
||||||
|
};
|
||||||
|
|
||||||
|
let dots = match command.len() {
|
||||||
|
_len if _len > 30 => "[...]",
|
||||||
|
_ => "",
|
||||||
|
};
|
||||||
|
|
||||||
|
let command = command.substring(0, 30);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => {
|
||||||
|
info!(
|
||||||
|
"[{}] {} <{}{}> - success",
|
||||||
|
ctx.origin_room.display_name, ctx.username, command, dots
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"[{}] {} <{}{}> - {}",
|
||||||
|
ctx.origin_room.display_name, ctx.username, command, dots, e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use management::RegisterCommand;
|
||||||
|
use url::Url;
|
||||||
|
use matrix_sdk::ruma::room_id;
|
||||||
|
|
||||||
|
macro_rules! dummy_room {
|
||||||
|
() => {
|
||||||
|
crate::context::RoomContext {
|
||||||
|
id: &room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "displayname".to_owned(),
|
||||||
|
secure: false,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! secure_room {
|
||||||
|
() => {
|
||||||
|
crate::context::RoomContext {
|
||||||
|
id: &room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "displayname".to_owned(),
|
||||||
|
secure: true,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn secure_context_secure_command_allows_execution() {
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: secure_room!(),
|
||||||
|
active_room: secure_room!(),
|
||||||
|
username: "myusername",
|
||||||
|
message_body: "!notacommand",
|
||||||
|
};
|
||||||
|
|
||||||
|
let cmd = RegisterCommand;
|
||||||
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn secure_context_insecure_command_allows_execution() {
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: secure_room!(),
|
||||||
|
active_room: secure_room!(),
|
||||||
|
username: "myusername",
|
||||||
|
message_body: "!notacommand",
|
||||||
|
};
|
||||||
|
|
||||||
|
let cmd = variables::GetVariableCommand("".to_owned());
|
||||||
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn insecure_context_insecure_command_allows_execution() {
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "myusername",
|
||||||
|
message_body: "!notacommand",
|
||||||
|
};
|
||||||
|
|
||||||
|
let cmd = variables::GetVariableCommand("".to_owned());
|
||||||
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn insecure_context_secure_command_denies_execution() {
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "myusername",
|
||||||
|
message_body: "!notacommand",
|
||||||
|
};
|
||||||
|
|
||||||
|
let cmd = RegisterCommand;
|
||||||
|
assert_eq!(execution_allowed(&cmd, &ctx).is_err(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn unrecognized_command() {
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "myusername",
|
||||||
|
message_body: "!notacommand",
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = execute_command(&ctx).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +1,24 @@
|
||||||
use crate::cofd::parser::{create_chance_die, parse_dice_pool};
|
/**
|
||||||
|
* In addition to the terms of the AGPL, portions of this file are
|
||||||
|
* governed by the terms of the MIT license, from the original
|
||||||
|
* axfive-matrix-dicebot project.
|
||||||
|
*/
|
||||||
use crate::commands::{
|
use crate::commands::{
|
||||||
basic_rolling::RollCommand,
|
basic_rolling::RollCommand,
|
||||||
cofd::PoolRollCommand,
|
cofd::PoolRollCommand,
|
||||||
cthulhu::{CthAdvanceRoll, CthRoll},
|
cthulhu::{CthAdvanceRoll, CthRoll},
|
||||||
|
management::{CheckCommand, LinkCommand, RegisterCommand, UnlinkCommand, UnregisterCommand},
|
||||||
misc::HelpCommand,
|
misc::HelpCommand,
|
||||||
|
rooms::{ListRoomsCommand, SetRoomCommand},
|
||||||
variables::{
|
variables::{
|
||||||
DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, SetVariableCommand,
|
DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, SetVariableCommand,
|
||||||
},
|
},
|
||||||
Command,
|
Command,
|
||||||
};
|
};
|
||||||
use crate::cthulhu::parser::{parse_advancement_roll, parse_regular_roll};
|
|
||||||
use crate::dice::parser::parse_element_expression;
|
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::help::parse_help_topic;
|
|
||||||
use crate::variables::parse_set_variable;
|
|
||||||
use combine::parser::char::{char, letter, space};
|
use combine::parser::char::{char, letter, space};
|
||||||
use combine::{any, many1, optional, Parser};
|
use combine::{any, many1, optional, Parser};
|
||||||
use nom::Err as NomErr;
|
use std::convert::TryFrom;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Error)]
|
#[derive(Debug, Clone, PartialEq, Error)]
|
||||||
|
@ -28,61 +30,6 @@ pub enum CommandParsingError {
|
||||||
InternalParseError(#[from] combine::error::StringStreamError),
|
InternalParseError(#[from] combine::error::StringStreamError),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a roll expression.
|
|
||||||
fn parse_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let result = parse_element_expression(input);
|
|
||||||
match result {
|
|
||||||
Ok((rest, expression)) if rest.len() == 0 => Ok(Box::new(RollCommand(expression))),
|
|
||||||
//Legacy code boundary translates nom errors into BotErrors.
|
|
||||||
Ok(_) => Err(BotError::NomParserIncomplete),
|
|
||||||
Err(NomErr::Error(e)) => Err(BotError::NomParserError(e.1)),
|
|
||||||
Err(NomErr::Failure(e)) => Err(BotError::NomParserError(e.1)),
|
|
||||||
Err(NomErr::Incomplete(_)) => Err(BotError::NomParserIncomplete),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_get_variable_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
Ok(Box::new(GetVariableCommand(input.to_owned())))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_set_variable_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let (variable_name, value) = parse_set_variable(input)?;
|
|
||||||
Ok(Box::new(SetVariableCommand(variable_name, value)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_delete_variable_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
Ok(Box::new(DeleteVariableCommand(input.to_owned())))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_pool_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let pool = parse_dice_pool(input)?;
|
|
||||||
Ok(Box::new(PoolRollCommand(pool)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_cth_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let roll = parse_regular_roll(input)?;
|
|
||||||
Ok(Box::new(CthRoll(roll)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_cth_advancement_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let roll = parse_advancement_roll(input)?;
|
|
||||||
Ok(Box::new(CthAdvanceRoll(roll)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn chance_die() -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let pool = create_chance_die()?;
|
|
||||||
Ok(Box::new(PoolRollCommand(pool)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_all_variables() -> Result<Box<dyn Command>, BotError> {
|
|
||||||
Ok(Box::new(GetAllVariablesCommand))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn help(topic: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
let topic = parse_help_topic(topic);
|
|
||||||
Ok(Box::new(HelpCommand(topic)))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Split an input string into its constituent command and "everything
|
/// Split an input string into its constituent command and "everything
|
||||||
/// else" parts. Extracts the command separately from its input (i.e.
|
/// else" parts. Extracts the command separately from its input (i.e.
|
||||||
/// rest of the line) and returns a tuple of (command_input, command).
|
/// rest of the line) and returns a tuple of (command_input, command).
|
||||||
|
@ -114,23 +61,37 @@ fn split_command(input: &str) -> Result<(String, String), CommandParsingError> {
|
||||||
Ok((command, command_input))
|
Ok((command, command_input))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Atempt to convert text input to a Boxed command type. Shortens
|
||||||
|
/// boilerplate.
|
||||||
|
macro_rules! convert_to {
|
||||||
|
($type:ident, $input: expr) => {
|
||||||
|
$type::try_from($input).map(|cmd| Box::new(cmd) as Box<dyn Command>)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Potentially parse a command expression. If we recognize the
|
/// Potentially parse a command expression. If we recognize the
|
||||||
/// command, an error should be raised if the command is misparsed. If
|
/// command, an error should be raised if the command is misparsed. If
|
||||||
/// we don't recognize the command, ignore it and return None.
|
/// we don't recognize the command, return an error.
|
||||||
pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
||||||
match split_command(input) {
|
match split_command(input) {
|
||||||
Ok((cmd, cmd_input)) => match cmd.as_ref() {
|
Ok((cmd, cmd_input)) => match cmd.to_lowercase().as_ref() {
|
||||||
"variables" => get_all_variables(),
|
"variables" => convert_to!(GetAllVariablesCommand, cmd_input),
|
||||||
"get" => parse_get_variable_command(&cmd_input),
|
"get" => convert_to!(GetVariableCommand, cmd_input),
|
||||||
"set" => parse_set_variable_command(&cmd_input),
|
"set" => convert_to!(SetVariableCommand, cmd_input),
|
||||||
"del" => parse_delete_variable_command(&cmd_input),
|
"del" => convert_to!(DeleteVariableCommand, cmd_input),
|
||||||
"r" | "roll" => parse_roll(&cmd_input),
|
"r" | "roll" => convert_to!(RollCommand, cmd_input),
|
||||||
"rp" | "pool" => parse_pool_roll(&cmd_input),
|
"rp" | "pool" => convert_to!(PoolRollCommand, cmd_input),
|
||||||
"cthroll" | "cthRoll" => parse_cth_roll(&cmd_input),
|
"chance" => PoolRollCommand::chance_die().map(|cmd| Box::new(cmd) as Box<dyn Command>),
|
||||||
"cthadv" | "cthARoll" => parse_cth_advancement_roll(&cmd_input),
|
"cthroll" => convert_to!(CthRoll, cmd_input),
|
||||||
"chance" => chance_die(),
|
"cthadv" | "ctharoll" => convert_to!(CthAdvanceRoll, cmd_input),
|
||||||
"help" => help(&cmd_input),
|
"help" => convert_to!(HelpCommand, cmd_input),
|
||||||
// No recognized command, ignore this.
|
"register" => convert_to!(RegisterCommand, cmd_input),
|
||||||
|
"link" => convert_to!(LinkCommand, cmd_input),
|
||||||
|
"unlink" => convert_to!(UnlinkCommand, cmd_input),
|
||||||
|
"check" => convert_to!(CheckCommand, cmd_input),
|
||||||
|
"unregister" => convert_to!(UnregisterCommand, cmd_input),
|
||||||
|
"rooms" => convert_to!(ListRoomsCommand, cmd_input),
|
||||||
|
"room" => convert_to!(SetRoomCommand, cmd_input),
|
||||||
_ => Err(CommandParsingError::UnrecognizedCommand(cmd).into()),
|
_ => Err(CommandParsingError::UnrecognizedCommand(cmd).into()),
|
||||||
},
|
},
|
||||||
//All other errors passed up.
|
//All other errors passed up.
|
||||||
|
@ -163,6 +124,21 @@ mod tests {
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn newline_test() {
|
||||||
|
assert!(parse_command("\n!roll 1d4").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn whitespace_and_newline_test() {
|
||||||
|
assert!(parse_command(" \n!roll 1d4").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn newline_and_whitespace_test() {
|
||||||
|
assert!(parse_command("\n !cthroll 50").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn word_with_exclamation_mark_test() {
|
fn word_with_exclamation_mark_test() {
|
||||||
let result1 = parse_command("hello !notacommand");
|
let result1 = parse_command("hello !notacommand");
|
||||||
|
@ -225,4 +201,47 @@ mod tests {
|
||||||
assert!(split_command("roll 1d4").is_err());
|
assert!(split_command("roll 1d4").is_err());
|
||||||
assert!(split_command("roll").is_err());
|
assert!(split_command("roll").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn chance_die_is_not_malformed() {
|
||||||
|
assert!(parse_command("!chance").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roll_malformed_expression_test() {
|
||||||
|
assert!(parse_command("!roll 1d20asdlfkj").is_err());
|
||||||
|
assert!(parse_command("!roll 1d20asdlfkj ").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roll_dice_pool_malformed_expression_test() {
|
||||||
|
assert!(parse_command("!pool 8abc").is_err());
|
||||||
|
assert!(parse_command("!pool 8abc ").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pool_whitespace_test() {
|
||||||
|
parse_command("!pool 8 ns3 ").expect("was error");
|
||||||
|
parse_command(" !pool 8 ns3").expect("was error");
|
||||||
|
parse_command(" !pool 8 ns3 ").expect("was error");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn help_whitespace_test() {
|
||||||
|
parse_command("!help stuff ").expect("was error");
|
||||||
|
parse_command(" !help stuff").expect("was error");
|
||||||
|
parse_command(" !help stuff ").expect("was error");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roll_whitespace_test() {
|
||||||
|
parse_command("!roll 1d4 + 5d6 -3 ").expect("was error");
|
||||||
|
parse_command("!roll 1d4 + 5d6 -3 ").expect("was error");
|
||||||
|
parse_command(" !roll 1d4 + 5d6 -3 ").expect("was error");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_insensitive_test() {
|
||||||
|
parse_command("!CTHROLL 40").expect("command parsing is not case sensitive.");
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,187 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::db::Users;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::matrix;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use fuse_rust::{Fuse, FuseProperty, Fuseable};
|
||||||
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
|
use matrix_sdk::{ruma::OwnedUserId, Client};
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
/// Holds matrix room ID and display name as strings, for use with
|
||||||
|
/// searching. See search_for_room.
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
struct RoomNameAndId {
|
||||||
|
id: String,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allows searching for a room name and ID struct, instead of just
|
||||||
|
/// searching room display names directly.
|
||||||
|
impl Fuseable for RoomNameAndId {
|
||||||
|
fn properties(&self) -> Vec<FuseProperty> {
|
||||||
|
vec![FuseProperty {
|
||||||
|
value: String::from("name"),
|
||||||
|
weight: 1.0,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup(&self, key: &str) -> Option<&str> {
|
||||||
|
match key {
|
||||||
|
"name" => Some(&self.name),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempt to find a room by either name or Matrix Room ID query
|
||||||
|
/// string. It prefers the exact room ID first, and then falls back to
|
||||||
|
/// fuzzy searching based on room display name. The best match is
|
||||||
|
/// returned, or None if no matches were found.
|
||||||
|
fn search_for_room<'a>(
|
||||||
|
rooms_for_user: &'a [RoomNameAndId],
|
||||||
|
search_for: &str,
|
||||||
|
) -> Option<&'a RoomNameAndId> {
|
||||||
|
//Lowest score is the best match.
|
||||||
|
let best_fuzzy_match = || -> Option<&RoomNameAndId> {
|
||||||
|
Fuse::default()
|
||||||
|
.search_text_in_fuse_list(search_for, &rooms_for_user)
|
||||||
|
.into_iter()
|
||||||
|
.min_by(|r1, r2| r1.score.partial_cmp(&r2.score).unwrap())
|
||||||
|
.and_then(|result| rooms_for_user.get(result.index))
|
||||||
|
};
|
||||||
|
|
||||||
|
rooms_for_user
|
||||||
|
.iter()
|
||||||
|
.find(|room| room.id == search_for)
|
||||||
|
.or_else(best_fuzzy_match)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_rooms_for_user(
|
||||||
|
client: &Client,
|
||||||
|
user_id: &str,
|
||||||
|
) -> Result<Vec<RoomNameAndId>, BotError> {
|
||||||
|
let user_id = OwnedUserId::try_from(user_id)?;
|
||||||
|
let rooms_for_user = matrix::get_rooms_for_user(client, &user_id).await?;
|
||||||
|
let mut rooms_for_user: Vec<RoomNameAndId> = stream::iter(rooms_for_user)
|
||||||
|
.filter_map(|room| async move {
|
||||||
|
Some(room.display_name().await.map(|room_name| RoomNameAndId {
|
||||||
|
id: room.room_id().to_string(),
|
||||||
|
name: room_name.to_string(),
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
.try_collect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
//Alphabetically descending, symbols first, ignore case.
|
||||||
|
let sort = |r1: &RoomNameAndId, r2: &RoomNameAndId| {
|
||||||
|
r1.name.to_lowercase().cmp(&r2.name.to_lowercase())
|
||||||
|
};
|
||||||
|
|
||||||
|
rooms_for_user.sort_by(sort);
|
||||||
|
Ok(rooms_for_user)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ListRoomsCommand;
|
||||||
|
|
||||||
|
impl TryFrom<String> for ListRoomsCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(_: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(ListRoomsCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for ListRoomsCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"list rooms"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let rooms_for_user: Vec<String> = get_rooms_for_user(&ctx.matrix_client, ctx.username)
|
||||||
|
.await
|
||||||
|
.map(|rooms| {
|
||||||
|
rooms
|
||||||
|
.into_iter()
|
||||||
|
.map(|room| format!(" {} | {}", room.id, room.name))
|
||||||
|
.collect()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let html = format!("<pre>{}</pre>", rooms_for_user.join("\n"));
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SetRoomCommand(String);
|
||||||
|
|
||||||
|
impl TryFrom<String> for SetRoomCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(SetRoomCommand(input))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for SetRoomCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"set active room"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
if !ctx.account.is_registered() {
|
||||||
|
return Err(BotError::AccountDoesNotExist);
|
||||||
|
}
|
||||||
|
|
||||||
|
let rooms_for_user = get_rooms_for_user(&ctx.matrix_client, ctx.username).await?;
|
||||||
|
let room = search_for_room(&rooms_for_user, &self.0);
|
||||||
|
|
||||||
|
if let Some(room) = room {
|
||||||
|
let mut new_user = ctx
|
||||||
|
.account
|
||||||
|
.registered_user()
|
||||||
|
.cloned()
|
||||||
|
.ok_or(BotError::AccountDoesNotExist)?;
|
||||||
|
|
||||||
|
new_user.active_room = Some(room.id.clone());
|
||||||
|
ctx.db.upsert_user(&new_user).await?;
|
||||||
|
Execution::success(format!(r#"Active room set to "{}""#, room.name))
|
||||||
|
} else {
|
||||||
|
Err(BotError::RoomDoesNotExist)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn set_room_prefers_room_id_over_name() {
|
||||||
|
let rooms = vec![
|
||||||
|
RoomNameAndId {
|
||||||
|
id: "roomid".to_string(),
|
||||||
|
name: "room_name".to_string(),
|
||||||
|
},
|
||||||
|
RoomNameAndId {
|
||||||
|
id: "anotherone".to_string(),
|
||||||
|
name: "roomid".to_string(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let found_room = search_for_room(&rooms, "roomid");
|
||||||
|
|
||||||
|
assert!(found_room.is_some());
|
||||||
|
assert_eq!(found_room.unwrap(), &rooms[0]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,161 @@
|
||||||
|
use super::{Command, Execution, ExecutionResult};
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::db::errors::DataError;
|
||||||
|
use crate::db::Variables;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
pub struct GetAllVariablesCommand;
|
||||||
|
|
||||||
|
impl TryFrom<String> for GetAllVariablesCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(_: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(GetAllVariablesCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for GetAllVariablesCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"get all variables"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let variables = ctx
|
||||||
|
.db
|
||||||
|
.get_user_variables(&ctx.username, ctx.active_room_id().as_str())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut variable_list: Vec<String> = variables
|
||||||
|
.into_iter()
|
||||||
|
.map(|(name, value)| format!(" - {} = {}", name, value))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
variable_list.sort();
|
||||||
|
|
||||||
|
let value = variable_list.join("\n");
|
||||||
|
let html = format!(
|
||||||
|
"<strong>Variables:</strong><br/>{}",
|
||||||
|
value.replace("\n", "<br/>")
|
||||||
|
);
|
||||||
|
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GetVariableCommand(pub String);
|
||||||
|
|
||||||
|
impl TryFrom<String> for GetVariableCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(GetVariableCommand(input))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for GetVariableCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"retrieve variable value"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let name = &self.0;
|
||||||
|
let result = ctx
|
||||||
|
.db
|
||||||
|
.get_user_variable(&ctx.username, ctx.active_room_id().as_str(), name)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let value = match result {
|
||||||
|
Ok(num) => format!("{} = {}", name, num),
|
||||||
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let html = format!("<strong>Variable:</strong> {}", value);
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SetVariableCommand(pub String, pub i32);
|
||||||
|
|
||||||
|
impl TryFrom<String> for SetVariableCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
let (variable_name, value) = crate::parser::variables::parse_set_variable(&input)?;
|
||||||
|
Ok(SetVariableCommand(variable_name, value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for SetVariableCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"set variable value"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let name = &self.0;
|
||||||
|
let value = self.1;
|
||||||
|
|
||||||
|
ctx.db
|
||||||
|
.set_user_variable(&ctx.username, ctx.active_room_id().as_str(), name, value)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let content = format!("{} = {}", name, value);
|
||||||
|
let html = format!("<strong>Set Variable:</strong> {}", content);
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DeleteVariableCommand(pub String);
|
||||||
|
|
||||||
|
impl TryFrom<String> for DeleteVariableCommand {
|
||||||
|
type Error = BotError;
|
||||||
|
|
||||||
|
fn try_from(input: String) -> Result<Self, Self::Error> {
|
||||||
|
Ok(DeleteVariableCommand(input))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for DeleteVariableCommand {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"delete variable"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_secure(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
let name = &self.0;
|
||||||
|
let result = ctx
|
||||||
|
.db
|
||||||
|
.delete_user_variable(&ctx.username, ctx.active_room_id().as_str(), name)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let value = match result {
|
||||||
|
Ok(()) => format!("{} now unset", name),
|
||||||
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let html = format!("<strong>Remove Variable:</strong> {}", value);
|
||||||
|
Execution::success(html)
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,10 +4,6 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
/// Shortcut to defining db migration versions. Will probably
|
|
||||||
/// eventually be moved to a config file.
|
|
||||||
const MIGRATION_VERSION: u32 = 5;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ConfigError {
|
pub enum ConfigError {
|
||||||
#[error("i/o error: {0}")]
|
#[error("i/o error: {0}")]
|
||||||
|
@ -53,10 +49,19 @@ fn db_path_from_env() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The "bot" section of the config file, for bot settings.
|
/// The "bot" section of the config file, for bot settings.
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||||
struct BotConfig {
|
struct BotConfig {
|
||||||
/// How far back from current time should we process a message?
|
/// How far back from current time should we process a message?
|
||||||
oldest_message_age: Option<u64>,
|
oldest_message_age: Option<u64>,
|
||||||
|
|
||||||
|
/// What address and port to run the RPC service on. If not
|
||||||
|
/// specified, RPC will not be enabled.
|
||||||
|
rpc_addr: Option<String>,
|
||||||
|
|
||||||
|
/// The shared secret key between the bot and any RPC clients that
|
||||||
|
/// want to connect to it. The RPC server will reject any clients
|
||||||
|
/// that don't present the shared key.
|
||||||
|
rpc_key: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The "database" section of the config file.
|
/// The "database" section of the config file.
|
||||||
|
@ -84,6 +89,18 @@ impl BotConfig {
|
||||||
self.oldest_message_age
|
self.oldest_message_age
|
||||||
.unwrap_or(DEFAULT_OLDEST_MESSAGE_AGE)
|
.unwrap_or(DEFAULT_OLDEST_MESSAGE_AGE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
fn rpc_addr(&self) -> Option<String> {
|
||||||
|
self.rpc_addr.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
fn rpc_key(&self) -> Option<String> {
|
||||||
|
self.rpc_key.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the toml config file for the dicebot. The sections of
|
/// Represents the toml config file for the dicebot. The sections of
|
||||||
|
@ -128,15 +145,6 @@ impl Config {
|
||||||
.unwrap_or_else(|| db_path_from_env())
|
.unwrap_or_else(|| db_path_from_env())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The current migration version we expect of the database. If
|
|
||||||
/// this number is higher than the one in the database, we will
|
|
||||||
/// execute migrations to update the data.
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn migration_version(&self) -> u32 {
|
|
||||||
MIGRATION_VERSION
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Figure out the allowed oldest message age, in seconds. This will
|
/// Figure out the allowed oldest message age, in seconds. This will
|
||||||
/// be the defined oldest message age in the bot config, if the bot
|
/// be the defined oldest message age in the bot config, if the bot
|
||||||
/// configuration and associated "oldest_message_age" setting are
|
/// configuration and associated "oldest_message_age" setting are
|
||||||
|
@ -150,6 +158,18 @@ impl Config {
|
||||||
.map(|bc| bc.oldest_message_age())
|
.map(|bc| bc.oldest_message_age())
|
||||||
.unwrap_or(DEFAULT_OLDEST_MESSAGE_AGE)
|
.unwrap_or(DEFAULT_OLDEST_MESSAGE_AGE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn rpc_addr(&self) -> Option<String> {
|
||||||
|
self.bot.as_ref().and_then(|bc| bc.rpc_addr())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn rpc_key(&self) -> Option<String> {
|
||||||
|
self.bot.as_ref().and_then(|bc| bc.rpc_key())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -169,6 +189,7 @@ mod tests {
|
||||||
}),
|
}),
|
||||||
bot: Some(BotConfig {
|
bot: Some(BotConfig {
|
||||||
oldest_message_age: None,
|
oldest_message_age: None,
|
||||||
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::models::Account;
|
||||||
|
use matrix_sdk::room::Joined;
|
||||||
|
use matrix_sdk::ruma::{RoomId, UserId};
|
||||||
|
use matrix_sdk::Client;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
/// A context carried through the system providing access to things
|
||||||
|
/// like the database.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Context<'a> {
|
||||||
|
pub db: Database,
|
||||||
|
pub matrix_client: Client,
|
||||||
|
pub origin_room: RoomContext<'a>,
|
||||||
|
pub active_room: RoomContext<'a>,
|
||||||
|
pub username: &'a str,
|
||||||
|
pub message_body: &'a str,
|
||||||
|
pub account: Account,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context<'_> {
|
||||||
|
pub fn active_room_id(&self) -> &RoomId {
|
||||||
|
self.active_room.id
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn room_id(&self) -> &RoomId {
|
||||||
|
self.origin_room.id
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_secure(&self) -> bool {
|
||||||
|
self.origin_room.secure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct RoomContext<'a> {
|
||||||
|
pub id: &'a RoomId,
|
||||||
|
pub display_name: String,
|
||||||
|
pub secure: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoomContext<'_> {
|
||||||
|
pub async fn new_with_name<'a>(
|
||||||
|
room: &'a Joined,
|
||||||
|
sending_user: &str,
|
||||||
|
) -> Result<RoomContext<'a>, BotError> {
|
||||||
|
// TODO is_direct is a hack; the bot should set eligible rooms
|
||||||
|
// to Direct Message upon joining, if other contact has
|
||||||
|
// requested it. Waiting on SDK support.
|
||||||
|
let display_name =
|
||||||
|
room
|
||||||
|
.display_name()
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.map(|d| d.to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let sending_user = <&UserId>::try_from(sending_user)?;
|
||||||
|
let user_in_room = room.get_member(sending_user).await.ok().is_some();
|
||||||
|
let is_direct = room.active_members().await?.len() == 2;
|
||||||
|
|
||||||
|
Ok(RoomContext {
|
||||||
|
id: room.room_id(),
|
||||||
|
display_name,
|
||||||
|
secure: room.is_encrypted() && is_direct && user_in_room,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new<'a>(
|
||||||
|
room: &'a Joined,
|
||||||
|
sending_user: &'a str,
|
||||||
|
) -> Result<RoomContext<'a>, BotError> {
|
||||||
|
Self::new_with_name(room, sending_user).await
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +1,22 @@
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::db::Variables;
|
||||||
|
use crate::error::{BotError, DiceRollingError};
|
||||||
|
use crate::logic::calculate_single_die_amount;
|
||||||
|
use crate::parser::dice::{Amount, DiceParsingError, Element};
|
||||||
|
use rand::rngs::StdRng;
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::SeedableRng;
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// A planned dice roll.
|
/// A planned dice roll.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct DiceRoll {
|
pub struct DiceRoll {
|
||||||
pub target: u32,
|
pub amount: Amount,
|
||||||
pub modifier: DiceRollModifier,
|
pub modifier: DiceRollModifier,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for DiceRoll {
|
pub struct DiceRollWithContext<'a>(pub &'a DiceRoll, pub &'a Context<'a>);
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
let message = format!("target: {}, with {}", self.target, self.modifier);
|
|
||||||
write!(f, "{}", message)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Potential modifier on the die roll to be made.
|
/// Potential modifier on the die roll to be made.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
@ -91,6 +94,51 @@ impl fmt::Display for RollResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A struct wrapping the target and the actual dice roll result. This
|
||||||
|
/// is done for formatting purposes, so we can display the target
|
||||||
|
/// number (calculated from resolving variables) separately from the
|
||||||
|
/// result.
|
||||||
|
pub struct ExecutedDiceRoll {
|
||||||
|
/// The number we must meet for the roll to be considered a
|
||||||
|
/// success.
|
||||||
|
pub target: u32,
|
||||||
|
|
||||||
|
/// Stored for informational purposes in display.
|
||||||
|
pub modifier: DiceRollModifier,
|
||||||
|
|
||||||
|
/// The actual roll result.
|
||||||
|
pub roll: RolledDice,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ExecutedDiceRoll {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let message = format!("target: {}, with {}", self.target, self.modifier);
|
||||||
|
write!(f, "{}", message)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A struct wrapping the target and the actual advancement roll
|
||||||
|
/// result. This is done for formatting purposes, so we can display
|
||||||
|
/// the target number (calculated from resolving variables) separately
|
||||||
|
/// from the result.
|
||||||
|
pub struct ExecutedAdvancementRoll {
|
||||||
|
/// The number we must exceed for the roll to be considered a
|
||||||
|
/// success.
|
||||||
|
pub target: u32,
|
||||||
|
|
||||||
|
/// The actual roll result.
|
||||||
|
pub roll: RolledAdvancement,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ExecutedAdvancementRoll {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let message = format!("target: {}", self.target);
|
||||||
|
write!(f, "{}", message)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//TODO need to keep track of all rolled numbers for informational purposes!
|
//TODO need to keep track of all rolled numbers for informational purposes!
|
||||||
/// The outcome of a roll.
|
/// The outcome of a roll.
|
||||||
pub struct RolledDice {
|
pub struct RolledDice {
|
||||||
|
@ -100,10 +148,6 @@ pub struct RolledDice {
|
||||||
/// The number we must meet for the roll to be considered a
|
/// The number we must meet for the roll to be considered a
|
||||||
/// success.
|
/// success.
|
||||||
target: u32,
|
target: u32,
|
||||||
|
|
||||||
/// Stored for informational purposes in display.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
modifier: DiceRollModifier,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RolledDice {
|
impl RolledDice {
|
||||||
|
@ -144,21 +188,25 @@ impl fmt::Display for RolledDice {
|
||||||
|
|
||||||
/// A planned advancement roll, where the target number is the
|
/// A planned advancement roll, where the target number is the
|
||||||
/// existing skill amount.
|
/// existing skill amount.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct AdvancementRoll {
|
pub struct AdvancementRoll {
|
||||||
/// The amount (0 to 100) of the existing skill. We must beat this
|
/// The amount (0 to 100) of the existing skill. We must beat this
|
||||||
/// target number to advance the skill, or roll above a 95.
|
/// target number to advance the skill, or roll above a 95.
|
||||||
pub existing_skill: u32,
|
pub existing_skill: Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for AdvancementRoll {
|
impl fmt::Display for AdvancementRoll {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let message = format!("advancement for skill of {}", self.existing_skill);
|
let message = format!("advancement for skill of {:?}", self.existing_skill);
|
||||||
write!(f, "{}", message)?;
|
write!(f, "{}", message)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A struct holding an advancement roll and the context, so we can
|
||||||
|
/// translate variables to numbers.
|
||||||
|
pub struct AdvancementRollWithContext<'a>(pub &'a AdvancementRoll, pub &'a Context<'a>);
|
||||||
|
|
||||||
/// A completed advancement roll.
|
/// A completed advancement roll.
|
||||||
pub struct RolledAdvancement {
|
pub struct RolledAdvancement {
|
||||||
existing_skill: u32,
|
existing_skill: u32,
|
||||||
|
@ -207,16 +255,32 @@ impl fmt::Display for RolledAdvancement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is a trait so we can inject controlled dice rolls in unit
|
||||||
|
/// tests.
|
||||||
trait DieRoller {
|
trait DieRoller {
|
||||||
fn roll(&mut self) -> u32;
|
fn roll(&mut self) -> u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
///A version of DieRoller that uses a rand::Rng to roll numbers.
|
/// Macro to determine if an Amount is a variable.
|
||||||
struct RngDieRoller<R: rand::Rng>(R);
|
macro_rules! is_variable {
|
||||||
|
($existing_skill:ident) => {
|
||||||
|
matches!(
|
||||||
|
$existing_skill,
|
||||||
|
Amount {
|
||||||
|
element: Element::Variable(_),
|
||||||
|
..
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl<R: rand::Rng> DieRoller for RngDieRoller<R> {
|
/// A die roller than can have an RNG implementation injected, but
|
||||||
|
/// must be thread-safe. Required for the async dice rolling code.
|
||||||
|
struct RngDieRoller<R: Rng + ?Sized + Send>(R);
|
||||||
|
|
||||||
|
impl<R: Rng + ?Sized + Send> DieRoller for RngDieRoller<R> {
|
||||||
fn roll(&mut self) -> u32 {
|
fn roll(&mut self) -> u32 {
|
||||||
self.0.gen_range(0, 10)
|
self.0.gen_range(0..=9)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,9 +299,14 @@ fn roll_percentile_dice<R: DieRoller>(roller: &mut R, unit_roll: u32) -> u32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn roll_regular_dice<R: DieRoller>(roll: &DiceRoll, roller: &mut R) -> RolledDice {
|
fn roll_regular_dice<R: DieRoller>(
|
||||||
|
modifier: &DiceRollModifier,
|
||||||
|
target: u32,
|
||||||
|
roller: &mut R,
|
||||||
|
) -> RolledDice {
|
||||||
use DiceRollModifier::*;
|
use DiceRollModifier::*;
|
||||||
let num_rolls = match roll.modifier {
|
|
||||||
|
let num_rolls = match modifier {
|
||||||
Normal => 1,
|
Normal => 1,
|
||||||
OneBonus | OnePenalty => 2,
|
OneBonus | OnePenalty => 2,
|
||||||
TwoBonus | TwoPenalty => 3,
|
TwoBonus | TwoPenalty => 3,
|
||||||
|
@ -249,7 +318,7 @@ fn roll_regular_dice<R: DieRoller>(roll: &DiceRoll, roller: &mut R) -> RolledDic
|
||||||
.map(|_| roll_percentile_dice(roller, unit_roll))
|
.map(|_| roll_percentile_dice(roller, unit_roll))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let num_rolled = match roll.modifier {
|
let num_rolled = match modifier {
|
||||||
Normal => rolls.first(),
|
Normal => rolls.first(),
|
||||||
OneBonus | TwoBonus => rolls.iter().min(),
|
OneBonus | TwoBonus => rolls.iter().min(),
|
||||||
OnePenalty | TwoPenalty => rolls.iter().max(),
|
OnePenalty | TwoPenalty => rolls.iter().max(),
|
||||||
|
@ -257,61 +326,118 @@ fn roll_regular_dice<R: DieRoller>(roll: &DiceRoll, roller: &mut R) -> RolledDic
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
RolledDice {
|
RolledDice {
|
||||||
modifier: roll.modifier,
|
|
||||||
num_rolled: *num_rolled,
|
num_rolled: *num_rolled,
|
||||||
target: roll.target,
|
target: target,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn roll_advancement_dice<R: DieRoller>(
|
fn roll_advancement_dice<R: DieRoller>(target: u32, roller: &mut R) -> RolledAdvancement {
|
||||||
roll: &AdvancementRoll,
|
|
||||||
roller: &mut R,
|
|
||||||
) -> RolledAdvancement {
|
|
||||||
let unit_roll = roller.roll();
|
let unit_roll = roller.roll();
|
||||||
let percentile_roll = roll_percentile_dice(roller, unit_roll);
|
let percentile_roll = roll_percentile_dice(roller, unit_roll);
|
||||||
|
|
||||||
if percentile_roll > roll.existing_skill || percentile_roll > 95 {
|
if percentile_roll > target || percentile_roll > 95 {
|
||||||
RolledAdvancement {
|
RolledAdvancement {
|
||||||
num_rolled: percentile_roll,
|
num_rolled: percentile_roll,
|
||||||
existing_skill: roll.existing_skill,
|
existing_skill: target,
|
||||||
advancement: roller.roll() + 1,
|
advancement: roller.roll() + 1,
|
||||||
successful: true,
|
successful: true,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
RolledAdvancement {
|
RolledAdvancement {
|
||||||
num_rolled: percentile_roll,
|
num_rolled: percentile_roll,
|
||||||
existing_skill: roll.existing_skill,
|
existing_skill: target,
|
||||||
advancement: 0,
|
advancement: 0,
|
||||||
successful: false,
|
successful: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DiceRoll {
|
/// Make a roll with a target number and potential modifier. In a
|
||||||
/// Make a roll with a target number and potential modifier. In a
|
/// normal roll, only one percentile die is rolled (1d100). With
|
||||||
/// normal roll, only one percentile die is rolled (1d100). With
|
/// bonuses or penalties, more dice are rolled, and either the lowest
|
||||||
/// bonuses or penalties, more dice are rolled, and either the lowest
|
/// (in case of bonus) or highest (in case of penalty) result is
|
||||||
/// (in case of bonus) or highest (in case of penalty) result is
|
/// picked. Rolls are not simply d100; the unit roll (ones place) is
|
||||||
/// picked. Rolls are not simply d100; the unit roll (ones place) is
|
/// rolled separately from the tens place, and then the unit number is
|
||||||
/// rolled separately from the tens place, and then the unit number is
|
/// added to each potential roll before picking the lowest/highest
|
||||||
/// added to each potential roll before picking the lowest/highest
|
/// result.
|
||||||
/// result.
|
pub async fn regular_roll(
|
||||||
pub fn roll(&self) -> RolledDice {
|
roll_with_ctx: &DiceRollWithContext<'_>,
|
||||||
let mut roller = RngDieRoller(rand::thread_rng());
|
) -> Result<ExecutedDiceRoll, BotError> {
|
||||||
roll_regular_dice(&self, &mut roller)
|
let target = calculate_single_die_amount(&roll_with_ctx.0.amount, roll_with_ctx.1).await?;
|
||||||
}
|
let target = u32::try_from(target).map_err(|_| DiceRollingError::InvalidAmount)?;
|
||||||
|
|
||||||
|
let mut roller = RngDieRoller::<StdRng>(SeedableRng::from_entropy());
|
||||||
|
let rolled_dice = roll_regular_dice(&roll_with_ctx.0.modifier, target, &mut roller);
|
||||||
|
|
||||||
|
Ok(ExecutedDiceRoll {
|
||||||
|
target,
|
||||||
|
modifier: roll_with_ctx.0.modifier,
|
||||||
|
roll: rolled_dice,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AdvancementRoll {
|
async fn update_skill(ctx: &Context<'_>, variable: &str, value: u32) -> Result<(), BotError> {
|
||||||
pub fn roll(&self) -> RolledAdvancement {
|
use std::convert::TryInto;
|
||||||
let mut roller = RngDieRoller(rand::thread_rng());
|
let value: i32 = value.try_into()?;
|
||||||
roll_advancement_dice(self, &mut roller)
|
ctx.db
|
||||||
|
.set_user_variable(
|
||||||
|
&ctx.username,
|
||||||
|
&ctx.active_room_id().as_str(),
|
||||||
|
variable,
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_variable(amount: &Amount) -> Result<&str, DiceParsingError> {
|
||||||
|
match amount.element {
|
||||||
|
Element::Variable(ref varname) => Ok(&varname[..]),
|
||||||
|
_ => Err(DiceParsingError::WrongElementType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub async fn advancement_roll(
|
||||||
|
roll_with_ctx: &AdvancementRollWithContext<'_>,
|
||||||
|
) -> Result<ExecutedAdvancementRoll, BotError> {
|
||||||
|
let existing_skill = &roll_with_ctx.0.existing_skill;
|
||||||
|
let target = calculate_single_die_amount(existing_skill, roll_with_ctx.1).await?;
|
||||||
|
|
||||||
|
let target = u32::try_from(target).map_err(|_| DiceRollingError::InvalidAmount)?;
|
||||||
|
|
||||||
|
if target > 100 {
|
||||||
|
return Err(DiceRollingError::InvalidAmount.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut roller = RngDieRoller::<StdRng>(SeedableRng::from_entropy());
|
||||||
|
let roll = roll_advancement_dice(target, &mut roller);
|
||||||
|
|
||||||
|
drop(roller);
|
||||||
|
|
||||||
|
if roll.successful && is_variable!(existing_skill) {
|
||||||
|
let variable_name: &str = extract_variable(existing_skill)?;
|
||||||
|
update_skill(roll_with_ctx.1, variable_name, roll.new_skill_amount()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ExecutedAdvancementRoll { target, roll })
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::parser::dice::{Amount, Element, Operator};
|
||||||
|
use url::Url;
|
||||||
|
use matrix_sdk::ruma::room_id;
|
||||||
|
|
||||||
|
macro_rules! dummy_room {
|
||||||
|
() => {
|
||||||
|
crate::context::RoomContext {
|
||||||
|
id: &room_id!("!fakeroomid:example.com"),
|
||||||
|
display_name: "displayname".to_owned(),
|
||||||
|
secure: false,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Generate a series of numbers manually for testing. For this
|
/// Generate a series of numbers manually for testing. For this
|
||||||
/// die system, the first roll in the Vec should be the unit roll,
|
/// die system, the first roll in the Vec should be the unit roll,
|
||||||
|
@ -325,7 +451,7 @@ mod tests {
|
||||||
impl SequentialDieRoller {
|
impl SequentialDieRoller {
|
||||||
fn new(results: Vec<u32>) -> SequentialDieRoller {
|
fn new(results: Vec<u32>) -> SequentialDieRoller {
|
||||||
SequentialDieRoller {
|
SequentialDieRoller {
|
||||||
results: results,
|
results,
|
||||||
position: 0,
|
position: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -340,204 +466,287 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_succeeds_when_below_target() {
|
fn extract_variable_gets_variable_name() {
|
||||||
|
let amount = Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable("abc".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = extract_variable(&amount);
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(result.unwrap(), "abc");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extract_variable_fails_on_number() {
|
||||||
|
let result = extract_variable(&Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(1),
|
||||||
|
});
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(result, Err(DiceParsingError::WrongElementType)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn regular_roll_rejects_negative_numbers() {
|
||||||
let roll = DiceRoll {
|
let roll = DiceRoll {
|
||||||
target: 50,
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(-10),
|
||||||
|
},
|
||||||
modifier: DiceRollModifier::Normal,
|
modifier: DiceRollModifier::Normal,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
|
let roll_with_ctx = DiceRollWithContext(&roll, &ctx);
|
||||||
|
let result = regular_roll(&roll_with_ctx).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(
|
||||||
|
result,
|
||||||
|
Err(BotError::DiceRollingError(DiceRollingError::InvalidAmount))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn advancement_roll_rejects_negative_numbers() {
|
||||||
|
let roll = AdvancementRoll {
|
||||||
|
existing_skill: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(-10),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
|
let roll_with_ctx = AdvancementRollWithContext(&roll, &ctx);
|
||||||
|
let result = advancement_roll(&roll_with_ctx).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(
|
||||||
|
result,
|
||||||
|
Err(BotError::DiceRollingError(DiceRollingError::InvalidAmount))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn advancement_roll_rejects_big_numbers() {
|
||||||
|
let roll = AdvancementRoll {
|
||||||
|
existing_skill: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(3000),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
|
let ctx = Context {
|
||||||
|
account: crate::models::Account::default(),
|
||||||
|
db: db,
|
||||||
|
matrix_client: matrix_sdk::Client::new(homeserver).await.unwrap(),
|
||||||
|
origin_room: dummy_room!(),
|
||||||
|
active_room: dummy_room!(),
|
||||||
|
username: "username",
|
||||||
|
message_body: "message",
|
||||||
|
};
|
||||||
|
|
||||||
|
let roll_with_ctx = AdvancementRollWithContext(&roll, &ctx);
|
||||||
|
let result = advancement_roll(&roll_with_ctx).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(
|
||||||
|
result,
|
||||||
|
Err(BotError::DiceRollingError(DiceRollingError::InvalidAmount))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_variable_macro_succeds_on_variable() {
|
||||||
|
let amount = Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable("abc".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(is_variable!(amount), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_variable_macro_fails_on_number() {
|
||||||
|
let amount = Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(1),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(is_variable!(amount), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_succeeds_when_below_target() {
|
||||||
//Roll 30, succeeding.
|
//Roll 30, succeeding.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::Success, rolled.result());
|
assert_eq!(RollResult::Success, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_hard_success_when_rolling_half() {
|
fn regular_roll_hard_success_when_rolling_half() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 25, succeeding.
|
//Roll 25, succeeding.
|
||||||
let mut roller = SequentialDieRoller::new(vec![5, 2]);
|
let mut roller = SequentialDieRoller::new(vec![5, 2]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::HardSuccess, rolled.result());
|
assert_eq!(RollResult::HardSuccess, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_extreme_success_when_rolling_one_fifth() {
|
fn regular_roll_extreme_success_when_rolling_one_fifth() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 10, succeeding extremely.
|
//Roll 10, succeeding extremely.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 1]);
|
let mut roller = SequentialDieRoller::new(vec![0, 1]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::ExtremeSuccess, rolled.result());
|
assert_eq!(RollResult::ExtremeSuccess, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_extreme_success_target_above_100() {
|
fn regular_roll_extreme_success_target_above_100() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 150,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 30, succeeding extremely.
|
//Roll 30, succeeding extremely.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 150, &mut roller);
|
||||||
assert_eq!(RollResult::ExtremeSuccess, rolled.result());
|
assert_eq!(RollResult::ExtremeSuccess, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_critical_success_on_one() {
|
fn regular_roll_critical_success_on_one() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 1.
|
//Roll 1.
|
||||||
let mut roller = SequentialDieRoller::new(vec![1, 0]);
|
let mut roller = SequentialDieRoller::new(vec![1, 0]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::CriticalSuccess, rolled.result());
|
assert_eq!(RollResult::CriticalSuccess, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_fail_when_above_target() {
|
fn regular_roll_fail_when_above_target() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 60.
|
//Roll 60.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 6]);
|
let mut roller = SequentialDieRoller::new(vec![0, 6]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::Failure, rolled.result());
|
assert_eq!(RollResult::Failure, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_is_fumble_when_skill_below_50_and_roll_at_least_96() {
|
fn regular_roll_is_fumble_when_skill_below_50_and_roll_at_least_96() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 49,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 96.
|
//Roll 96.
|
||||||
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 49, &mut roller);
|
||||||
assert_eq!(RollResult::Fumble, rolled.result());
|
assert_eq!(RollResult::Fumble, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_is_failure_when_skill_at_or_above_50_and_roll_at_least_96() {
|
fn regular_roll_is_failure_when_skill_at_or_above_50_and_roll_at_least_96() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 96.
|
//Roll 96.
|
||||||
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(RollResult::Failure, rolled.result());
|
assert_eq!(RollResult::Failure, rolled.result());
|
||||||
|
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 68,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 96.
|
//Roll 96.
|
||||||
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
let mut roller = SequentialDieRoller::new(vec![6, 9]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 68, &mut roller);
|
||||||
assert_eq!(RollResult::Failure, rolled.result());
|
assert_eq!(RollResult::Failure, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regular_roll_always_fumble_on_100() {
|
fn regular_roll_always_fumble_on_100() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 100,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Roll 100.
|
//Roll 100.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 0]);
|
let mut roller = SequentialDieRoller::new(vec![0, 0]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 100, &mut roller);
|
||||||
assert_eq!(RollResult::Fumble, rolled.result());
|
assert_eq!(RollResult::Fumble, rolled.result());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn one_penalty_picks_highest_of_two() {
|
fn one_penalty_picks_highest_of_two() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::OnePenalty,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Should only roll 30 and 40, not 50.
|
//Should only roll 30 and 40, not 50.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::OnePenalty, 50, &mut roller);
|
||||||
assert_eq!(40, rolled.num_rolled);
|
assert_eq!(40, rolled.num_rolled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn two_penalty_picks_highest_of_three() {
|
fn two_penalty_picks_highest_of_three() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::TwoPenalty,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Should only roll 30, 40, 50, and not 60.
|
//Should only roll 30, 40, 50, and not 60.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5, 6]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5, 6]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::TwoPenalty, 50, &mut roller);
|
||||||
assert_eq!(50, rolled.num_rolled);
|
assert_eq!(50, rolled.num_rolled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn one_bonus_picks_lowest_of_two() {
|
fn one_bonus_picks_lowest_of_two() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::OneBonus,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Should only roll 30 and 40, not 20.
|
//Should only roll 30 and 40, not 20.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 2]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 2]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::OneBonus, 50, &mut roller);
|
||||||
assert_eq!(30, rolled.num_rolled);
|
assert_eq!(30, rolled.num_rolled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn two_bonus_picks_lowest_of_three() {
|
fn two_bonus_picks_lowest_of_three() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::TwoBonus,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Should only roll 30, 40, 50, and not 20.
|
//Should only roll 30, 40, 50, and not 20.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5, 2]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3, 4, 5, 2]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::TwoBonus, 50, &mut roller);
|
||||||
assert_eq!(30, rolled.num_rolled);
|
assert_eq!(30, rolled.num_rolled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn normal_modifier_rolls_once() {
|
fn normal_modifier_rolls_once() {
|
||||||
let roll = DiceRoll {
|
|
||||||
target: 50,
|
|
||||||
modifier: DiceRollModifier::Normal,
|
|
||||||
};
|
|
||||||
|
|
||||||
//Should only roll 30, not 40.
|
//Should only roll 30, not 40.
|
||||||
let mut roller = SequentialDieRoller::new(vec![0, 3, 4]);
|
let mut roller = SequentialDieRoller::new(vec![0, 3, 4]);
|
||||||
let rolled = roll_regular_dice(&roll, &mut roller);
|
let rolled = roll_regular_dice(&DiceRollModifier::Normal, 50, &mut roller);
|
||||||
assert_eq!(30, rolled.num_rolled);
|
assert_eq!(30, rolled.num_rolled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn advancement_succeeds_on_above_skill() {
|
fn advancement_succeeds_on_above_skill() {
|
||||||
let roll = AdvancementRoll { existing_skill: 30 };
|
|
||||||
|
|
||||||
//Roll 52, then advance skill by 5. (advancement adds +1 to 0-9 roll)
|
//Roll 52, then advance skill by 5. (advancement adds +1 to 0-9 roll)
|
||||||
let mut roller = SequentialDieRoller::new(vec![2, 5, 4]);
|
let mut roller = SequentialDieRoller::new(vec![2, 5, 4]);
|
||||||
let rolled = roll_advancement_dice(&roll, &mut roller);
|
|
||||||
|
let rolled = roll_advancement_dice(30, &mut roller);
|
||||||
assert!(rolled.successful());
|
assert!(rolled.successful());
|
||||||
assert_eq!(5, rolled.advancement());
|
assert_eq!(5, rolled.advancement());
|
||||||
assert_eq!(35, rolled.new_skill_amount());
|
assert_eq!(35, rolled.new_skill_amount());
|
||||||
|
@ -545,11 +754,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn advancement_succeeds_on_above_95() {
|
fn advancement_succeeds_on_above_95() {
|
||||||
let roll = AdvancementRoll { existing_skill: 97 };
|
|
||||||
|
|
||||||
//Roll 96, then advance skill by 1. (advancement adds +1 to 0-9 roll)
|
//Roll 96, then advance skill by 1. (advancement adds +1 to 0-9 roll)
|
||||||
let mut roller = SequentialDieRoller::new(vec![6, 9, 0]);
|
let mut roller = SequentialDieRoller::new(vec![6, 9, 0]);
|
||||||
let rolled = roll_advancement_dice(&roll, &mut roller);
|
|
||||||
|
let rolled = roll_advancement_dice(97, &mut roller);
|
||||||
assert!(rolled.successful());
|
assert!(rolled.successful());
|
||||||
assert_eq!(1, rolled.advancement());
|
assert_eq!(1, rolled.advancement());
|
||||||
assert_eq!(98, rolled.new_skill_amount());
|
assert_eq!(98, rolled.new_skill_amount());
|
||||||
|
@ -557,11 +765,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn advancement_fails_on_below_skill() {
|
fn advancement_fails_on_below_skill() {
|
||||||
let roll = AdvancementRoll { existing_skill: 30 };
|
|
||||||
|
|
||||||
//Roll 25, failing.
|
//Roll 25, failing.
|
||||||
let mut roller = SequentialDieRoller::new(vec![5, 2]);
|
let mut roller = SequentialDieRoller::new(vec![5, 2]);
|
||||||
let rolled = roll_advancement_dice(&roll, &mut roller);
|
|
||||||
|
let rolled = roll_advancement_dice(30, &mut roller);
|
||||||
assert!(!rolled.successful());
|
assert!(!rolled.successful());
|
||||||
assert_eq!(0, rolled.advancement());
|
assert_eq!(0, rolled.advancement());
|
||||||
}
|
}
|
|
@ -0,0 +1,242 @@
|
||||||
|
use super::dice::{AdvancementRoll, DiceRoll, DiceRollModifier};
|
||||||
|
use crate::parser::dice::DiceParsingError;
|
||||||
|
|
||||||
|
//TOOD convert these to use parse_amounts from the common dice code.
|
||||||
|
|
||||||
|
fn parse_modifier(input: &str) -> Result<DiceRollModifier, DiceParsingError> {
|
||||||
|
match input.trim() {
|
||||||
|
"bb" => Ok(DiceRollModifier::TwoBonus),
|
||||||
|
"b" => Ok(DiceRollModifier::OneBonus),
|
||||||
|
"pp" => Ok(DiceRollModifier::TwoPenalty),
|
||||||
|
"p" => Ok(DiceRollModifier::OnePenalty),
|
||||||
|
"" => Ok(DiceRollModifier::Normal),
|
||||||
|
_ => Err(DiceParsingError::InvalidModifiers),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make diceroll take a vec of Amounts
|
||||||
|
//Split based on :, send first part to parse_modifier.
|
||||||
|
//Send second part to parse_amounts
|
||||||
|
pub fn parse_regular_roll(input: &str) -> Result<DiceRoll, DiceParsingError> {
|
||||||
|
let (amount, modifiers_str) = crate::parser::dice::parse_single_amount(input)?;
|
||||||
|
let modifier = parse_modifier(modifiers_str)?;
|
||||||
|
Ok(DiceRoll { modifier, amount })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_advancement_roll(input: &str) -> Result<AdvancementRoll, DiceParsingError> {
|
||||||
|
let input = input.trim();
|
||||||
|
let (amounts, unconsumed_input) = crate::parser::dice::parse_single_amount(input)?;
|
||||||
|
|
||||||
|
if unconsumed_input.len() == 0 {
|
||||||
|
Ok(AdvancementRoll {
|
||||||
|
existing_skill: amounts,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err(DiceParsingError::InvalidAmount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::parser::dice::{Amount, DiceParsingError, Element, Operator};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_rejects_bad_value() {
|
||||||
|
let modifier = parse_modifier("qqq");
|
||||||
|
assert!(matches!(modifier, Err(DiceParsingError::InvalidModifiers)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_one_bonus() {
|
||||||
|
let modifier = parse_modifier("b");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::OneBonus)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_two_bonus() {
|
||||||
|
let modifier = parse_modifier("bb");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::TwoBonus)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_two_penalty() {
|
||||||
|
let modifier = parse_modifier("pp");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::TwoPenalty)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_one_penalty() {
|
||||||
|
let modifier = parse_modifier("p");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::OnePenalty)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_normal() {
|
||||||
|
let modifier = parse_modifier("");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::Normal)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_modifier_accepts_normal_unaffected_by_whitespace() {
|
||||||
|
let modifier = parse_modifier(" ");
|
||||||
|
assert!(matches!(modifier, Ok(DiceRollModifier::Normal)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_single_number() {
|
||||||
|
let result = parse_regular_roll("60");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll {
|
||||||
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
},
|
||||||
|
modifier: DiceRollModifier::Normal
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_rejects_complex_expressions() {
|
||||||
|
let result = parse_regular_roll("3 + abc + bob - 4");
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_two_bonus() {
|
||||||
|
let result = parse_regular_roll("60 bb");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll {
|
||||||
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
},
|
||||||
|
modifier: DiceRollModifier::TwoBonus
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_one_bonus() {
|
||||||
|
let result = parse_regular_roll("60 b");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll {
|
||||||
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
},
|
||||||
|
modifier: DiceRollModifier::OneBonus
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_two_penalty() {
|
||||||
|
let result = parse_regular_roll("60 pp");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll {
|
||||||
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
},
|
||||||
|
modifier: DiceRollModifier::TwoPenalty
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_one_penalty() {
|
||||||
|
let result = parse_regular_roll("60 p");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
DiceRoll {
|
||||||
|
amount: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
},
|
||||||
|
modifier: DiceRollModifier::OnePenalty
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regular_roll_accepts_whitespace() {
|
||||||
|
assert!(parse_regular_roll("60 ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 ").is_ok());
|
||||||
|
|
||||||
|
assert!(parse_regular_roll("60bb ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 bb").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 bb ").is_ok());
|
||||||
|
|
||||||
|
assert!(parse_regular_roll("60b ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 b").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 b ").is_ok());
|
||||||
|
|
||||||
|
assert!(parse_regular_roll("60pp ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 pp").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 pp ").is_ok());
|
||||||
|
|
||||||
|
assert!(parse_regular_roll("60p ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60p ").is_ok());
|
||||||
|
assert!(parse_regular_roll(" 60 p ").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn advancement_roll_accepts_whitespacen() {
|
||||||
|
assert!(parse_advancement_roll("60 ").is_ok());
|
||||||
|
assert!(parse_advancement_roll(" 60").is_ok());
|
||||||
|
assert!(parse_advancement_roll(" 60 ").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn advancement_roll_accepts_single_number() {
|
||||||
|
let result = parse_advancement_roll("60");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
AdvancementRoll {
|
||||||
|
existing_skill: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(60)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn advancement_roll_allows_big_numbers() {
|
||||||
|
assert!(parse_advancement_roll("3000").is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn advancement_roll_allows_variables() {
|
||||||
|
let result = parse_advancement_roll("abc");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
AdvancementRoll {
|
||||||
|
existing_skill: Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable(String::from("abc"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
result.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn advancement_roll_rejects_complex_expressions() {
|
||||||
|
let result = parse_advancement_roll("3 + abc + bob - 4");
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
use std::num::TryFromIntError;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum DataError {
|
||||||
|
#[error("value does not exist for key: {0}")]
|
||||||
|
KeyDoesNotExist(String),
|
||||||
|
|
||||||
|
#[error("too many entries")]
|
||||||
|
TooManyEntries,
|
||||||
|
|
||||||
|
#[error("expected i32, but i32 schema was violated")]
|
||||||
|
I32SchemaViolation,
|
||||||
|
|
||||||
|
#[error("unexpected or corruptd data bytes")]
|
||||||
|
InvalidValue,
|
||||||
|
|
||||||
|
#[error("expected string ref, but utf8 schema was violated: {0}")]
|
||||||
|
Utf8RefSchemaViolation(#[from] std::str::Utf8Error),
|
||||||
|
|
||||||
|
#[error("expected string, but utf8 schema was violated: {0}")]
|
||||||
|
Utf8SchemaViolation(#[from] std::string::FromUtf8Error),
|
||||||
|
|
||||||
|
#[error("data migration error: {0}")]
|
||||||
|
MigrationError(#[from] crate::db::sqlite::migrator::MigrationError),
|
||||||
|
|
||||||
|
#[error("internal database error: {0}")]
|
||||||
|
SqlxError(#[from] sqlx::Error),
|
||||||
|
|
||||||
|
#[error("numeric conversion error")]
|
||||||
|
NumericConversionError(#[from] TryFromIntError),
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::models::User;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use errors::DataError;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub mod errors;
|
||||||
|
pub mod sqlite;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub(crate) trait DbState {
|
||||||
|
async fn get_device_id(&self) -> Result<Option<String>, DataError>;
|
||||||
|
|
||||||
|
async fn set_device_id(&self, device_id: &str) -> Result<(), DataError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub(crate) trait Users {
|
||||||
|
async fn upsert_user(&self, user: &User) -> Result<(), DataError>;
|
||||||
|
|
||||||
|
async fn get_user(&self, username: &str) -> Result<Option<User>, DataError>;
|
||||||
|
|
||||||
|
async fn delete_user(&self, username: &str) -> Result<(), DataError>;
|
||||||
|
|
||||||
|
async fn authenticate_user(
|
||||||
|
&self,
|
||||||
|
username: &str,
|
||||||
|
raw_password: &str,
|
||||||
|
) -> Result<Option<User>, BotError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub(crate) trait Rooms {
|
||||||
|
async fn should_process(&self, room_id: &str, event_id: &str) -> Result<bool, DataError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO move this up to the top once we delete sled. Traits will be the
|
||||||
|
// main API, then we can have different impls for different DBs.
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Variables {
|
||||||
|
async fn get_user_variables(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
) -> Result<HashMap<String, i32>, DataError>;
|
||||||
|
|
||||||
|
async fn get_variable_count(&self, user: &str, room_id: &str) -> Result<i32, DataError>;
|
||||||
|
|
||||||
|
async fn get_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
) -> Result<i32, DataError>;
|
||||||
|
|
||||||
|
async fn set_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
value: i32,
|
||||||
|
) -> Result<(), DataError>;
|
||||||
|
|
||||||
|
async fn delete_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
) -> Result<(), DataError>;
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
use crate::systems::GameSystem;
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, types::Type, Migration};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
|
fn primary_id() -> Type {
|
||||||
|
types::text().unique(true).primary(true).nullable(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Normally we would add a CHECK clause here, but types::custom requires a 'static string.
|
||||||
|
//Which means we can't automagically generate one from the enum.
|
||||||
|
m.create_table("room_info", move |t| {
|
||||||
|
t.add_column("room_id", primary_id());
|
||||||
|
t.add_column("game_system", types::text().nullable(false));
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, Migration};
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
m.create_table("user_variables", |t| {
|
||||||
|
t.add_column("room_id", types::text());
|
||||||
|
t.add_column("user_id", types::text());
|
||||||
|
t.add_column("key", types::text());
|
||||||
|
t.add_column("value", types::integer());
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, types::Type, Migration};
|
||||||
|
|
||||||
|
fn primary_uuid() -> Type {
|
||||||
|
types::text().unique(true).primary(true).nullable(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Table for basic room information: room ID, room name
|
||||||
|
m.create_table("room_info", move |t| {
|
||||||
|
t.add_column("room_id", primary_uuid());
|
||||||
|
t.add_column("room_name", types::text());
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, Migration};
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Basic state table with only device ID for now. Uses only one row.
|
||||||
|
m.create_table("bot_state", move |t| {
|
||||||
|
t.add_column("device_id", types::text());
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, Migration};
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Table of room ID, event ID, event timestamp
|
||||||
|
m.create_table("room_events", move |t| {
|
||||||
|
t.add_column("room_id", types::text().nullable(false));
|
||||||
|
t.add_column("event_id", types::text().nullable(false));
|
||||||
|
t.add_column("event_timestamp", types::integer());
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut res = m.make::<Sqlite>();
|
||||||
|
|
||||||
|
//This is a hack that gives us a composite primary key.
|
||||||
|
if res.ends_with(");") {
|
||||||
|
res.pop();
|
||||||
|
res.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
format!("{}, PRIMARY KEY (room_id, event_id));", res)
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, Migration};
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Table of users in rooms.
|
||||||
|
m.create_table("room_users", move |t| {
|
||||||
|
t.add_column("room_id", types::text());
|
||||||
|
t.add_column("username", types::text());
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut res = m.make::<Sqlite>();
|
||||||
|
|
||||||
|
//This is a hack that gives us a composite primary key.
|
||||||
|
if res.ends_with(");") {
|
||||||
|
res.pop();
|
||||||
|
res.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
format!("{}, PRIMARY KEY (room_id, username));", res)
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, types::Type, Migration};
|
||||||
|
|
||||||
|
fn primary_uuid() -> Type {
|
||||||
|
types::text().unique(true).primary(true).nullable(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
//Table of room ID, event ID, event timestamp
|
||||||
|
m.create_table("accounts", move |t| {
|
||||||
|
t.add_column("user_id", primary_uuid());
|
||||||
|
t.add_column("password", types::text().nullable(false));
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::Migration;
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
m.drop_table_if_exists("room_info");
|
||||||
|
m.drop_table_if_exists("room_users");
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
use barrel::backend::Sqlite;
|
||||||
|
use barrel::{types, types::Type, Migration};
|
||||||
|
|
||||||
|
fn primary_uuid() -> Type {
|
||||||
|
types::text().unique(true).primary(true).nullable(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn migration() -> String {
|
||||||
|
let mut m = Migration::new();
|
||||||
|
|
||||||
|
// Keep track of contextual user state.
|
||||||
|
m.create_table("user_state", move |t| {
|
||||||
|
t.add_column("user_id", primary_uuid());
|
||||||
|
t.add_column("active_room", types::text().nullable(true));
|
||||||
|
});
|
||||||
|
|
||||||
|
m.make::<Sqlite>()
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
pub fn migration() -> String {
|
||||||
|
// sqlite does really support alter column, and barrel does not
|
||||||
|
// implement the required workaround, so we do it ourselves!
|
||||||
|
r#"
|
||||||
|
CREATE TABLE IF NOT EXISTS "accounts2" (
|
||||||
|
"user_id" TEXT PRIMARY KEY NOT NULL UNIQUE,
|
||||||
|
"password" TEXT NULL,
|
||||||
|
"account_status" TEXT NOT NULL CHECK(
|
||||||
|
account_status IN ('not_registered', 'registered', 'awaiting_activation'
|
||||||
|
))
|
||||||
|
);
|
||||||
|
INSERT INTO accounts2 select *, 'registered' FROM accounts;
|
||||||
|
DROP TABLE accounts;
|
||||||
|
ALTER TABLE accounts2 RENAME TO accounts;
|
||||||
|
"#
|
||||||
|
.to_string()
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
use log::info;
|
||||||
|
use refinery::config::{Config, ConfigDbType};
|
||||||
|
use sqlx::sqlite::SqliteConnectOptions;
|
||||||
|
use sqlx::ConnectOptions;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
//pub mod migrations;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum MigrationError {
|
||||||
|
#[error("sqlite connection error: {0}")]
|
||||||
|
SqlxError(#[from] sqlx::Error),
|
||||||
|
|
||||||
|
#[error("refinery migration error: {0}")]
|
||||||
|
RefineryError(#[from] refinery::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
mod embedded {
|
||||||
|
use refinery::embed_migrations;
|
||||||
|
embed_migrations!("src/db/sqlite/migrator/migrations");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run database migrations against the sqlite database.
|
||||||
|
pub async fn migrate(db_path: &str) -> Result<(), MigrationError> {
|
||||||
|
//Create database if missing.
|
||||||
|
let conn = SqliteConnectOptions::from_str(&format!("sqlite://{}", db_path))?
|
||||||
|
.create_if_missing(true)
|
||||||
|
.connect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
drop(conn);
|
||||||
|
|
||||||
|
let mut conn = Config::new(ConfigDbType::Sqlite).set_db_path(db_path);
|
||||||
|
info!("Running migrations");
|
||||||
|
embedded::migrations::runner().run(&mut conn)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
use crate::db::errors::DataError;
|
||||||
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
|
||||||
|
use sqlx::ConnectOptions;
|
||||||
|
use std::clone::Clone;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
pub mod migrator;
|
||||||
|
pub mod rooms;
|
||||||
|
pub mod state;
|
||||||
|
pub mod users;
|
||||||
|
pub mod variables;
|
||||||
|
|
||||||
|
pub struct Database {
|
||||||
|
conn: SqlitePool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Database {
|
||||||
|
fn new_db(conn: SqlitePool) -> Result<Database, DataError> {
|
||||||
|
let database = Database { conn: conn.clone() };
|
||||||
|
Ok(database)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new(path: &str) -> Result<Database, DataError> {
|
||||||
|
//Create database if missing.
|
||||||
|
let conn = SqliteConnectOptions::from_str(path)?
|
||||||
|
.create_if_missing(true)
|
||||||
|
.connect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
drop(conn);
|
||||||
|
|
||||||
|
//Migrate database.
|
||||||
|
migrator::migrate(&path).await?;
|
||||||
|
|
||||||
|
//Return actual conncetion pool.
|
||||||
|
let conn = SqlitePoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.connect(path)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Self::new_db(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Database {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Database {
|
||||||
|
conn: self.conn.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
use super::Database;
|
||||||
|
use crate::db::{errors::DataError, Rooms};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
async fn record_event(conn: &SqlitePool, room_id: &str, event_id: &str) -> Result<(), DataError> {
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
let now: i64 = i64::try_from(
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Clock has gone backwards")
|
||||||
|
.as_secs(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"INSERT INTO room_events
|
||||||
|
(room_id, event_id, event_timestamp)
|
||||||
|
VALUES (?, ?, ?)"#,
|
||||||
|
)
|
||||||
|
.bind(room_id)
|
||||||
|
.bind(event_id)
|
||||||
|
.bind(now)
|
||||||
|
.execute(conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Rooms for Database {
|
||||||
|
async fn should_process(&self, room_id: &str, event_id: &str) -> Result<bool, DataError> {
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"SELECT event_id FROM room_events
|
||||||
|
WHERE room_id = ? AND event_id = ?"#,
|
||||||
|
room_id,
|
||||||
|
event_id
|
||||||
|
)
|
||||||
|
.fetch_optional(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match row {
|
||||||
|
Some(_) => Ok(false),
|
||||||
|
None => {
|
||||||
|
record_event(&self.conn, room_id, event_id).await?;
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::Rooms;
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
||||||
|
where
|
||||||
|
Fut: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
f(db).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn should_process_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let first_check = db
|
||||||
|
.should_process("myroom", "myeventid")
|
||||||
|
.await
|
||||||
|
.expect("should_process failed in first insert");
|
||||||
|
|
||||||
|
assert_eq!(first_check, true);
|
||||||
|
|
||||||
|
let second_check = db
|
||||||
|
.should_process("myroom", "myeventid")
|
||||||
|
.await
|
||||||
|
.expect("should_process failed in first insert");
|
||||||
|
|
||||||
|
assert_eq!(second_check, false);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,100 @@
|
||||||
|
use super::Database;
|
||||||
|
use crate::db::{errors::DataError, DbState};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl DbState for Database {
|
||||||
|
async fn get_device_id(&self) -> Result<Option<String>, DataError> {
|
||||||
|
let state = sqlx::query!(r#"SELECT device_id FROM bot_state limit 1"#)
|
||||||
|
.fetch_optional(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(state.map(|s| s.device_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_device_id(&self, device_id: &str) -> Result<(), DataError> {
|
||||||
|
// This will have to be updated if we ever add another column
|
||||||
|
// to this table!
|
||||||
|
sqlx::query("DELETE FROM bot_state")
|
||||||
|
.execute(&self.conn)
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"INSERT INTO bot_state
|
||||||
|
(device_id)
|
||||||
|
VALUES (?)"#,
|
||||||
|
)
|
||||||
|
.bind(device_id)
|
||||||
|
.execute(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::DbState;
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
||||||
|
where
|
||||||
|
Fut: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
f(db).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn set_and_get_device_id() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
db.set_device_id("device_id")
|
||||||
|
.await
|
||||||
|
.expect("Could not set device ID");
|
||||||
|
|
||||||
|
let device_id = db.get_device_id().await.expect("Could not get device ID");
|
||||||
|
|
||||||
|
assert!(device_id.is_some());
|
||||||
|
assert_eq!(device_id.unwrap(), "device_id");
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn no_device_id_set_returns_none() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let device_id = db.get_device_id().await.expect("Could not get device ID");
|
||||||
|
assert!(device_id.is_none());
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_update_device_id() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
db.set_device_id("device_id")
|
||||||
|
.await
|
||||||
|
.expect("Could not set device ID");
|
||||||
|
|
||||||
|
db.set_device_id("device_id2")
|
||||||
|
.await
|
||||||
|
.expect("Could not set device ID");
|
||||||
|
|
||||||
|
let device_id = db.get_device_id().await.expect("Could not get device ID");
|
||||||
|
|
||||||
|
assert!(device_id.is_some());
|
||||||
|
assert_eq!(device_id.unwrap(), "device_id2");
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,361 @@
|
||||||
|
use super::Database;
|
||||||
|
use crate::db::{errors::DataError, Users};
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::models::User;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Users for Database {
|
||||||
|
async fn upsert_user(&self, user: &User) -> Result<(), DataError> {
|
||||||
|
let mut tx = self.conn.begin().await?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO accounts (user_id, password, account_status)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(user_id) DO
|
||||||
|
UPDATE SET password = ?, account_status = ?"#,
|
||||||
|
user.username,
|
||||||
|
user.password,
|
||||||
|
user.account_status,
|
||||||
|
user.password,
|
||||||
|
user.account_status
|
||||||
|
)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO user_state (user_id, active_room)
|
||||||
|
VALUES (?, ?)
|
||||||
|
ON CONFLICT(user_id) DO
|
||||||
|
UPDATE SET active_room = ?"#,
|
||||||
|
user.username,
|
||||||
|
user.active_room,
|
||||||
|
user.active_room
|
||||||
|
)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_user(&self, username: &str) -> Result<(), DataError> {
|
||||||
|
let mut tx = self.conn.begin().await?;
|
||||||
|
|
||||||
|
sqlx::query!(r#"DELETE FROM accounts WHERE user_id = ?"#, username)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query!(r#"DELETE FROM user_state WHERE user_id = ?"#, username)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_user(&self, username: &str) -> Result<Option<User>, DataError> {
|
||||||
|
// Should be query_as! macro, but the left join breaks it with a
|
||||||
|
// non existing error message.
|
||||||
|
let user_row: Option<User> = sqlx::query_as(
|
||||||
|
r#"SELECT
|
||||||
|
a.user_id as "username",
|
||||||
|
a.password,
|
||||||
|
s.active_room,
|
||||||
|
COALESCE(a.account_status, 'not_registered') as "account_status"
|
||||||
|
FROM accounts a
|
||||||
|
LEFT JOIN user_state s on a.user_id = s.user_id
|
||||||
|
WHERE a.user_id = ?"#,
|
||||||
|
)
|
||||||
|
.bind(username)
|
||||||
|
.fetch_optional(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(user_row)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn authenticate_user(
|
||||||
|
&self,
|
||||||
|
username: &str,
|
||||||
|
raw_password: &str,
|
||||||
|
) -> Result<Option<User>, BotError> {
|
||||||
|
let user = self.get_user(username).await?;
|
||||||
|
Ok(user.filter(|u| u.verify_password(raw_password)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::Users;
|
||||||
|
use crate::models::AccountStatus;
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
||||||
|
where
|
||||||
|
Fut: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
f(db).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn create_and_get_full_user_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("abc".to_string()),
|
||||||
|
account_status: AccountStatus::Registered,
|
||||||
|
active_room: Some("myroom".to_string()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
assert_eq!(user.password, Some("abc".to_string()));
|
||||||
|
assert_eq!(user.account_status, AccountStatus::Registered);
|
||||||
|
assert_eq!(user.active_room, Some("myroom".to_string()));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_get_user_with_no_state_record() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("abc".to_string()),
|
||||||
|
account_status: AccountStatus::AwaitingActivation,
|
||||||
|
active_room: Some("myroom".to_string()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM user_state")
|
||||||
|
.execute(&db.conn)
|
||||||
|
.await
|
||||||
|
.expect("Could not delete from user_state table.");
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
assert_eq!(user.password, Some("abc".to_string()));
|
||||||
|
assert_eq!(user.account_status, AccountStatus::AwaitingActivation);
|
||||||
|
|
||||||
|
//These should be default values because the state record is missing.
|
||||||
|
assert_eq!(user.active_room, None);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_insert_without_password() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: None,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
assert_eq!(user.password, None);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_insert_without_active_room() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
active_room: None,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
assert_eq!(user.active_room, None);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_update_user() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result1 = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("abc".to_string()),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result1.is_ok());
|
||||||
|
|
||||||
|
let insert_result2 = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("123".to_string()),
|
||||||
|
active_room: Some("room".to_string()),
|
||||||
|
account_status: AccountStatus::AwaitingActivation,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result2.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
|
||||||
|
//From second upsert
|
||||||
|
assert_eq!(user.password, Some("123".to_string()));
|
||||||
|
assert_eq!(user.active_room, Some("room".to_string()));
|
||||||
|
assert_eq!(user.account_status, AccountStatus::AwaitingActivation);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn can_delete_user() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("abc".to_string()),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
db.delete_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User deletion query failed");
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.get_user("myuser")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_none());
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn username_not_in_db_returns_none() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let user = db
|
||||||
|
.get_user("does not exist")
|
||||||
|
.await
|
||||||
|
.expect("Get user query failure");
|
||||||
|
|
||||||
|
assert!(user.is_none());
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn authenticate_user_is_some_with_valid_password() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some(
|
||||||
|
crate::logic::hash_password("abc").expect("password hash error!"),
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.authenticate_user("myuser", "abc")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_some());
|
||||||
|
let user = user.unwrap();
|
||||||
|
assert_eq!(user.username, "myuser");
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn authenticate_user_is_none_with_wrong_password() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let insert_result = db
|
||||||
|
.upsert_user(&User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some(
|
||||||
|
crate::logic::hash_password("abc").expect("password hash error!"),
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let user = db
|
||||||
|
.authenticate_user("myuser", "wrong-password")
|
||||||
|
.await
|
||||||
|
.expect("User retrieval query failed");
|
||||||
|
|
||||||
|
assert!(user.is_none());
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,257 @@
|
||||||
|
use super::Database;
|
||||||
|
use crate::db::{errors::DataError, Variables};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Variables for Database {
|
||||||
|
async fn get_user_variables(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
) -> Result<HashMap<String, i32>, DataError> {
|
||||||
|
let rows = sqlx::query!(
|
||||||
|
r#"SELECT key, value as "value: i32" FROM user_variables
|
||||||
|
WHERE room_id = ? AND user_id = ?"#,
|
||||||
|
room_id,
|
||||||
|
user
|
||||||
|
)
|
||||||
|
.fetch_all(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(rows.into_iter().map(|row| (row.key, row.value)).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_variable_count(&self, user: &str, room_id: &str) -> Result<i32, DataError> {
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"SELECT count(*) as "count: i32" FROM user_variables
|
||||||
|
WHERE room_id = ? and user_id = ?"#,
|
||||||
|
room_id,
|
||||||
|
user
|
||||||
|
)
|
||||||
|
.fetch_optional(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.map(|r| r.count).unwrap_or(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
) -> Result<i32, DataError> {
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"SELECT value as "value: i32" FROM user_variables
|
||||||
|
WHERE user_id = ? AND room_id = ? AND key = ?"#,
|
||||||
|
user,
|
||||||
|
room_id,
|
||||||
|
variable_name
|
||||||
|
)
|
||||||
|
.fetch_optional(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
row.map(|r| r.value)
|
||||||
|
.ok_or_else(|| DataError::KeyDoesNotExist(variable_name.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
value: i32,
|
||||||
|
) -> Result<(), DataError> {
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO user_variables
|
||||||
|
(user_id, room_id, key, value)
|
||||||
|
values (?, ?, ?, ?)",
|
||||||
|
)
|
||||||
|
.bind(user)
|
||||||
|
.bind(room_id)
|
||||||
|
.bind(variable_name)
|
||||||
|
.bind(value)
|
||||||
|
.execute(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_user_variable(
|
||||||
|
&self,
|
||||||
|
user: &str,
|
||||||
|
room_id: &str,
|
||||||
|
variable_name: &str,
|
||||||
|
) -> Result<(), DataError> {
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM user_variables
|
||||||
|
WHERE user_id = ? AND room_id = ? AND key = ?",
|
||||||
|
)
|
||||||
|
.bind(user)
|
||||||
|
.bind(room_id)
|
||||||
|
.bind(variable_name)
|
||||||
|
.execute(&self.conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::db::sqlite::Database;
|
||||||
|
use crate::db::Variables;
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
||||||
|
where
|
||||||
|
Fut: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
f(db).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn set_and_get_variable_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
db.set_user_variable("myuser", "myroom", "myvariable", 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
|
||||||
|
let value = db
|
||||||
|
.get_user_variable("myuser", "myroom", "myvariable")
|
||||||
|
.await
|
||||||
|
.expect("Could not get variable");
|
||||||
|
|
||||||
|
assert_eq!(value, 1);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn get_missing_variable_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let value = db.get_user_variable("myuser", "myroom", "myvariable").await;
|
||||||
|
|
||||||
|
assert!(value.is_err());
|
||||||
|
assert!(matches!(
|
||||||
|
value.err().unwrap(),
|
||||||
|
DataError::KeyDoesNotExist(_)
|
||||||
|
));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn get_other_user_variable_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
db.set_user_variable("myuser1", "myroom", "myvariable", 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
|
||||||
|
let value = db
|
||||||
|
.get_user_variable("myuser2", "myroom", "myvariable")
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(value.is_err());
|
||||||
|
assert!(matches!(
|
||||||
|
value.err().unwrap(),
|
||||||
|
DataError::KeyDoesNotExist(_)
|
||||||
|
));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn count_variables_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
for variable_name in &["var1", "var2", "var3"] {
|
||||||
|
db.set_user_variable("myuser", "myroom", variable_name, 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
let count = db
|
||||||
|
.get_variable_count("myuser", "myroom")
|
||||||
|
.await
|
||||||
|
.expect("Could not get count.");
|
||||||
|
|
||||||
|
assert_eq!(count, 3);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn count_variables_respects_user_id() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
for variable_name in &["var1", "var2", "var3"] {
|
||||||
|
db.set_user_variable("different-user", "myroom", variable_name, 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
let count = db
|
||||||
|
.get_variable_count("myuser", "myroom")
|
||||||
|
.await
|
||||||
|
.expect("Could not get count.");
|
||||||
|
|
||||||
|
assert_eq!(count, 0);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn count_variables_respects_room_id() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
for variable_name in &["var1", "var2", "var3"] {
|
||||||
|
db.set_user_variable("myuser", "different-room", variable_name, 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
let count = db
|
||||||
|
.get_variable_count("myuser", "myroom")
|
||||||
|
.await
|
||||||
|
.expect("Could not get count.");
|
||||||
|
|
||||||
|
assert_eq!(count, 0);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn delete_variable_test() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
for variable_name in &["var1", "var2", "var3"] {
|
||||||
|
db.set_user_variable("myuser", "myroom", variable_name, 1)
|
||||||
|
.await
|
||||||
|
.expect("Could not set variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
db.delete_user_variable("myuser", "myroom", "var1")
|
||||||
|
.await
|
||||||
|
.expect("Could not delete variable.");
|
||||||
|
|
||||||
|
let count = db
|
||||||
|
.get_variable_count("myuser", "myroom")
|
||||||
|
.await
|
||||||
|
.expect("Could not get count");
|
||||||
|
|
||||||
|
assert_eq!(count, 2);
|
||||||
|
|
||||||
|
let var1 = db.get_user_variable("myuser", "myroom", "var1").await;
|
||||||
|
assert!(var1.is_err());
|
||||||
|
assert!(matches!(var1.err().unwrap(), DataError::KeyDoesNotExist(_)));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,126 @@
|
||||||
|
use std::net::AddrParseError;
|
||||||
|
|
||||||
|
use crate::commands::CommandError;
|
||||||
|
use crate::config::ConfigError;
|
||||||
|
use crate::db::errors::DataError;
|
||||||
|
use thiserror::Error;
|
||||||
|
use tonic::metadata::errors::InvalidMetadataValue;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum BotError {
|
||||||
|
#[error("configuration error: {0}")]
|
||||||
|
ConfigurationError(#[from] ConfigError),
|
||||||
|
|
||||||
|
/// Sync token couldn't be found.
|
||||||
|
#[error("the sync token could not be retrieved")]
|
||||||
|
SyncTokenRequired,
|
||||||
|
|
||||||
|
#[error("could not retrieve device id")]
|
||||||
|
NoDeviceIdFound,
|
||||||
|
|
||||||
|
#[error("could not build client: {0}")]
|
||||||
|
ClientBuildError(#[from] matrix_sdk::ClientBuildError),
|
||||||
|
|
||||||
|
#[error("could not open matrix store: {0}")]
|
||||||
|
OpenStoreError(#[from] matrix_sdk::store::OpenStoreError),
|
||||||
|
|
||||||
|
#[error("command error: {0}")]
|
||||||
|
CommandError(#[from] CommandError),
|
||||||
|
|
||||||
|
#[error("database error: {0}")]
|
||||||
|
DataError(#[from] DataError),
|
||||||
|
|
||||||
|
#[error("the message should not be processed because it failed validation")]
|
||||||
|
ShouldNotProcessError,
|
||||||
|
|
||||||
|
#[error("no cache directory found")]
|
||||||
|
NoCacheDirectoryError,
|
||||||
|
|
||||||
|
#[error("could not parse URL")]
|
||||||
|
UrlParseError(#[from] url::ParseError),
|
||||||
|
|
||||||
|
#[error("could not parse ID")]
|
||||||
|
IdParseError(#[from] matrix_sdk::ruma::IdParseError),
|
||||||
|
|
||||||
|
#[error("error in matrix state store: {0}")]
|
||||||
|
MatrixStateStoreError(#[from] matrix_sdk::StoreError),
|
||||||
|
|
||||||
|
#[error("uncategorized matrix SDK error: {0}")]
|
||||||
|
MatrixError(#[from] matrix_sdk::Error),
|
||||||
|
|
||||||
|
#[error("future canceled")]
|
||||||
|
FutureCanceledError,
|
||||||
|
|
||||||
|
//de = deserialization
|
||||||
|
#[error("toml parsing error")]
|
||||||
|
TomlParsingError(#[from] toml::de::Error),
|
||||||
|
|
||||||
|
#[error("i/o error: {0}")]
|
||||||
|
IoError(#[from] std::io::Error),
|
||||||
|
|
||||||
|
#[error("dice parsing error: {0}")]
|
||||||
|
DiceParsingError(#[from] crate::parser::dice::DiceParsingError),
|
||||||
|
|
||||||
|
#[error("command parsing error: {0}")]
|
||||||
|
CommandParsingError(#[from] crate::commands::parser::CommandParsingError),
|
||||||
|
|
||||||
|
#[error("dice rolling error: {0}")]
|
||||||
|
DiceRollingError(#[from] DiceRollingError),
|
||||||
|
|
||||||
|
#[error("variable parsing error: {0}")]
|
||||||
|
VariableParsingError(#[from] crate::parser::variables::VariableParsingError),
|
||||||
|
|
||||||
|
#[error("legacy parsing error")]
|
||||||
|
NomParserError(nom::error::ErrorKind),
|
||||||
|
|
||||||
|
#[error("legacy parsing error: not enough data")]
|
||||||
|
NomParserIncomplete,
|
||||||
|
|
||||||
|
#[error("variables not yet supported")]
|
||||||
|
VariablesNotSupported,
|
||||||
|
|
||||||
|
#[error("too many commands or message was too large")]
|
||||||
|
MessageTooLarge,
|
||||||
|
|
||||||
|
#[error("could not convert to proper integer type")]
|
||||||
|
TryFromIntError(#[from] std::num::TryFromIntError),
|
||||||
|
|
||||||
|
// #[error("identifier error: {0}")]
|
||||||
|
// IdentifierError(#[from] matrix_sdk::ruma::Error),
|
||||||
|
|
||||||
|
#[error("password creation error: {0}")]
|
||||||
|
PasswordCreationError(argon2::Error),
|
||||||
|
|
||||||
|
#[error("account does not exist, or password incorrect")]
|
||||||
|
AuthenticationError,
|
||||||
|
|
||||||
|
#[error("user account does not exist, try registering")]
|
||||||
|
AccountDoesNotExist,
|
||||||
|
|
||||||
|
#[error("user account already exists")]
|
||||||
|
AccountAlreadyExists,
|
||||||
|
|
||||||
|
#[error("room name or id does not exist")]
|
||||||
|
RoomDoesNotExist,
|
||||||
|
|
||||||
|
#[error("tonic transport error: {0}")]
|
||||||
|
TonicTransportError(#[from] tonic::transport::Error),
|
||||||
|
|
||||||
|
#[error("address parsing error: {0}")]
|
||||||
|
AddressParseError(#[from] AddrParseError),
|
||||||
|
|
||||||
|
#[error("invalid metadata value: {0}")]
|
||||||
|
TonicInvalidMetadata(#[from] InvalidMetadataValue),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum DiceRollingError {
|
||||||
|
#[error("variable not found: {0}")]
|
||||||
|
VariableNotFound(String),
|
||||||
|
|
||||||
|
#[error("invalid amount")]
|
||||||
|
InvalidAmount,
|
||||||
|
|
||||||
|
#[error("dice pool expression too large")]
|
||||||
|
ExpressionTooLarge,
|
||||||
|
}
|
|
@ -0,0 +1,156 @@
|
||||||
|
use indoc::indoc;
|
||||||
|
|
||||||
|
pub fn parse_help_topic(input: &str) -> Option<HelpTopic> {
|
||||||
|
match input {
|
||||||
|
"cofd" => Some(HelpTopic::ChroniclesOfDarkness),
|
||||||
|
"dicepool" => Some(HelpTopic::DicePool),
|
||||||
|
"dice" => Some(HelpTopic::RollingDice),
|
||||||
|
"cthulhu" => Some(HelpTopic::Cthulhu),
|
||||||
|
"variables" => Some(HelpTopic::Variables),
|
||||||
|
"var" => Some(HelpTopic::Variables),
|
||||||
|
"variable" => Some(HelpTopic::Variables),
|
||||||
|
"" => Some(HelpTopic::General),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum HelpTopic {
|
||||||
|
ChroniclesOfDarkness,
|
||||||
|
DicePool,
|
||||||
|
Cthulhu,
|
||||||
|
RollingDice,
|
||||||
|
Variables,
|
||||||
|
General,
|
||||||
|
}
|
||||||
|
|
||||||
|
const COFD_HELP: &'static str = indoc! {"
|
||||||
|
Chronicles of Darkness
|
||||||
|
|
||||||
|
Commands available:
|
||||||
|
!pool, !rp: roll a dice pool
|
||||||
|
!chance: roll a chance die
|
||||||
|
|
||||||
|
See also:
|
||||||
|
!help dicepool
|
||||||
|
"};
|
||||||
|
|
||||||
|
const DICE_HELP: &'static str = indoc! {"
|
||||||
|
Rolling basic dice
|
||||||
|
|
||||||
|
Command: !roll, !r
|
||||||
|
|
||||||
|
Syntax !roll <dice-expression>
|
||||||
|
|
||||||
|
Dice expression can be a basic die (e.g. 1d4), with a bonus (1d4+3),
|
||||||
|
or a more complex series of dice rolls or arbitrary numbers.
|
||||||
|
Parentheses are not supported.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
!roll 1d4
|
||||||
|
!roll 1d4+5
|
||||||
|
!roll 2d6+8
|
||||||
|
!roll 2d8 + 4d6 - 3
|
||||||
|
"};
|
||||||
|
|
||||||
|
const DICEPOOL_HELP: &'static str = indoc! {"
|
||||||
|
Rolling dice pools
|
||||||
|
|
||||||
|
Command: !pool, !rp
|
||||||
|
|
||||||
|
Syntax: !pool <modifiers>:<expression>
|
||||||
|
|
||||||
|
Short syntax: !pool <expression>
|
||||||
|
|
||||||
|
Expression Syntax: <num|variable> [+/- <expression> ...]
|
||||||
|
|
||||||
|
Modifiers:
|
||||||
|
n = nine-again
|
||||||
|
e = eight-again
|
||||||
|
r = rote quality
|
||||||
|
x = do not re-roll 10s
|
||||||
|
s<num> = number of successes for exceptional
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
!pool 8 (roll a regular pool of 8 dice)
|
||||||
|
!pool n:5 (roll dice pool of 5, nine-again)
|
||||||
|
!pool rs3:6 (roll dice pool of 6, rote quality, 3 successes for exceptional)
|
||||||
|
!pool 10 + 3 (roll dice pool of 10 + 3, which is 13)
|
||||||
|
!pool myskill - 4 (roll pool of the value of myskill - 4).
|
||||||
|
!pool n:myskill - 5 (roll pool of myskill - 5, with nine-again)
|
||||||
|
"};
|
||||||
|
|
||||||
|
const CTHULHU_HELP: &'static str = indoc! {"
|
||||||
|
Rolling Call of Cthlhu dice
|
||||||
|
|
||||||
|
Commands: !cthroll (regular rolls), !cthadv (advancement rolls)
|
||||||
|
|
||||||
|
Regular roll syntax: !cthroll <modifiers>:<num|variable>
|
||||||
|
|
||||||
|
Advancement roll syntax: !cthadv <num|variable>
|
||||||
|
|
||||||
|
Modifiers:
|
||||||
|
b = one bonus die
|
||||||
|
bb = two bonus dice
|
||||||
|
p = one penalty die
|
||||||
|
pp = two penalty dice
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
!cthroll 60 (make a roll against a skill of 60)
|
||||||
|
!cthroll spothidden (make a roll against variable spothidden)
|
||||||
|
!cthroll bb:30 (make a roll against skill of 30 with two bonus dice)
|
||||||
|
!cthadv 50 (make an advancement roll against a skill of 50)
|
||||||
|
!cthadv spothidden (make an advancement roll against the number in spothidden)
|
||||||
|
|
||||||
|
Note: If !cthadv is given a variable, and the roll is successful, it will
|
||||||
|
update the variable with the new skill.
|
||||||
|
"};
|
||||||
|
|
||||||
|
const VARIABLES_HELP: &'static str = indoc! {"
|
||||||
|
Variables
|
||||||
|
|
||||||
|
Commands: !get, !set, !variables
|
||||||
|
|
||||||
|
Manage variables that can be substituted into roll commands.
|
||||||
|
|
||||||
|
Examples: !get myvar, !set myvar 10
|
||||||
|
|
||||||
|
!get <variable> = show variable of the given name
|
||||||
|
!set <variable> <num> = set a variable to a number
|
||||||
|
|
||||||
|
The !variables command will list all variables for the room. The
|
||||||
|
variables command cna be used in a secure room to avoid spamming the
|
||||||
|
actual room that the variable is set in.
|
||||||
|
|
||||||
|
Variable names can be used in all types of dice rolls:
|
||||||
|
|
||||||
|
!pool myvar + 3
|
||||||
|
!roll myvar
|
||||||
|
|
||||||
|
There are some limitations on variables: they cannot themselves be
|
||||||
|
dice expressions (i.e. can only be numbers), and they must be uniquely
|
||||||
|
parseable in an expression (i.e 'myvard6' does not work for the !roll
|
||||||
|
command).
|
||||||
|
|
||||||
|
"};
|
||||||
|
|
||||||
|
const GENERAL_HELP: &'static str = indoc! {"
|
||||||
|
General Help
|
||||||
|
|
||||||
|
Try these help commands:
|
||||||
|
!help cofd
|
||||||
|
!help dice
|
||||||
|
!help cthulhu
|
||||||
|
"};
|
||||||
|
|
||||||
|
impl HelpTopic {
|
||||||
|
pub fn message(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
HelpTopic::ChroniclesOfDarkness => COFD_HELP,
|
||||||
|
HelpTopic::DicePool => DICEPOOL_HELP,
|
||||||
|
HelpTopic::Cthulhu => CTHULHU_HELP,
|
||||||
|
HelpTopic::RollingDice => DICE_HELP,
|
||||||
|
HelpTopic::Variables => VARIABLES_HELP,
|
||||||
|
HelpTopic::General => GENERAL_HELP,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod basic;
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod cofd;
|
pub mod cofd;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
|
@ -5,10 +6,12 @@ pub mod config;
|
||||||
pub mod context;
|
pub mod context;
|
||||||
pub mod cthulhu;
|
pub mod cthulhu;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod dice;
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod help;
|
mod help;
|
||||||
|
pub mod logic;
|
||||||
|
pub mod matrix;
|
||||||
|
pub mod models;
|
||||||
mod parser;
|
mod parser;
|
||||||
pub mod roll;
|
pub mod rpc;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod variables;
|
pub mod systems;
|
|
@ -0,0 +1,131 @@
|
||||||
|
use crate::error::{BotError, DiceRollingError};
|
||||||
|
use crate::parser::dice::{Amount, Element};
|
||||||
|
use crate::{context::Context, models::Account};
|
||||||
|
use crate::{
|
||||||
|
db::{sqlite::Database, Users, Variables},
|
||||||
|
models::TransientUser,
|
||||||
|
};
|
||||||
|
use argon2::{self, Config, Error as ArgonError};
|
||||||
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
|
use rand::Rng;
|
||||||
|
use std::slice;
|
||||||
|
|
||||||
|
/// Calculate the amount of dice to roll by consulting the database
|
||||||
|
/// and replacing variables with corresponding the amount. Errors out
|
||||||
|
/// if it cannot find a variable defined, or if the database errors.
|
||||||
|
pub async fn calculate_single_die_amount(
|
||||||
|
amount: &Amount,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
) -> Result<i32, BotError> {
|
||||||
|
calculate_dice_amount(slice::from_ref(amount), ctx).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculate the amount of dice to roll by consulting the database
|
||||||
|
/// and replacing variables with corresponding amounts. Errors out if
|
||||||
|
/// it cannot find a variable defined, or if the database errors.
|
||||||
|
pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result<i32, BotError> {
|
||||||
|
let stream = stream::iter(amounts);
|
||||||
|
let variables = &ctx
|
||||||
|
.db
|
||||||
|
.get_user_variables(&ctx.username, ctx.active_room_id().as_str())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
use DiceRollingError::VariableNotFound;
|
||||||
|
let dice_amount: i32 = stream
|
||||||
|
.then(|amount| async move {
|
||||||
|
match &amount.element {
|
||||||
|
Element::Number(num_dice) => Ok(num_dice * amount.operator.mult()),
|
||||||
|
Element::Variable(variable) => variables
|
||||||
|
.get(variable)
|
||||||
|
.ok_or_else(|| VariableNotFound(variable.clone()))
|
||||||
|
.map(|i| *i),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.try_fold(0, |total, num_dice| async move { Ok(total + num_dice) })
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(dice_amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hash a password using the argon2 algorithm with a 16 byte salt.
|
||||||
|
pub(crate) fn hash_password(raw_password: &str) -> Result<String, ArgonError> {
|
||||||
|
let salt = rand::thread_rng().gen::<[u8; 16]>();
|
||||||
|
let config = Config::default();
|
||||||
|
argon2::hash_encoded(raw_password.as_bytes(), &salt, &config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_account(db: &Database, username: &str) -> Result<Account, BotError> {
|
||||||
|
Ok(db
|
||||||
|
.get_user(username)
|
||||||
|
.await?
|
||||||
|
.map(|user| Account::Registered(user))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
Account::Transient(TransientUser {
|
||||||
|
username: username.to_owned(),
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::db::Users;
|
||||||
|
use crate::models::{AccountStatus, User};
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
||||||
|
where
|
||||||
|
Fut: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
||||||
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::new(db_path.path().to_str().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
f(db).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn get_account_no_user_exists() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let account = get_account(&db, "@test:example.com")
|
||||||
|
.await
|
||||||
|
.expect("Account retrieval didn't work");
|
||||||
|
|
||||||
|
assert!(matches!(account, Account::Transient(_)));
|
||||||
|
|
||||||
|
let user = account.transient_user().unwrap();
|
||||||
|
assert_eq!(user.username, "@test:example.com");
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||||
|
async fn get_or_create_user_when_user_exists() {
|
||||||
|
with_db(|db| async move {
|
||||||
|
let user = User {
|
||||||
|
username: "myuser".to_string(),
|
||||||
|
password: Some("abc".to_string()),
|
||||||
|
account_status: AccountStatus::Registered,
|
||||||
|
active_room: Some("myroom".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let insert_result = db.upsert_user(&user).await;
|
||||||
|
assert!(insert_result.is_ok());
|
||||||
|
|
||||||
|
let account = get_account(&db, "myuser")
|
||||||
|
.await
|
||||||
|
.expect("Account retrieval did not work");
|
||||||
|
|
||||||
|
assert!(matches!(account, Account::Registered(_)));
|
||||||
|
|
||||||
|
let user_again = account.registered_user().unwrap();
|
||||||
|
assert_eq!(user, *user_again);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,113 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
|
use log::error;
|
||||||
|
use matrix_sdk::ruma::events::room::message::{InReplyTo, RoomMessageEventContent, Relation};
|
||||||
|
use matrix_sdk::ruma::events::AnyMessageLikeEventContent;
|
||||||
|
use matrix_sdk::ruma::{RoomId, OwnedEventId, OwnedUserId};
|
||||||
|
use matrix_sdk::Client;
|
||||||
|
use matrix_sdk::Error as MatrixError;
|
||||||
|
use matrix_sdk::room::Joined;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
use crate::{config::Config, error::BotError};
|
||||||
|
|
||||||
|
fn cache_dir() -> Result<PathBuf, BotError> {
|
||||||
|
let mut dir = dirs::cache_dir().ok_or(BotError::NoCacheDirectoryError)?;
|
||||||
|
dir.push("matrix-dicebot");
|
||||||
|
Ok(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extracts more detailed error messages out of a matrix SDK error.
|
||||||
|
fn extract_error_message(error: MatrixError) -> String {
|
||||||
|
use matrix_sdk::{Error::Http, HttpError};
|
||||||
|
if let Http(HttpError::Api(ruma_err)) = error {
|
||||||
|
ruma_err.to_string()
|
||||||
|
} else {
|
||||||
|
error.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates the matrix client.
|
||||||
|
pub async fn create_client(config: &Config) -> Result<Client, BotError> {
|
||||||
|
let cache_dir = cache_dir()?;
|
||||||
|
let homeserver_url = Url::parse(&config.matrix_homeserver())?;
|
||||||
|
|
||||||
|
let client = Client::builder()
|
||||||
|
.sled_store(cache_dir, None)?
|
||||||
|
.homeserver_url(homeserver_url).build()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(client)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieve a list of users in a given room.
|
||||||
|
pub async fn get_users_in_room(
|
||||||
|
client: &Client,
|
||||||
|
room_id: &RoomId,
|
||||||
|
) -> Result<Vec<String>, MatrixError> {
|
||||||
|
if let Some(joined_room) = client.get_joined_room(room_id) {
|
||||||
|
let members = joined_room.joined_members().await?;
|
||||||
|
|
||||||
|
Ok(members
|
||||||
|
.into_iter()
|
||||||
|
.map(|member| member.user_id().to_string())
|
||||||
|
.collect())
|
||||||
|
} else {
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_rooms_for_user(
|
||||||
|
client: &Client,
|
||||||
|
user: &OwnedUserId,
|
||||||
|
) -> Result<Vec<Joined>, MatrixError> {
|
||||||
|
// Carries errors through, in case we cannot load joined user IDs
|
||||||
|
// from the room for some reason.
|
||||||
|
let user_is_in_room = |room: Joined| async move {
|
||||||
|
match room.joined_user_ids().await {
|
||||||
|
Ok(users) => match users.contains(user) {
|
||||||
|
true => Some(Ok(room)),
|
||||||
|
false => None,
|
||||||
|
},
|
||||||
|
Err(e) => Some(Err(e)),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let rooms_for_user: Vec<Joined> = stream::iter(client.joined_rooms())
|
||||||
|
.filter_map(user_is_in_room)
|
||||||
|
.try_collect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(rooms_for_user)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a message. The message is a tuple of HTML and plain text
|
||||||
|
/// responses.
|
||||||
|
pub async fn send_message(
|
||||||
|
client: &Client,
|
||||||
|
room_id: &RoomId,
|
||||||
|
message: (&str, &str),
|
||||||
|
reply_to: Option<OwnedEventId>,
|
||||||
|
) {
|
||||||
|
let (html, plain) = message;
|
||||||
|
let room = match client.get_joined_room(room_id) {
|
||||||
|
Some(room) => room,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut content = RoomMessageEventContent::notice_html(plain.trim(), html);
|
||||||
|
|
||||||
|
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
||||||
|
in_reply_to: InReplyTo::new(event_id)
|
||||||
|
});
|
||||||
|
|
||||||
|
let content = AnyMessageLikeEventContent::RoomMessage(content);
|
||||||
|
|
||||||
|
let result = room.send(content, None).await;
|
||||||
|
|
||||||
|
if let Err(e) = result {
|
||||||
|
let html = extract_error_message(e);
|
||||||
|
error!("Error sending html: {}", html);
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,157 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// RoomInfo has basic metadata about a room: its name, ID, etc.
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
|
pub struct RoomInfo {
|
||||||
|
pub room_id: String,
|
||||||
|
pub room_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Clone, Copy, Debug, sqlx::Type)]
|
||||||
|
#[sqlx(rename_all = "snake_case")]
|
||||||
|
pub enum AccountStatus {
|
||||||
|
/// Account is not registered, which means a transient "account"
|
||||||
|
/// with limited information exists only for the duration of the
|
||||||
|
/// command request.
|
||||||
|
NotRegistered,
|
||||||
|
|
||||||
|
/// User account is fully registered, either via Matrix directly,
|
||||||
|
/// or a web UI sign-up.
|
||||||
|
Registered,
|
||||||
|
|
||||||
|
/// Account is awaiting activation with a registration
|
||||||
|
/// code. Account cannot do privileged actions yet.
|
||||||
|
AwaitingActivation,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AccountStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
AccountStatus::NotRegistered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub enum Account {
|
||||||
|
/// A registered user account, stored in the database.
|
||||||
|
Registered(User),
|
||||||
|
|
||||||
|
/// A transient account. Not stored in the database. Represents a
|
||||||
|
/// user in a public channel that has not registered directly with
|
||||||
|
/// the bot yet.
|
||||||
|
Transient(TransientUser),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Account {
|
||||||
|
/// Whether or not this account is a registered user account.
|
||||||
|
pub fn is_registered(&self) -> bool {
|
||||||
|
matches!(self, Self::Registered(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the account status. For registered users, this is their
|
||||||
|
/// actual account status (fully registered or awaiting
|
||||||
|
/// activation). For transient users, this is
|
||||||
|
/// AccountStatus::NotRegistered.
|
||||||
|
pub fn account_status(&self) -> AccountStatus {
|
||||||
|
match self {
|
||||||
|
Self::Registered(user) => user.account_status,
|
||||||
|
Self::Transient(_) => AccountStatus::NotRegistered,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consume self into an Option<User> instance, which will be Some
|
||||||
|
/// if this account has a registered user, and None otherwise.
|
||||||
|
pub fn registered_user(&self) -> Option<&User> {
|
||||||
|
match self {
|
||||||
|
Self::Registered(ref user) => Some(user),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consume self into an Option<TransientUser> instance, which
|
||||||
|
/// will be Some if this account has a non-registered user, and
|
||||||
|
/// None otherwise.
|
||||||
|
pub fn transient_user(self) -> Option<TransientUser> {
|
||||||
|
match self {
|
||||||
|
Self::Transient(user) => Some(user),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Account {
|
||||||
|
fn default() -> Self {
|
||||||
|
Account::Transient(TransientUser {
|
||||||
|
username: "".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub struct TransientUser {
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Clone, Debug, Default, sqlx::FromRow)]
|
||||||
|
pub struct User {
|
||||||
|
pub username: String,
|
||||||
|
pub password: Option<String>,
|
||||||
|
pub active_room: Option<String>,
|
||||||
|
pub account_status: AccountStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl User {
|
||||||
|
/// Create a new unregistered skeleton marker account for a
|
||||||
|
/// username.
|
||||||
|
pub fn unregistered(username: &str) -> User {
|
||||||
|
User {
|
||||||
|
username: username.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn verify_password(&self, raw_password: &str) -> bool {
|
||||||
|
self.password
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|p| argon2::verify_encoded(p, raw_password.as_bytes()).ok())
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn verify_password_passes_with_correct_password() {
|
||||||
|
let user = User {
|
||||||
|
password: Some(
|
||||||
|
crate::logic::hash_password("mypassword").expect("Password hashing error!"),
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(user.verify_password("mypassword"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn verify_password_fails_with_wrong_password() {
|
||||||
|
let user = User {
|
||||||
|
password: Some(
|
||||||
|
crate::logic::hash_password("mypassword").expect("Password hashing error!"),
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(user.verify_password("wrong-password"), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn verify_password_fails_with_no_password() {
|
||||||
|
let user = User {
|
||||||
|
password: None,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(user.verify_password("wrong-password"), false);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,318 @@
|
||||||
|
use combine::error::ParseError;
|
||||||
|
use combine::parser::char::{digit, letter, spaces};
|
||||||
|
use combine::stream::Stream;
|
||||||
|
use combine::{many, many1, one_of, Parser};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
/// Errors for dice parsing.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Copy, Error)]
|
||||||
|
pub enum DiceParsingError {
|
||||||
|
#[error("invalid amount")]
|
||||||
|
InvalidAmount,
|
||||||
|
|
||||||
|
#[error("modifiers not specified properly")]
|
||||||
|
InvalidModifiers,
|
||||||
|
|
||||||
|
#[error("extraneous input detected")]
|
||||||
|
UnconsumedInput,
|
||||||
|
|
||||||
|
#[error("{0}")]
|
||||||
|
InternalParseError(#[from] combine::error::StringStreamError),
|
||||||
|
|
||||||
|
#[error("number parsing error (too large?)")]
|
||||||
|
ConversionError,
|
||||||
|
|
||||||
|
#[error("unexpected element in expression")]
|
||||||
|
WrongElementType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::num::ParseIntError> for DiceParsingError {
|
||||||
|
fn from(_: std::num::ParseIntError) -> Self {
|
||||||
|
DiceParsingError::ConversionError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParseResult<T> = Result<T, DiceParsingError>;
|
||||||
|
|
||||||
|
/// A parsed operator for a number. Whether to add or remove it from
|
||||||
|
/// the total amount of dice rolled.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
pub enum Operator {
|
||||||
|
Plus,
|
||||||
|
Minus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Operator {
|
||||||
|
/// Calculate multiplier for how to convert the number. Returns 1
|
||||||
|
/// for positive, and -1 for negative.
|
||||||
|
pub fn mult(&self) -> i32 {
|
||||||
|
match self {
|
||||||
|
Operator::Plus => 1,
|
||||||
|
Operator::Minus => -1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One part of the dice amount in an expression. Can be a number or a
|
||||||
|
/// variable name.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum Element {
|
||||||
|
/// This element in the expression is a variable, which will be
|
||||||
|
/// resolved to a number by consulting the dtaabase.
|
||||||
|
Variable(String),
|
||||||
|
|
||||||
|
/// This element is a simple number, and will be added or
|
||||||
|
/// subtracted from the total dice amount depending on its
|
||||||
|
/// corresponding Operator.
|
||||||
|
Number(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One part of the parsed dice rolling expression. Combines an
|
||||||
|
/// operator and an element into one struct. Examples of Amounts would
|
||||||
|
/// be "+4" or "- myvariable", which translate to Operator::Plus and
|
||||||
|
/// Element::Number(4), and Operator::Minus and
|
||||||
|
/// Element::Variable("myvariable"), respectively.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub struct Amount {
|
||||||
|
pub operator: Operator,
|
||||||
|
pub element: Element,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parser that attempt to convert the text at the start of the dice
|
||||||
|
/// parsing into an Amount instance.
|
||||||
|
fn first_amount_parser<Input>() -> impl Parser<Input, Output = ParseResult<Amount>>
|
||||||
|
where
|
||||||
|
Input: Stream<Token = char>,
|
||||||
|
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
|
||||||
|
{
|
||||||
|
let map_first_amount = |value: String| {
|
||||||
|
if value.chars().all(char::is_numeric) {
|
||||||
|
let num = value.parse::<i32>()?;
|
||||||
|
Ok(Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(num),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable(value),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
many1(letter())
|
||||||
|
.or(many1(digit()))
|
||||||
|
.skip(spaces().silent()) //Consume any space after first amount
|
||||||
|
.map(map_first_amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempt to convert some text in the middle or end of the dice roll
|
||||||
|
/// string into an Amount.
|
||||||
|
fn amount_parser<Input>() -> impl Parser<Input, Output = ParseResult<Amount>>
|
||||||
|
where
|
||||||
|
Input: Stream<Token = char>,
|
||||||
|
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
|
||||||
|
{
|
||||||
|
let plus_or_minus = one_of("+-".chars());
|
||||||
|
let parse_operator = plus_or_minus.map(|sign: char| match sign {
|
||||||
|
'+' => Operator::Plus,
|
||||||
|
'-' => Operator::Minus,
|
||||||
|
_ => Operator::Plus,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Element must either be a proper i32, or a variable name.
|
||||||
|
let map_element = |value: String| -> ParseResult<Element> {
|
||||||
|
if value.chars().all(char::is_numeric) {
|
||||||
|
let num = value.parse::<i32>()?;
|
||||||
|
Ok(Element::Number(num))
|
||||||
|
} else {
|
||||||
|
Ok(Element::Variable(value))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let parse_element = many1(letter()).or(many1(digit())).map(map_element);
|
||||||
|
|
||||||
|
let element_parser = parse_operator
|
||||||
|
.skip(spaces().silent())
|
||||||
|
.and(parse_element)
|
||||||
|
.skip(spaces().silent());
|
||||||
|
|
||||||
|
let convert_to_amount = |(operator, element_result)| match element_result {
|
||||||
|
Ok(element) => Ok(Amount { operator, element }),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
};
|
||||||
|
|
||||||
|
element_parser.map(convert_to_amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse an expression of numbers and/or variables into elements
|
||||||
|
/// coupled with operators, where an operator is "+" or "-", and an
|
||||||
|
/// element is either a number or variable name. The first element
|
||||||
|
/// should not have an operator, but every one after that should.
|
||||||
|
/// Accepts expressions like "8", "10 + variablename", "variablename -
|
||||||
|
/// 3", etc. This function is currently common to systems that don't
|
||||||
|
/// deal with XdY rolls. Support for that will be added later. Returns
|
||||||
|
/// parsed amounts and unconsumed input (e.g. roll modifiers).
|
||||||
|
pub fn parse_amounts(input: &str) -> ParseResult<(Vec<Amount>, &str)> {
|
||||||
|
let input = input.trim();
|
||||||
|
|
||||||
|
let remaining_amounts = many(amount_parser()).map(|amounts: Vec<ParseResult<Amount>>| amounts);
|
||||||
|
let mut parser = first_amount_parser().and(remaining_amounts);
|
||||||
|
|
||||||
|
// Collapses first amount + remaining amounts into a single Vec,
|
||||||
|
// while collecting extraneous input.
|
||||||
|
type ParsedAmountExpr = (ParseResult<Amount>, Vec<ParseResult<Amount>>);
|
||||||
|
let (results, rest) = parser
|
||||||
|
.parse(input)
|
||||||
|
.map(|mut results: (ParsedAmountExpr, &str)| {
|
||||||
|
let mut amounts = vec![(results.0).0];
|
||||||
|
amounts.append(&mut (results.0).1);
|
||||||
|
(amounts, results.1)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// Any ParseResult errors will short-circuit the collect.
|
||||||
|
let results: Vec<Amount> = results.into_iter().collect::<ParseResult<_>>()?;
|
||||||
|
Ok((results, rest))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse an expression that expects a single number or variable. No
|
||||||
|
/// operators are allowed. This function is common to systems that
|
||||||
|
/// don't deal with XdY rolls. Currently. this function does not
|
||||||
|
/// support parsing negative numbers. Returns the parsed amount and
|
||||||
|
/// any unconsumed input (useful for dice roll modifiers).
|
||||||
|
pub fn parse_single_amount(input: &str) -> ParseResult<(Amount, &str)> {
|
||||||
|
// TODO add support for negative numbers, as technically they
|
||||||
|
// should be allowed.
|
||||||
|
let input = input.trim();
|
||||||
|
let mut parser = first_amount_parser().map(|amount: ParseResult<Amount>| amount);
|
||||||
|
let (result, rest) = parser.parse(input)?;
|
||||||
|
Ok((result?, rest))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod parse_single_amount_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_single_variable_test() {
|
||||||
|
let result = parse_single_amount("abc");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable("abc".to_string())
|
||||||
|
},
|
||||||
|
""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO add support for negative numbers in parse_single_amount
|
||||||
|
// #[test]
|
||||||
|
// fn parse_single_negative_number_test() {
|
||||||
|
// let result = parse_single_amount("-1");
|
||||||
|
// assert!(result.is_ok());
|
||||||
|
// assert_eq!(
|
||||||
|
// result.unwrap(),
|
||||||
|
// Amount {
|
||||||
|
// operator: Operator::Minus,
|
||||||
|
// element: Element::Number(1)
|
||||||
|
// }
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_single_number_test() {
|
||||||
|
let result = parse_single_amount("1");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(1)
|
||||||
|
},
|
||||||
|
""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod parse_many_amounts_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_single_number_amount_test() {
|
||||||
|
let result = parse_amounts("1");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
vec![Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(1)
|
||||||
|
}],
|
||||||
|
""
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = parse_amounts("10");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
vec![Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Number(10)
|
||||||
|
}],
|
||||||
|
""
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parsing_huge_number_should_error() {
|
||||||
|
// A number outside the bounds of i32 should not be a valid
|
||||||
|
// parse.
|
||||||
|
let result = parse_amounts("159875294375198734982379875392");
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(result.unwrap_err() == DiceParsingError::ConversionError);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_single_variable_amount_test() {
|
||||||
|
let result = parse_amounts("asdf");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
vec![Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable("asdf".to_string())
|
||||||
|
}],
|
||||||
|
""
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = parse_amounts("nosis");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
(
|
||||||
|
vec![Amount {
|
||||||
|
operator: Operator::Plus,
|
||||||
|
element: Element::Variable("nosis".to_string())
|
||||||
|
}],
|
||||||
|
""
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_complex_amount_expression() {
|
||||||
|
assert!(parse_amounts("1 + myvariable - 2").is_ok());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod dice;
|
||||||
|
pub mod variables;
|
|
@ -0,0 +1,50 @@
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::{config::Config, db::sqlite::Database};
|
||||||
|
use log::{info, warn};
|
||||||
|
use matrix_sdk::Client;
|
||||||
|
use service::DicebotRpcService;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tenebrous_rpc::protos::dicebot::dicebot_server::DicebotServer;
|
||||||
|
use tonic::{metadata::MetadataValue, transport::Server, Request, Status};
|
||||||
|
|
||||||
|
pub(crate) mod service;
|
||||||
|
|
||||||
|
pub async fn serve_grpc(
|
||||||
|
config: &Arc<Config>,
|
||||||
|
db: &Database,
|
||||||
|
client: &Client,
|
||||||
|
) -> Result<(), BotError> {
|
||||||
|
match config.rpc_addr().zip(config.rpc_key()) {
|
||||||
|
Some((addr, rpc_key)) => {
|
||||||
|
let expected_bearer = MetadataValue::from_str(&format!("Bearer {}", rpc_key))?;
|
||||||
|
let addr = addr.parse()?;
|
||||||
|
|
||||||
|
let rpc_service = DicebotRpcService {
|
||||||
|
db: db.clone(),
|
||||||
|
config: config.clone(),
|
||||||
|
client: client.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
info!("Serving Dicebot gRPC service on {}", addr);
|
||||||
|
|
||||||
|
let interceptor = move |req: Request<()>| match req.metadata().get("authorization") {
|
||||||
|
Some(bearer) if bearer == expected_bearer => Ok(req),
|
||||||
|
_ => Err(Status::unauthenticated("No valid auth token")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let server = DicebotServer::with_interceptor(rpc_service, interceptor);
|
||||||
|
|
||||||
|
Server::builder()
|
||||||
|
.add_service(server)
|
||||||
|
.serve(addr)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
_ => noop().await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn noop() -> Result<(), BotError> {
|
||||||
|
warn!("RPC address or shared secret not specified. Not enabling gRPC.");
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
use crate::db::{errors::DataError, Variables};
|
||||||
|
use crate::error::BotError;
|
||||||
|
use crate::matrix;
|
||||||
|
use crate::{config::Config, db::sqlite::Database};
|
||||||
|
use futures::stream;
|
||||||
|
use futures::{StreamExt, TryFutureExt, TryStreamExt};
|
||||||
|
use matrix_sdk::ruma::OwnedUserId;
|
||||||
|
use matrix_sdk::{room::Joined, Client};
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tenebrous_rpc::protos::dicebot::{
|
||||||
|
dicebot_server::Dicebot, rooms_list_reply::Room, GetAllVariablesReply, GetAllVariablesRequest,
|
||||||
|
RoomsListReply, SetVariableReply, SetVariableRequest, UserIdRequest,
|
||||||
|
};
|
||||||
|
use tenebrous_rpc::protos::dicebot::{GetVariableReply, GetVariableRequest};
|
||||||
|
use tonic::{Code, Request, Response, Status};
|
||||||
|
|
||||||
|
impl From<BotError> for Status {
|
||||||
|
fn from(error: BotError) -> Status {
|
||||||
|
Status::new(Code::Internal, error.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DataError> for Status {
|
||||||
|
fn from(error: DataError) -> Status {
|
||||||
|
Status::new(Code::Internal, error.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(super) struct DicebotRpcService {
|
||||||
|
pub(super) config: Arc<Config>,
|
||||||
|
pub(super) db: Database,
|
||||||
|
pub(super) client: Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl Dicebot for DicebotRpcService {
|
||||||
|
async fn set_variable(
|
||||||
|
&self,
|
||||||
|
request: Request<SetVariableRequest>,
|
||||||
|
) -> Result<Response<SetVariableReply>, Status> {
|
||||||
|
let SetVariableRequest {
|
||||||
|
user_id,
|
||||||
|
room_id,
|
||||||
|
variable_name,
|
||||||
|
value,
|
||||||
|
} = request.into_inner();
|
||||||
|
|
||||||
|
self.db
|
||||||
|
.set_user_variable(&user_id, &room_id, &variable_name, value)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(Response::new(SetVariableReply { success: true }))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_variable(
|
||||||
|
&self,
|
||||||
|
request: Request<GetVariableRequest>,
|
||||||
|
) -> Result<Response<GetVariableReply>, Status> {
|
||||||
|
let request = request.into_inner();
|
||||||
|
let value = self
|
||||||
|
.db
|
||||||
|
.get_user_variable(&request.user_id, &request.room_id, &request.variable_name)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(Response::new(GetVariableReply { value }))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_all_variables(
|
||||||
|
&self,
|
||||||
|
request: Request<GetAllVariablesRequest>,
|
||||||
|
) -> Result<Response<GetAllVariablesReply>, Status> {
|
||||||
|
let request = request.into_inner();
|
||||||
|
let variables = self
|
||||||
|
.db
|
||||||
|
.get_user_variables(&request.user_id, &request.room_id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(Response::new(GetAllVariablesReply { variables }))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn rooms_for_user(
|
||||||
|
&self,
|
||||||
|
request: Request<UserIdRequest>,
|
||||||
|
) -> Result<Response<RoomsListReply>, Status> {
|
||||||
|
let UserIdRequest { user_id } = request.into_inner();
|
||||||
|
let user_id = OwnedUserId::try_from(user_id).map_err(BotError::from)?;
|
||||||
|
|
||||||
|
let rooms_for_user = matrix::get_rooms_for_user(&self.client, &user_id)
|
||||||
|
.err_into::<BotError>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut rooms: Vec<Room> = stream::iter(rooms_for_user)
|
||||||
|
.filter_map(|room: Joined| async move {
|
||||||
|
let room: Result<Room, _> = room.display_name().await.map(|room_name| Room {
|
||||||
|
room_id: room.room_id().to_string(),
|
||||||
|
display_name: room_name.to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
Some(room)
|
||||||
|
})
|
||||||
|
.err_into::<BotError>()
|
||||||
|
.try_collect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let sort = |r1: &Room, r2: &Room| {
|
||||||
|
r1.display_name
|
||||||
|
.to_lowercase()
|
||||||
|
.cmp(&r2.display_name.to_lowercase())
|
||||||
|
};
|
||||||
|
|
||||||
|
rooms.sort_by(sort);
|
||||||
|
|
||||||
|
Ok(Response::new(RoomsListReply { rooms }))
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
use strum::{AsRefStr, Display, EnumIter, EnumString};
|
||||||
|
|
||||||
|
#[derive(EnumString, EnumIter, AsRefStr, Display)]
|
||||||
|
pub(crate) enum GameSystem {
|
||||||
|
ChroniclesOfDarkness,
|
||||||
|
Changeling,
|
||||||
|
MageTheAwakening,
|
||||||
|
WerewolfTheForsaken,
|
||||||
|
DeviantTheRenegades,
|
||||||
|
MummyTheCurse,
|
||||||
|
PrometheanTheCreated,
|
||||||
|
CallOfCthulhu,
|
||||||
|
DungeonsAndDragons5e,
|
||||||
|
DungeonsAndDragons4e,
|
||||||
|
DungeonsAndDragons35e,
|
||||||
|
DungeonsAndDragons2e,
|
||||||
|
DungeonsAndDragons1e,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameSystem {}
|
|
@ -0,0 +1,18 @@
|
||||||
|
[package]
|
||||||
|
name = "tenebrous-rpc"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["projectmoon <projectmoon@agnos.is>"]
|
||||||
|
edition = "2018"
|
||||||
|
description = "gRPC protobuf models for Tenebrous."
|
||||||
|
homepage = "https://git.agnos.is/projectmoon/tenebrous-dicebot"
|
||||||
|
repository = "https://git.agnos.is/projectmoon/tenebrous-dicebot"
|
||||||
|
license = "AGPL-3.0-or-later"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tonic-build = "0.4"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tonic = "0.4"
|
||||||
|
prost = "0.7"
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
tonic_build::compile_protos("protos/dicebot.proto")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
package dicebot;
|
||||||
|
|
||||||
|
service Dicebot {
|
||||||
|
rpc GetVariable(GetVariableRequest) returns (GetVariableReply);
|
||||||
|
rpc GetAllVariables(GetAllVariablesRequest) returns (GetAllVariablesReply);
|
||||||
|
rpc SetVariable(SetVariableRequest) returns (SetVariableReply);
|
||||||
|
rpc RoomsForUser(UserIdRequest) returns (RoomsListReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetVariableRequest {
|
||||||
|
string user_id = 1;
|
||||||
|
string room_id = 2;
|
||||||
|
string variable_name = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetVariableReply {
|
||||||
|
int32 value = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetAllVariablesRequest {
|
||||||
|
string user_id = 1;
|
||||||
|
string room_id = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetAllVariablesReply {
|
||||||
|
map<string, int32> variables = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SetVariableRequest {
|
||||||
|
string user_id = 1;
|
||||||
|
string room_id = 2;
|
||||||
|
string variable_name = 3;
|
||||||
|
int32 value = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SetVariableReply {
|
||||||
|
bool success = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UserIdRequest {
|
||||||
|
string user_id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message RoomsListReply {
|
||||||
|
message Room {
|
||||||
|
string room_id = 1;
|
||||||
|
string display_name = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
repeated Room rooms = 1;
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
pub mod protos {
|
||||||
|
pub mod dicebot {
|
||||||
|
tonic::include_proto!("dicebot");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
use chronicle_dicebot::commands;
|
|
||||||
use chronicle_dicebot::context::Context;
|
|
||||||
use chronicle_dicebot::db::Database;
|
|
||||||
use chronicle_dicebot::error::BotError;
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> Result<(), BotError> {
|
|
||||||
let db = Database::new_temp()?;
|
|
||||||
let input = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
|
||||||
let command = match commands::parse(&input) {
|
|
||||||
Ok(command) => command,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
|
|
||||||
let context = Context::new(&db, "roomid", "localuser", &input);
|
|
||||||
println!("{}", command.execute(&context).await.plain());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
//Needed for nested Result handling from tokio. Probably can go away after 1.47.0.
|
|
||||||
#![type_length_limit = "7605144"]
|
|
||||||
use chronicle_dicebot::bot::DiceBot;
|
|
||||||
use chronicle_dicebot::config::*;
|
|
||||||
use chronicle_dicebot::db::Database;
|
|
||||||
use chronicle_dicebot::error::BotError;
|
|
||||||
use chronicle_dicebot::state::DiceBotState;
|
|
||||||
use env_logger::Env;
|
|
||||||
use log::error;
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() {
|
|
||||||
env_logger::from_env(Env::default().default_filter_or("chronicle_dicebot=info,dicebot=info"))
|
|
||||||
.init();
|
|
||||||
match run().await {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => error!("Error: {}", e),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run() -> Result<(), BotError> {
|
|
||||||
let config_path = std::env::args()
|
|
||||||
.skip(1)
|
|
||||||
.next()
|
|
||||||
.expect("Need a config as an argument");
|
|
||||||
|
|
||||||
let cfg = Arc::new(read_config(config_path)?);
|
|
||||||
let db = Database::new(&cfg.database_path())?;
|
|
||||||
let state = Arc::new(RwLock::new(DiceBotState::new(&cfg)));
|
|
||||||
|
|
||||||
db.migrate(cfg.migration_version())?;
|
|
||||||
|
|
||||||
match DiceBot::new(&cfg, &state, &db) {
|
|
||||||
Ok(bot) => bot.run().await?,
|
|
||||||
Err(e) => println!("Error connecting: {:?}", e),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
285
src/bot.rs
285
src/bot.rs
|
@ -1,285 +0,0 @@
|
||||||
use crate::commands::execute_command;
|
|
||||||
use crate::config::*;
|
|
||||||
use crate::context::Context;
|
|
||||||
use crate::db::Database;
|
|
||||||
use crate::error::BotError;
|
|
||||||
use crate::state::DiceBotState;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use dirs;
|
|
||||||
use log::{debug, error, info, trace, warn};
|
|
||||||
use matrix_sdk::Error as MatrixError;
|
|
||||||
use matrix_sdk::{
|
|
||||||
self,
|
|
||||||
events::{
|
|
||||||
room::member::MemberEventContent,
|
|
||||||
room::message::{MessageEventContent, NoticeMessageEventContent, TextMessageEventContent},
|
|
||||||
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
|
|
||||||
},
|
|
||||||
Client, ClientConfig, EventEmitter, JsonStore, Room, SyncRoom, SyncSettings,
|
|
||||||
};
|
|
||||||
//use matrix_sdk_common_macros::async_trait;
|
|
||||||
use std::clone::Clone;
|
|
||||||
use std::ops::Sub;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
use std::time::{Duration, SystemTime};
|
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
/// The DiceBot struct represents an active dice bot. The bot is not
|
|
||||||
/// connected to Matrix until its run() function is called.
|
|
||||||
pub struct DiceBot {
|
|
||||||
/// A reference to the configuration read in on application start.
|
|
||||||
config: Arc<Config>,
|
|
||||||
|
|
||||||
/// The matrix client.
|
|
||||||
client: Client,
|
|
||||||
|
|
||||||
/// State of the dicebot
|
|
||||||
state: Arc<RwLock<DiceBotState>>,
|
|
||||||
|
|
||||||
/// Active database layer
|
|
||||||
db: Database,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cache_dir() -> Result<PathBuf, BotError> {
|
|
||||||
let mut dir = dirs::cache_dir().ok_or(BotError::NoCacheDirectoryError)?;
|
|
||||||
dir.push("matrix-dicebot");
|
|
||||||
Ok(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates the matrix client.
|
|
||||||
fn create_client(config: &Config) -> Result<Client, BotError> {
|
|
||||||
let cache_dir = cache_dir()?;
|
|
||||||
let store = JsonStore::open(&cache_dir)?;
|
|
||||||
let client_config = ClientConfig::new().state_store(Box::new(store));
|
|
||||||
let homeserver_url = Url::parse(&config.matrix_homeserver())?;
|
|
||||||
|
|
||||||
Ok(Client::new_with_config(homeserver_url, client_config)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extracts more detailed error messages out of a matrix SDK error.
|
|
||||||
fn extract_error_message(error: MatrixError) -> String {
|
|
||||||
use matrix_sdk::Error::RumaResponse;
|
|
||||||
match error {
|
|
||||||
RumaResponse(ruma_error) => ruma_error.to_string(),
|
|
||||||
_ => error.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DiceBot {
|
|
||||||
/// Create a new dicebot with the given configuration and state
|
|
||||||
/// actor. This function returns a Result because it is possible
|
|
||||||
/// for client creation to fail for some reason (e.g. invalid
|
|
||||||
/// homeserver URL).
|
|
||||||
pub fn new(
|
|
||||||
config: &Arc<Config>,
|
|
||||||
state: &Arc<RwLock<DiceBotState>>,
|
|
||||||
db: &Database,
|
|
||||||
) -> Result<Self, BotError> {
|
|
||||||
Ok(DiceBot {
|
|
||||||
client: create_client(&config)?,
|
|
||||||
config: config.clone(),
|
|
||||||
state: state.clone(),
|
|
||||||
db: db.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Logs the bot into Matrix and listens for events until program
|
|
||||||
/// terminated, or a panic occurs. Originally adapted from the
|
|
||||||
/// matrix-rust-sdk command bot example.
|
|
||||||
pub async fn run(self) -> Result<(), BotError> {
|
|
||||||
let username = &self.config.matrix_username();
|
|
||||||
let password = &self.config.matrix_password();
|
|
||||||
|
|
||||||
//TODO provide a device id from config.
|
|
||||||
let mut client = self.client.clone();
|
|
||||||
client
|
|
||||||
.login(username, password, None, Some("matrix dice bot"))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
info!("Logged in as {}", username);
|
|
||||||
|
|
||||||
//If the local json store has not been created yet, we need to do a single initial sync.
|
|
||||||
//It stores data under username's localpart.
|
|
||||||
let should_sync = {
|
|
||||||
let mut cache = cache_dir()?;
|
|
||||||
cache.push(username);
|
|
||||||
!cache.exists()
|
|
||||||
};
|
|
||||||
|
|
||||||
if should_sync {
|
|
||||||
info!("Performing initial sync");
|
|
||||||
self.client.sync(SyncSettings::default()).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Attach event handler.
|
|
||||||
client.add_event_emitter(Box::new(self)).await;
|
|
||||||
info!("Listening for commands");
|
|
||||||
|
|
||||||
let token = client
|
|
||||||
.sync_token()
|
|
||||||
.await
|
|
||||||
.ok_or(BotError::SyncTokenRequired)?;
|
|
||||||
|
|
||||||
let settings = SyncSettings::default().token(token);
|
|
||||||
|
|
||||||
//this keeps state from the server streaming in to the dice bot via the EventEmitter trait
|
|
||||||
//TODO somehow figure out how to "sync_until" instead of sync_forever... copy code and modify?
|
|
||||||
client.sync_forever(settings, |_| async {}).await;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute_commands(&self, room: &Room, sender_username: &str, msg_body: &str) {
|
|
||||||
let room_name = room.display_name().clone();
|
|
||||||
let room_id = room.room_id.clone();
|
|
||||||
|
|
||||||
let mut results = Vec::with_capacity(msg_body.lines().count());
|
|
||||||
|
|
||||||
for command in msg_body.lines() {
|
|
||||||
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
|
|
||||||
|
|
||||||
if let Some(cmd_result) = execute_command(&ctx).await {
|
|
||||||
results.push(cmd_result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if results.len() >= 1 {
|
|
||||||
if results.len() == 1 {
|
|
||||||
let cmd_result = &results[0];
|
|
||||||
let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
|
|
||||||
NoticeMessageEventContent::html(
|
|
||||||
cmd_result.plain.clone(),
|
|
||||||
cmd_result.html.clone(),
|
|
||||||
),
|
|
||||||
));
|
|
||||||
|
|
||||||
let result = self.client.room_send(&room_id, response, None).await;
|
|
||||||
if let Err(e) = result {
|
|
||||||
let message = extract_error_message(e);
|
|
||||||
error!("Error sending message: {}", message);
|
|
||||||
};
|
|
||||||
} else if results.len() > 1 {
|
|
||||||
let message = format!("{}: Executed {} commands", sender_username, results.len());
|
|
||||||
let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
|
|
||||||
NoticeMessageEventContent::html(&message, &message),
|
|
||||||
));
|
|
||||||
|
|
||||||
let result = self.client.room_send(&room_id, response, None).await;
|
|
||||||
if let Err(e) = result {
|
|
||||||
let message = extract_error_message(e);
|
|
||||||
error!("Error sending message: {}", message);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("[{}] {} executed: {}", room_name, sender_username, msg_body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if a message is recent enough to actually process. If the
|
|
||||||
/// message is within "oldest_message_age" seconds, this function
|
|
||||||
/// returns true. If it's older than that, it returns false and logs a
|
|
||||||
/// debug message.
|
|
||||||
fn check_message_age(
|
|
||||||
event: &SyncMessageEvent<MessageEventContent>,
|
|
||||||
oldest_message_age: u64,
|
|
||||||
) -> bool {
|
|
||||||
let sending_time = event.origin_server_ts;
|
|
||||||
let oldest_timestamp = SystemTime::now().sub(Duration::new(oldest_message_age, 0));
|
|
||||||
|
|
||||||
if sending_time > oldest_timestamp {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
let age = match oldest_timestamp.duration_since(sending_time) {
|
|
||||||
Ok(n) => format!("{} seconds too old", n.as_secs()),
|
|
||||||
Err(_) => "before the UNIX epoch".to_owned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
debug!("Ignoring message because it is {}: {:?}", age, event);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn should_process<'a>(
|
|
||||||
bot: &DiceBot,
|
|
||||||
event: &SyncMessageEvent<MessageEventContent>,
|
|
||||||
) -> Result<(String, String), BotError> {
|
|
||||||
//Ignore messages that are older than configured duration.
|
|
||||||
if !check_message_age(event, bot.config.oldest_message_age()) {
|
|
||||||
let state_check = bot.state.read().unwrap();
|
|
||||||
if !((*state_check).logged_skipped_old_messages()) {
|
|
||||||
drop(state_check);
|
|
||||||
let mut state = bot.state.write().unwrap();
|
|
||||||
(*state).skipped_old_messages();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Err(BotError::ShouldNotProcessError);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (msg_body, sender_username) = if let SyncMessageEvent {
|
|
||||||
content: MessageEventContent::Text(TextMessageEventContent { body, .. }),
|
|
||||||
sender,
|
|
||||||
..
|
|
||||||
} = event
|
|
||||||
{
|
|
||||||
(
|
|
||||||
body.clone(),
|
|
||||||
format!("@{}:{}", sender.localpart(), sender.server_name()),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(String::new(), String::new())
|
|
||||||
};
|
|
||||||
|
|
||||||
//Command parser can handle non-commands, but faster to
|
|
||||||
//not parse them.
|
|
||||||
if !msg_body.starts_with("!") {
|
|
||||||
trace!("Ignoring non-command: {}", msg_body);
|
|
||||||
return Err(BotError::ShouldNotProcessError);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((msg_body, sender_username))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This event emitter listens for messages with dice rolling commands.
|
|
||||||
/// Originally adapted from the matrix-rust-sdk examples.
|
|
||||||
#[async_trait]
|
|
||||||
impl EventEmitter for DiceBot {
|
|
||||||
async fn on_stripped_state_member(
|
|
||||||
&self,
|
|
||||||
room: SyncRoom,
|
|
||||||
room_member: &StrippedStateEvent<MemberEventContent>,
|
|
||||||
_: Option<MemberEventContent>,
|
|
||||||
) {
|
|
||||||
if let SyncRoom::Invited(room) = room {
|
|
||||||
if let Some(user_id) = self.client.user_id().await {
|
|
||||||
if room_member.state_key != user_id {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let room = room.read().await;
|
|
||||||
info!("Autojoining room {}", room.display_name());
|
|
||||||
|
|
||||||
if let Err(e) = self.client.join_room_by_id(&room.room_id).await {
|
|
||||||
warn!("Could not join room: {}", e.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
|
|
||||||
if let SyncRoom::Joined(room) = room {
|
|
||||||
let (msg_body, sender_username) =
|
|
||||||
if let Ok((msg_body, sender_username)) = should_process(self, &event).await {
|
|
||||||
(msg_body, sender_username)
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
//we clone here to hold the lock for as little time as possible.
|
|
||||||
let real_room = room.read().await.clone();
|
|
||||||
|
|
||||||
self.execute_commands(&real_room, &sender_username, &msg_body)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
133
src/commands.rs
133
src/commands.rs
|
@ -1,133 +0,0 @@
|
||||||
use crate::context::Context;
|
|
||||||
use crate::error::{BotError, BotError::CommandParsingError};
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use parser::CommandParsingError::UnrecognizedCommand;
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
pub mod basic_rolling;
|
|
||||||
pub mod cofd;
|
|
||||||
pub mod cthulhu;
|
|
||||||
pub mod misc;
|
|
||||||
pub mod parser;
|
|
||||||
pub mod variables;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum CommandError {
|
|
||||||
#[error("invalid command: {0}")]
|
|
||||||
InvalidCommand(String),
|
|
||||||
|
|
||||||
#[error("ignored command")]
|
|
||||||
IgnoredCommand,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Execution {
|
|
||||||
plain: String,
|
|
||||||
html: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Execution {
|
|
||||||
pub fn plain(&self) -> &str {
|
|
||||||
&self.plain
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn html(&self) -> &str {
|
|
||||||
&self.html
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait Command: Send + Sync {
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution;
|
|
||||||
fn name(&self) -> &'static str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse a command string into a dynamic command execution trait
|
|
||||||
/// object. Returns an error if a command was recognized but not
|
|
||||||
/// parsed correctly. Returns IgnoredCommand error if no command was
|
|
||||||
/// recognized.
|
|
||||||
pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
|
|
||||||
match parser::parse_command(s) {
|
|
||||||
Ok(command) => Ok(command),
|
|
||||||
Err(CommandParsingError(UnrecognizedCommand(_))) => {
|
|
||||||
Err(CommandError::IgnoredCommand.into())
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CommandResult {
|
|
||||||
pub plain: String,
|
|
||||||
pub html: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Attempt to execute a command, and return the content that should
|
|
||||||
/// go back to Matrix, if the command was executed (successfully or
|
|
||||||
/// not). If a command is determined to be ignored, this function will
|
|
||||||
/// return None, signifying that we should not send a response.
|
|
||||||
pub async fn execute_command(ctx: &Context<'_>) -> Option<CommandResult> {
|
|
||||||
let res = parse(&ctx.message_body);
|
|
||||||
|
|
||||||
let (plain, html) = match res {
|
|
||||||
Ok(cmd) => {
|
|
||||||
let execution = cmd.execute(ctx).await;
|
|
||||||
(execution.plain().into(), execution.html().into())
|
|
||||||
}
|
|
||||||
Err(BotError::CommandError(CommandError::IgnoredCommand)) => return None,
|
|
||||||
Err(e) => {
|
|
||||||
let message = format!("Error parsing command: {}", e);
|
|
||||||
let html_message = format!("<p><strong>{}</strong></p>", message);
|
|
||||||
(message, html_message)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("{}\n{}", ctx.username, plain);
|
|
||||||
let html = format!("<p>{}</p>\n{}", ctx.username, html);
|
|
||||||
|
|
||||||
Some(CommandResult {
|
|
||||||
plain: plain,
|
|
||||||
html: html,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn chance_die_is_not_malformed() {
|
|
||||||
assert!(parse("!chance").is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn roll_malformed_expression_test() {
|
|
||||||
assert!(parse("!roll 1d20asdlfkj").is_err());
|
|
||||||
assert!(parse("!roll 1d20asdlfkj ").is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn roll_dice_pool_malformed_expression_test() {
|
|
||||||
assert!(parse("!pool 8abc").is_err());
|
|
||||||
assert!(parse("!pool 8abc ").is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn pool_whitespace_test() {
|
|
||||||
parse("!pool ns3:8 ").expect("was error");
|
|
||||||
parse(" !pool ns3:8").expect("was error");
|
|
||||||
parse(" !pool ns3:8 ").expect("was error");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn help_whitespace_test() {
|
|
||||||
parse("!help stuff ").expect("was error");
|
|
||||||
parse(" !help stuff").expect("was error");
|
|
||||||
parse(" !help stuff ").expect("was error");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn roll_whitespace_test() {
|
|
||||||
parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
||||||
parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
||||||
parse(" !roll 1d4 + 5d6 -3 ").expect("was error");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
use super::{Command, Execution};
|
|
||||||
use crate::context::Context;
|
|
||||||
use crate::dice::ElementExpression;
|
|
||||||
use crate::roll::Roll;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
pub struct RollCommand(pub ElementExpression);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for RollCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"roll regular dice"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, _ctx: &Context<'_>) -> Execution {
|
|
||||||
let roll = self.0.roll();
|
|
||||||
let plain = format!("Dice: {}\nResult: {}", self.0, roll);
|
|
||||||
let html = format!(
|
|
||||||
"<p><strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
||||||
self.0, roll
|
|
||||||
);
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
use super::{Command, Execution};
|
|
||||||
use crate::cofd::dice::{roll_pool, DicePool, DicePoolWithContext};
|
|
||||||
use crate::context::Context;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
pub struct PoolRollCommand(pub DicePool);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for PoolRollCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"roll dice pool"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
|
||||||
let pool_with_ctx = DicePoolWithContext(&self.0, ctx);
|
|
||||||
let roll_result = roll_pool(&pool_with_ctx).await;
|
|
||||||
|
|
||||||
let (plain, html) = match roll_result {
|
|
||||||
Ok(rolled_pool) => {
|
|
||||||
let plain = format!("Pool: {}\nResult: {}", rolled_pool, rolled_pool.roll);
|
|
||||||
let html = format!(
|
|
||||||
"<p><strong>Pool:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
||||||
rolled_pool, rolled_pool.roll
|
|
||||||
);
|
|
||||||
(plain, html)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let plain = format!("Error: {}", e);
|
|
||||||
let html = format!("<p><strong>Error:</strong> {}</p>", e);
|
|
||||||
(plain, html)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
use super::{Command, Execution};
|
|
||||||
use crate::context::Context;
|
|
||||||
use crate::cthulhu::dice::{AdvancementRoll, DiceRoll};
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
pub struct CthRoll(pub DiceRoll);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for CthRoll {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"roll percentile pool"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, _ctx: &Context<'_>) -> Execution {
|
|
||||||
//TODO this will be converted to a result when supporting variables.
|
|
||||||
let roll = self.0.roll();
|
|
||||||
let plain = format!("Roll: {}\nResult: {}", self.0, roll);
|
|
||||||
let html = format!(
|
|
||||||
"<p><strong>Roll:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
||||||
self.0, roll
|
|
||||||
);
|
|
||||||
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CthAdvanceRoll(pub AdvancementRoll);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for CthAdvanceRoll {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"roll percentile pool"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, _ctx: &Context<'_>) -> Execution {
|
|
||||||
//TODO this will be converted to a result when supporting variables.
|
|
||||||
let roll = self.0.roll();
|
|
||||||
let plain = format!("Roll: {}\nResult: {}", self.0, roll);
|
|
||||||
let html = format!(
|
|
||||||
"<p><strong>Roll:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
||||||
self.0, roll
|
|
||||||
);
|
|
||||||
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
use super::{Command, Execution};
|
|
||||||
use crate::context::Context;
|
|
||||||
use crate::help::HelpTopic;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
pub struct HelpCommand(pub Option<HelpTopic>);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for HelpCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"help information"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, _ctx: &Context<'_>) -> Execution {
|
|
||||||
let help = match &self.0 {
|
|
||||||
Some(topic) => topic.message(),
|
|
||||||
_ => "There is no help for this topic",
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("Help: {}", help);
|
|
||||||
let html = format!("<p><strong>Help:</strong> {}", help.replace("\n", "<br/>"));
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,114 +0,0 @@
|
||||||
use super::{Command, Execution};
|
|
||||||
use crate::context::Context;
|
|
||||||
use crate::db::errors::DataError;
|
|
||||||
use crate::db::variables::UserAndRoom;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
pub struct GetAllVariablesCommand;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for GetAllVariablesCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"get all variables"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
|
||||||
let key = UserAndRoom(&ctx.username, &ctx.room_id);
|
|
||||||
let result = ctx.db.variables.get_user_variables(&key);
|
|
||||||
|
|
||||||
let value = match result {
|
|
||||||
Ok(variables) => {
|
|
||||||
let mut variable_list = variables
|
|
||||||
.into_iter()
|
|
||||||
.map(|(name, value)| format!(" - {} = {}", name, value))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
variable_list.sort();
|
|
||||||
variable_list.join("\n")
|
|
||||||
}
|
|
||||||
Err(e) => format!("error getting variables: {}", e),
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("Variables:\n{}", value);
|
|
||||||
let html = format!(
|
|
||||||
"<p><strong>Variables:</strong><br/>{}",
|
|
||||||
value.replace("\n", "<br/>")
|
|
||||||
);
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct GetVariableCommand(pub String);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for GetVariableCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"retrieve variable value"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
|
||||||
let name = &self.0;
|
|
||||||
let key = UserAndRoom(&ctx.username, &ctx.room_id);
|
|
||||||
let result = ctx.db.variables.get_user_variable(&key, name);
|
|
||||||
|
|
||||||
let value = match result {
|
|
||||||
Ok(num) => format!("{} = {}", name, num),
|
|
||||||
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
|
||||||
Err(e) => format!("error getting {}: {}", name, e),
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("Variable: {}", value);
|
|
||||||
let html = format!("<p><strong>Variable:</strong> {}", value);
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SetVariableCommand(pub String, pub i32);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for SetVariableCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"set variable value"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
|
||||||
let name = &self.0;
|
|
||||||
let value = self.1;
|
|
||||||
let key = UserAndRoom(&ctx.username, ctx.room_id);
|
|
||||||
let result = ctx.db.variables.set_user_variable(&key, name, value);
|
|
||||||
|
|
||||||
let content = match result {
|
|
||||||
Ok(_) => format!("{} = {}", name, value),
|
|
||||||
Err(e) => format!("error setting {}: {}", name, e),
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("Set Variable: {}", content);
|
|
||||||
let html = format!("<p><strong>Set Variable:</strong> {}", content);
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DeleteVariableCommand(pub String);
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Command for DeleteVariableCommand {
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"delete variable"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
|
||||||
let name = &self.0;
|
|
||||||
let key = UserAndRoom(&ctx.username, ctx.room_id);
|
|
||||||
let result = ctx.db.variables.delete_user_variable(&key, name);
|
|
||||||
|
|
||||||
let value = match result {
|
|
||||||
Ok(()) => format!("{} now unset", name),
|
|
||||||
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
|
||||||
Err(e) => format!("error deleting {}: {}", name, e),
|
|
||||||
};
|
|
||||||
|
|
||||||
let plain = format!("Remove Variable: {}", value);
|
|
||||||
let html = format!("<p><strong>Remove Variable:</strong> {}", value);
|
|
||||||
Execution { plain, html }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
use crate::db::Database;
|
|
||||||
|
|
||||||
/// A context carried through the system providing access to things
|
|
||||||
/// like the database.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Context<'a> {
|
|
||||||
pub db: Database,
|
|
||||||
pub room_id: &'a str,
|
|
||||||
pub username: &'a str,
|
|
||||||
pub message_body: &'a str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Context<'a> {
|
|
||||||
pub fn new(
|
|
||||||
db: &Database,
|
|
||||||
room_id: &'a str,
|
|
||||||
username: &'a str,
|
|
||||||
message_body: &'a str,
|
|
||||||
) -> Context<'a> {
|
|
||||||
Context {
|
|
||||||
db: db.clone(),
|
|
||||||
room_id: room_id,
|
|
||||||
username: username,
|
|
||||||
message_body: message_body,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,174 +0,0 @@
|
||||||
use super::dice::{AdvancementRoll, DiceRoll, DiceRollModifier};
|
|
||||||
use crate::parser::DiceParsingError;
|
|
||||||
|
|
||||||
//TOOD convert these to use parse_amounts from the common dice code.
|
|
||||||
|
|
||||||
fn parse_modifier(input: &str) -> Result<(DiceRollModifier, &str), DiceParsingError> {
|
|
||||||
if input.ends_with("bb") {
|
|
||||||
Ok((DiceRollModifier::TwoBonus, input.trim_end_matches("bb")))
|
|
||||||
} else if input.ends_with("b") {
|
|
||||||
Ok((DiceRollModifier::OneBonus, input.trim_end_matches("b")))
|
|
||||||
} else if input.ends_with("pp") {
|
|
||||||
Ok((DiceRollModifier::TwoPenalty, input.trim_end_matches("pp")))
|
|
||||||
} else if input.ends_with("p") {
|
|
||||||
Ok((DiceRollModifier::OnePenalty, input.trim_end_matches("p")))
|
|
||||||
} else {
|
|
||||||
Ok((DiceRollModifier::Normal, input))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_regular_roll(input: &str) -> Result<DiceRoll, DiceParsingError> {
|
|
||||||
let input = input.trim();
|
|
||||||
let (modifier, input) = parse_modifier(input)?;
|
|
||||||
let target: u32 = input.parse().map_err(|_| DiceParsingError::InvalidAmount)?;
|
|
||||||
|
|
||||||
if target <= 100 {
|
|
||||||
Ok(DiceRoll {
|
|
||||||
target: target,
|
|
||||||
modifier: modifier,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(DiceParsingError::InvalidAmount)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_advancement_roll(input: &str) -> Result<AdvancementRoll, DiceParsingError> {
|
|
||||||
let input = input.trim();
|
|
||||||
let target: u32 = input.parse().map_err(|_| DiceParsingError::InvalidAmount)?;
|
|
||||||
|
|
||||||
if target <= 100 {
|
|
||||||
Ok(AdvancementRoll {
|
|
||||||
existing_skill: target,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(DiceParsingError::InvalidAmount)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_single_number() {
|
|
||||||
let result = parse_regular_roll("60");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
DiceRoll {
|
|
||||||
target: 60,
|
|
||||||
modifier: DiceRollModifier::Normal
|
|
||||||
},
|
|
||||||
result.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_two_bonus() {
|
|
||||||
let result = parse_regular_roll("60bb");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
DiceRoll {
|
|
||||||
target: 60,
|
|
||||||
modifier: DiceRollModifier::TwoBonus
|
|
||||||
},
|
|
||||||
result.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_one_bonus() {
|
|
||||||
let result = parse_regular_roll("60b");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
DiceRoll {
|
|
||||||
target: 60,
|
|
||||||
modifier: DiceRollModifier::OneBonus
|
|
||||||
},
|
|
||||||
result.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_two_penalty() {
|
|
||||||
let result = parse_regular_roll("60pp");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
DiceRoll {
|
|
||||||
target: 60,
|
|
||||||
modifier: DiceRollModifier::TwoPenalty
|
|
||||||
},
|
|
||||||
result.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_one_penalty() {
|
|
||||||
let result = parse_regular_roll("60p");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
DiceRoll {
|
|
||||||
target: 60,
|
|
||||||
modifier: DiceRollModifier::OnePenalty
|
|
||||||
},
|
|
||||||
result.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_accepts_whitespacen() {
|
|
||||||
assert!(parse_regular_roll("60 ").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60 ").is_ok());
|
|
||||||
|
|
||||||
assert!(parse_regular_roll("60bb ").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60bb").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60bb ").is_ok());
|
|
||||||
|
|
||||||
assert!(parse_regular_roll("60b ").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60b").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60b ").is_ok());
|
|
||||||
|
|
||||||
assert!(parse_regular_roll("60pp ").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60pp").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60pp ").is_ok());
|
|
||||||
|
|
||||||
assert!(parse_regular_roll("60p ").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60p").is_ok());
|
|
||||||
assert!(parse_regular_roll(" 60p ").is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn advancement_roll_accepts_whitespacen() {
|
|
||||||
assert!(parse_advancement_roll("60 ").is_ok());
|
|
||||||
assert!(parse_advancement_roll(" 60").is_ok());
|
|
||||||
assert!(parse_advancement_roll(" 60 ").is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn advancement_roll_accepts_single_number() {
|
|
||||||
let result = parse_advancement_roll("60");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(AdvancementRoll { existing_skill: 60 }, result.unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_rejects_big_numbers() {
|
|
||||||
assert!(parse_regular_roll("3000").is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn advancement_roll_rejects_big_numbers() {
|
|
||||||
assert!(parse_advancement_roll("3000").is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_roll_rejects_invalid_input() {
|
|
||||||
assert!(parse_regular_roll("abc").is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn advancement_roll_rejects_invalid_input() {
|
|
||||||
assert!(parse_advancement_roll("abc").is_err());
|
|
||||||
}
|
|
||||||
}
|
|
83
src/db.rs
83
src/db.rs
|
@ -1,83 +0,0 @@
|
||||||
use crate::db::errors::{DataError, MigrationError};
|
|
||||||
use crate::db::migrations::{get_migration_version, Migrations};
|
|
||||||
use crate::db::variables::Variables;
|
|
||||||
use log::info;
|
|
||||||
use sled::{Config, Db};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
pub mod data_migrations;
|
|
||||||
pub mod errors;
|
|
||||||
pub mod migrations;
|
|
||||||
pub mod schema;
|
|
||||||
pub mod variables;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Database {
|
|
||||||
db: Db,
|
|
||||||
pub(crate) variables: Variables,
|
|
||||||
pub(crate) migrations: Migrations,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Database {
|
|
||||||
fn new_db(db: sled::Db) -> Result<Database, DataError> {
|
|
||||||
let migrations = db.open_tree("migrations")?;
|
|
||||||
|
|
||||||
Ok(Database {
|
|
||||||
db: db.clone(),
|
|
||||||
variables: Variables::new(&db)?,
|
|
||||||
migrations: Migrations(migrations),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Database, DataError> {
|
|
||||||
let db = sled::open(path)?;
|
|
||||||
Self::new_db(db)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_temp() -> Result<Database, DataError> {
|
|
||||||
let config = Config::new().temporary(true);
|
|
||||||
let db = config.open()?;
|
|
||||||
Self::new_db(db)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn migrate(&self, to_version: u32) -> Result<(), DataError> {
|
|
||||||
//get version from db
|
|
||||||
let db_version = get_migration_version(&self)?;
|
|
||||||
|
|
||||||
if db_version < to_version {
|
|
||||||
info!(
|
|
||||||
"Migrating database from version {} to version {}",
|
|
||||||
db_version, to_version
|
|
||||||
);
|
|
||||||
//if db version < to_version, proceed
|
|
||||||
//produce range of db_version+1 .. to_version (inclusive)
|
|
||||||
let versions_to_run: Vec<u32> = ((db_version + 1)..=to_version).collect();
|
|
||||||
let migrations = data_migrations::get_migrations(&versions_to_run)?;
|
|
||||||
|
|
||||||
//execute each closure.
|
|
||||||
for (version, migration) in versions_to_run.iter().zip(migrations) {
|
|
||||||
let (migration_func, name) = migration;
|
|
||||||
//This needs to be transactional on migrations
|
|
||||||
//keyspace. abort on migration func error.
|
|
||||||
|
|
||||||
info!("Applying migration {} :: {}", version, name);
|
|
||||||
match migration_func(&self) {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}?;
|
|
||||||
|
|
||||||
self.migrations.set_migration_version(*version)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Done applying migrations.");
|
|
||||||
Ok(())
|
|
||||||
} else if db_version > to_version {
|
|
||||||
//if db version > to_version, cannot downgrade error
|
|
||||||
Err(MigrationError::CannotDowngrade.into())
|
|
||||||
} else {
|
|
||||||
//if db version == to_version, do nothing
|
|
||||||
info!("No database migrations needed.");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
use crate::db::errors::{DataError, MigrationError};
|
|
||||||
use crate::db::variables::migrations::*;
|
|
||||||
use crate::db::Database;
|
|
||||||
use phf::phf_map;
|
|
||||||
|
|
||||||
pub(super) type DataMigration = (fn(&Database) -> Result<(), DataError>, &'static str);
|
|
||||||
|
|
||||||
static MIGRATIONS: phf::Map<u32, DataMigration> = phf_map! {
|
|
||||||
1u32 => (add_room_user_variable_count::migrate, "add_room_user_variable_count"),
|
|
||||||
2u32 => (delete_v0_schema, "delete_v0_schema"),
|
|
||||||
3u32 => (delete_variable_count, "delete_variable_count"),
|
|
||||||
4u32 => (change_delineator_delimiter::migrate, "change_delineator_delimiter"),
|
|
||||||
5u32 => (change_tree_structure::migrate, "change_tree_structure"),
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn get_migrations(versions: &[u32]) -> Result<Vec<DataMigration>, MigrationError> {
|
|
||||||
let mut migrations: Vec<DataMigration> = vec![];
|
|
||||||
|
|
||||||
for version in versions {
|
|
||||||
match MIGRATIONS.get(version) {
|
|
||||||
Some(func) => migrations.push(*func),
|
|
||||||
None => return Err(MigrationError::MigrationNotFound(*version)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(migrations)
|
|
||||||
}
|
|
|
@ -1,74 +0,0 @@
|
||||||
use sled::transaction::{TransactionError, UnabortableTransactionError};
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum MigrationError {
|
|
||||||
#[error("cannot downgrade to an older database version")]
|
|
||||||
CannotDowngrade,
|
|
||||||
|
|
||||||
#[error("migration for version {0} not defined")]
|
|
||||||
MigrationNotFound(u32),
|
|
||||||
|
|
||||||
#[error("migration failed: {0}")]
|
|
||||||
MigrationFailed(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO better combining of key and value in certain errors (namely
|
|
||||||
//I32SchemaViolation).
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum DataError {
|
|
||||||
#[error("value does not exist for key: {0}")]
|
|
||||||
KeyDoesNotExist(String),
|
|
||||||
|
|
||||||
#[error("too many entries")]
|
|
||||||
TooManyEntries,
|
|
||||||
|
|
||||||
#[error("expected i32, but i32 schema was violated")]
|
|
||||||
I32SchemaViolation,
|
|
||||||
|
|
||||||
#[error("expected string, but utf8 schema was violated: {0}")]
|
|
||||||
Utf8chemaViolation(#[from] std::str::Utf8Error),
|
|
||||||
|
|
||||||
#[error("internal database error: {0}")]
|
|
||||||
InternalError(#[from] sled::Error),
|
|
||||||
|
|
||||||
#[error("transaction error: {0}")]
|
|
||||||
TransactionError(#[from] sled::transaction::TransactionError),
|
|
||||||
|
|
||||||
#[error("unabortable transaction error: {0}")]
|
|
||||||
UnabortableTransactionError(#[from] UnabortableTransactionError),
|
|
||||||
|
|
||||||
#[error("data migration error: {0}")]
|
|
||||||
MigrationError(#[from] MigrationError),
|
|
||||||
|
|
||||||
#[error("deserialization error: {0}")]
|
|
||||||
DeserializationError(#[from] bincode::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This From implementation is necessary to deal with the recursive
|
|
||||||
/// error type in the error enum. We defined a transaction error, but
|
|
||||||
/// the only place we use it is when converting from
|
|
||||||
/// sled::transaction::TransactionError<DataError>. This converter
|
|
||||||
/// extracts the inner data error from transaction aborted errors, and
|
|
||||||
/// forwards anything else onward as-is, but wrapped in DataError.
|
|
||||||
impl From<TransactionError<DataError>> for DataError {
|
|
||||||
fn from(error: TransactionError<DataError>) -> Self {
|
|
||||||
match error {
|
|
||||||
TransactionError::Abort(data_err) => data_err,
|
|
||||||
TransactionError::Storage(storage_err) => {
|
|
||||||
DataError::TransactionError(TransactionError::Storage(storage_err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// impl From<ConflictableTransactionError<DataError>> for DataError {
|
|
||||||
// fn from(error: ConflictableTransactionError<DataError>) -> Self {
|
|
||||||
// match error {
|
|
||||||
// ConflictableTransactionError::Abort(data_err) => data_err,
|
|
||||||
// ConflictableTransactionError::Storage(storage_err) => {
|
|
||||||
// DataError::TransactionError(TransactionError::Storage(storage_err))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
|
@ -1,54 +0,0 @@
|
||||||
use crate::db::errors::DataError;
|
|
||||||
use crate::db::schema::convert_u32;
|
|
||||||
use crate::db::Database;
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use sled::Tree;
|
|
||||||
use zerocopy::byteorder::U32;
|
|
||||||
use zerocopy::AsBytes;
|
|
||||||
|
|
||||||
//This file is for controlling the migration info stored in the
|
|
||||||
//database, not actually running migrations.
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Migrations(pub(super) Tree);
|
|
||||||
|
|
||||||
const COLON: &'static [u8] = b":";
|
|
||||||
const METADATA_SPACE: &'static str = "metadata";
|
|
||||||
const MIGRATION_KEY: &'static str = "migration_version";
|
|
||||||
|
|
||||||
fn to_key(keyspace: &str, key_name: &str) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(keyspace.as_bytes());
|
|
||||||
key.extend_from_slice(COLON);
|
|
||||||
key.extend_from_slice(key_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
fn metadata_key(key_name: &str) -> Vec<u8> {
|
|
||||||
to_key(METADATA_SPACE, key_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Migrations {
|
|
||||||
pub(super) fn set_migration_version(&self, version: u32) -> Result<(), DataError> {
|
|
||||||
//Rust cannot type infer this transaction
|
|
||||||
let result: Result<_, sled::transaction::TransactionError<DataError>> =
|
|
||||||
self.0.transaction(|tx| {
|
|
||||||
let key = metadata_key(MIGRATION_KEY);
|
|
||||||
let db_value: U32<LittleEndian> = U32::new(version);
|
|
||||||
tx.insert(key, db_value.as_bytes())?;
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
result?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn get_migration_version(db: &Database) -> Result<u32, DataError> {
|
|
||||||
let key = metadata_key(MIGRATION_KEY);
|
|
||||||
match db.migrations.0.get(key)? {
|
|
||||||
Some(bytes) => convert_u32(&bytes),
|
|
||||||
None => Ok(0),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
use crate::db::errors::DataError;
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use zerocopy::byteorder::{I32, U32};
|
|
||||||
use zerocopy::LayoutVerified;
|
|
||||||
|
|
||||||
/// User variables are stored as little-endian 32-bit integers in the
|
|
||||||
/// database. This type alias makes the database code more pleasant to
|
|
||||||
/// read.
|
|
||||||
type LittleEndianI32Layout<'a> = LayoutVerified<&'a [u8], I32<LittleEndian>>;
|
|
||||||
|
|
||||||
type LittleEndianU32Layout<'a> = LayoutVerified<&'a [u8], U32<LittleEndian>>;
|
|
||||||
|
|
||||||
/// Convert bytes to an i32 with zero-copy deserialization. An error
|
|
||||||
/// is returned if the bytes do not represent an i32.
|
|
||||||
pub(super) fn convert_i32(raw_value: &[u8]) -> Result<i32, DataError> {
|
|
||||||
let layout = LittleEndianI32Layout::new_unaligned(raw_value.as_ref());
|
|
||||||
|
|
||||||
if let Some(layout) = layout {
|
|
||||||
let value: I32<LittleEndian> = *layout;
|
|
||||||
Ok(value.get())
|
|
||||||
} else {
|
|
||||||
Err(DataError::I32SchemaViolation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn convert_u32(raw_value: &[u8]) -> Result<u32, DataError> {
|
|
||||||
let layout = LittleEndianU32Layout::new_unaligned(raw_value.as_ref());
|
|
||||||
|
|
||||||
if let Some(layout) = layout {
|
|
||||||
let value: U32<LittleEndian> = *layout;
|
|
||||||
Ok(value.get())
|
|
||||||
} else {
|
|
||||||
Err(DataError::I32SchemaViolation)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,371 +0,0 @@
|
||||||
use crate::db::errors::DataError;
|
|
||||||
use crate::db::schema::convert_i32;
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use sled::transaction::{abort, TransactionalTree};
|
|
||||||
use sled::Transactional;
|
|
||||||
use sled::Tree;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::convert::From;
|
|
||||||
use std::str;
|
|
||||||
use zerocopy::byteorder::I32;
|
|
||||||
use zerocopy::AsBytes;
|
|
||||||
|
|
||||||
pub(super) mod migrations;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Variables {
|
|
||||||
//room id + username + variable = i32
|
|
||||||
pub(in crate::db) room_user_variables: Tree,
|
|
||||||
|
|
||||||
//room id + username = i32
|
|
||||||
pub(in crate::db) room_user_variable_count: Tree,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Request soemthing by a username and room ID.
|
|
||||||
pub struct UserAndRoom<'a>(pub &'a str, pub &'a str);
|
|
||||||
|
|
||||||
fn to_vec(value: &UserAndRoom<'_>) -> Vec<u8> {
|
|
||||||
let mut bytes = vec![];
|
|
||||||
bytes.extend_from_slice(value.0.as_bytes());
|
|
||||||
bytes.push(0xfe);
|
|
||||||
bytes.extend_from_slice(value.1.as_bytes());
|
|
||||||
bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<UserAndRoom<'_>> for Vec<u8> {
|
|
||||||
fn from(value: UserAndRoom) -> Vec<u8> {
|
|
||||||
to_vec(&value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&UserAndRoom<'_>> for Vec<u8> {
|
|
||||||
fn from(value: &UserAndRoom) -> Vec<u8> {
|
|
||||||
to_vec(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Use a transaction to atomically alter the count of variables in
|
|
||||||
/// the database by the given amount. Count cannot go below 0.
|
|
||||||
fn alter_room_variable_count(
|
|
||||||
room_variable_count: &TransactionalTree,
|
|
||||||
user_and_room: &UserAndRoom<'_>,
|
|
||||||
amount: i32,
|
|
||||||
) -> Result<i32, DataError> {
|
|
||||||
let key: Vec<u8> = user_and_room.into();
|
|
||||||
|
|
||||||
let mut new_count = match room_variable_count.get(&key)? {
|
|
||||||
Some(bytes) => convert_i32(&bytes)? + amount,
|
|
||||||
None => amount,
|
|
||||||
};
|
|
||||||
|
|
||||||
if new_count < 0 {
|
|
||||||
new_count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
let db_value: I32<LittleEndian> = I32::new(new_count);
|
|
||||||
room_variable_count.insert(key, db_value.as_bytes())?;
|
|
||||||
Ok(new_count)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Variables {
|
|
||||||
pub(in crate::db) fn new(db: &sled::Db) -> Result<Variables, sled::Error> {
|
|
||||||
Ok(Variables {
|
|
||||||
room_user_variables: db.open_tree("variables")?,
|
|
||||||
room_user_variable_count: db.open_tree("room_user_variable_count")?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_user_variables(
|
|
||||||
&self,
|
|
||||||
key: &UserAndRoom<'_>,
|
|
||||||
) -> Result<HashMap<String, i32>, DataError> {
|
|
||||||
let mut prefix: Vec<u8> = key.into();
|
|
||||||
prefix.push(0xff);
|
|
||||||
let prefix_len: usize = prefix.len();
|
|
||||||
|
|
||||||
let variables: Result<Vec<_>, DataError> = self
|
|
||||||
.room_user_variables
|
|
||||||
.scan_prefix(prefix)
|
|
||||||
.map(|entry| match entry {
|
|
||||||
Ok((key, raw_value)) => {
|
|
||||||
//Strips room and username from key, leaving behind name.
|
|
||||||
let variable_name = str::from_utf8(&key[prefix_len..])?;
|
|
||||||
Ok((variable_name.to_owned(), convert_i32(&raw_value)?))
|
|
||||||
}
|
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
//Convert I32 to hash map. collect() inferred via return type.
|
|
||||||
variables.map(|entries| entries.into_iter().collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_variable_count(&self, user_and_room: &UserAndRoom<'_>) -> Result<i32, DataError> {
|
|
||||||
let key: Vec<u8> = user_and_room.into();
|
|
||||||
|
|
||||||
match self.room_user_variable_count.get(&key)? {
|
|
||||||
Some(raw_value) => convert_i32(&raw_value),
|
|
||||||
None => Ok(0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_user_variable(
|
|
||||||
&self,
|
|
||||||
user_and_room: &UserAndRoom<'_>,
|
|
||||||
variable_name: &str,
|
|
||||||
) -> Result<i32, DataError> {
|
|
||||||
let mut key: Vec<u8> = user_and_room.into();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(variable_name.as_bytes());
|
|
||||||
|
|
||||||
match self.room_user_variables.get(&key)? {
|
|
||||||
Some(raw_value) => convert_i32(&raw_value),
|
|
||||||
_ => Err(DataError::KeyDoesNotExist(variable_name.to_owned())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_user_variable(
|
|
||||||
&self,
|
|
||||||
user_and_room: &UserAndRoom<'_>,
|
|
||||||
variable_name: &str,
|
|
||||||
value: i32,
|
|
||||||
) -> Result<(), DataError> {
|
|
||||||
if self.get_variable_count(user_and_room)? >= 100 {
|
|
||||||
return Err(DataError::TooManyEntries);
|
|
||||||
}
|
|
||||||
|
|
||||||
(&self.room_user_variables, &self.room_user_variable_count).transaction(
|
|
||||||
|(tx_vars, tx_counts)| {
|
|
||||||
let mut key: Vec<u8> = user_and_room.into();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(variable_name.as_bytes());
|
|
||||||
|
|
||||||
let db_value: I32<LittleEndian> = I32::new(value);
|
|
||||||
let old_value = tx_vars.insert(key, db_value.as_bytes())?;
|
|
||||||
|
|
||||||
//Only increment variable count on new keys.
|
|
||||||
if let None = old_value {
|
|
||||||
if let Err(e) = alter_room_variable_count(&tx_counts, &user_and_room, 1) {
|
|
||||||
return abort(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_user_variable(
|
|
||||||
&self,
|
|
||||||
user_and_room: &UserAndRoom<'_>,
|
|
||||||
variable_name: &str,
|
|
||||||
) -> Result<(), DataError> {
|
|
||||||
(&self.room_user_variables, &self.room_user_variable_count).transaction(
|
|
||||||
|(tx_vars, tx_counts)| {
|
|
||||||
let mut key: Vec<u8> = user_and_room.into();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(variable_name.as_bytes());
|
|
||||||
|
|
||||||
//TODO why does tx.remove require moving the key?
|
|
||||||
if let Some(_) = tx_vars.remove(key.clone())? {
|
|
||||||
if let Err(e) = alter_room_variable_count(&tx_counts, user_and_room, -1) {
|
|
||||||
return abort(e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return abort(DataError::KeyDoesNotExist(variable_name.to_owned()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use sled::Config;
|
|
||||||
|
|
||||||
fn create_test_instance() -> Variables {
|
|
||||||
let config = Config::new().temporary(true);
|
|
||||||
let db = config.open().unwrap();
|
|
||||||
Variables::new(&db).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
//Room Variable count tests
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn alter_room_variable_count_test() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
let alter_count = |amount: i32| {
|
|
||||||
variables
|
|
||||||
.room_user_variable_count
|
|
||||||
.transaction(|tx| match alter_room_variable_count(&tx, &key, amount) {
|
|
||||||
Err(e) => abort(e),
|
|
||||||
_ => Ok(()),
|
|
||||||
})
|
|
||||||
.expect("got transaction failure");
|
|
||||||
};
|
|
||||||
|
|
||||||
let get_count = |variables: &Variables| -> i32 {
|
|
||||||
variables
|
|
||||||
.get_variable_count(&key)
|
|
||||||
.expect("could not get variable count")
|
|
||||||
};
|
|
||||||
|
|
||||||
//addition
|
|
||||||
alter_count(5);
|
|
||||||
assert_eq!(5, get_count(&variables));
|
|
||||||
|
|
||||||
//subtraction
|
|
||||||
alter_count(-3);
|
|
||||||
assert_eq!(2, get_count(&variables));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn alter_room_variable_count_cannot_go_below_0_test() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.room_user_variable_count
|
|
||||||
.transaction(|tx| match alter_room_variable_count(&tx, &key, -1000) {
|
|
||||||
Err(e) => abort(e),
|
|
||||||
_ => Ok(()),
|
|
||||||
})
|
|
||||||
.expect("got transaction failure");
|
|
||||||
|
|
||||||
let count = variables
|
|
||||||
.get_variable_count(&key)
|
|
||||||
.expect("could not get variable count");
|
|
||||||
|
|
||||||
assert_eq!(0, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn empty_db_reports_0_room_variable_count_test() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
let count = variables
|
|
||||||
.get_variable_count(&key)
|
|
||||||
.expect("could not get variable count");
|
|
||||||
|
|
||||||
assert_eq!(0, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_user_variable_increments_count() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, "myvariable", 5)
|
|
||||||
.expect("could not insert variable");
|
|
||||||
|
|
||||||
let count = variables
|
|
||||||
.get_variable_count(&key)
|
|
||||||
.expect("could not get variable count");
|
|
||||||
|
|
||||||
assert_eq!(1, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn update_user_variable_does_not_increment_count() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, "myvariable", 5)
|
|
||||||
.expect("could not insert variable");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, "myvariable", 10)
|
|
||||||
.expect("could not update variable");
|
|
||||||
|
|
||||||
let count = variables
|
|
||||||
.get_variable_count(&key)
|
|
||||||
.expect("could not get variable count");
|
|
||||||
|
|
||||||
assert_eq!(1, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set/get/delete variable tests
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_and_get_variable_test() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, "myvariable", 5)
|
|
||||||
.expect("could not insert variable");
|
|
||||||
|
|
||||||
let value = variables
|
|
||||||
.get_user_variable(&key, "myvariable")
|
|
||||||
.expect("could not get value");
|
|
||||||
|
|
||||||
assert_eq!(5, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn cannot_set_more_than_100_variables_per_room() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
for c in 0..100 {
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, &format!("myvariable{}", c), 5)
|
|
||||||
.expect("could not insert variable");
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = variables.set_user_variable(&key, "myvariable101", 5);
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert!(matches!(result, Err(DataError::TooManyEntries)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn delete_variable_test() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.set_user_variable(&key, "myvariable", 5)
|
|
||||||
.expect("could not insert variable");
|
|
||||||
|
|
||||||
variables
|
|
||||||
.delete_user_variable(&key, "myvariable")
|
|
||||||
.expect("could not delete value");
|
|
||||||
|
|
||||||
let result = variables.get_user_variable(&key, "myvariable");
|
|
||||||
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn get_missing_variable_returns_key_does_not_exist() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
let result = variables.get_user_variable(&key, "myvariable");
|
|
||||||
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn remove_missing_variable_returns_key_does_not_exist() {
|
|
||||||
let variables = create_test_instance();
|
|
||||||
let key = UserAndRoom("username", "room");
|
|
||||||
let result = variables.delete_user_variable(&key, "myvariable");
|
|
||||||
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,354 +0,0 @@
|
||||||
use super::*;
|
|
||||||
use crate::db::errors::{DataError, MigrationError};
|
|
||||||
use crate::db::Database;
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use memmem::{Searcher, TwoWaySearcher};
|
|
||||||
use sled::transaction::TransactionError;
|
|
||||||
use sled::{Batch, IVec};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use zerocopy::byteorder::{I32, U32};
|
|
||||||
use zerocopy::AsBytes;
|
|
||||||
|
|
||||||
pub(in crate::db) mod add_room_user_variable_count {
|
|
||||||
use super::*;
|
|
||||||
//Not to be confused with the super::RoomAndUser delineator.
|
|
||||||
#[derive(PartialEq, Eq, std::hash::Hash)]
|
|
||||||
struct RoomAndUser {
|
|
||||||
room_id: String,
|
|
||||||
username: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a version 0 user variable key.
|
|
||||||
fn v0_variable_key(info: &RoomAndUser, variable_name: &str) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(info.room_id.as_bytes());
|
|
||||||
key.extend_from_slice(info.username.as_bytes());
|
|
||||||
key.extend_from_slice(variable_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_value_to_room_and_user(
|
|
||||||
entry: sled::Result<(IVec, IVec)>,
|
|
||||||
) -> Result<RoomAndUser, MigrationError> {
|
|
||||||
if let Ok((key, _)) = entry {
|
|
||||||
let keys: Vec<Result<&str, _>> = key
|
|
||||||
.split(|&b| b == 0xff)
|
|
||||||
.map(|b| str::from_utf8(b))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if let &[_, Ok(room_id), Ok(username), Ok(_variable)] = keys.as_slice() {
|
|
||||||
Ok(RoomAndUser {
|
|
||||||
room_id: room_id.to_owned(),
|
|
||||||
username: username.to_owned(),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"a key violates utf8 schema".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"encountered unexpected key".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_key(room_id: &str, username: &str) -> Vec<u8> {
|
|
||||||
let mut key = b"variables".to_vec();
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(room_id.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(username.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(b"variable_count");
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(in crate::db) fn migrate(db: &Database) -> Result<(), DataError> {
|
|
||||||
let tree = &db.variables.room_user_variables;
|
|
||||||
let prefix = b"variables";
|
|
||||||
|
|
||||||
//Extract a vec of tuples, consisting of room id + username.
|
|
||||||
let results: Vec<RoomAndUser> = tree
|
|
||||||
.scan_prefix(prefix)
|
|
||||||
.map(map_value_to_room_and_user)
|
|
||||||
.collect::<Result<Vec<_>, MigrationError>>()?;
|
|
||||||
|
|
||||||
let counts: HashMap<RoomAndUser, u32> =
|
|
||||||
results
|
|
||||||
.into_iter()
|
|
||||||
.fold(HashMap::new(), |mut count_map, room_and_user| {
|
|
||||||
let count = count_map.entry(room_and_user).or_insert(0);
|
|
||||||
*count += 1;
|
|
||||||
count_map
|
|
||||||
});
|
|
||||||
|
|
||||||
//Start a transaction on the variables tree.
|
|
||||||
let tx_result: Result<_, TransactionError<DataError>> =
|
|
||||||
db.variables.room_user_variables.transaction(|tx_vars| {
|
|
||||||
let batch = counts.iter().fold(Batch::default(), |mut batch, entry| {
|
|
||||||
let (info, count) = entry;
|
|
||||||
|
|
||||||
//Add variable count according to new schema.
|
|
||||||
let key = create_key(&info.room_id, &info.username);
|
|
||||||
let db_value: U32<LittleEndian> = U32::new(*count);
|
|
||||||
batch.insert(key, db_value.as_bytes());
|
|
||||||
|
|
||||||
//Delete the old variable_count variable if exists.
|
|
||||||
let old_key = v0_variable_key(&info, "variable_count");
|
|
||||||
batch.remove(old_key);
|
|
||||||
batch
|
|
||||||
});
|
|
||||||
|
|
||||||
tx_vars.apply_batch(&batch)?;
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
tx_result?; //For some reason, it cannot infer the type
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(in crate::db) fn delete_v0_schema(db: &Database) -> Result<(), DataError> {
|
|
||||||
let mut vars = db.variables.room_user_variables.scan_prefix("");
|
|
||||||
let mut batch = Batch::default();
|
|
||||||
|
|
||||||
while let Some(Ok((key, _))) = vars.next() {
|
|
||||||
let key = key.to_vec();
|
|
||||||
|
|
||||||
if !key.contains(&0xff) {
|
|
||||||
batch.remove(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db.variables.room_user_variables.apply_batch(batch)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(in crate::db) fn delete_variable_count(db: &Database) -> Result<(), DataError> {
|
|
||||||
let prefix = b"variables";
|
|
||||||
let mut vars = db.variables.room_user_variables.scan_prefix(prefix);
|
|
||||||
let mut batch = Batch::default();
|
|
||||||
|
|
||||||
while let Some(Ok((key, _))) = vars.next() {
|
|
||||||
let search = TwoWaySearcher::new(b"variable_count");
|
|
||||||
let ends_with = {
|
|
||||||
match search.search_in(&key) {
|
|
||||||
Some(index) => key.len() - index == b"variable_count".len(),
|
|
||||||
None => false,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if ends_with {
|
|
||||||
batch.remove(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db.variables.room_user_variables.apply_batch(batch)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(in crate::db) mod change_delineator_delimiter {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
/// An entry in the room user variables keyspace.
|
|
||||||
struct UserVariableEntry {
|
|
||||||
room_id: String,
|
|
||||||
username: String,
|
|
||||||
variable_name: String,
|
|
||||||
value: IVec,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract keys and values from the variables keyspace according
|
|
||||||
/// to the v1 schema.
|
|
||||||
fn extract_v1_entries(
|
|
||||||
entry: sled::Result<(IVec, IVec)>,
|
|
||||||
) -> Result<UserVariableEntry, MigrationError> {
|
|
||||||
if let Ok((key, value)) = entry {
|
|
||||||
let keys: Vec<Result<&str, _>> = key
|
|
||||||
.split(|&b| b == 0xff)
|
|
||||||
.map(|b| str::from_utf8(b))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if let &[_, Ok(room_id), Ok(username), Ok(variable)] = keys.as_slice() {
|
|
||||||
Ok(UserVariableEntry {
|
|
||||||
room_id: room_id.to_owned(),
|
|
||||||
username: username.to_owned(),
|
|
||||||
variable_name: variable.to_owned(),
|
|
||||||
value: value,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"a key violates utf8 schema".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"encountered unexpected key".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an old key, where delineator is separated by 0xff.
|
|
||||||
fn create_old_key(prefix: &[u8], insert: &UserVariableEntry) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(&prefix); //prefix already has 0xff.
|
|
||||||
key.extend_from_slice(&insert.room_id.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.username.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.variable_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an old key, where delineator is separated by 0xfe.
|
|
||||||
fn create_new_key(prefix: &[u8], insert: &UserVariableEntry) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(&prefix); //prefix already has 0xff.
|
|
||||||
key.extend_from_slice(&insert.room_id.as_bytes());
|
|
||||||
key.push(0xfe);
|
|
||||||
key.extend_from_slice(&insert.username.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.variable_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn migrate(db: &Database) -> Result<(), DataError> {
|
|
||||||
let tree = &db.variables.room_user_variables;
|
|
||||||
let prefix = b"variables";
|
|
||||||
|
|
||||||
let results: Vec<UserVariableEntry> = tree
|
|
||||||
.scan_prefix(&prefix)
|
|
||||||
.map(extract_v1_entries)
|
|
||||||
.collect::<Result<Vec<_>, MigrationError>>()?;
|
|
||||||
|
|
||||||
let mut batch = Batch::default();
|
|
||||||
|
|
||||||
for insert in results {
|
|
||||||
let old = create_old_key(prefix, &insert);
|
|
||||||
let new = create_new_key(prefix, &insert);
|
|
||||||
|
|
||||||
batch.remove(old);
|
|
||||||
batch.insert(new, insert.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
tree.apply_batch(batch)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Move the user variable entries into two tree structures, with yet
|
|
||||||
/// another key format change. Now there is one tree for variable
|
|
||||||
/// counts, and one tree for actual user variables. Keys in the user
|
|
||||||
/// variable tree were changed to be username-first, then room ID.
|
|
||||||
/// They are still separated by 0xfe, while the variable name is
|
|
||||||
/// separated by 0xff. Variable count now stores just
|
|
||||||
/// USERNAME0xfeROOM_ID and a count in its own tree. This enables
|
|
||||||
/// public use of a strongly typed UserAndRoom struct for getting
|
|
||||||
/// variables.
|
|
||||||
pub(in crate::db) mod change_tree_structure {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
/// An entry in the room user variables keyspace.
|
|
||||||
struct UserVariableEntry {
|
|
||||||
room_id: String,
|
|
||||||
username: String,
|
|
||||||
variable_name: String,
|
|
||||||
value: IVec,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract keys and values from the variables keyspace according
|
|
||||||
/// to the v1 schema.
|
|
||||||
fn extract_v1_entries(
|
|
||||||
entry: sled::Result<(IVec, IVec)>,
|
|
||||||
) -> Result<UserVariableEntry, MigrationError> {
|
|
||||||
if let Ok((key, value)) = entry {
|
|
||||||
let keys: Vec<Result<&str, _>> = key
|
|
||||||
.split(|&b| b == 0xff || b == 0xfe)
|
|
||||||
.map(|b| str::from_utf8(b))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if let &[_, Ok(room_id), Ok(username), Ok(variable)] = keys.as_slice() {
|
|
||||||
Ok(UserVariableEntry {
|
|
||||||
room_id: room_id.to_owned(),
|
|
||||||
username: username.to_owned(),
|
|
||||||
variable_name: variable.to_owned(),
|
|
||||||
value: value,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"a key violates utf8 schema".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(MigrationError::MigrationFailed(
|
|
||||||
"encountered unexpected key".to_string(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an old key, of "variables" 0xff "room id" 0xfe "username" 0xff "variablename".
|
|
||||||
fn create_old_key(prefix: &[u8], insert: &UserVariableEntry) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(&prefix); //prefix already has 0xff.
|
|
||||||
key.extend_from_slice(&insert.room_id.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.username.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.variable_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new key, of "username" 0xfe "room id" 0xff "variablename".
|
|
||||||
fn create_new_key(insert: &UserVariableEntry) -> Vec<u8> {
|
|
||||||
let mut key = vec![];
|
|
||||||
key.extend_from_slice(&insert.username.as_bytes());
|
|
||||||
key.push(0xfe);
|
|
||||||
key.extend_from_slice(&insert.room_id.as_bytes());
|
|
||||||
key.push(0xff);
|
|
||||||
key.extend_from_slice(&insert.variable_name.as_bytes());
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn migrate(db: &Database) -> Result<(), DataError> {
|
|
||||||
let variables_tree = &db.variables.room_user_variables;
|
|
||||||
let count_tree = &db.variables.room_user_variable_count;
|
|
||||||
let prefix = b"variables";
|
|
||||||
|
|
||||||
let results: Vec<UserVariableEntry> = variables_tree
|
|
||||||
.scan_prefix(&prefix)
|
|
||||||
.map(extract_v1_entries)
|
|
||||||
.collect::<Result<Vec<_>, MigrationError>>()?;
|
|
||||||
|
|
||||||
let mut counts: HashMap<(String, String), i32> = HashMap::new();
|
|
||||||
let mut batch = Batch::default();
|
|
||||||
|
|
||||||
for insert in results {
|
|
||||||
let count = counts
|
|
||||||
.entry((insert.username.clone(), insert.room_id.clone()))
|
|
||||||
.or_insert(0);
|
|
||||||
*count += 1;
|
|
||||||
|
|
||||||
let old = create_old_key(prefix, &insert);
|
|
||||||
let new = create_new_key(&insert);
|
|
||||||
|
|
||||||
batch.remove(old);
|
|
||||||
batch.insert(new, insert.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut count_batch = Batch::default();
|
|
||||||
counts.into_iter().for_each(|((username, room_id), count)| {
|
|
||||||
let mut key = username.as_bytes().to_vec();
|
|
||||||
key.push(0xfe);
|
|
||||||
key.extend_from_slice(room_id.as_bytes());
|
|
||||||
|
|
||||||
let db_value: I32<LittleEndian> = I32::new(count);
|
|
||||||
count_batch.insert(key, db_value.as_bytes());
|
|
||||||
});
|
|
||||||
|
|
||||||
variables_tree.apply_batch(batch)?;
|
|
||||||
count_tree.apply_batch(count_batch)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,170 +0,0 @@
|
||||||
use nom::{
|
|
||||||
alt, bytes::complete::tag, character::complete::digit1, complete, many0, named,
|
|
||||||
sequence::tuple, tag, IResult,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::dice::{Dice, Element, ElementExpression, SignedElement};
|
|
||||||
use crate::parser::eat_whitespace;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
||||||
enum Sign {
|
|
||||||
Plus,
|
|
||||||
Minus,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a dice expression. Does not eat whitespace
|
|
||||||
fn parse_dice(input: &str) -> IResult<&str, Dice> {
|
|
||||||
let (input, (count, _, sides)) = tuple((digit1, tag("d"), digit1))(input)?;
|
|
||||||
Ok((
|
|
||||||
input,
|
|
||||||
Dice::new(count.parse().unwrap(), sides.parse().unwrap()),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a single digit expression. Does not eat whitespace
|
|
||||||
fn parse_bonus(input: &str) -> IResult<&str, u32> {
|
|
||||||
let (input, bonus) = digit1(input)?;
|
|
||||||
Ok((input, bonus.parse().unwrap()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a sign expression. Eats whitespace.
|
|
||||||
fn parse_sign(input: &str) -> IResult<&str, Sign> {
|
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
named!(sign(&str) -> Sign, alt!(
|
|
||||||
complete!(tag!("+")) => { |_| Sign::Plus } |
|
|
||||||
complete!(tag!("-")) => { |_| Sign::Minus }
|
|
||||||
));
|
|
||||||
|
|
||||||
let (input, sign) = sign(input)?;
|
|
||||||
Ok((input, sign))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse an element expression. Eats whitespace.
|
|
||||||
fn parse_element(input: &str) -> IResult<&str, Element> {
|
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
named!(element(&str) -> Element, alt!(
|
|
||||||
parse_dice => { |d| Element::Dice(d) } |
|
|
||||||
parse_bonus => { |b| Element::Bonus(b) }
|
|
||||||
));
|
|
||||||
|
|
||||||
let (input, element) = element(input)?;
|
|
||||||
Ok((input, element))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a signed element expression. Eats whitespace.
|
|
||||||
fn parse_signed_element(input: &str) -> IResult<&str, SignedElement> {
|
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
let (input, sign) = parse_sign(input)?;
|
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
|
|
||||||
let (input, element) = parse_element(input)?;
|
|
||||||
let element = match sign {
|
|
||||||
Sign::Plus => SignedElement::Positive(element),
|
|
||||||
Sign::Minus => SignedElement::Negative(element),
|
|
||||||
};
|
|
||||||
Ok((input, element))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a full element expression. Eats whitespace.
|
|
||||||
pub fn parse_element_expression(input: &str) -> IResult<&str, ElementExpression> {
|
|
||||||
named!(first_element(&str) -> SignedElement, alt!(
|
|
||||||
parse_signed_element => { |e| e } |
|
|
||||||
parse_element => { |e| SignedElement::Positive(e) }
|
|
||||||
));
|
|
||||||
let (input, first) = first_element(input)?;
|
|
||||||
let (input, rest) = if input.trim().is_empty() {
|
|
||||||
(input, vec![first])
|
|
||||||
} else {
|
|
||||||
named!(rest_elements(&str) -> Vec<SignedElement>, many0!(parse_signed_element));
|
|
||||||
let (input, mut rest) = rest_elements(input)?;
|
|
||||||
rest.insert(0, first);
|
|
||||||
(input, rest)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((input, ElementExpression(rest)))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[test]
|
|
||||||
fn dice_test() {
|
|
||||||
assert_eq!(parse_dice("2d4"), Ok(("", Dice::new(2, 4))));
|
|
||||||
assert_eq!(parse_dice("20d40"), Ok(("", Dice::new(20, 40))));
|
|
||||||
assert_eq!(parse_dice("8d7"), Ok(("", Dice::new(8, 7))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn element_test() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_element(" \t\n\r\n 8d7 \n"),
|
|
||||||
Ok((" \n", Element::Dice(Dice::new(8, 7))))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_element(" \t\n\r\n 8 \n"),
|
|
||||||
Ok((" \n", Element::Bonus(8)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn signed_element_test() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_signed_element("+ 7"),
|
|
||||||
Ok(("", SignedElement::Positive(Element::Bonus(7))))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_signed_element(" \t\n\r\n- 8 \n"),
|
|
||||||
Ok((" \n", SignedElement::Negative(Element::Bonus(8))))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_signed_element(" \t\n\r\n- 8d4 \n"),
|
|
||||||
Ok((
|
|
||||||
" \n",
|
|
||||||
SignedElement::Negative(Element::Dice(Dice::new(8, 4)))
|
|
||||||
))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_signed_element(" \t\n\r\n+ 8d4 \n"),
|
|
||||||
Ok((
|
|
||||||
" \n",
|
|
||||||
SignedElement::Positive(Element::Dice(Dice::new(8, 4)))
|
|
||||||
))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn element_expression_test() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_element_expression("8d4"),
|
|
||||||
Ok((
|
|
||||||
"",
|
|
||||||
ElementExpression(vec![SignedElement::Positive(Element::Dice(Dice::new(
|
|
||||||
8, 4
|
|
||||||
)))])
|
|
||||||
))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_element_expression(" - 8d4 \n "),
|
|
||||||
Ok((
|
|
||||||
" \n ",
|
|
||||||
ElementExpression(vec![SignedElement::Negative(Element::Dice(Dice::new(
|
|
||||||
8, 4
|
|
||||||
)))])
|
|
||||||
))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
parse_element_expression("\t3d4 + 7 - 5 - 6d12 + 1d1 + 53 1d5 "),
|
|
||||||
Ok((
|
|
||||||
" 1d5 ",
|
|
||||||
ElementExpression(vec![
|
|
||||||
SignedElement::Positive(Element::Dice(Dice::new(3, 4))),
|
|
||||||
SignedElement::Positive(Element::Bonus(7)),
|
|
||||||
SignedElement::Negative(Element::Bonus(5)),
|
|
||||||
SignedElement::Negative(Element::Dice(Dice::new(6, 12))),
|
|
||||||
SignedElement::Positive(Element::Dice(Dice::new(1, 1))),
|
|
||||||
SignedElement::Positive(Element::Bonus(53)),
|
|
||||||
])
|
|
||||||
))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
70
src/error.rs
70
src/error.rs
|
@ -1,70 +0,0 @@
|
||||||
use crate::cofd::dice::DiceRollingError;
|
|
||||||
use crate::commands::CommandError;
|
|
||||||
use crate::config::ConfigError;
|
|
||||||
use crate::db::errors::DataError;
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum BotError {
|
|
||||||
#[error("configuration error: {0}")]
|
|
||||||
ConfigurationError(#[from] ConfigError),
|
|
||||||
|
|
||||||
/// Sync token couldn't be found.
|
|
||||||
#[error("the sync token could not be retrieved")]
|
|
||||||
SyncTokenRequired,
|
|
||||||
|
|
||||||
#[error("command error: {0}")]
|
|
||||||
CommandError(#[from] CommandError),
|
|
||||||
|
|
||||||
#[error("database error: {0}")]
|
|
||||||
DataError(#[from] DataError),
|
|
||||||
|
|
||||||
#[error("the message should not be processed because it failed validation")]
|
|
||||||
ShouldNotProcessError,
|
|
||||||
|
|
||||||
#[error("no cache directory found")]
|
|
||||||
NoCacheDirectoryError,
|
|
||||||
|
|
||||||
#[error("could not parse URL")]
|
|
||||||
UrlParseError(#[from] url::ParseError),
|
|
||||||
|
|
||||||
#[error("uncategorized matrix SDK error")]
|
|
||||||
MatrixError(#[from] matrix_sdk::Error),
|
|
||||||
|
|
||||||
#[error("uncategorized matrix SDK base error")]
|
|
||||||
MatrixBaseError(#[from] matrix_sdk::BaseError),
|
|
||||||
|
|
||||||
#[error("future canceled")]
|
|
||||||
FutureCanceledError,
|
|
||||||
|
|
||||||
//de = deserialization
|
|
||||||
#[error("toml parsing error")]
|
|
||||||
TomlParsingError(#[from] toml::de::Error),
|
|
||||||
|
|
||||||
#[error("i/o error: {0}")]
|
|
||||||
IoError(#[from] std::io::Error),
|
|
||||||
|
|
||||||
#[error("dice parsing error: {0}")]
|
|
||||||
DiceParsingError(#[from] crate::parser::DiceParsingError),
|
|
||||||
|
|
||||||
#[error("command parsing error: {0}")]
|
|
||||||
CommandParsingError(#[from] crate::commands::parser::CommandParsingError),
|
|
||||||
|
|
||||||
#[error("dice pool roll error: {0}")]
|
|
||||||
DiceRollingError(#[from] DiceRollingError),
|
|
||||||
|
|
||||||
#[error("variable parsing error: {0}")]
|
|
||||||
VariableParsingError(#[from] crate::variables::VariableParsingError),
|
|
||||||
|
|
||||||
#[error("legacy parsing error")]
|
|
||||||
NomParserError(nom::error::ErrorKind),
|
|
||||||
|
|
||||||
#[error("legacy parsing error: not enough data")]
|
|
||||||
NomParserIncomplete,
|
|
||||||
|
|
||||||
#[error("variables not yet supported")]
|
|
||||||
VariablesNotSupported,
|
|
||||||
|
|
||||||
#[error("database error")]
|
|
||||||
DatabaseErrror(#[from] sled::Error),
|
|
||||||
}
|
|
88
src/help.rs
88
src/help.rs
|
@ -1,88 +0,0 @@
|
||||||
use indoc::indoc;
|
|
||||||
|
|
||||||
pub fn parse_help_topic(input: &str) -> Option<HelpTopic> {
|
|
||||||
match input {
|
|
||||||
"cofd" => Some(HelpTopic::ChroniclesOfDarkness),
|
|
||||||
"dicepool" => Some(HelpTopic::DicePool),
|
|
||||||
"dice" => Some(HelpTopic::RollingDice),
|
|
||||||
"" => Some(HelpTopic::General),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum HelpTopic {
|
|
||||||
ChroniclesOfDarkness,
|
|
||||||
DicePool,
|
|
||||||
RollingDice,
|
|
||||||
General,
|
|
||||||
}
|
|
||||||
|
|
||||||
const COFD_HELP: &'static str = indoc! {"
|
|
||||||
Chronicles of Darkness
|
|
||||||
|
|
||||||
Commands available:
|
|
||||||
!pool, !rp: roll a dice pool
|
|
||||||
!chance: roll a chance die
|
|
||||||
|
|
||||||
See also:
|
|
||||||
!help dicepool
|
|
||||||
"};
|
|
||||||
|
|
||||||
const DICE_HELP: &'static str = indoc! {"
|
|
||||||
Rolling basic dice
|
|
||||||
|
|
||||||
Command: !roll, !r
|
|
||||||
|
|
||||||
Syntax !roll <dice-expression>
|
|
||||||
|
|
||||||
Dice expression can be a basic die (e.g. 1d4), with a bonus (1d4+3),
|
|
||||||
or a more complex series of dice rolls or arbitrary numbers.
|
|
||||||
Parentheses are not supported.
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
!roll 1d4
|
|
||||||
!roll 1d4+5
|
|
||||||
!roll 2d6+8
|
|
||||||
!roll 2d8 + 4d6 - 3
|
|
||||||
"};
|
|
||||||
|
|
||||||
const DICEPOOL_HELP: &'static str = indoc! {"
|
|
||||||
Rolling dice pools
|
|
||||||
|
|
||||||
Command: !pool, !rp
|
|
||||||
|
|
||||||
Syntax: !pool <modifiers>:<num>
|
|
||||||
|
|
||||||
Short syntax: !pool <num>
|
|
||||||
|
|
||||||
Modifiers:
|
|
||||||
n = nine-again
|
|
||||||
e = eight-again
|
|
||||||
r = rote quality
|
|
||||||
x = do not re-roll 10s
|
|
||||||
s<num> = number of successes for exceptional
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
!pool 8 (roll a regular pool of 8 dice)
|
|
||||||
!pool n:5 (roll dice pool of 5, nine-again)
|
|
||||||
!pool rs3:6 (roll dice pool of 6, rote quality, 3 successes for exceptional)
|
|
||||||
"};
|
|
||||||
|
|
||||||
const GENERAL_HELP: &'static str = indoc! {"
|
|
||||||
General Help
|
|
||||||
|
|
||||||
Try these help commands:
|
|
||||||
!help cofd
|
|
||||||
!help dice
|
|
||||||
"};
|
|
||||||
|
|
||||||
impl HelpTopic {
|
|
||||||
pub fn message(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
HelpTopic::ChroniclesOfDarkness => COFD_HELP,
|
|
||||||
HelpTopic::DicePool => DICEPOOL_HELP,
|
|
||||||
HelpTopic::RollingDice => DICE_HELP,
|
|
||||||
HelpTopic::General => GENERAL_HELP,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
194
src/parser.rs
194
src/parser.rs
|
@ -1,194 +0,0 @@
|
||||||
use combine::parser::char::{digit, letter, spaces};
|
|
||||||
use combine::{many, many1, one_of, Parser};
|
|
||||||
use nom::{bytes::complete::take_while, IResult};
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
//******************************
|
|
||||||
//New hotness
|
|
||||||
//******************************
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Error)]
|
|
||||||
pub enum DiceParsingError {
|
|
||||||
#[error("invalid amount")]
|
|
||||||
InvalidAmount,
|
|
||||||
|
|
||||||
#[error("modifiers not specified properly")]
|
|
||||||
InvalidModifiers,
|
|
||||||
|
|
||||||
#[error("extraneous input detected")]
|
|
||||||
UnconsumedInput,
|
|
||||||
|
|
||||||
#[error("parser error: {0}")]
|
|
||||||
InternalParseError(#[from] combine::error::StringStreamError),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
||||||
pub enum Operator {
|
|
||||||
Plus,
|
|
||||||
Minus,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Operator {
|
|
||||||
pub fn mult(&self) -> i32 {
|
|
||||||
match self {
|
|
||||||
Operator::Plus => 1,
|
|
||||||
Operator::Minus => -1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
||||||
pub enum Element {
|
|
||||||
Variable(String),
|
|
||||||
Number(i32),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
||||||
pub struct Amount {
|
|
||||||
pub operator: Operator,
|
|
||||||
pub element: Element,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse an expression of numbers and/or variables into elements
|
|
||||||
/// coupled with operators, where an operator is "+" or "-", and an
|
|
||||||
/// element is either a number or variable name. The first element
|
|
||||||
/// should not have an operator, but every one after that should.
|
|
||||||
/// Accepts expressions like "8", "10 + variablename", "variablename -
|
|
||||||
/// 3", etc. This function is currently common to systems that don't
|
|
||||||
/// deal with XdY rolls. Support for that will be added later. Parsers
|
|
||||||
/// utilzing this function should layer their own checks on top of
|
|
||||||
/// this; perhaps they do not want more than one expression, or some
|
|
||||||
/// other rules.
|
|
||||||
pub fn parse_amounts(input: &str) -> Result<Vec<Amount>, DiceParsingError> {
|
|
||||||
let input = input.trim();
|
|
||||||
|
|
||||||
let plus_or_minus = one_of("+-".chars());
|
|
||||||
let maybe_sign = plus_or_minus.map(|sign: char| match sign {
|
|
||||||
'+' => Operator::Plus,
|
|
||||||
'-' => Operator::Minus,
|
|
||||||
_ => Operator::Plus,
|
|
||||||
});
|
|
||||||
|
|
||||||
//TODO make this a macro or something
|
|
||||||
let first = many1(letter())
|
|
||||||
.or(many1(digit()))
|
|
||||||
.skip(spaces().silent()) //Consume any space after first amount
|
|
||||||
.map(|value: String| match value.parse::<i32>() {
|
|
||||||
Ok(num) => Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Number(num),
|
|
||||||
},
|
|
||||||
_ => Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Variable(value),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
let variable_or_number =
|
|
||||||
many1(letter())
|
|
||||||
.or(many1(digit()))
|
|
||||||
.map(|value: String| match value.parse::<i32>() {
|
|
||||||
Ok(num) => Element::Number(num),
|
|
||||||
_ => Element::Variable(value),
|
|
||||||
});
|
|
||||||
|
|
||||||
let sign_and_word = maybe_sign
|
|
||||||
.skip(spaces().silent())
|
|
||||||
.and(variable_or_number)
|
|
||||||
.skip(spaces().silent())
|
|
||||||
.map(|parsed: (Operator, Element)| Amount {
|
|
||||||
operator: parsed.0,
|
|
||||||
element: parsed.1,
|
|
||||||
});
|
|
||||||
|
|
||||||
let rest = many(sign_and_word).map(|expr: Vec<_>| expr);
|
|
||||||
|
|
||||||
let mut parser = first.and(rest);
|
|
||||||
|
|
||||||
//Maps the found expression into a Vec of Amount instances,
|
|
||||||
//tacking the first one on.
|
|
||||||
type ParsedAmountExpr = (Amount, Vec<Amount>);
|
|
||||||
let (results, rest) = parser
|
|
||||||
.parse(input)
|
|
||||||
.map(|mut results: (ParsedAmountExpr, &str)| {
|
|
||||||
let mut amounts = vec![(results.0).0];
|
|
||||||
amounts.append(&mut (results.0).1);
|
|
||||||
(amounts, results.1)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if rest.len() == 0 {
|
|
||||||
Ok(results)
|
|
||||||
} else {
|
|
||||||
Err(DiceParsingError::UnconsumedInput)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//******************************
|
|
||||||
//Legacy Code
|
|
||||||
//******************************
|
|
||||||
|
|
||||||
fn is_whitespace(input: char) -> bool {
|
|
||||||
input == ' ' || input == '\n' || input == '\t' || input == '\r'
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Eat whitespace, returning it
|
|
||||||
pub fn eat_whitespace(input: &str) -> IResult<&str, &str> {
|
|
||||||
let (input, whitespace) = take_while(is_whitespace)(input)?;
|
|
||||||
Ok((input, whitespace))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_single_number_amount_test() {
|
|
||||||
let result = parse_amounts("1");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
result.unwrap(),
|
|
||||||
vec![Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Number(1)
|
|
||||||
}]
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = parse_amounts("10");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
result.unwrap(),
|
|
||||||
vec![Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Number(10)
|
|
||||||
}]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_single_variable_amount_test() {
|
|
||||||
let result = parse_amounts("asdf");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
result.unwrap(),
|
|
||||||
vec![Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Variable("asdf".to_string())
|
|
||||||
}]
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = parse_amounts("nosis");
|
|
||||||
assert!(result.is_ok());
|
|
||||||
assert_eq!(
|
|
||||||
result.unwrap(),
|
|
||||||
vec![Amount {
|
|
||||||
operator: Operator::Plus,
|
|
||||||
element: Element::Variable("nosis".to_string())
|
|
||||||
}]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_complex_amount_expression() {
|
|
||||||
assert!(parse_amounts("1 + myvariable - 2").is_ok());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue