Skip to content

Commit

Permalink
feat: Add "Result" option for local game search
Browse files Browse the repository at this point in the history
  • Loading branch information
LiberaTeMetuMortis committed Dec 22, 2024
1 parent d439bd8 commit 3d71a51
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src-tauri/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ pub struct GameQueryJs {
pub outcome: Option<String>,
#[specta(optional)]
pub position: Option<PositionQueryJs>,
#[specta(optional)]
pub wanted_result: Option<String>,
}

impl GameQueryJs {
Expand Down
31 changes: 28 additions & 3 deletions src-tauri/src/db/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,37 @@ pub async fn search_position(
}
}

if let Some(black) = query.player2 {
if black != *black_id {
return;
if let Some(black) = query.player2 {
if black != *black_id {
return;
}
}

// "any" | "whitewon" | "draw" | "blackwon"
if let Some(result) = result {
if let Some(wanted_result) = &query.wanted_result {
match wanted_result.as_str() {
"whitewon" => {
if result != "1-0" {
return
}
}
"blackwon" => {
if result != "0-1" {
return
}
}
"draw" => {
if result != "1/2-1/2" {
return
}
}
&_ => {}
}
}
}


if let Some(position_query) = &query.position {
let position_query =
convert_position_query(position_query.clone()).expect("Invalid position query");
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export type EngineOptions = { fen: string; moves: string[]; extraOptions: Engine
export type Event = { id: number; name: string | null }
export type FidePlayer = { fideid: number; name: string; country: string; sex: string; title: string | null; w_title: string | null; o_title: string | null; foa_title: string | null; rating: number | null; games: number | null; k: number | null; rapid_rating: number | null; rapid_games: number | null; rapid_k: number | null; blitz_rating: number | null; blitz_games: number | null; blitz_k: number | null; birthday: number | null; flag: string | null }
export type FileMetadata = { last_modified: number }
export type GameQueryJs = { options?: QueryOptions<GameSort> | null; player1?: number | null; player2?: number | null; tournament_id?: number | null; start_date?: string | null; end_date?: string | null; range1?: [number, number] | null; range2?: [number, number] | null; sides?: Sides | null; outcome?: string | null; position?: PositionQueryJs | null }
export type GameQueryJs = { options?: QueryOptions<GameSort> | null; player1?: number | null; player2?: number | null; tournament_id?: number | null; start_date?: string | null; end_date?: string | null; range1?: [number, number] | null; range2?: [number, number] | null; sides?: Sides | null; outcome?: string | null; position?: PositionQueryJs | null; wanted_result?: string | null }
export type GameSort = "id" | "date" | "whiteElo" | "blackElo" | "ply_count"
export type GoMode = { t: "PlayersTime"; c: PlayersTime } | { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type MonthData = { count: number; avg_elo: number }
Expand Down
1 change: 1 addition & 0 deletions src/components/panels/database/DatabasePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type LocalOptions = {
color: "white" | "black";
start_date?: string;
end_date?: string;
result: "any" | "whitewon" | "draw" | "blackwon",
};

function sortOpenings(openings: Opening[]) {
Expand Down
16 changes: 16 additions & 0 deletions src/components/panels/database/options/LocalOptionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SegmentedControl,
Stack,
Text,
NativeSelect
} from "@mantine/core";
import { DateInput } from "@mantine/dates";
import { parseSquare } from "chessops";
Expand Down Expand Up @@ -57,6 +58,21 @@ function LocalOptionsPanel({ boardFen }: { boardFen: string }) {
}
/>
</Group>
<Group>
<Text fw="bold">Result:</Text>
<NativeSelect
data={[
{ value: "any", label: "Any" },
{ value: "whitewon", label: "White Won" },
{ value: "draw", label: "Draw" },
{ value: "blackwon", label: "Black Won" },
]}
value={options.result}
onChange={(v) =>
setOptions({ ...options, result: v.currentTarget.value as "any" | "whitewon" | "draw" | "blackwon" })
}
/>
</Group>
<Group>
<DateInput
label="From"
Expand Down
1 change: 1 addition & 0 deletions src/state/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ const localOptionsFamily = atomFamily((tab: string) =>
fen: "",
player: null,
color: "white",
result: "any",
}),
);
export const currentLocalOptionsAtom = tabValue(localOptionsFamily);
Expand Down
1 change: 1 addition & 0 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export async function searchPosition(options: LocalOptions, tab: string) {
},
start_date: options.start_date,
end_date: options.end_date,
wanted_result: options.result
},
tab,
);
Expand Down
1 change: 1 addition & 0 deletions src/utils/repertoire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function openingReport({
fen: item.node.fen,
color: "white",
player: null,
result: "any"
},
"opening",
);
Expand Down

0 comments on commit 3d71a51

Please sign in to comment.