forked from projectmoon/tenebrous-dicebot
Use variables namespace for user variables.
This commit is contained in:
parent
6b7acbe520
commit
ebfd230f31
|
@ -1,5 +1,4 @@
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::db::DataError::KeyDoesNotExist;
|
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::roll::Rolled;
|
use crate::roll::Rolled;
|
||||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
|
@ -526,7 +525,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn rejects_large_expression_test() {
|
async fn rejects_large_expression_test() {
|
||||||
let db = Database::new(&sled::open(tempdir().unwrap()).unwrap());
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
||||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||||
|
|
||||||
let mut amounts = vec![];
|
let mut amounts = vec![];
|
||||||
|
@ -551,7 +550,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn can_resolve_variables_test() {
|
async fn can_resolve_variables_test() {
|
||||||
let db = Database::new(&sled::open(tempdir().unwrap()).unwrap());
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
||||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||||
|
|
||||||
db.set_user_variable(&ctx.room_id, &ctx.username, "myvariable", 10)
|
db.set_user_variable(&ctx.room_id, &ctx.username, "myvariable", 10)
|
||||||
|
|
|
@ -82,7 +82,7 @@ impl Database {
|
||||||
let prefix_len: usize = prefix.len();
|
let prefix_len: usize = prefix.len();
|
||||||
|
|
||||||
let variables: Result<Vec<_>, DataError> = self
|
let variables: Result<Vec<_>, DataError> = self
|
||||||
.db
|
.variables
|
||||||
.scan_prefix(prefix)
|
.scan_prefix(prefix)
|
||||||
.map(|entry| match entry {
|
.map(|entry| match entry {
|
||||||
Ok((key, raw_value)) => {
|
Ok((key, raw_value)) => {
|
||||||
|
@ -108,7 +108,7 @@ impl Database {
|
||||||
) -> Result<i32, DataError> {
|
) -> Result<i32, DataError> {
|
||||||
let key = to_key(room_id, username, variable_name);
|
let key = to_key(room_id, username, variable_name);
|
||||||
|
|
||||||
if let Some(raw_value) = self.db.get(&key)? {
|
if let Some(raw_value) = self.variables.get(&key)? {
|
||||||
convert(&raw_value)
|
convert(&raw_value)
|
||||||
} else {
|
} else {
|
||||||
Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
||||||
|
@ -124,7 +124,8 @@ impl Database {
|
||||||
) -> Result<(), DataError> {
|
) -> Result<(), DataError> {
|
||||||
let key = to_key(room_id, username, variable_name);
|
let key = to_key(room_id, username, variable_name);
|
||||||
let db_value: I32<LittleEndian> = I32::new(value);
|
let db_value: I32<LittleEndian> = I32::new(value);
|
||||||
self.db.insert(&key, IVec::from(db_value.as_bytes()))?;
|
self.variables
|
||||||
|
.insert(&key, IVec::from(db_value.as_bytes()))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +136,7 @@ impl Database {
|
||||||
variable_name: &str,
|
variable_name: &str,
|
||||||
) -> Result<(), DataError> {
|
) -> Result<(), DataError> {
|
||||||
let key = to_key(room_id, username, variable_name);
|
let key = to_key(room_id, username, variable_name);
|
||||||
if let Some(_) = self.db.remove(&key)? {
|
if let Some(_) = self.variables.remove(&key)? {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
||||||
|
|
Loading…
Reference in New Issue