dice_nom_shim 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2 -2
- data/ext/dice_nom_shim/Cargo.toml +1 -1
- data/ext/dice_nom_shim/src/lib.rs +24 -18
- data/lib/dice_nom_shim/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6219410b5f2a939413f488c9ec51d7da0008781ad6cda38159a22074e5922f3
|
4
|
+
data.tar.gz: 8a32bbd3ffb35fac6fa21bac67b243521cbe4bef883e5353574322cfe2ba2aad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe956b9f166b878c2a17995dbd4fe580b99cb302932e2886c8b8531a80047b13f1f7b31743251da2b6f710265d36fabbcb02b2a46a656216385c881028900888
|
7
|
+
data.tar.gz: ee2abeb34d39eb2cb55bc814c0f8c6f01f1bfc140b3d715cc539a88f1a7fa66c76ae7e998e159ebe1e52613a7efa9bf59e499380ede3fc9baca6ba81300c2704
|
data/Cargo.lock
CHANGED
@@ -161,9 +161,9 @@ checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
|
161
161
|
|
162
162
|
[[package]]
|
163
163
|
name = "dice-nom"
|
164
|
-
version = "0.
|
164
|
+
version = "0.5.0"
|
165
165
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
166
|
-
checksum = "
|
166
|
+
checksum = "2361b70380afbbc1fb4c2bfb8f85251f3147b61dfa3a160b025bd2459d096eca"
|
167
167
|
dependencies = [
|
168
168
|
"clap",
|
169
169
|
"nom 8.0.0",
|
@@ -31,7 +31,7 @@ struct Histo {
|
|
31
31
|
}
|
32
32
|
|
33
33
|
impl Histo {
|
34
|
-
pub fn build(roll: String, count: u32) ->
|
34
|
+
pub fn build(roll: String, count: u32) -> Histo {
|
35
35
|
let mut histo = Histo {
|
36
36
|
min: MAX,
|
37
37
|
max: 0,
|
@@ -40,12 +40,9 @@ impl Histo {
|
|
40
40
|
};
|
41
41
|
let g = match generator_parser(roll.as_ref()) {
|
42
42
|
Ok((_, g)) => g,
|
43
|
-
Err(
|
43
|
+
Err(_) => return histo,
|
44
44
|
};
|
45
45
|
let mut rng = rand::rng();
|
46
|
-
let results = g.generate(&mut rng);
|
47
|
-
let result_json = serde_json::to_string(&results);
|
48
|
-
|
49
46
|
for _ in 0..count {
|
50
47
|
let v = g.generate(&mut rng).sum();
|
51
48
|
if v < histo.min {
|
@@ -67,23 +64,32 @@ impl Histo {
|
|
67
64
|
}
|
68
65
|
}
|
69
66
|
}
|
70
|
-
|
71
|
-
|
72
|
-
let result_error = format!("{{\"error\": \"could not parse result '{}'\"}}", roll);
|
73
|
-
let histo_error = format!(
|
74
|
-
"{{\"error\": \"could not parse result '{}' count={}\"}}",
|
75
|
-
roll, count
|
76
|
-
);
|
77
|
-
format!(
|
78
|
-
"[{}, {}]",
|
79
|
-
result_json.unwrap_or(result_error),
|
80
|
-
histo_json.unwrap_or(histo_error)
|
81
|
-
)
|
67
|
+
histo
|
82
68
|
}
|
83
69
|
}
|
84
70
|
|
85
71
|
fn histo(roll: String) -> String {
|
86
|
-
|
72
|
+
let num = 5000.0;
|
73
|
+
let histo = Histo::build(roll, num as u32);
|
74
|
+
let mut chart_data = Vec::new();
|
75
|
+
let mut total = 100.0;
|
76
|
+
for k in histo.min..=histo.max {
|
77
|
+
let count = histo.map.get(&k).unwrap_or(&0);
|
78
|
+
let percentage = (*count as f64 / num) * 100.0;
|
79
|
+
chart_data.push(serde_json::json!({
|
80
|
+
"value": k,
|
81
|
+
"count": count,
|
82
|
+
"percentage": percentage,
|
83
|
+
"total": total,
|
84
|
+
}));
|
85
|
+
total = total - percentage;
|
86
|
+
}
|
87
|
+
|
88
|
+
let json = serde_json::to_string(&chart_data);
|
89
|
+
match json {
|
90
|
+
Ok(json) => format!("{}", json),
|
91
|
+
Err(err) => format!("{{\"error\": \"{}\"}}", err),
|
92
|
+
}
|
87
93
|
}
|
88
94
|
|
89
95
|
#[magnus::init]
|