Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 81 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ homepage = "https://github.com/YukariExpress/pythia_gata"

[dependencies]
teloxide = { version = "0.17", features = ["full"] }
rand = "0.9"
rand_core = "0.10"
chacha20 = { version = "0.10", features = ["rng"] }
sha2 = "0.10"
tokio = { version = "1", features = ["full"] }
pretty_env_logger = "0.5"
Expand Down
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
use chacha20::ChaCha12Rng;
use rand_core::{Rng, SeedableRng};

use sha2::{Digest, Sha256};
use std::time::{SystemTime, UNIX_EPOCH};
use teloxide::types::UpdateKind;
Expand All @@ -9,7 +10,7 @@ use teloxide::types::{
};
use teloxide::{prelude::*, update_listeners::webhooks};

fn new_rng(user_id: i64, query: &str) -> StdRng {
fn new_rng(user_id: i64, query: &str) -> impl Rng {
// Truncate time to 30 minutes
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -23,10 +24,10 @@ fn new_rng(user_id: i64, query: &str) -> StdRng {
let hash = hasher.finalize();
let mut seed = [0u8; 32];
seed.copy_from_slice(&hash);
StdRng::from_seed(seed)
ChaCha12Rng::from_seed(seed)
}

fn pia(query: &str, rng: &mut StdRng) -> String {
fn pia(query: &str, rng: &mut impl Rng) -> String {
let prefix = if rng.next_u64() % 8 == 0 {
"Pia!▼(o ‵-′)ノ★ "
} else {
Expand All @@ -35,7 +36,7 @@ fn pia(query: &str, rng: &mut StdRng) -> String {
format!("{prefix}{query}")
}

fn divine(query: &str, rng: &mut StdRng) -> String {
fn divine(query: &str, rng: &mut impl Rng) -> String {
let mut result = format!("所求事项: {query}\n结果: ");
let o = rng.next_u64() % 16;
let omen = if o >= 9 {
Expand Down Expand Up @@ -71,7 +72,7 @@ fn divine(query: &str, rng: &mut StdRng) -> String {
fn build_inline_query_results(
user: &teloxide::types::User,
query_text: &str,
mut rng: StdRng,
mut rng: impl Rng,
) -> Vec<InlineQueryResult> {
let locale = user.language_code.as_deref().unwrap_or("zh");
let (divine_title, pia_title) = match locale {
Expand Down
6 changes: 3 additions & 3 deletions src/main_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ mod tests {

#[test]
fn test_new_rng_determinism() {
let rng1 = new_rng(12345, "test");
let rng2 = new_rng(12345, "test");
let mut rng1 = new_rng(12345, "test");
let mut rng2 = new_rng(12345, "test");
// The first value from both RNGs should be the same
assert_eq!(rng1.clone().next_u64(), rng2.clone().next_u64());
assert_eq!(rng1.next_u64(), rng2.next_u64());
}

#[test]
Expand Down
Loading