Skip to content

Commit

Permalink
refactor opening initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Oct 1, 2023
1 parent a351ff1 commit 0f8e352
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub enum Error {
#[error("Missing reference database")]
MissingReferenceDatabase,

#[error("No opening found")]
NoOpeningFound,

#[error("No match found")]
NoMatchFound,

Expand Down
71 changes: 35 additions & 36 deletions src-tauri/src/opening.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use std::collections::HashMap;

use log::info;
use serde::{Deserialize, Serialize};
use shakmaty::{
fen::Fen,
san::San,
zobrist::{Zobrist64, ZobristHash},
CastlingMode, Chess, EnPassantMode, Position,
};
use shakmaty::{fen::Fen, san::San, Chess, EnPassantMode, Position};

use lazy_static::lazy_static;
use strsim::jaro_winkler;
Expand Down Expand Up @@ -37,23 +30,27 @@ const TSV_DATA: [&[u8]; 5] = [
];

#[tauri::command]
pub fn get_opening_from_fen(fen: &str) -> Result<&str, &str> {
let fen: Fen = fen.parse().or(Err("Invalid FEN"))?;
let pos: Chess = fen
.into_position(CastlingMode::Standard)
.or(Err("Invalid Position"))?;
let hash: Zobrist64 = pos.zobrist_hash(EnPassantMode::Legal);
pub fn get_opening_from_fen(fen: &str) -> Result<&str, Error> {
OPENINGS
.get(&hash)
.iter()
.find(|o| o.fen == fen)
.map(|o| o.name.as_str())
.ok_or("No opening found")
.ok_or(Error::NoOpeningFound)
}

pub fn get_opening_from_eco(eco: &str) -> Result<&str, Error> {
OPENINGS
.iter()
.find(|o| o.eco == eco)
.map(|o| o.name.as_str())
.ok_or(Error::NoOpeningFound)
}

#[tauri::command]
pub async fn search_opening_name(query: String) -> Result<Vec<Opening>, Error> {
let mut best_matches: Vec<(Opening, f64)> = Vec::new();

for opening in OPENINGS.values() {
for opening in OPENINGS.iter() {
if best_matches.iter().any(|(m, _)| m.name == opening.name) {
continue;
}
Expand All @@ -80,18 +77,23 @@ pub async fn search_opening_name(query: String) -> Result<Vec<Opening>, Error> {
}
}

pub fn get_opening_from_eco(eco: &str) -> Result<&str, &str> {
OPENINGS
.values()
.find(|o| o.eco == eco)
.map(|o| o.name.as_str())
.ok_or("No opening found")
}

lazy_static! {
static ref OPENINGS: HashMap<Zobrist64, Opening> = {
static ref OPENINGS: Vec<Opening> = {
info!("Initializing openings table...");
let mut map = HashMap::new();

let mut positions = vec![
Opening {
eco: "Extra".to_string(),
name: "Starting Position".to_string(),
fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string(),
},
Opening {
eco: "Extra".to_string(),
name: "Empty Board".to_string(),
fen: "8/8/8/8/8/8/8/8 w - - 0 1".to_string(),
},
];

for tsv in TSV_DATA {
let mut rdr = csv::ReaderBuilder::new().delimiter(b'\t').from_reader(tsv);
for result in rdr.deserialize() {
Expand All @@ -103,17 +105,14 @@ lazy_static! {
}
}
let fen = Fen::from_position(pos.clone(), EnPassantMode::Legal);
map.insert(
pos.zobrist_hash(EnPassantMode::Legal),
Opening {
eco: record.eco,
name: record.name,
fen: fen.to_string(),
},
);
positions.push(Opening {
eco: record.eco,
name: record.name,
fen: fen.to_string(),
});
}
}
map
positions
};
}

Expand Down

0 comments on commit 0f8e352

Please sign in to comment.