1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! This is a crate for dice rolling utilities.
//! 
//! # Examples
//! Roll some dice using destiny::parse_dice_string:
//! ```
//! use destiny::parse_dice_string;
//!
//! println!("{}", parse_dice_string("1d4"));
//! println!("{}", parse_dice_string("1d6"));
//! println!("{}", parse_dice_string("2d6"));
//! println!("{}", parse_dice_string("1d8 + 3"));
//! println!("{}", parse_dice_string("1d6 + 2d8"));
//! ```
//! 
//! Calculate distributions using destiny::DiceDistribution:
//! ```
//! use destiny::DiceDistribution;
//! 
//! let dd = DiceDistribution::new("2d6");
//! dd.ptable();
//! /* this will output:
//! +------+--------+--------+---------+--------+
//! | Roll | #Rolls | Roll%  | Roll>=% | Roll<% |
//! +======+========+========+=========+========+
//! | 2    | 1      | 2.78%  | 100.00% | 0.00%  |
//! +------+--------+--------+---------+--------+
//! | 3    | 2      | 5.56%  | 97.22%  | 2.78%  |
//! +------+--------+--------+---------+--------+
//! | 4    | 3      | 8.33%  | 91.67%  | 8.33%  |
//! +------+--------+--------+---------+--------+
//! | 5    | 4      | 11.11% | 83.33%  | 16.67% |
//! +------+--------+--------+---------+--------+
//! | 6    | 5      | 13.89% | 72.22%  | 27.78% |
//! +------+--------+--------+---------+--------+
//! | 7    | 6      | 16.67% | 58.33%  | 41.67% |
//! +------+--------+--------+---------+--------+
//! | 8    | 5      | 13.89% | 41.67%  | 58.33% |
//! +------+--------+--------+---------+--------+
//! | 9    | 4      | 11.11% | 27.78%  | 72.22% |
//! +------+--------+--------+---------+--------+
//! | 10   | 3      | 8.33%  | 16.67%  | 83.33% |
//! +------+--------+--------+---------+--------+
//! | 11   | 2      | 5.56%  | 8.33%   | 91.67% |
//! +------+--------+--------+---------+--------+
//! | 12   | 1      | 2.78%  | 2.78%   | 97.22% |
//! +------+--------+--------+---------+--------+
//! */
//! ```

extern crate meval;
extern crate regex;
use prettytable::{Cell, Row, Table};

#[macro_use]
extern crate prettytable;

use itertools::Itertools;
use rand::Rng;
use regex::Captures;
use regex::Regex;
use std::collections::HashMap;

extern crate rayon;

use rayon::prelude::*;

/// Simulates the rolling of num_dice of size size_dice.
fn roll_dice(num_dice: usize, size_dice: usize) -> usize {
    let mut rng = rand::thread_rng();

    let mut total = 0;
    for _ in 0..num_dice {
        total += rng.gen_range(1, size_dice + 1);
    }

    total
}

/// Takes a string with dice notation and replaces them with a simulated roll.
fn replace_dice(string: &str) -> String {
    let re = Regex::new(r"(?P<num_dice>[0-9]+)d(?P<size_dice>[0-9]+)").unwrap();
    let result = re.replace_all(string, |caps: &Captures| {
        format!(
            "{}",
            roll_dice(
                caps["num_dice"].parse().unwrap(),
                caps["size_dice"].parse().unwrap()
            )
        )
    });
    format!("{}", result)
}

/// Parses and evaluates Strings with dice notation.
///
/// # Examples
/// ```
/// use destiny::parse_dice_string;
///
/// let roll = parse_dice_string("1d6");
/// assert!(roll >= 1 && roll <= 6);
/// ```
///
/// ```
/// use destiny::parse_dice_string;
///
/// let roll = parse_dice_string("1d6 + 3");
/// assert!(roll >= 4 && roll <= 9);
/// ```
///
/// ```
/// use destiny::parse_dice_string;
///
/// let roll = parse_dice_string("2d6");
/// assert!(roll >= 2 && roll <= 12);
/// ```
pub fn parse_dice_string(string: &str) -> i64 {
    let result = meval::eval_str(replace_dice(string)).unwrap();
    result.trunc() as i64
}

struct RollInfo {
    pub num: i64,
    pub size: i64,
}

impl RollInfo {
    pub fn roll_values(&self) -> Vec<i64> {
        let mut ranges = Vec::new();
        for _ in 0..self.num {
            ranges.push(1..(self.size + 1));
        }

        let mut roll_values = Vec::new();
        for combination in ranges.into_iter().multi_cartesian_product() {
            roll_values.push(combination.iter().sum());
        }

        roll_values
    }

    pub fn num_possible_rolls(&self) -> i64 {
        self.size.pow(self.num as u32)
    }
}

