Skip to content

Commit

Permalink
save game statistics
Browse files Browse the repository at this point in the history
this is a work in progress
  • Loading branch information
9034725985 committed Aug 8, 2024
1 parent 3767694 commit 4205515
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/game_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ pub enum GameError {
Reqwest(#[from] reqwest::Error),
#[error("IO error")]
Io(#[from] std::io::Error),
#[error("Serde JSON error")]
SerdeJson(#[from] serde_json::Error),
}
26 changes: 25 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ fn main() -> Result<(), GameError> {
return Ok(());
}

if args.len() > 1 && args[1] == "--show-stats" {
let game_stats = read_game_stats()?;
println!("Game Statistics:");
println!("Attempts: {:?}", game_stats.attempts);
println!("Secret Number: {}", game_stats.secret_number);
println!("Guesses: {:?}", game_stats.guesses);
return Ok(());
}

play_guessing_game(config.analytics_consent)?;
Ok(())
}
Expand All @@ -59,7 +68,7 @@ fn play_guessing_game(analytics_consent: bool) -> Result<(), GameError> {

let secret_number = rand::thread_rng().gen_range(1..=100);
let mut attempts = Vec::new(); // Initialize attempts as a vector
let guesses = Vec::new();
let mut guesses = Vec::new(); // Initialize guesses as a mutable vector

loop {
println!("Please input your guess.");
Expand All @@ -76,6 +85,7 @@ fn play_guessing_game(analytics_consent: bool) -> Result<(), GameError> {
};

attempts.push(guess); // Add each guess to attempts
guesses.push(guess); // Add each guess to guesses

println!("You guessed: {guess}");

Expand Down Expand Up @@ -142,3 +152,17 @@ fn fetch_hello_world() -> Result<(), GameError> {
}
Ok(())
}

fn read_game_stats() -> Result<GameStats, GameError> {
let strategy = choose_app_strategy(AppStrategyArgs {
top_level_domain: "org".to_string(),
author: "Kushal Hada".to_string(),
app_name: "KusGuessingGame".to_string(),
}).unwrap();

let stats_path = strategy.data_dir().join("game_stats.json");
let stats_data = fs::read_to_string(&stats_path)?;
let game_stats: GameStats = serde_json::from_str(&stats_data)?;

Ok(game_stats)
}

0 comments on commit 4205515

Please sign in to comment.