Commit Graph

188 Commits

Author SHA1 Message Date
projectmoon b3c4d8a38c Centralize plain text formatting at point of message sending.
Instead of relying on all parts of the application to construct both
HTML and plain-text responses, we now construct only HTML responses,
and convert the HTML to plain text right before sending the message to
Matrix.

This is a first iteration, because the plain text has a few extra
newlines than it should, created by use of nested <p> tags.
2021-01-31 14:12:09 +00:00
projectmoon a4e66a0ca6 Basic output for multiple command failures.
Also replace newlines with <br/>s in HTML output.
2021-01-31 08:15:22 +00:00
projectmoon d0c6ca3de8 Print out how many commands failed in a multi-command scenario.
The number of failing commands are now printed out when at least one
command in a multi-command execution fails. This commit does not
introduce printing out WHICH commands failed, nor their error
messages.

There was also some minor refactoring to move command response
handling into their own functions (one for single response, one for
multiple) so that the code is more readable.
2021-01-30 22:13:06 +00:00
projectmoon 055bad3a46 Move a type on collect() to variable assignment 2021-01-30 22:12:44 +00:00
projectmoon 16eb87e50f Convert command execution to use results. 2021-01-30 14:28:14 +00:00
projectmoon 1b0003ff1b Upgrade to Matrix SDK latest (Store Rewrite) and Tokio 1.0
This upgrade introduces a handful of breaking changes in the Rust
Matrix SDK.
 - Some types have disappeared and changed name.
 - Some functions are no longer async.
 - Room display name now has a Result type instead of just returning
   the value.
 - Client state store has breaking changes (not really a big deal).

This required introduction of a new type to store room information
that we are interested in on the context struct. This new RoomContext
is required mostly due to unit tests, because it is no longer possible
to instantiate the Room type in the Matrix SDK.
2021-01-30 12:54:47 +00:00
projectmoon 297a8454f6 Avoid cloning when counting successes. 2020-12-18 14:16:22 +00:00
projectmoon c9c80b974c Tests for dice pool formatting. 2020-12-17 21:14:43 +00:00
projectmoon eb42704380 Switch to into_iter instead of a non-consuming iterator. 2020-12-17 21:06:43 +00:00
projectmoon f355fad06b Rename some variables for consistency.
Fixes #50.
2020-12-17 20:59:29 +00:00
projectmoon 23cf9e6ba4 Show all rolls if we are below the max amount shown (15 dice). 2020-12-17 20:54:01 +00:00
projectmoon e177da9c25 Centralize record_room_information function. 2020-11-30 19:53:26 +00:00
projectmoon a65084e04a Unit test for updating room info data. 2020-11-29 21:33:46 +00:00
projectmoon 979dc8ea34 Clearer test name for clearing room info. 2020-11-29 21:29:35 +00:00
projectmoon 0b2246cbd5 Unit tests for inserting and loading room info from db. 2020-11-29 21:29:13 +00:00
projectmoon 7e1abab66d Resync command now uses record_room_information. 2020-11-29 21:18:41 +00:00
projectmoon 569ba4f2e0 Carry full room instance in context instead of just room id. 2020-11-29 21:03:45 +00:00
projectmoon 118e4b00c7 Store room info when joining a room. 2020-11-29 17:06:04 +00:00
projectmoon c8c38ac1d4 Avoid nested map when retrieving room info from db. 2020-11-29 16:55:23 +00:00
projectmoon 91cfc52e5b Change record_users_in_room to record_room_information. 2020-11-29 14:02:40 +00:00
projectmoon 224f8cd0f1 Functions for storing RoomInfo in db. Refactor bot joins room event.
Add get/insert functions for RoomInfo in the rooms db.

Move 'bot joins room' code to single method, so we can also record a
RoomInfo struct into the database.
2020-11-29 14:00:05 +00:00
projectmoon 68db038336 Properly avoid allocation for our_username in resync command. 2020-11-23 19:54:20 +00:00
projectmoon 18352c8c19 Filter out our username when resyncing (with an allocation). 2020-11-22 22:13:11 +00:00
projectmoon dda0d74f45 Implement resync command without filtering ourselves out. 2020-11-22 21:30:24 +00:00
projectmoon f46b914239 Add matrix client to context. 2020-11-22 20:52:44 +00:00
projectmoon f352c90b6b Return error on unrecognized commands. 2020-11-12 21:05:14 +00:00
projectmoon e251294b5f Only execute lines with commands.
Fixes #45 and #46.
2020-11-12 20:22:09 +00:00
projectmoon 0e04e67f6e Log debug instead of trace for timestamp index inserts. 2020-11-10 20:37:14 +00:00
projectmoon 551f21a49a Fix minor typo in rooms db code. 2020-11-10 20:22:26 +00:00
projectmoon 9ed2a81dd3 Record all users in room when joining. 2020-11-10 20:18:00 +00:00
projectmoon 0939feee84 Placeholder to record all user info when joining room 2020-11-09 21:17:03 +00:00
projectmoon 9349dd5f00 Add event processing check to received messages.
Also rename the existing `should_process` function to be more clear,
given presence of another similarly named function:
should_process_message.
2020-11-09 21:16:20 +00:00
projectmoon 74d0b88e80 Add should process check to room member event 2020-11-09 21:16:07 +00:00
projectmoon fb24090952 Keep seen events in database, don't process already-seen events.
Adds a new function `should_process` to rooms impl that determines if
calling could should proceed with processing an event ID. Event IDs
are recorded (along with room ID) as a key pointing to the
system-local timestamp of when the event was received. If the key was
not originally present, we instruct calling code to process the event.