/// A struct used to hold the information about a dice distribution.
/// 
/// # Examples
/// ```
/// use destiny::DiceDistribution;
/// 
/// let dd = DiceDistribution::new("1d4");
/// 
/// assert_eq!(vec![1, 2, 3, 4], dd.possible_rolls);
/// ```
pub struct DiceDistribution {
    pub dice_string: String,
    pub possible_rolls: Vec<i64>,
    pub distribution: HashMap<i64, i64>,
    pub roll_percentages: HashMap<i64, f64>,
    pub roll_over: HashMap<i64, f64>,
    pub roll_under: HashMap<i64, f64>,

}

impl DiceDistribution {
    /// Creates a new DiceDistribution. This uses the supplied string to calculate the
    /// possible rolls, the distrribution of those rolls and the percentage chance to roll any particular value.
    pub fn new(dice_string: &str) -> DiceDistribution {
        let dice_string = String::from(dice_string);
        let possible_rolls = possible_rolls(&dice_string);
        let distribution = roll_distribution(&possible_rolls);
        let roll_percentages = roll_percentage(&distribution);
        let roll_over = roll_over_percentage(&roll_percentages);
        let roll_under = roll_under_percentage(&roll_percentages);

        DiceDistribution {
            dice_string,
            possible_rolls,
            distribution,
            roll_percentages,
            roll_over,
            roll_under
        }
    }

    /// Creates a prettytable::Table containing a representation of self.
    pub fn table(&self) -> Table {
        // distribution_table(&self.distribution, &self.roll_percentages)
        let mut tuples = Vec::new();

        for (roll, num_rolled) in &self.distribution {
            tuples.push([roll, num_rolled]);
        }
        tuples.sort_by(|a, b| a[0].cmp(&b[0]));

        let mut table = Table::new();

        table.set_titles(row!["Roll", "#Rolls", "Roll%", "Roll>=%", "Roll<%"]);

        for tuple in tuples {
            let percent = format!("{:.2}%", self.roll_percentages[&tuple[0]] * 100f64);
            let over_percent = format!("{:.2}%", self.roll_over[&tuple[0]] * 100f64);
            let under_percent = format!("{:.2}%", self.roll_under[&tuple[0]] * 100f64);
            
            table.add_row(Row::new(vec![
                Cell::new(&tuple[0].to_string()),
                Cell::new(&tuple[1].to_string()),
                Cell::new(&percent),
                Cell::new(&over_percent),
                Cell::new(&under_percent)
            ]));
        }
        
        table
    }

    /// Creates and prints to stdout a table representation of self.
    pub fn ptable(&self) {
        &self.table().printstd();
    }
}

/// Calculates the complexity of a roll. This counts the total number of possible 
/// dice combinations. This is usefull if you want to make sure a dice roll is 
/// resonable to calculate before trying it. The best use case for this is if you
/// are taking user input. if a user tries to calculate the distribution of 
/// "8d10" the there are 100,000,000 possible dice rolls. Calculating a distribution
/// on this would take a very long time.
/// 
/// # Examples
/// ```
/// use destiny::roll_complexity;
/// 
/// let num_possibilities = roll_complexity("8d10");
/// assert_eq!(num_possibilities, 100_000_000)
/// ```
pub fn roll_complexity(dice_string: &str) -> i64 {

    let (_, roll_infos) = extract_dice_values(dice_string);

    roll_infos.iter()
    .map(|x| x.num_possible_rolls())
    .product()
}

/// Replaces all the dice notions in a string with a '{}' placeholder and creates a Vector of coresponding RollInfo structs
fn extract_dice_values(string: &str) -> (String, Vec<RollInfo>) {
    // TODO remove duplicate regex definition
    let mut roll_infos = Vec::new();

    let re = Regex::new(r"(?P<num_dice>[0-9]+)d(?P<size_dice>[0-9]+)").unwrap();
    let result = re
        .replace_all(string, |caps: &Captures| {
            roll_infos.push(RollInfo {
                num: caps["num_dice"].parse().unwrap(),
                size: caps["size_dice"].parse().unwrap(),
            });
            "{}"
        })
        .into_owned();

    (result, roll_infos)
}

/// Calculates all the possible roll combinations for a given dice string.
///
/// # Examples
///
/// Calculate the possibilities of rolling 1d6:
/// ```
/// use destiny::possible_rolls;
///
/// let rolls = possible_rolls("1d6");
/// assert_eq!(vec![1, 2, 3, 4, 5, 6], rolls);
/// ```
///
/// Calulate the possible rolls of 1d4 + 2:
/// ```
/// use destiny::possible_rolls;
///
/// let rolls = possible_rolls("1d4 + 2");
/// assert_eq!(vec![3, 4, 5, 6], rolls);
/// ```
///
/// Calculating the possibilities of 2d4:
/// ```
/// use destiny::possible_rolls;
///
/// let rolls = possible_rolls("2d4");
/// assert_eq!(vec![2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7, 5, 6, 7, 8], rolls);
/// ```
pub fn possible_rolls(string: &str) -> Vec<i64> {
    let (format_string, roll_infos) = extract_dice_values(string);
    let roll_numbers: Vec<i64> = roll_infos
        .iter()
        .map(|x| x.roll_values())
        .multi_cartesian_product()
        .collect::<Vec<Vec<i64>>>()
        .par_iter()
        .map(|x| parse_dice_string(&format_dice_string(&format_string, x)))
        .collect();

    roll_numbers
}

