Skip to content

Commit

Permalink
make difficulty persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
msuesskraut committed Dec 25, 2018
1 parent 459466e commit 701ca6a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use binoxxo::field::Field;
use crate::model::{Difficulty, Model};

pub const DIFFICULTY_STORAGE : &'static str = "Binoxxo-Difficulty";

#[derive(Clone, Debug)]
pub struct CellPos {
pub col: usize,
Expand Down Expand Up @@ -35,6 +37,11 @@ fn toggle_field(model: Model, pos: CellPos) -> Model {
fn new_game(difficulty: Difficulty) -> Model {
let model = Model::new(difficulty);
seed::log!(model.board.to_string());
let storage = seed::storage::get_storage();
if let Some(storage) = storage {
seed::log!(format!("Store {} = {}", DIFFICULTY_STORAGE, difficulty));
seed::storage::store_data(&storage, DIFFICULTY_STORAGE, &difficulty);
}
model
}

Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ mod model;
mod view;

use wasm_bindgen::prelude::*;
use crate::model::Model;
use crate::control::update;
use crate::model::{Model, Difficulty};
use crate::control::{update, DIFFICULTY_STORAGE};
use crate::view::view;

fn load_difficulty() -> Option<Difficulty> {
if let Some(storage) = seed::storage::get_storage() {
if let Ok(Some(loaded_serialized)) = storage.get_item(DIFFICULTY_STORAGE) {
return serde_json::from_str(&loaded_serialized).ok();
}
}
None
}

#[wasm_bindgen]
pub fn render() {
seed::run(Model::default(), update, view, "main", None);
let difficulty = load_difficulty().unwrap_or_default();
seed::run(Model::new(difficulty), update, view, "main", None);
}
4 changes: 2 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use binoxxo::field::{Board, Field};
use binoxxo::bruteforce::create_puzzle_board;
use std::fmt;
use serde_derive::{Serialize, Deserialize};

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum Difficulty {
Easy,
Medium,
Expand Down

0 comments on commit 701ca6a

Please sign in to comment.