Skip to content

Commit

Permalink
refactor: refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed May 19, 2024
1 parent 57705e4 commit 1284b35
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 1,016 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
uses: taiki-e/install-action@cargo-llvm-cov

- name: Install cargo-audit
run: cargo install cargo-audit
run: cargo install --force cargo-audit

- name: Audit
run: cargo audit
Expand Down
61 changes: 20 additions & 41 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::level::Level;
use crate::Tile;

use nalgebra::Vector2;
use soukoban::{direction::Direction, Action, Actions};
use soukoban::{direction::Direction, Action, Actions, Level, Tiles};

#[derive(Clone)]
pub struct Board {
Expand All @@ -23,21 +20,13 @@ impl Board {

/// Checks if the player can move or push in the specified direction.
pub fn moveable(&self, direction: Direction) -> bool {
let player_next_position = self.level.player_position + &direction.into();
if self.level.get(&player_next_position).intersects(Tile::Wall) {
let player_next_position = self.level.player_position() + &direction.into();
if self.level[player_next_position].intersects(Tiles::Wall) {
return false;
}
if self
.level
.get(&player_next_position)
.intersects(Tile::Crate)
{
if self.level[player_next_position].intersects(Tiles::Box) {
let crate_next_position = player_next_position + &direction.into();
if self
.level
.get(&crate_next_position)
.intersects(Tile::Crate | Tile::Wall)
{
if self.level[crate_next_position].intersects(Tiles::Box | Tiles::Wall) {
return false;
}
}
Expand All @@ -47,21 +36,13 @@ impl Board {
/// Moves the player or pushes a crate in the specified direction.
pub fn move_or_push(&mut self, direction: Direction) {
let direction_vector = &direction.into();
let player_next_position = self.level.player_position + direction_vector;
if self.level.get(&player_next_position).intersects(Tile::Wall) {
let player_next_position = self.level.player_position() + direction_vector;
if self.level[player_next_position].intersects(Tiles::Wall) {
return;
}
if self
.level
.get(&player_next_position)
.intersects(Tile::Crate)
{
if self.level[player_next_position].intersects(Tiles::Box) {
let crate_next_position = player_next_position + direction_vector;
if self
.level
.get(&crate_next_position)
.intersects(Tile::Wall | Tile::Crate)
{
if self.level[crate_next_position].intersects(Tiles::Wall | Tiles::Box) {
return;
}
self.move_crate(player_next_position, crate_next_position);
Expand Down Expand Up @@ -91,10 +72,10 @@ impl Board {
let history = self.actions.pop().unwrap();
let direction = history.direction();
if history.is_push() {
let crate_position = self.level.player_position + &direction.into();
self.move_crate(crate_position, self.level.player_position);
let crate_position = self.level.player_position() + &direction.into();
self.move_crate(crate_position, self.level.player_position());
}
let player_prev_position = self.level.player_position - &direction.into();
let player_prev_position = self.level.player_position() - &direction.into();
self.move_player(player_prev_position);
self.undone_actions.push(history);
}
Expand All @@ -121,7 +102,7 @@ impl Board {

/// Checks if the level is solved.
pub fn is_solved(&self) -> bool {
self.level.crate_positions == self.level.target_positions
self.level.box_positions() == self.level.goal_positions()
}

pub fn actions(&self) -> &Actions {
Expand All @@ -137,17 +118,15 @@ impl Board {
}

fn move_player(&mut self, to: Vector2<i32>) {
self.level
.get_mut(&self.level.player_position.clone())
.remove(Tile::Player);
self.level.get_mut(&to).insert(Tile::Player);
self.level.player_position = to;
let player_position = self.level.player_position();
self.level[player_position].remove(Tiles::Player);
self.level[to].insert(Tiles::Player);
self.level.set_player_position(to);
}

fn move_crate(&mut self, from: Vector2<i32>, to: Vector2<i32>) {
self.level.get_mut(&from).remove(Tile::Crate);
self.level.get_mut(&to).insert(Tile::Crate);
self.level.crate_positions.remove(&from);
self.level.crate_positions.insert(to);
self.level[from].remove(Tiles::Box);
self.level[to].insert(Tiles::Box);
self.level.set_box_position(from, to);
}
}
38 changes: 14 additions & 24 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::str::FromStr;

use crate::level::Level;

use nalgebra::Vector2;
use rusqlite::Connection;
use siphasher::sip::SipHasher24;
use soukoban::Actions;
use soukoban::{Actions, Level};

pub struct Database {
connection: Connection,
Expand Down Expand Up @@ -75,14 +71,14 @@ impl Database {

/// Imports a single level into the database.
pub fn import_level(&self, level: &Level) {
let title = level.metadata.get("title");
let author = level.metadata.get("author");
let comments = level.metadata.get("comments");
let title = level.metadata().get("title");
let author = level.metadata().get("author");
let comments = level.metadata().get("comments");
let hash = Database::normalized_hash(level);

let _ = self.connection.execute(
"INSERT INTO tb_level(title, author, comments, map, width, height, hash, date) VALUES (?, ?, ?, ?, ?, ?, ?, DATE('now'))",
(title, author, comments, level.export_map(), level.dimensions().x, level.dimensions().y, hash),
(title, author, comments, level.map().to_string(), level.dimensions().x, level.dimensions().y, hash),
);
}

Expand Down Expand Up @@ -110,24 +106,18 @@ impl Database {
let mut rows = statement.query([id]).unwrap();
let row = rows.next().unwrap()?;

let map = row
.get::<_, String>(0)
.unwrap()
.split('\n')
.map(|x| x.to_string())
.collect();
let size = Vector2::new(row.get(1).unwrap(), row.get(2).unwrap());
let mut metadata = HashMap::new();
if let Ok(title) = row.get(3) {
metadata.insert("title".to_string(), title);
let map = row.get::<_, String>(0).unwrap();
let mut metadata = String::new();
if let Ok(title) = row.get::<_, String>(3) {
metadata.push_str(&format!("title: {title}\n"));
}
if let Ok(author) = row.get(4) {
metadata.insert("author".to_string(), author);
if let Ok(author) = row.get::<_, String>(4) {
metadata.push_str(&format!("author: {author}\n"));
}
if let Ok(comments) = row.get(5) {
metadata.insert("comments".to_string(), comments);
if let Ok(comments) = row.get::<_, String>(5) {
metadata.push_str(&format!("comments: {comments}\n"));
}
let level = Level::new(map, size, metadata).unwrap();
let level = Level::from_str(&(map + &metadata)).unwrap();
Some(level)
}

Expand Down
Loading

0 comments on commit 1284b35

Please sign in to comment.