This commit is contained in:
Cadey Ratio 2020-07-06 21:34:48 -04:00
commit dc3b5351dd
5 changed files with 90 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
eval "$(lorri direnv)"

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
Cargo.lock

19
Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "dnd_dice"
version = "0.1.0"
authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
[dependencies]
dnd_dice_roller = "0.3"
mlua = { version = "0.4", features = ["lua53", "module", "async"] }
mlua_derive = "0.4"
[profile.release]
lto = true
opt-level = "s"

14
shell.nix Normal file
View File

@ -0,0 +1,14 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
buildInputs = with pkgs; [
rustc
cargo
rls
rustfmt
cargo-watch
pkg-config
lua5_3
bashInteractive
];
}

54
src/lib.rs Normal file
View File

@ -0,0 +1,54 @@
#[macro_use]
extern crate mlua_derive;
use std::str::FromStr;
use dnd_dice_roller::{dice::{Dice, DiceResult}, error::DiceError};
use mlua::{prelude::*, Table, Error};
use std::sync::Arc;
fn roll(lua: &Lua, roll: String) -> LuaResult<Table> {
match dice_roll(roll) {
Ok(res) => {
let result = lua.create_table()?;
if res.final_result.len() == 1 {
result.set("result", res.final_result[0])?;
} else {
result.set("result", res.final_result)?;
}
result.set("first", res.dice_results.first_roll)?;
if let Some(roll) = res.dice_results.second_roll {
result.set("second", roll)?;
}
Ok(result)
}
Err(why) => {
Err(Error::ExternalError(Arc::new(why)))
}
}
}
fn dice_roll<T: Into<String>>(roll: T) -> Result<DiceResult, DiceError> {
let mut dice = Dice::from_str(&roll.into())?;
if dice.number_of_dice_to_roll > 100 {
dice.number_of_dice_to_roll = 100;
}
if dice.sides > 100 {
dice.sides = 100
}
if dice.sides == 0 {
dice.sides = 6;
}
Ok(dice.roll_dice())
}
#[lua_module]
fn dnd_dice(lua: &Lua) -> LuaResult<LuaFunction> {
Ok(lua.create_function(roll)?)
}