atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
2026362c76a617af2cdca92573dd85e96c0abfb0
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/config.rs

159f0998263batagen2026-10-12 11:01 UTC1
use std::{path::PathBuf, time::Duration};
782069860236atagen2026-10-10 12:44 UTC2
3f9091557ec3atagen2026-10-11 11:43 UTC3
use crate::game::oracle::OracleTemplate;
782069860236atagen2026-10-10 12:44 UTC4
use crate::secret::Secret;
782069860236atagen2026-10-10 12:44 UTC5
782069860236atagen2026-10-10 12:44 UTC6
pub struct Config {
782069860236atagen2026-10-10 12:44 UTC7
    pub addr: String,
782069860236atagen2026-10-10 12:44 UTC8
    pub secret: Secret,
782069860236atagen2026-10-10 12:44 UTC9
    pub max_runs: usize,
782069860236atagen2026-10-10 12:44 UTC10
    pub idle_ttl: Duration,
159f0998263batagen2026-10-12 11:01 UTC11
    /// Where finished runs are kept. `None` keeps them in memory, which only tests want.
159f0998263batagen2026-10-12 11:01 UTC12
    pub db: Option<PathBuf>,
3f9091557ec3atagen2026-10-11 11:43 UTC13
    pub(crate) oracle: OracleTemplate,
782069860236atagen2026-10-10 12:44 UTC14
}
782069860236atagen2026-10-10 12:44 UTC15
782069860236atagen2026-10-10 12:44 UTC16
impl Default for Config {
782069860236atagen2026-10-10 12:44 UTC17
    fn default() -> Self {
782069860236atagen2026-10-10 12:44 UTC18
        Self {
782069860236atagen2026-10-10 12:44 UTC19
            addr: "127.0.0.1:6969".into(),
782069860236atagen2026-10-10 12:44 UTC20
            secret: Secret::random(),
782069860236atagen2026-10-10 12:44 UTC21
            max_runs: 10_000,
782069860236atagen2026-10-10 12:44 UTC22
            idle_ttl: Duration::from_secs(6 * 60 * 60),
159f0998263batagen2026-10-12 11:01 UTC23
            db: None,
3f9091557ec3atagen2026-10-11 11:43 UTC24
            oracle: OracleTemplate::Rng,
782069860236atagen2026-10-10 12:44 UTC25
        }
782069860236atagen2026-10-10 12:44 UTC26
    }
782069860236atagen2026-10-10 12:44 UTC27
}
782069860236atagen2026-10-10 12:44 UTC28
8b8e83329a1datagen2026-10-12 12:01 UTC29
/// The XDG state directory, so `nix run` from anywhere finds the same graveyard.
8b8e83329a1datagen2026-10-12 12:01 UTC30
fn default_db() -> PathBuf {
8b8e83329a1datagen2026-10-12 12:01 UTC31
    let state = std::env::var_os("XDG_STATE_HOME")
8b8e83329a1datagen2026-10-12 12:01 UTC32
        .map(PathBuf::from)
8b8e83329a1datagen2026-10-12 12:01 UTC33
        .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/state")));
8b8e83329a1datagen2026-10-12 12:01 UTC34
    match state {
8b8e83329a1datagen2026-10-12 12:01 UTC35
        Some(dir) => dir.join("jelqd").join("jelqd.sqlite"),
8b8e83329a1datagen2026-10-12 12:01 UTC36
        None => PathBuf::from("jelqd.sqlite"),
8b8e83329a1datagen2026-10-12 12:01 UTC37
    }
8b8e83329a1datagen2026-10-12 12:01 UTC38
}
8b8e83329a1datagen2026-10-12 12:01 UTC39
782069860236atagen2026-10-10 12:44 UTC40
impl Config {
782069860236atagen2026-10-10 12:44 UTC41
    pub fn from_env() -> Self {
782069860236atagen2026-10-10 12:44 UTC42
        let var = |name| std::env::var(name).ok();
782069860236atagen2026-10-10 12:44 UTC43
        let defaults = Self::default();
782069860236atagen2026-10-10 12:44 UTC44
        Self {
782069860236atagen2026-10-10 12:44 UTC45
            addr: var("JELQD_ADDR").unwrap_or(defaults.addr),
8b8e83329a1datagen2026-10-12 12:01 UTC46
            db: Some(var("JELQD_DB").map_or_else(default_db, PathBuf::from)),
782069860236atagen2026-10-10 12:44 UTC47
            secret: var("JELQD_SECRET").map_or(defaults.secret, |s| Secret::new(s.into_bytes())),
3f9091557ec3atagen2026-10-11 11:43 UTC48
            oracle: match var("JELQD_ORACLE").as_deref() {
3f9091557ec3atagen2026-10-11 11:43 UTC49
                Some("clm") => OracleTemplate::Clm {
3f9091557ec3atagen2026-10-11 11:43 UTC50
                    url: var("JELQD_CLM_URL")
3f9091557ec3atagen2026-10-11 11:43 UTC51
                        .unwrap_or_else(|| "http://127.0.0.1:8700/v1/systemone".into()),
3f9091557ec3atagen2026-10-11 11:43 UTC52
                },
3f9091557ec3atagen2026-10-11 11:43 UTC53
                _ => defaults.oracle,
3f9091557ec3atagen2026-10-11 11:43 UTC54
            },
782069860236atagen2026-10-10 12:44 UTC55
            ..defaults
782069860236atagen2026-10-10 12:44 UTC56
        }
782069860236atagen2026-10-10 12:44 UTC57
    }
782069860236atagen2026-10-10 12:44 UTC58
}