1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-22 02:54:21 +00:00

🗑 No need for seed

This commit is contained in:
Cookie-CHR 2024-07-10 23:33:04 +02:00
parent af66ff66c0
commit 4859e72929

View file

@ -1,6 +1,5 @@
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use anyhow::{Context}; use anyhow::{Context};
use chrono::{DateTime, Utc};
use rand::SeedableRng; use rand::SeedableRng;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use teloxide::Bot; use teloxide::Bot;
@ -78,38 +77,8 @@ const ANSWERS: [&str; 60] = [
"❔ [RADIO] Mantengo la posizione.", "❔ [RADIO] Mantengo la posizione.",
]; ];
struct AnswerKey {
seed: chrono::DateTime<Utc>,
}
impl Hash for AnswerKey {
fn hash<H: Hasher>(&self, state: &mut H) {
let seed: i64 = self.seed.timestamp();
state.write_i64(seed);
}
}
pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { pub async fn handler(bot: &Bot, message: &Message) -> CommandResult {
let seed = chrono::Utc::now(); let mut rng = rand::rngs::SmallRng::from_entropy();
let key = AnswerKey {seed};
let mut hasher = std::hash::DefaultHasher::new();
key.hash(&mut hasher);
let hash = hasher.finish()
.to_le_bytes()
.into_iter()
.cycle()
.take(32)
.collect::<Vec<u8>>()
.try_into();
if hash.is_err() {
anyhow::bail!("Non è stato possibile determinare una risposta.");
}
let hash = hash.unwrap();
let mut rng = rand::rngs::SmallRng::from_seed(hash);
let answer = ANSWERS.choose(&mut rng) let answer = ANSWERS.choose(&mut rng)
.context("Non è stato possibile selezionare una risposta.")?; .context("Non è stato possibile selezionare una risposta.")?;