Events are also asychronously recorded by timestamp using a sled event
watcher that listens to inserts in the main tree (described above).
This secondary tree will allow easy cleanup of old events in the
future.
2020-11-09 21:16:07 +00:00
projectmoon d7aaed9e00 Implement room clearing, log to info for member updates. 2020-11-08 21:47:01 +00:00
projectmoon b5c78bcee5 Remove userandroom struct from rooms db 2020-11-08 21:47:01 +00:00
projectmoon 51ba3e3f42 Record user join/leaves in rooms. Move event emitter to its own file. 2020-11-08 21:47:01 +00:00
projectmoon 8e2f34819e Half implemented room state management foundations. 2020-11-08 21:47:01 +00:00
projectmoon a5dde18899 Update to the latest matrix SDK, and fix the Cargo.toml entry.
By using rev instead of branch, we were somehow stuck on a very old
version of the SDK. The dependency has now been switched to branch
instead of rev, and the SDK updates properly to latest master when
carg update is called.
2020-11-08 21:43:18 +00:00
projectmoon 472f02d153 Execute commands even when surrounded by weird whitespace. 2020-11-05 23:03:22 +00:00
projectmoon 66f9bc6013 Move original dice rolling code into its own 'basic' module.
This gives it parity with the other systems: cofd and cthulhu. More
refactoring and a rewrite later as we trend towards more
system-specific implementations.
2020-11-04 20:46:25 +00:00
projectmoon d2642d1fd3 Less verbose errors from internal dice parsing errors. 2020-11-04 20:33:30 +00:00
projectmoon 39e6eb9b46 Implement support for user variables in CoC dice rolling.
Also comes with reorganization of the dice rolling code to centralize
the variable -> dice amount logic, and changes the way the results of
those rolls are displayed.
2020-11-04 20:33:30 +00:00
projectmoon bf4ce24b79 Better public API for user variables, avoid allocations in Context.
The database API for user variables has changed somewhat again, this
time closer to the proper vision. There are now two separate sled
Trees in the Variables struct, one for user-defined variables, and one
for counts. Keys have been changed to be username-first, then room ID.
The signatures of the functions now also use a strongly-typed struct,
UserAndRoom.

As part of this, the Context object now once again avoids allocating
new strings.

Other random changes included here:
 - Remove tempfile crate in favor of sled temporary db config.
 - Add bincode crate in anticipation of future (de)serializing.