/// Calculates how many times each roll could have rolled to show the distribution.
///
/// A hashmapp is returned with the key being the number rolled and the value being the amount it was rolled.
///
/// # Examples
/// ```
/// use destiny::{ possible_rolls, roll_distribution };
///
/// let rolls = possible_rolls("2d6");
/// let distribution = roll_distribution(&rolls);
///
/// // There is only one way to roll 2 on 2d6(Two 1's).
/// assert_eq!(1, distribution[&2]);
pub fn roll_distribution(roll_numbers: &Vec<i64>) -> HashMap<i64, i64> {
    let mut roll_distribution = HashMap::new();
    for roll in roll_numbers {
        roll_distribution
            .entry(*roll)
            .and_modify(|e| *e += 1)
            .or_insert(1);
    }

    roll_distribution
}

/// Takes a distribution and calculates the chance to roll every value
pub fn roll_percentage(distribution: &HashMap<i64, i64>) -> HashMap<i64, f64> {
    let total_rolls: i64 = distribution.values().sum();
    let mut roll_percentages = HashMap::new();

    for (roll, num_rolled) in distribution {
        roll_percentages.insert(*roll, *num_rolled as f64 / total_rolls as f64);
    }

    roll_percentages
}

fn roll_over_percentage(roll_percentages: &HashMap<i64, f64>) -> HashMap<i64, f64> {
    let mut roll_over = HashMap::new();
    for roll in roll_percentages.keys() {
        for (proll, percentage) in roll_percentages {
            if proll >= roll {
                roll_over.entry(*roll).and_modify(|e| *e += percentage).or_insert(*percentage);
            }
        }
    }
    
    roll_over
}

fn roll_under_percentage(roll_percentages: &HashMap<i64, f64>) -> HashMap<i64, f64> {
    let mut roll_under = HashMap::new();
    for roll in roll_percentages.keys() {
        roll_under.insert(*roll, 0f64);
        for (proll, percentage) in roll_percentages {
            if proll < roll {
                roll_under.entry(*roll).and_modify(|e| *e += percentage).or_insert(*percentage);
            }
        }
    }

    roll_under
}

fn format_dice_string(dice_string: &str, rolls: &Vec<i64>) -> String {
    let re = Regex::new(r"\{}").unwrap();
    let mut new_string = dice_string.to_string();

    for roll in rolls {
        new_string = re
            .replace(new_string.as_str(), roll.to_string().as_str())
            .to_string();
    }
    new_string
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_dice_string() {
        for _ in 0..100 {
            let value = parse_dice_string("1d6");
            assert!(value > 0 && value < 7);
        }
    }

    #[test]
    fn test_math() {
        let value = parse_dice_string("1d1+2");
        assert_eq!(value, 3);
    }

    #[test]
    fn test_math_1() {
        let value = parse_dice_string("1d1 + 2");
        assert_eq!(value, 3);
    }

    #[test]
    fn test_math_2() {
        let value = parse_dice_string("1d1 - 1d1 + 2");
        assert_eq!(value, 2);
    }

    #[test]
    fn test_math_3() {
        let value = parse_dice_string("3 + 1d1 * 2");
        assert_eq!(value, 5);
    }

    #[test]
    fn test_math_4() {
        let value = parse_dice_string("(3 + 1d1) * 2");
        assert_eq!(value, 8);
    }

    #[test]
    fn test_less_whitespace() {
        let value = parse_dice_string("(3+1d1)*2");
        assert_eq!(value, 8);
    }

    #[test]
    fn test_more_whitespace() {
        let value = parse_dice_string("  (   3 + 1d1 ) *2 ");
        assert_eq!(value, 8);
    }

    #[test]
    fn test_complexity_1() {
        assert_eq!(roll_complexity("2d6"), 36);
    }

    #[test]
    fn test_complexity_2() {
        assert_eq!(roll_complexity("2d6 + 2d6"), 1296);
    }

    #[test]
    fn test_complexity_3() {
        assert_eq!(roll_complexity("1d20 + 3d6"), 4320);
    }

    #[test]
    fn test_complexity_4() {
        assert_eq!(roll_complexity("8d10"), 100_000_000);
    }

    #[test]
    fn test_distribution_1() {
        let dd = DiceDistribution::new("1d4");
        assert_eq!(dd.possible_rolls, vec![1, 2, 3, 4]);
    }

    #[test]
    fn test_distribution_2() {
        let dd = DiceDistribution::new("2d4");
        assert_eq!(dd.possible_rolls, vec![2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7, 5, 6, 7, 8]);
    }
}