2020-06-22 18:20:11 +00:00
|
|
|
use regex::Regex;
|
2020-06-23 19:37:45 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
use anyhow::Result;
|
2020-08-22 20:07:28 +00:00
|
|
|
use rand::Rng;
|
2020-08-26 20:05:23 +00:00
|
|
|
use rand::seq::SliceRandom;
|
2020-06-22 18:20:11 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod lib_test;
|
|
|
|
|
|
2020-08-22 20:07:28 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-06-23 19:37:45 +00:00
|
|
|
enum DiceFilter {
|
2020-08-26 20:05:23 +00:00
|
|
|
DropLowest(usize), DropHighest(usize), KeepLowest(usize), KeepHighest(usize),
|
2020-06-23 19:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 20:07:28 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-06-23 19:37:45 +00:00
|
|
|
enum Segment {
|
|
|
|
|
DiceRoll{
|
|
|
|
|
op: char,
|
|
|
|
|
count: i32,
|
|
|
|
|
size: i32,
|
|
|
|
|
filter: Option<DiceFilter>,
|
|
|
|
|
},
|
|
|
|
|
Modifier{
|
|
|
|
|
op: char,
|
|
|
|
|
amount: i32,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Error, Debug, PartialEq)]
|
|
|
|
|
enum SegmentError {
|
|
|
|
|
#[error("No dice size was given")]
|
|
|
|
|
SizeIsMissing,
|
|
|
|
|
#[error("Invalid filter operator: {op}")]
|
|
|
|
|
InvalidFilterOperator{op: String},
|
|
|
|
|
#[error("Incomplete filter")]
|
|
|
|
|
IncompleteFilter,
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 20:05:23 +00:00
|
|
|
fn construct_dice_filter(op: &str, amount: usize) -> Result<DiceFilter> {
|
2020-06-23 19:37:45 +00:00
|
|
|
match op.to_lowercase().as_str() {
|
|
|
|
|
"d" | "dl" => Ok(DiceFilter::DropLowest(amount)),
|
|
|
|
|
"dh" => Ok(DiceFilter::DropHighest(amount)),
|
|
|
|
|
"k" | "kh" => Ok(DiceFilter::KeepHighest(amount)),
|
|
|
|
|
"kl" => Ok(DiceFilter::KeepLowest(amount)),
|
|
|
|
|
_ => Err(SegmentError::InvalidFilterOperator{op: op.to_owned()})?,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 20:05:23 +00:00
|
|
|
fn construct_dice_segment(cap: regex::Captures<'_>) -> Result<Segment> {
|
2020-06-23 19:37:45 +00:00
|
|
|
let op = cap.name("op").and_then(|i| i.as_str().chars().next()).unwrap_or('+');
|
|
|
|
|
let modifier = cap.name("mod").map(|i| i.as_str().parse()).transpose()?;
|
|
|
|
|
|
|
|
|
|
if let Some(amount) = modifier {
|
|
|
|
|
return Ok(Segment::Modifier{ op, amount });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let count:i32 = cap.name("count").map(|i| i.as_str().parse()).transpose()?.unwrap_or(1);
|
|
|
|
|
let size:i32 = cap.name("size").map(|i| i.as_str().parse()).transpose()?.ok_or(SegmentError::SizeIsMissing)?;
|
2020-08-26 20:05:23 +00:00
|
|
|
let filter_amount = cap.name("filter").map(|i| i.as_str().parse::<usize>()).transpose()?;
|
2020-06-23 19:37:45 +00:00
|
|
|
let filter_op = cap.name("filter_op").map(|op| op.as_str());
|
2020-08-26 20:05:23 +00:00
|
|
|
/*
|
2020-06-23 19:37:45 +00:00
|
|
|
dbg!(filter_amount);
|
|
|
|
|
dbg!(filter_op);
|
2020-08-26 20:05:23 +00:00
|
|
|
*/
|
2020-06-23 19:37:45 +00:00
|
|
|
if filter_amount.is_some() != filter_op.is_some() {
|
|
|
|
|
Err(SegmentError::IncompleteFilter)?;
|
|
|
|
|
}
|
|
|
|
|
let filter = filter_amount.and_then(|amount| (filter_op.map(|op| construct_dice_filter(op, amount)))).transpose()?;
|
|
|
|
|
|
|
|
|
|
Ok(Segment::DiceRoll{op, count, size, filter})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_dice_segments(cmd: &str) -> Result<Vec<Segment>> {
|
2020-06-22 18:20:11 +00:00
|
|
|
let regex = Regex::new(r#"(?x)
|
2020-06-22 20:13:06 +00:00
|
|
|
(?P<op>[+\-/*])?
|
|
|
|
|
\s*
|
|
|
|
|
(?:
|
|
|
|
|
(?:
|
|
|
|
|
(?P<count>\d+)? # count (optional)
|
|
|
|
|
d
|
|
|
|
|
(?P<size>\d+) # dice size
|
2020-06-23 19:37:45 +00:00
|
|
|
(?P<filter_op>[dk][hl]?)?
|
|
|
|
|
(?P<filter>\d+)?
|
2020-06-22 20:13:06 +00:00
|
|
|
) | (?:
|
|
|
|
|
(?P<mod>\d+)
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-06-22 18:20:11 +00:00
|
|
|
"#).expect("Failed to compile regex");
|
|
|
|
|
|
2020-06-23 19:37:45 +00:00
|
|
|
regex.captures_iter(cmd).map(construct_dice_segment).collect()
|
2020-06-22 18:20:11 +00:00
|
|
|
}
|
2020-08-22 20:07:28 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
struct RollWithModifier {
|
2020-08-26 20:05:23 +00:00
|
|
|
diceroll: Option<Segment>,
|
2020-08-22 20:07:28 +00:00
|
|
|
modifiers: Vec<Segment>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn group_modifiers_to_dicerolls(segments: &[Segment]) -> Vec<RollWithModifier> {
|
|
|
|
|
let mut results = Vec::new();
|
|
|
|
|
|
|
|
|
|
let mut current_diceroll : Option<Segment> = None;
|
|
|
|
|
let mut current_modifiers = Vec::new();
|
|
|
|
|
|
|
|
|
|
for segment in segments {
|
|
|
|
|
if let Segment::DiceRoll {..} = segment {
|
2020-08-26 20:05:23 +00:00
|
|
|
if current_diceroll.is_some() || current_modifiers.len() > 0 {
|
2020-08-22 20:07:28 +00:00
|
|
|
let with_modifier = RollWithModifier {
|
2020-08-26 20:05:23 +00:00
|
|
|
diceroll: current_diceroll.clone(),
|
2020-08-22 20:07:28 +00:00
|
|
|
modifiers: current_modifiers.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
current_modifiers.clear();
|
|
|
|
|
|
|
|
|
|
results.push(with_modifier);
|
|
|
|
|
}
|
2020-08-26 20:05:23 +00:00
|
|
|
|
2020-08-22 20:07:28 +00:00
|
|
|
current_diceroll = Some(segment.clone());
|
|
|
|
|
}
|
|
|
|
|
else if let Segment::Modifier { .. } = segment {
|
|
|
|
|
current_modifiers.push(segment.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 20:05:23 +00:00
|
|
|
if current_diceroll.is_some() || current_modifiers.len() > 0 {
|
2020-08-22 20:07:28 +00:00
|
|
|
let with_modifier = RollWithModifier {
|
2020-08-26 20:05:23 +00:00
|
|
|
diceroll: current_diceroll.clone(),
|
2020-08-22 20:07:28 +00:00
|
|
|
modifiers: current_modifiers.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
current_modifiers.clear();
|
|
|
|
|
|
|
|
|
|
results.push(with_modifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results
|
|
|
|
|
}
|
2020-08-26 20:05:23 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
struct Roll {
|
|
|
|
|
operator: char,
|
|
|
|
|
results: Vec<i32>,
|
|
|
|
|
total: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Error, Debug, PartialEq, Clone)]
|
|
|
|
|
enum RollError {
|
|
|
|
|
#[error("Roll failed. Empty RollWithModifier")]
|
|
|
|
|
EmptyRollWithModifier,
|
|
|
|
|
#[error("Roll failed. Invalid filter operator")]
|
|
|
|
|
InvalidFilterOperator,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn roll_dice_segments<R: Rng>(rwms: &[RollWithModifier], mut rng: R) -> Result<Vec<Roll>, RollError> {
|
|
|
|
|
rwms.iter()
|
|
|
|
|
.map(|rwm| {
|
|
|
|
|
let mut results = Vec::new();
|
|
|
|
|
let mut total = 0;
|
|
|
|
|
|
|
|
|
|
// store first operator
|
|
|
|
|
let operator = if let Some(Segment::DiceRoll{op, ..}) = rwm.diceroll {
|
|
|
|
|
op
|
|
|
|
|
} else if let Some(Segment::Modifier{op, ..}) = rwm.modifiers.iter().next() {
|
|
|
|
|
*op
|
|
|
|
|
} else {
|
|
|
|
|
return Err(RollError::EmptyRollWithModifier);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(Segment::DiceRoll{count, size, filter, ..}) = &rwm.diceroll {
|
|
|
|
|
results = (0..*count).map(|_| {
|
|
|
|
|
rng.gen_range(1, size + 1)
|
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
|
|
results.sort();
|
|
|
|
|
if let Some(filter) = filter {
|
|
|
|
|
match filter {
|
|
|
|
|
DiceFilter::DropLowest(n) => {
|
|
|
|
|
results = results.into_iter().skip(*n).collect();
|
|
|
|
|
},
|
|
|
|
|
DiceFilter::DropHighest(n) => {
|
|
|
|
|
let len = results.len();
|
|
|
|
|
results = results.into_iter().take(len - n).collect();
|
|
|
|
|
},
|
|
|
|
|
DiceFilter::KeepLowest(n) => {
|
|
|
|
|
results = results.into_iter().take(*n).collect();
|
|
|
|
|
},
|
|
|
|
|
DiceFilter::KeepHighest(n) => {
|
|
|
|
|
let len = results.len();
|
|
|
|
|
results = results.into_iter().skip(len - n).collect();
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
results.shuffle(&mut rng);
|
|
|
|
|
|
|
|
|
|
total = results.iter().sum();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for segment in &rwm.modifiers {
|
|
|
|
|
if let Segment::Modifier{op,amount} = segment {
|
|
|
|
|
match op {
|
|
|
|
|
'+' => { total += amount; },
|
|
|
|
|
'-' => { total -= amount; },
|
|
|
|
|
'/' => { total /= amount; },
|
|
|
|
|
'*' => { total *= amount; },
|
|
|
|
|
_ => {
|
|
|
|
|
return Err(RollError::InvalidFilterOperator);
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Roll{operator, results, total})
|
|
|
|
|
})
|
|
|
|
|
.collect::<Result<Vec<_>,_>>()
|
|
|
|
|
}
|