diff --git a/Cargo.toml b/Cargo.toml index 27a15b5..adabc18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,10 @@ rand = "0.8.5" wasm-logger = "0.2.0" log = "0.4.19" regex = "1.9.3" +timer = "0.2.0" +rayon = "1.7.0" +time = "0.3.25" +chrono = "0.4.26" [workspace] members = ["src-tauri"] diff --git a/src/app.rs b/src/app.rs index ad42036..3f2b207 100644 --- a/src/app.rs +++ b/src/app.rs @@ -14,6 +14,12 @@ use minesweeper_ui::components::board::BoardComponent; use minesweeper_ui::minesweeper::{cell::Cell, Game}; +// use timer::Timer; + +// use chrono::Duration; + +// use std::marker::Sync; + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = ["window", "__TAURI__", "tauri"])] @@ -28,6 +34,7 @@ pub enum Msg { UpdateHeight, UpdateWidth, UpdateMines, + // AddTime { time: usize }, } pub struct App { @@ -37,6 +44,7 @@ pub struct App { width: usize, num_mines: usize, show_settings: bool, + // timer: Timer, } impl Component for App { @@ -45,20 +53,22 @@ impl Component for App { type Properties = (); fn create(ctx: &Context) -> Self { - let height = 10; - let width = 10; - let num_mines = height * width / 10; - - let game = Game::new(height, width, 5); + let game = Game::new(10, 10, 5); // game.start_board(); + // let t = Timer::new(); + // t.schedule_repeating(Duration::new(1, 0), || { + // Callback(||) + // }); + Self { link: ctx.link().clone(), game, - height, - width, - num_mines, + height: 10, + width: 10, + num_mines: 5, show_settings: false, + // timer: Timer::new(), } } @@ -84,7 +94,7 @@ impl Component for App { {"Reset"}
- {"00:00"} + {self.game.get_time()}
- - + + + @@ -153,7 +163,7 @@ impl Component for App { .value(); let re = Regex::new( - "^((0+)|((0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)))$", + "^((0+)|(1|[2-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|[1-9][0-9]{4}|[1-8][0-9]{5}|9[0-8][0-9]{4}|99[0-7][0-9]{3}|998[0--1][0-9]{2}|9980[0--1][0-9]|99800[0-0]|998001))$", ) .unwrap(); let mut value: usize = self.num_mines; @@ -182,7 +192,7 @@ impl Component for App { } Msg::Reset => { self.game = Game::new(self.height, self.width, self.num_mines); - // self.game.start_board(); + self.game.set_time(0); } Msg::ToggleSettings => { self.show_settings = !self.show_settings; @@ -203,7 +213,7 @@ impl Component for App { let mines = get_mines(); self.num_mines = mines.min(self.height * self.width); self.game = Game::new(self.height, self.width, self.num_mines); - // self.game.start_board(); + self.game.set_time(0); } } Msg::UpdateWidth => { @@ -222,7 +232,7 @@ impl Component for App { let mines = get_mines(); self.num_mines = mines.min(self.height * self.width); self.game = Game::new(self.height, self.width, self.num_mines); - // self.game.start_board(); + self.game.set_time(0); } } Msg::UpdateMines => { @@ -230,9 +240,11 @@ impl Component for App { if mines <= self.height * self.width { self.num_mines = mines; self.game = Game::new(self.height, self.width, self.num_mines); - // self.game.start_board(); + self.game.set_time(0); } - } + } // Msg::AddTime { time } => { + // self.game.add_time(time); + // } } true } diff --git a/src/minesweeper/mod.rs b/src/minesweeper/mod.rs index 3bc8e3f..fbbf201 100644 --- a/src/minesweeper/mod.rs +++ b/src/minesweeper/mod.rs @@ -1,6 +1,8 @@ pub mod board; pub mod cell; +use std::sync::{Arc, Mutex}; + use board::Board; use cell::Cell; use rand::Rng; @@ -13,6 +15,7 @@ pub struct Game { board: Board, first_interaction: bool, gamestate: usize, + time: Arc>, } impl Game { @@ -21,6 +24,7 @@ impl Game { board: Board::new(height, width, num_mines), first_interaction: false, gamestate: 0, + time: Arc::new(Mutex::new(0)), } } @@ -81,6 +85,7 @@ impl Game { for k in 0..cells_to_show.len() { let pos = cells_to_show[k]; + for i in -1..=1 { for j in -1..=1 { let new_pos = @@ -167,4 +172,16 @@ impl Game { pub fn get_gamestate(&self) -> usize { self.gamestate } + + pub fn get_time(&self) -> usize { + *self.time.lock().unwrap() + } + + pub fn set_time(&mut self, time: usize) { + *self.time.lock().unwrap() = time + } + + pub fn add_time(&mut self, time: usize) { + *self.time.lock().unwrap() += time + } }