2020-11-03 20:31:29 +00:00
projectmoon a15d3e0bac Fix cthulhu advancement rolls, add remaining dice tests. 2020-11-01 19:29:09 +00:00
projectmoon 7e15379c58 Migrate existing delineators to delimiter 0xfe. 2020-11-01 19:08:54 +00:00
projectmoon 3ccd60c173 Do not respond to or log ignored commands. 2020-11-01 12:20:45 +00:00
projectmoon f4417d4c1a Remove unnecessary Option from parse_command return type. 2020-10-31 21:03:17 +00:00
projectmoon c55926a005 Support modifiers for cthulhu rolls, and add tests. 2020-10-31 20:51:17 +00:00
projectmoon 08b0e58193 Implement parsing of Cthulhu dice, only basic for now.
Does not understand anything besides single numbers at the moment.
2020-10-31 20:51:17 +00:00
projectmoon c290393ddf Centralize common parsing code.
Needed for further development of different systems that rely on these
kind of expressions, and lays groundwork for future changes.
2020-10-31 20:51:17 +00:00
projectmoon e3b819ecb0 Wire up regular cthulhu roll commmand (not yet parsed). 2020-10-31 20:51:17 +00:00
projectmoon 7a302c4489 Extract Ruma error messages from matrix SDK errors. 2020-10-31 20:51:17 +00:00
projectmoon 88db00cc3d Move commands into separate submodules 2020-10-31 20:51:17 +00:00
projectmoon d5034c11dc Basic implementation of CoC dice rolling.
Not hooked up to parser or commands yet.
2020-10-31 20:51:17 +00:00
projectmoon 9f598541fb Remove variable_count keys missed in manual data migration. 2020-10-30 22:44:19 +00:00
projectmoon 5224357f8b Add migration 2: remove old data. 2020-10-30 21:53:22 +00:00
projectmoon 6b21b0aff8 Add string name to migrations 2020-10-30 21:53:22 +00:00
projectmoon 3cec676b2f Clean up migration to make it more readable 2020-10-30 21:53:22 +00:00
projectmoon a53ce85f38 Add db migration support, change variables schema.
This is a bit of a large commit that adds basic database migration
support. It also alters the way user variables are stored in a way
requiring manual migration of existing data. The first automated
migration adds variable count in a new place.
2020-10-30 21:53:22 +00:00
projectmoon 4d9ad42bdd De-asyncify database methods 2020-10-30 21:53:22 +00:00
projectmoon 5410c53513 Split database code into separate modules. 2020-10-30 21:53:22 +00:00
projectmoon 9176e858d5 A bit of database code cleanup. 2020-10-23 20:30:03 +00:00
projectmoon 9f8ef281de Store room user variable count under a room metadata space 2020-10-23 20:30:03 +00:00
projectmoon d482046b9b Actually roll a die when converting to chance die pool. 2020-10-22 23:36:50 +00:00
projectmoon 1a980aa608 Auto-convert dice pools to chance die if below 0. 2020-10-22 23:24:24 +00:00
projectmoon 7f971703e2 Sort variable list when showing all variables.
Fixes #28
2020-10-22 21:24:24 +00:00
projectmoon c10ee5c0f3 Allow negative variables. 2020-10-22 20:47:20 +00:00
projectmoon 0c394d0f79 Get all variables command. 2020-10-22 20:29:37 +00:00
projectmoon 314b0520d9 Support multiple command execution. 2020-10-22 19:54:48 +00:00
projectmoon 2ee8ae2e34 Document the From trait on the DataError type. 2020-10-20 21:10:54 +00:00
projectmoon 114c879c6f Count user variables on a per-room basis. 2020-10-20 21:10:54 +00:00
projectmoon ebfd230f31 Use variables namespace for user variables. 2020-10-18 21:01:52 +00:00
projectmoon 6b7acbe520 Open trees for rooms and variables, but not yet use them. 2020-10-18 21:01:52 +00:00
projectmoon ce82e6ddad Fetch all variables into a map before rolling dice.
Goes from about 30 seconds to do 1 million variable resolutions down
to 18 seconds.
2020-10-18 20:37:02 +00:00
projectmoon d5aae3ebb1 Fully async dice rolling. Also remove more unnecessary stuff. 2020-10-18 20:37:02 +00:00
projectmoon 3c2a37c0f7 Make command execution async. 2020-10-18 20:37:02 +00:00
projectmoon 97d91704a1 Make context owned, in hopes of async 2020-10-18 20:37:02 +00:00
projectmoon 9268314421 Remove actix, move state to RwLock. Update dependencies. 2020-10-18 20:37:02 +00:00
projectmoon 3b70891b0a Reject dice pool expressions over 100 elements. 2020-10-16 22:20:46 +00:00
projectmoon d0a1f59ec7 Better error message when dice pool variable not defined. 2020-10-16 22:20:46 +00:00
projectmoon af2e58351f Resolve variables in dice pools. 2020-10-16 22:20:46 +00:00
projectmoon dc8a74cc35 Update help for new dice pool syntax. 2020-10-16 22:20:46 +00:00
projectmoon 4856360c6a Custom schema violation error for DB value retrieval. 2020-10-16 13:18:27 +00:00
projectmoon 4234263ee4 Localize error enums to modules, better DB error reportng. 2020-10-16 13:18:27 +00:00
projectmoon 6cdc465a2e Add database and storage of user variables.
This commit introduces the Sled embedded key-value store for keeping
track of user variables on a per-room basis. Extensive changes were
made to the command module to separate concerns and also pass the
database "connection" down the line.

 - A new "Context" object was created to hold information and state
   needed for command execution (namely the database).
 - Database is very simple for now, storing only user variables.
   Refactoring later for storing more complicated types.
 - State actor moved into Actors struct, in preparation for either
   more actors, or ripping the whole thing out entirely.
 - Other modules are also more properly separated, notably
   the config module is entirely self-contained.
