feat(web): add solver output
This commit is contained in:
@@ -151,6 +151,31 @@ impl Solver {
|
||||
SolverJob { receiver: rc }
|
||||
}
|
||||
|
||||
pub fn evaluate_all_words_cb<F>(&self, progress: F) -> Result<Vec<WordStats>, String>
|
||||
where
|
||||
F: Fn(usize, usize) + Send + Sync + 'static,
|
||||
{
|
||||
let mut completed = 0;
|
||||
let mut result = self
|
||||
.valid_words
|
||||
.words()
|
||||
.map(|w| {
|
||||
let r = self.evaluate_word(w);
|
||||
completed += 1;
|
||||
progress(completed, self.valid_words.len());
|
||||
r
|
||||
})
|
||||
.collect::<Result<Vec<WordStats>, String>>();
|
||||
|
||||
if let Ok(result) = &mut result {
|
||||
result.sort_by(|a, b| {
|
||||
a.expected_score_after_guess
|
||||
.total_cmp(&b.expected_score_after_guess)
|
||||
});
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn evaluate_all_words(&mut self) -> Result<Vec<WordStats>, String> {
|
||||
let mut best_words: Vec<WordStats> = self
|
||||
.valid_words
|
||||
|
||||
@@ -21,6 +21,30 @@ pub enum Color {
|
||||
GREEN,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct WordStats(solver::WordStats);
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[wasm_bindgen]
|
||||
impl WordStats {
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn word(&self) -> String {
|
||||
self.0.word.clone()
|
||||
}
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn solution_probability(&self) -> f64 {
|
||||
self.0.solution_probability
|
||||
}
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn expected_information_gained(&self) -> f64 {
|
||||
self.0.expected_information_gained
|
||||
}
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn expected_score_after_guess(&self) -> f64 {
|
||||
self.0.expected_score_after_guess
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[wasm_bindgen]
|
||||
impl Solver {
|
||||
@@ -50,6 +74,22 @@ impl Solver {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn evaluate_words(
|
||||
&self,
|
||||
n: u32,
|
||||
progress: js_sys::Function,
|
||||
) -> Result<Vec<WordStats>, String> {
|
||||
Ok(self
|
||||
.0
|
||||
.evaluate_all_words_cb(move |i, n| {
|
||||
let _ = progress.call2(&JsValue::NULL, &JsValue::from(i), &JsValue::from(n));
|
||||
})?
|
||||
.into_iter()
|
||||
.take(n as usize)
|
||||
.map(|ws| WordStats(ws))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn guess_infos(&self) -> Vec<f64> {
|
||||
self.0
|
||||
.guesses()
|
||||
|
||||
Reference in New Issue
Block a user