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 c1a00ec commit 3767694
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions src/game_error.rs
Original file line number Diff line number Diff line change
@@ -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),
}
9 changes: 9 additions & 0 deletions src/game_stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// src/game_stats.rs
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct GameStats {
pub attempts: Vec<u32>,
pub secret_number: u32,
pub guesses: Vec<u32>,
}
38 changes: 34 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ 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;
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(),
Expand Down Expand Up @@ -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.");
Expand All @@ -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) {
Expand All @@ -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) => {
Expand Down

0 comments on commit 3767694

Please sign in to comment.