solution saving and loading
This commit is contained in:
parent
c4381ac1a1
commit
0c2d241745
6 changed files with 72 additions and 21 deletions
50
src/main.rs
50
src/main.rs
|
@ -13,7 +13,6 @@ mod util;
|
|||
|
||||
use editor::{Editor, ExitState};
|
||||
use level::Level;
|
||||
use marble_engine::board::Board;
|
||||
use solution::Solution;
|
||||
use util::*;
|
||||
|
||||
|
@ -40,8 +39,6 @@ fn main() {
|
|||
|
||||
let mut game = Game::new(&mut rl, &thread);
|
||||
game.run(&mut rl, &thread);
|
||||
// let board = Board::parse(&read_to_string("boards/adder.mbl").unwrap());
|
||||
// game.load_board(board);
|
||||
}
|
||||
|
||||
impl Game {
|
||||
|
@ -52,7 +49,7 @@ impl Game {
|
|||
|
||||
Self {
|
||||
levels: get_levels(),
|
||||
solutions: HashMap::new(),
|
||||
solutions: get_solutions(),
|
||||
open_editor: None,
|
||||
textures,
|
||||
selected_level: 0,
|
||||
|
@ -69,13 +66,20 @@ impl Game {
|
|||
editor.draw(&mut d, &self.textures);
|
||||
match editor.get_exit_state() {
|
||||
ExitState::Dont => (),
|
||||
ExitState::ExitNoSave => self.open_editor = None,
|
||||
ExitState::ExitAndSave => {
|
||||
self.solutions.get_mut(editor.level_id()).unwrap()
|
||||
[self.selected_solution]
|
||||
.board = editor.source_board().to_string();
|
||||
let solution = &mut self.solutions.get_mut(editor.level_id()).unwrap()
|
||||
[self.selected_solution];
|
||||
solution.board = editor.source_board().to_string();
|
||||
solution.save();
|
||||
self.open_editor = None;
|
||||
}
|
||||
ExitState::Save => {
|
||||
let solution = &mut self.solutions.get_mut(editor.level_id()).unwrap()
|
||||
[self.selected_solution];
|
||||
solution.board = editor.source_board().to_string();
|
||||
solution.save();
|
||||
}
|
||||
ExitState::ExitNoSave => self.open_editor = None,
|
||||
}
|
||||
} else {
|
||||
self.draw(&mut d);
|
||||
|
@ -227,3 +231,33 @@ fn get_levels() -> Vec<Level> {
|
|||
levels.sort_by(|a, b| a.id().cmp(b.id()));
|
||||
levels
|
||||
}
|
||||
|
||||
fn get_solutions() -> HashMap<String, Vec<Solution>> {
|
||||
let mut levels = HashMap::new();
|
||||
let solution_dir = userdata_dir().join("solutions");
|
||||
if let Ok(dir_contents) = read_dir(solution_dir) {
|
||||
for dir in dir_contents.flatten() {
|
||||
if dir.path().is_dir() {
|
||||
let level_name = dir.file_name().to_string_lossy().to_string();
|
||||
let mut solutions = Vec::new();
|
||||
|
||||
if let Ok(files) = read_dir(dir.path()) {
|
||||
for file in files.flatten() {
|
||||
let s = read_to_string(file.path())
|
||||
.ok()
|
||||
.as_deref()
|
||||
.map(|s| serde_json::from_str(s).ok())
|
||||
.flatten();
|
||||
if let Some(solution) = s {
|
||||
solutions.push(solution)
|
||||
}
|
||||
}
|
||||
|
||||
levels.insert(level_name, solutions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
levels
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue