Skip to content

Commit

Permalink
fix add game when changing index (closes #450)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Dec 5, 2024
1 parent bd4edd4 commit f46f861
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src-tauri/src/pgn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ impl PgnParser {
fn offset_by_index(&mut self, n: usize, state: &AppState, file: &String) -> io::Result<()> {
let offset_index = n / GAME_OFFSET_FREQ;
let n_left = n % GAME_OFFSET_FREQ;
let pgn_offsets = state.pgn_offsets.get(file).unwrap();

let offset = match offset_index {
0 => self.start,
_ => state.pgn_offsets.get(file).unwrap()[offset_index - 1],
};
if offset_index == 0 || offset_index < pgn_offsets.len() {
let offset = match offset_index {
0 => self.start,
_ => pgn_offsets[offset_index - 1],
};

self.reader.seek(SeekFrom::Start(offset))?;
self.reader.seek(SeekFrom::Start(offset))?;

self.skip_games(n_left)?;
} else {
self.reader.seek(SeekFrom::Start(self.start))?;
self.skip_games(n)?;
}

self.skip_games(n_left)?;
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/boards/BoardAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
enableAllAtom,
} from "@/state/atoms";
import { keyMapAtom } from "@/state/keybinds";
import { getVariationLine } from "@/utils/chess";
import { defaultPGN, getVariationLine } from "@/utils/chess";
import { saveToFile } from "@/utils/tabs";
import { Paper, Portal, Stack, Tabs } from "@mantine/core";
import { useHotkeys, useToggle } from "@mantine/hooks";
Expand Down Expand Up @@ -67,7 +67,7 @@ function BoardAnalysis() {
tab: currentTab,
store,
});
}, [setCurrentTab, currentTab]);
}, [setCurrentTab, currentTab, documentDir, store]);
useEffect(() => {
if (currentTab?.file && autoSave && dirty) {
saveFile();
Expand All @@ -82,7 +82,7 @@ function BoardAnalysis() {
return { ...prev };
});
reset();
writeTextFile(currentTab?.file?.path!, "\n\n", {
writeTextFile(currentTab?.file?.path!, `\n\n${defaultPGN()}\n\n`, {
append: true,
});
}, [setCurrentTab, reset, currentTab?.file?.path]);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ function headersToPGN(game: GameHeaders): string {
return headers;
}

export function defaultPGN() {
return `[Event "?"]\n[Site "?"]\n[Date "????.??.??"]\n[Round "?"]\n[White "?"]\n[Black "?"]\n[Result "*"]\n\n*`;
}

export function getPGN(
tree: TreeNode,
{
Expand Down

0 comments on commit f46f861

Please sign in to comment.