tavern_keeper/src/generators.rs

45 lines
1.4 KiB
Rust

use rand::seq::SliceRandom;
use rand::prelude::*;
pub struct TownNameGenerator {
}
impl TownNameGenerator {
pub fn name() -> String {
let words = include_str!("names/towns/first.txt").split("\n").collect::<Vec<&str>>();
let mut rng = rand::thread_rng();
let first = words.choose(&mut rng).unwrap();
let second = words.choose(&mut rng).unwrap();
let name = format!("{}{}", first, second);
// capitalize first letter
name[0..1].to_uppercase() + &name[1..]
}
}
pub struct PersonNameGenerator {
}
impl PersonNameGenerator {
pub fn name() -> String {
let words = include_str!("names/towns/first.txt").split("\n").collect::<Vec<&str>>();
let syllables = include_str!("names/people/syllables.txt").split("\n").collect::<Vec<&str>>();
let mut rng = rand::thread_rng();
let first = words.choose(&mut rng).unwrap();
let second = words.choose(&mut rng).unwrap();
let mut name = "".to_owned();
for _ in 0..rng.gen_range(2..5) {
name = format!("{}{}", name, syllables.choose(&mut rng).unwrap());
}
name = name[0..1].to_uppercase() + &name[1..];
let mut surname = format!("{}{}", first, second);
// capitalize first letter
surname = surname[0..1].to_uppercase() + &surname[1..];
format!("{} {}", name, surname)
}
}