From 37676944159733a3881f851b8a44ff6cb23945ee Mon Sep 17 00:00:00 2001 From: Kushal Date: Thu, 8 Aug 2024 15:31:16 -0400 Subject: [PATCH] save game statistics this is a work in progress --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/game_error.rs | 9 +++++++++ src/game_stats.rs | 9 +++++++++ src/main.rs | 38 ++++++++++++++++++++++++++++++++++---- 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/game_error.rs create mode 100644 src/game_stats.rs diff --git a/Cargo.lock b/Cargo.lock index 1a65a8e..188def7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -282,6 +282,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "thiserror", ] [[package]] @@ -957,6 +958,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tinyvec" version = "1.8.0" diff --git a/Cargo.toml b/Cargo.toml index 20e3d0c..e179e1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ dotenv = "0.15" etcetera = "0.8.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +thiserror = "1.0.63" diff --git a/src/game_error.rs b/src/game_error.rs new file mode 100644 index 0000000..7ccc0d5 --- /dev/null +++ b/src/game_error.rs @@ -0,0 +1,9 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum GameError { + #[error("Reqwest error")] + Reqwest(#[from] reqwest::Error), + #[error("IO error")] + Io(#[from] std::io::Error), +} diff --git a/src/game_stats.rs b/src/game_stats.rs new file mode 100644 index 0000000..755e7ec --- /dev/null +++ b/src/game_stats.rs @@ -0,0 +1,9 @@ +// src/game_stats.rs +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct GameStats { + pub attempts: Vec, + pub secret_number: u32, + pub guesses: Vec, +} diff --git a/src/main.rs b/src/main.rs index 1c273f9..7d83ce4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ mod my_math; mod shadowing; mod my_data_types; mod greatest_common_divisor; +mod game_stats; +mod game_error; use rand::Rng; use std::cmp::Ordering; @@ -9,16 +11,17 @@ use std::env; use std::fs; use std::io; use reqwest::blocking::Client; -use reqwest::Error; use etcetera::{choose_app_strategy, AppStrategyArgs, AppStrategy}; use serde::{Deserialize, Serialize}; +use game_stats::GameStats; +use game_error::GameError; #[derive(Serialize, Deserialize)] struct Config { analytics_consent: bool, } -fn main() -> Result<(), Error> { +fn main() -> Result<(), GameError> { let strategy = choose_app_strategy(AppStrategyArgs { top_level_domain: "org".to_string(), author: "Kushal Hada".to_string(), @@ -50,11 +53,13 @@ fn main() -> Result<(), Error> { Ok(()) } -fn play_guessing_game(analytics_consent: bool) -> Result<(), Error> { +fn play_guessing_game(analytics_consent: bool) -> Result<(), GameError> { println!("Guess the number!"); println!("Remember, you can update your consent by running this application with the --update-consent flag."); let secret_number = rand::thread_rng().gen_range(1..=100); + let mut attempts = Vec::new(); // Initialize attempts as a vector + let guesses = Vec::new(); loop { println!("Please input your guess."); @@ -70,6 +75,8 @@ fn play_guessing_game(analytics_consent: bool) -> Result<(), Error> { Err(_) => continue, }; + attempts.push(guess); // Add each guess to attempts + println!("You guessed: {guess}"); match guess.cmp(&secret_number) { @@ -96,10 +103,33 @@ fn play_guessing_game(analytics_consent: bool) -> Result<(), Error> { println!("The greatest common divisor of your guess and the secret number is: {gcd}"); } + let game_stats = GameStats { + attempts, + secret_number, + guesses, + }; + + save_game_stats(&game_stats)?; + + Ok(()) +} + +fn save_game_stats(game_stats: &GameStats) -> Result<(), 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 = serde_json::to_string(&game_stats).expect("Unable to serialize game stats"); + fs::create_dir_all(strategy.data_dir()).expect("Unable to create data directory"); + fs::write(stats_path, stats_data)?; + Ok(()) } -fn fetch_hello_world() -> Result<(), Error> { +fn fetch_hello_world() -> Result<(), GameError> { let client = Client::new(); match client.get("https://nice.runasp.net/Analytics/HelloWorld").send() { Ok(response) => {