Skip to content

Commit

Permalink
feat: wpm scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed May 13, 2023
1 parent 0bb6621 commit cf831e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
6 changes: 0 additions & 6 deletions src/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct Algorithm {
pub version: &'static str,
pub description: &'static str,
pub lang: &'static str,
pub out_size: &'static u32,
pub random_function: &'static dyn Fn(u32) -> String,
}

Expand All @@ -25,39 +24,34 @@ pub const ALGS: [Algorithm; 5] = [
version: "0.2",
description: "some letters :)",
lang: "western",
out_size: &1,
random_function: &random_letters_inner,
},
Algorithm {
id: "words",
version: "0.1",
description: "some english words",
lang: "eng",
out_size: &2,
random_function: &random_english_words,
},
Algorithm {
id: "sentences",
version: "0.1",
description: "some english sentences",
lang: "eng",
out_size: &3,
random_function: &random_english_sentences,
},
Algorithm {
id: "symbols",
version: "0.1",
description: "some symbols",
lang: "well not really human",
out_size: &2,
random_function: &random_symbols,
},
Algorithm {
id: "rust_fun",
version: "0.1",
description: "a rust function signutare",
lang: "rust",
out_size: &10,
random_function: &random_function,
},
];
Expand Down
31 changes: 17 additions & 14 deletions src/scoring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ use egui::plot::{Line, PlotPoints};
#[derive(serde::Deserialize, serde::Serialize, Default)]
pub struct Score {
score: u128,
score_per_duration: ScorePerDuration,
score_per_duration: WordsPerDuration,
score_plot_state: Vec<[f64; 2]>,
}

impl Score {
pub fn render_scoring(&mut self, ui: &mut Ui) {
ui.collapsing("Statistics", |ui| {
ui.heading(format!("Score {}", self.score));
ui.heading(format!("words {}", self.score));
if ui.button("reset").clicked() {
self.score = 0;
self.score_plot_state.clear();
self.score_per_duration = Default::default()
};
ui.vertical(|ui| {
let score_points: PlotPoints = PlotPoints::from(self.score_plot_state.clone());

Expand All @@ -32,10 +37,7 @@ impl Score {
"elapsed seconds {}",
self.score_per_duration.elapsed.as_secs_f64()
));
ui.label(format!(
"average score per second {}",
self.score_per_duration.avg
));
ui.label(format!("average wpm {}", self.score_per_duration.avg));
});
});
}
Expand All @@ -44,12 +46,13 @@ impl Score {
self.score_per_duration.ready()
}

pub fn set(&mut self) {
self.score_per_duration.set(self.score);
pub fn set(&mut self, settings: &TFSetting) {
self.score_per_duration
.set((settings.current_challenge_len.div(6)) as u128);
}

pub fn won(&mut self, settings: &TFSetting) {
self.score += (settings.level.out_size * settings.size) as u128;
self.score += (settings.current_challenge_len.div(6)) as u128;

self.score_plot_state.push([
self.score_plot_state.len() as f64,
Expand All @@ -59,7 +62,7 @@ impl Score {
}

#[derive(serde::Deserialize, serde::Serialize)]
pub struct ScorePerDuration {
pub struct WordsPerDuration {
elapsed: Duration,
#[serde(skip)]
start: Time,
Expand All @@ -68,7 +71,7 @@ pub struct ScorePerDuration {
avg: f64,
}

impl Default for ScorePerDuration {
impl Default for WordsPerDuration {
fn default() -> Self {
Self {
elapsed: Duration::from_millis(0),
Expand All @@ -80,15 +83,15 @@ impl Default for ScorePerDuration {
}
}

impl ScorePerDuration {
impl WordsPerDuration {
pub fn ready(&mut self) {
self.start = Time::now();
}

pub fn set(&mut self, score: u128) {
pub fn set(&mut self, words: u128) {
self.elapsed = self.start.0.elapsed();

self.state = (score as f64).div(self.elapsed.as_secs_f64());
self.state = (words as f64).div(self.elapsed.as_secs_f64()) * 60.0;

self.history.push(self.state);
self.avg = average(&self.history);
Expand Down
8 changes: 0 additions & 8 deletions src/settings/level_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ pub fn render(level: &Algorithm, ui: &mut Ui) {
ui.label(level.lang);
});
});
body.row(30.0, |mut row| {
row.col(|ui| {
ui.label("score multiplier");
});
row.col(|ui| {
ui.label(level.out_size.to_string());
});
});
});
});
}
5 changes: 2 additions & 3 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct TFSetting {
pub level: Algorithm,
pub size: u32,
level_changed: bool,
pub current_challenge_len: u32,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -56,6 +57,7 @@ impl Default for TFSetting {
level_changed: false,
last_theme: TFTheme::Default,
theme: TFTheme::Macchiato,
current_challenge_len: 0,
}
}
}
Expand Down Expand Up @@ -128,7 +130,6 @@ impl TFSetting {
version: "0",
description: "this algorithm does not exist",
lang: "mhh",
out_size: &0,
random_function: &none,
},
};
Expand Down Expand Up @@ -175,7 +176,6 @@ mod tests {
version: "",
description: "",
lang: "",
out_size: &3,
random_function: &none,
}
}
Expand All @@ -186,7 +186,6 @@ mod tests {
version: "",
description: "",
lang: "",
out_size: &3,
random_function: &none,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/typewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ impl TypeState {
}
}
State::Won => {
settings.current_challenge_len = self.challenge.len() as u32;
self.state = State::Reset;
score.won(settings);
score.set();
score.set(settings);
}
State::Reset => {
self.challenge = settings.provide_next_string().to_challenge();
Expand Down

0 comments on commit cf831e1

Please sign in to comment.