Skip to content

Commit

Permalink
persist player name
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Mar 15, 2024
1 parent e17845b commit 157dd91
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src-tauri/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,21 @@ pub enum PlayerSort {
Elo,
}

#[tauri::command]
#[specta::specta]
pub async fn get_player(
file: PathBuf,
id: i32,
state: tauri::State<'_, AppState>,
) -> Result<Option<Player>, Error> {
let db = &mut get_db_or_create(&state, file.to_str().unwrap(), ConnectionOptions::default())?;
let player = players::table
.filter(players::id.eq(id))
.first::<Player>(db)
.optional()?;
Ok(player)
}

#[tauri::command]
pub async fn get_players(
file: PathBuf,
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Puzzle {
pub nb_plays: i32,
}

#[derive(Default, Debug, Queryable, Serialize, Deserialize, Identifiable, Clone)]
#[derive(Default, Debug, Queryable, Serialize, Deserialize, Identifiable, Clone, Type)]
#[diesel(table_name = players)]
pub struct Player {
pub id: i32,
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use tauri_plugin_log::LogTarget;
use crate::chess::{analyze_game, get_engine_config, get_engine_logs, kill_engines, stop_engine};
use crate::db::{
clear_games, convert_pgn, create_indexes, delete_database, delete_db_game, delete_empty_games,
delete_indexes, export_to_pgn, get_players_game_info, get_tournaments, search_position,
delete_indexes, export_to_pgn, get_player, get_players_game_info, get_tournaments,
search_position,
};
use crate::fide::{download_fide_db, find_fide_player};
use crate::fs::{append_to_file, set_file_as_executable, DownloadProgress};
Expand Down Expand Up @@ -146,6 +147,7 @@ fn main() {
get_file_metadata,
merge_players,
convert_pgn,
get_player,
))
.events(tauri_specta::collect_events!(
BestMovesPayload,
Expand Down
8 changes: 8 additions & 0 deletions src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ try {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getPlayer(file: string, id: number) : Promise<__Result__<{ id: number; name: string | null; elo: number | null } | null, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("plugin:tauri-specta|get_player", { file, id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/components/databases/PlayerSearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { commands } from "@/bindings";
import { type Player, query_players } from "@/utils/db";
import { unwrap } from "@/utils/invoke";
import { Autocomplete } from "@mantine/core";
import { IconSearch } from "@tabler/icons-react";
import { type ReactNode, useState } from "react";
import { type ReactNode, useEffect, useState } from "react";

export function PlayerSearchInput({
label,
value,
file,
rightSection,
setValue,
}: {
label: string;
value: number | null;
file: string;
rightSection?: ReactNode;
setValue: (val: number | undefined) => void;
}) {
const [tempValue, setTempValue] = useState("");
const [data, setData] = useState<Player[]>([]);

useEffect(() => {
if (value !== null) {
commands.getPlayer(file, value).then((res) => {
const player = unwrap(res);
if (player?.name) {
setTempValue(player.name);
}
});
}
}, [value]);

async function handleChange(val: string) {
setTempValue(val);
if (val.trim().length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function LocalOptionsPanel({ boardFen }: { boardFen: string }) {
{options.path && (
<PlayerSearchInput
label={"Search"}
value={options.player}
file={options.path}
setValue={(v) => setOptions((q) => ({ ...q, player: v || null }))}
/>
Expand Down

0 comments on commit 157dd91

Please sign in to comment.