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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d593a0a7e8b3ca9c6b063845f34e6345198bded7e222125a1fea4d89724f430b
4
- data.tar.gz: a384c2665745f6e848a1ba9cdc7de0b5fce54c014755364576675f9ada397d71
3
+ metadata.gz: ce27429c2bf107ec2c250cb90718d58519bdf927289c822e2e859b081d1036f2
4
+ data.tar.gz: 177c3c022305d2910b8ea5403f6e5d63641998f7b0d635bfa277165ebea6479f
5
5
  SHA512:
6
- metadata.gz: 24b1dddbdb2ab8bcde211f5c6d35171017a0492c164c4ce0d68b5a90f3bf2436ad64c12222e7d82ce933b269b8e0c1bb306fc82200755b05efd1b44a9a95476e
7
- data.tar.gz: 0dd3dfa6c79cd1684d76dded4d4ba809562af5bbbe7e12fb6efe5d98c10e2f78be46742ffbff1497dc6c6a450ff9cafbf15b1e7e3f9441683950f5320b152a3e
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) -> String {
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(e) => return format!("{{\"error\": \"{}\"}}", e),
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
- let histo_json = serde_json::to_string(&histo);
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
- Histo::build(roll, 2000)
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]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiceNomShim
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dice_nom_shim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer