dice_nom_shim 0.4.0 → 0.4.1
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/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: ce27429c2bf107ec2c250cb90718d58519bdf927289c822e2e859b081d1036f2
|
4
|
+
data.tar.gz: 177c3c022305d2910b8ea5403f6e5d63641998f7b0d635bfa277165ebea6479f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aec79bbd2d0e09c74bd31763d0b93545038e5051a4219fb64a1160cac295ec4f2a4b906bc4aafa8794c4def5b817956ce0a650546e3cbcc85f6a5e0f3ebe27d
|
7
|
+
data.tar.gz: b05178e970118c26a84a6735db56d31815b67055101ce08587959f075a36d2aa71e77b50473595cee27ad818bc3d42799d705f42d8b027dcadfadc06f2b77a1a
|
@@ -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 = 2000.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]
|