commit just in case

This commit is contained in:
Guilleag01
2023-08-12 12:57:14 +02:00
parent 80024609ef
commit 4c8150e703
3 changed files with 51 additions and 18 deletions

View File

@@ -17,6 +17,10 @@ rand = "0.8.5"
wasm-logger = "0.2.0" wasm-logger = "0.2.0"
log = "0.4.19" log = "0.4.19"
regex = "1.9.3" regex = "1.9.3"
timer = "0.2.0"
rayon = "1.7.0"
time = "0.3.25"
chrono = "0.4.26"
[workspace] [workspace]
members = ["src-tauri"] members = ["src-tauri"]

View File

@@ -14,6 +14,12 @@ use minesweeper_ui::components::board::BoardComponent;
use minesweeper_ui::minesweeper::{cell::Cell, Game}; use minesweeper_ui::minesweeper::{cell::Cell, Game};
// use timer::Timer;
// use chrono::Duration;
// use std::marker::Sync;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "tauri"])] #[wasm_bindgen(js_namespace = ["window", "__TAURI__", "tauri"])]
@@ -28,6 +34,7 @@ pub enum Msg {
UpdateHeight, UpdateHeight,
UpdateWidth, UpdateWidth,
UpdateMines, UpdateMines,
// AddTime { time: usize },
} }
pub struct App { pub struct App {
@@ -37,6 +44,7 @@ pub struct App {
width: usize, width: usize,
num_mines: usize, num_mines: usize,
show_settings: bool, show_settings: bool,
// timer: Timer,
} }
impl Component for App { impl Component for App {
@@ -45,20 +53,22 @@ impl Component for App {
type Properties = (); type Properties = ();
fn create(ctx: &Context<Self>) -> Self { fn create(ctx: &Context<Self>) -> Self {
let height = 10; let game = Game::new(10, 10, 5);
let width = 10;
let num_mines = height * width / 10;
let game = Game::new(height, width, 5);
// game.start_board(); // game.start_board();
// let t = Timer::new();
// t.schedule_repeating(Duration::new(1, 0), || {
// Callback(||)
// });
Self { Self {
link: ctx.link().clone(), link: ctx.link().clone(),
game, game,
height, height: 10,
width, width: 10,
num_mines, num_mines: 5,
show_settings: false, show_settings: false,
// timer: Timer::new(),
} }
} }
@@ -84,7 +94,7 @@ impl Component for App {
{"Reset"} {"Reset"}
</button> </button>
<div class="time"> <div class="time">
{"00:00"} {self.game.get_time()}
</div> </div>
<button <button
id="open-settings" id="open-settings"
@@ -116,9 +126,9 @@ impl Component for App {
</div> </div>
</div> </div>
<div class="preset-settings"> <div class="preset-settings">
<button class="preset-setting">{"Easy"}</button> <button class="preset-setting">{"Easy (WIP)"}</button>
<button class="preset-setting">{"Normal"}</button> <button class="preset-setting">{"Normal (WIP)"}</button>
<button class="preset-setting">{"Hard"}</button> <button class="preset-setting">{"Hard (WIP)"}</button>
</div> </div>
</div> </div>
</div> </div>
@@ -153,7 +163,7 @@ impl Component for App {
.value(); .value();
let re = Regex::new( 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(); .unwrap();
let mut value: usize = self.num_mines; let mut value: usize = self.num_mines;
@@ -182,7 +192,7 @@ impl Component for App {
} }
Msg::Reset => { Msg::Reset => {
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
// self.game.start_board(); self.game.set_time(0);
} }
Msg::ToggleSettings => { Msg::ToggleSettings => {
self.show_settings = !self.show_settings; self.show_settings = !self.show_settings;
@@ -203,7 +213,7 @@ impl Component for App {
let mines = get_mines(); let mines = get_mines();
self.num_mines = mines.min(self.height * self.width); self.num_mines = mines.min(self.height * self.width);
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
// self.game.start_board(); self.game.set_time(0);
} }
} }
Msg::UpdateWidth => { Msg::UpdateWidth => {
@@ -222,7 +232,7 @@ impl Component for App {
let mines = get_mines(); let mines = get_mines();
self.num_mines = mines.min(self.height * self.width); self.num_mines = mines.min(self.height * self.width);
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
// self.game.start_board(); self.game.set_time(0);
} }
} }
Msg::UpdateMines => { Msg::UpdateMines => {
@@ -230,9 +240,11 @@ impl Component for App {
if mines <= self.height * self.width { if mines <= self.height * self.width {
self.num_mines = mines; self.num_mines = mines;
self.game = Game::new(self.height, self.width, self.num_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 true
} }

View File

@@ -1,6 +1,8 @@
pub mod board; pub mod board;
pub mod cell; pub mod cell;
use std::sync::{Arc, Mutex};
use board::Board; use board::Board;
use cell::Cell; use cell::Cell;
use rand::Rng; use rand::Rng;
@@ -13,6 +15,7 @@ pub struct Game {
board: Board, board: Board,
first_interaction: bool, first_interaction: bool,
gamestate: usize, gamestate: usize,
time: Arc<Mutex<usize>>,
} }
impl Game { impl Game {
@@ -21,6 +24,7 @@ impl Game {
board: Board::new(height, width, num_mines), board: Board::new(height, width, num_mines),
first_interaction: false, first_interaction: false,
gamestate: 0, gamestate: 0,
time: Arc::new(Mutex::new(0)),
} }
} }
@@ -81,6 +85,7 @@ impl Game {
for k in 0..cells_to_show.len() { for k in 0..cells_to_show.len() {
let pos = cells_to_show[k]; let pos = cells_to_show[k];
for i in -1..=1 { for i in -1..=1 {
for j in -1..=1 { for j in -1..=1 {
let new_pos = let new_pos =
@@ -167,4 +172,16 @@ impl Game {
pub fn get_gamestate(&self) -> usize { pub fn get_gamestate(&self) -> usize {
self.gamestate 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
}
} }