2020-10-16 13:18:27 +00:00
projectmoon 7e44faf693 Dice pool and command parser rewrite to prepare for user variables.
This commit refactors the parsing and rolling for the dice pool system
to prepare for support of user variables. The nom parser was dropped
in favor of the easier-to-understand combine parser in most parts of
the code.

A breaking change was introduced into the dice pool syntax to allow
for proper expressions and variables. The syntax is now
"modifiers:pool-amount", e.g. "n:gnosis+8". The simple single-number
syntax with no modifiers is also still understood.

Dice pool expressions are translated into a Vec of "Amount" objects,
stored by the DicePool struct. They have an operator (+ or -) and
either a number or variable name. When the dice pool is rolled, this
list of Amonuts are is collapsed into a single number that is rolled,
as it was before the refactor.

The following changes were made to the dice rolling code:
 - Store Vec<Amount> on DicePool instead of single number to roll.
 - New struct RolledDicePool to store result of a dice pool roll.
 - Remove Display trait from DicePool, move it over to RolledDicePool.
 - Separate extra dice pool info into DicePoolModifiers.
 - DicePoolModifiers is shared between DicePool and RolledDicePool.
 - Dice parsing and rolling now return standard Result objects.

This commit does NOT enable support of actually using variables. Any
dice pool roll containing a variable will result in an eror.

The command parser was also rewritten to use combine and rely on the
standard Result pattern.
2020-10-11 21:22:12 +00:00
projectmoon 05ff6af8a1 Inline all config getters. 2020-10-03 20:59:04 +00:00
projectmoon 7880d950af Use unwrap_or instead of match to calculate oldest message age. 2020-10-03 20:56:00 +00:00
projectmoon 514ac84e73 Encapsulate config with Arc. Further bot code cleanup.
Only expose config settings via methods on the Config struct. This
allows default value handling to live entirely inside the config code,
solves various borrowing issues with the "create default bot config
value" solution, and allows us to avoid cloning the bot config values.
The downside is that the config must now be in an Arc since its
ownership is shared in multiple places and the matrix SDK requires
thread-safe types (perhaps in the future we re-compose traits and use
Rc for config instead).

This commit also further cleans up and splits up the bot code for the
matrix connection, notably making the main message event handler
smaller by splitting out the "should we process the message" checks
into a separate function.
2020-10-03 20:45:14 +00:00
projectmoon 938107feae Implement Actix for state, refactor bot code.
Instead of using an Arc Mutex for state management embedded directly
into the bot, utilize actor pattern, with the idea that this will be
much more useful than simply logging a message once in the future.

This also refactors the bot code so that instead of a single run_bot
function, the DiceBot struct now has a run() method attached to it.
This also necessitated changes and cleanup to the dicebot main, which
is for the better anyhow.

The error and config types are also now in their own files, and
implemented for more in-depth use cases.
2020-10-03 20:45:14 +00:00
projectmoon f844b09213 Recover from potentially but unlikely poisioned mutexes. 2020-09-27 09:44:59 +00:00
projectmoon 620bea0521 Only use one instance of state instead of cloning it everywhere. 2020-09-27 09:44:59 +00:00
projectmoon 624c748583 Add useful logging of skipping commands.
This commit lays the groundwork for a stateful dicebot, instead of one
that only responds to commands. It now maintains a simple state
machine, used to store the current state of the bot. Currently, it
only cares about whether or not the message about skipping old
messages was logged.
2020-09-27 09:44:59 +00:00
projectmoon 6d49c9e16c Fall back to 15 minutes if oldest message age not specified.
This makes the oldest message age setting optional, in additon to the
entire bot config (for now). If the oldest message age is not
specified (or if the entire bot config is missing), it will default to
15 minutes.
2020-09-27 09:44:59 +00:00
projectmoon fabda911fd Ignore messages that are too old. 2020-09-27 09:44:59 +00:00
projectmoon 92e4a7c29b Rote die roll now respects dice pool success_on property.
Fixes #5.
2020-09-26 14:04:12 +00:00
projectmoon 06ff6562c1 Add more tests for handling weird input. 2020-09-01 08:17:59 +00:00
projectmoon f3f5846826 Fix command parser returning non-commands/empty messages as errors.
This behavior became broken again after switching away from the
macro-based command parsing. The bot would return any non !command
message as an error, which would cause it to read more messages, and
return those as errors, until finally the matrix SDK would throw up.

Command parser now more properly handles empty messages and
non-commands, but we also simply abort processing if the incoming
message doesn't start with an exclamation point.
2020-08-31 23:33:46 +00:00
projectmoon 8803b83ddb Remove useless trim function and unnecessary uses of eat_whitespace. 2020-08-31 20:16:43 +00:00