lib: fixed pn selection so you get the same one on the day

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-10-24 04:57:57 -04:00
parent a242260ee2
commit 5ad04f1427
2 changed files with 12 additions and 7 deletions

View file

@ -127,7 +127,7 @@ impl Default for InstanceSettings {
/// she/her or she/her/hers. /// she/her or she/her/hers.
/// ///
/// Methods are provided to quickly generate these shorter forms, as well as example sentences. /// Methods are provided to quickly generate these shorter forms, as well as example sentences.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[serde(from = "[String; 5]", into = "[String; 5]")] #[serde(from = "[String; 5]", into = "[String; 5]")]
pub struct Pronoun { pub struct Pronoun {

View file

@ -3,7 +3,9 @@ use crate::{
util::{pcg64, pcg64_seed}, util::{pcg64, pcg64_seed},
}; };
use std::collections::HashMap; use std::{collections::BTreeMap, hash::Hash};
use std::fmt::Debug;
use std::cmp::Eq;
use time::{Date, Month, OffsetDateTime}; use time::{Date, Month, OffsetDateTime};
@ -25,9 +27,9 @@ pub const COVID_EPOCH: Date = match Date::from_calendar_date(2020, Month::Januar
/// pronoun. Additional methods are provided to perform this random selection on a weighted list, /// pronoun. Additional methods are provided to perform this random selection on a weighted list,
/// using as a seed both an arbitrary string of bytes and a Date. /// using as a seed both an arbitrary string of bytes and a Date.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct WeightedTable<Loot: std::cmp::Eq + std::hash::Hash>(HashMap<Loot, u8>); pub struct WeightedTable<Loot: Eq + Ord + Hash + Debug>(BTreeMap<Loot, u8>);
impl<Loot: std::cmp::Eq + std::hash::Hash + Copy> WeightedTable<Loot> { impl<Loot: Eq + Ord + Hash + Copy + Debug> WeightedTable<Loot> {
/// Attempt to generate a new table /// Attempt to generate a new table
/// ///
@ -40,7 +42,7 @@ impl<Loot: std::cmp::Eq + std::hash::Hash + Copy> WeightedTable<Loot> {
/// error if the safety check fails. The original tabel can be accessed as a /// error if the safety check fails. The original tabel can be accessed as a
/// [`HashMap`] via the [`WeightedTable::weights()`] method /// [`HashMap`] via the [`WeightedTable::weights()`] method
pub fn new(table: impl IntoIterator<Item=(Loot, u8)>) -> Result<Self, ParseError> { pub fn new(table: impl IntoIterator<Item=(Loot, u8)>) -> Result<Self, ParseError> {
let table: HashMap<Loot, u8> = table.into_iter() let table: BTreeMap<Loot, u8> = table.into_iter()
.filter(|(_, weight)| *weight > 0) .filter(|(_, weight)| *weight > 0)
.collect(); .collect();
@ -54,7 +56,7 @@ impl<Loot: std::cmp::Eq + std::hash::Hash + Copy> WeightedTable<Loot> {
/// Access the orginal table of weights underlying this table /// Access the orginal table of weights underlying this table
/// ///
/// Returns a [`HashMap`] mapping pronouns to their respective weights. /// Returns a [`HashMap`] mapping pronouns to their respective weights.
pub fn weights(&self) -> &HashMap<Loot, u8> { pub fn weights(&self) -> &BTreeMap<Loot, u8> {
&self.0 &self.0
} }
@ -101,7 +103,7 @@ impl<Loot: std::cmp::Eq + std::hash::Hash + Copy> WeightedTable<Loot> {
).to_le_bytes() ).to_le_bytes()
); );
new_seed.extend(seed); new_seed.extend(seed);
self.select(seed.as_ref()) self.select(&new_seed)
} }
/// Randomly select a pronoun set for a given seed /// Randomly select a pronoun set for a given seed
@ -124,3 +126,6 @@ impl<Loot: std::cmp::Eq + std::hash::Hash + Copy> WeightedTable<Loot> {
} }
} }