Skip to content

Commit

Permalink
fix headers in pgn
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Apr 13, 2023
1 parent 52fc816 commit a17e5cf
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 46 deletions.
17 changes: 13 additions & 4 deletions src/components/boards/BoardAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function BoardAnalysis({
defaultValue: initial_tree,
serialize: (value) => {
const storedTree = JSON.stringify({
pgn: value.getTopVariation().getPGN(),
pgn: value.getTopVariation().getPGN({ headers: game }),
currentMove: value.getPosition(),
});
return storedTree;
Expand Down Expand Up @@ -249,7 +249,10 @@ function BoardAnalysis({
],
});
if (filePath)
await writeTextFile(filePath, tree.getTopVariation().getPGN());
await writeTextFile(
filePath,
tree.getTopVariation().getPGN({ headers: game })
);
}

useHotkeys([
Expand Down Expand Up @@ -279,7 +282,12 @@ function BoardAnalysis({
return (
<TreeContext.Provider value={tree}>
<ReportModal
moves={tree.getTopVariation().getPGN(false, false, false)}
moves={tree.getTopVariation().getPGN({
headers: game,
comments: false,
specialSymbols: false,
symbols: false,
})}
reportingMode={reportingMode}
toggleReportingMode={toggleReportingMode}
setLoading={setAnalysisLoading}
Expand Down Expand Up @@ -327,7 +335,7 @@ function BoardAnalysis({
<Stack>
<GameInfo game={game} />
<FenInput onSubmit={resetToFen} />
<PgnInput />
<PgnInput game={game} />
</Stack>
</Tabs.Panel>
<Tabs.Panel value="database" pt="xs">
Expand Down Expand Up @@ -392,6 +400,7 @@ function BoardAnalysis({
</Tabs>
<Stack>
<GameNotation
game={game}
setTree={setTree}
deleteVariation={deleteVariation}
promoteVariation={promoteVariation}
Expand Down
7 changes: 4 additions & 3 deletions src/components/boards/BoardGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Select,
SimpleGrid,
Stack,
Text
Text,
} from "@mantine/core";
import { useHotkeys, useSessionStorage } from "@mantine/hooks";
import {
Expand Down Expand Up @@ -114,7 +114,7 @@ function BoardGame({
defaultValue: initial_tree,
serialize: (value) => {
const storedTree = JSON.stringify({
pgn: value.getTopVariation().getPGN(),
pgn: value.getTopVariation().getPGN({ headers: game }),
currentMove: value.getPosition(),
});
return storedTree;
Expand Down Expand Up @@ -366,7 +366,7 @@ function BoardGame({
setCompleteGame((prev) => ({
game: {
...prev.game,
result: Outcome.Unknown
result: Outcome.Unknown,
},
currentMove: [],
}));
Expand All @@ -385,6 +385,7 @@ function BoardGame({
</Button>
</Group>
<GameNotation
game={game}
setTree={setTree}
topVariation={tree.getTopVariation()}
result={Outcome.Unknown}
Expand Down
10 changes: 6 additions & 4 deletions src/components/boards/GameNotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Text,
Tooltip,
TypographyStylesProvider,
useMantineTheme
useMantineTheme,
} from "@mantine/core";
import { useForceUpdate, useToggle } from "@mantine/hooks";
import {
Expand All @@ -27,10 +27,10 @@ import {
IconEyeOff,
IconMinus,
IconPlus,
IconX
IconX,
} from "@tabler/icons-react";
import { VariationTree } from "../../utils/chess";
import { Outcome } from "../../utils/db";
import { NormalizedGame, Outcome } from "../../utils/db";
import CompleteMoveCell from "./CompleteMoveCell";
import OpeningName from "./OpeningName";

Expand All @@ -45,13 +45,15 @@ const useStyles = createStyles((theme) => ({
}));

function GameNotation({
game,
setTree,
topVariation,
result,
boardSize,
deleteVariation,
promoteVariation,
}: {
game: NormalizedGame;
setTree: (tree: VariationTree) => void;
topVariation: VariationTree;
deleteVariation?: () => void;
Expand All @@ -65,7 +67,7 @@ function GameNotation({
const [showVariations, toggleVariations] = useToggle([true, false]);
const [showComments, toggleComments] = useToggle([true, false]);
const { classes } = useStyles();
const pgn = topVariation.getPGN();
const pgn = topVariation.getPGN({ headers: game });

const multipleLine =
topVariation.commentHTML.split("</p>").length - 1 > 1 ||
Expand Down
11 changes: 9 additions & 2 deletions src/components/panels/info/PgnInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Checkbox, Group, Stack, Text, Textarea } from "@mantine/core";
import { useToggle } from "@mantine/hooks";
import { useContext } from "react";
import { NormalizedGame } from "../../../utils/db";
import TreeContext from "../../common/TreeContext";

function PgnInput() {
function PgnInput({ game }: { game: NormalizedGame }) {
const tree = useContext(TreeContext);
const [comments, toggleComments] = useToggle([true, false]);
const [symbols, toggleSymbols] = useToggle([true, false]);
Expand Down Expand Up @@ -42,7 +43,13 @@ function PgnInput() {
</Group>
<Textarea
readOnly
value={root.getPGN(symbols, comments, variations, specialSymbols)}
value={root.getPGN({
headers: game,
symbols,
comments,
variations,
specialSymbols,
})}
/>
</Stack>
</>
Expand Down
69 changes: 36 additions & 33 deletions src/utils/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
PieceSymbol,
ROOK,
Square,
SQUARES
SQUARES,
} from "chess.js";
import { DrawShape } from "chessground/draw";
import { Key } from "chessground/types";
import { CompleteGame, Outcome } from "./db";
import { CompleteGame, getHeaders, NormalizedGame, Outcome } from "./db";
import { formatScore } from "./format";

export type Score = {
Expand Down Expand Up @@ -229,43 +229,45 @@ export class VariationTree {
return moveText;
}

getPGN(
symbols: boolean = true,
comments: boolean = true,
variations: boolean = true,
specialSymbols: boolean = true
): string {
getPGN({
headers,
symbols = true,
comments = true,
variations = true,
specialSymbols = true,
}: {
headers: NormalizedGame | null;
symbols?: boolean;
comments?: boolean;
variations?: boolean;
specialSymbols?: boolean;
}): string {
let pgn = "";
if (headers) {
pgn += getHeaders(headers);
}
if (this.parent === null) {
pgn += '[Event "?"]\n';
pgn += '[Site "?"]\n';
pgn += '[Date "?"]\n';
pgn += '[Round "?"]\n';
pgn += '[White "?"]\n';
pgn += '[Black "?"]\n';
pgn += '[Result "*"]\n';
if (this.fen !== DEFAULT_POSITION) {
pgn += '[FEN "' + this.fen + '"]\n\n';
} else {
pgn += "\n";
}
}
const variationsPGN = variations
? this.children
.slice(1)
.map(
(variation) =>
`${variation.getMoveText(
symbols,
comments,
true
)} ${variation.getPGN(
symbols,
comments,
variations,
specialSymbols
)}`
)
? this.children.slice(1).map(
(variation) =>
`${variation.getMoveText(
symbols,
comments,
true
)} ${variation.getPGN({
headers: null,
symbols,
comments,
variations,
specialSymbols,
})}`
)
: [];
if (this.children.length > 0) {
const child = this.children[0];
Expand All @@ -278,12 +280,13 @@ export class VariationTree {
}

if (this.children.length > 0) {
pgn += this.children[0].getPGN(
pgn += this.children[0].getPGN({
headers: null,
symbols,
comments,
variations,
specialSymbols
);
specialSymbols,
});
}
return pgn.trim();
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ export interface NormalizedGame {
moves: string;
}

export function getHeaders(game: NormalizedGame): string {
let headers = `[Event "${game.event.name}"]
[Site "${game.site.name}"]
[Date "${game.date}"]
[Round "${game.round}"]
[White "${game.white.name}"]
[Black "${game.black.name}"]
[Result "${game.result}"]
`;
return headers;
}

export function defaultGame(): NormalizedGame {
return {
id: 0,
Expand Down

0 comments on commit a17e5cf

Please sign in to comment.