Skip to content

Commit 727df60

Browse files
Merge pull request #389 from CSSLab/claude/pgn-unknown-names
Show an unnamed imported player by colour, not "?"
2 parents 6473c9d + e3790ce commit 727df60

3 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎src/lib/pgn.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
graftVariations,
77
hasVariations,
88
parsePgnWithVariations,
9+
pgnNameOr,
910
pgnPlayerName,
11+
playerLabel,
1012
splitPgnGames,
1113
} from './pgn'
1214

@@ -213,3 +215,23 @@ describe('pgnPlayerName and formatPgnDate', () => {
213215
expect(formatPgnDate(new Date(2026, 0, 5))).toBe('2026.01.05')
214216
})
215217
})
218+
219+
describe('pgnNameOr and playerLabel', () => {
220+
it('keeps a real name', () => {
221+
expect(pgnNameOr(' Carlsen, Magnus ', '?')).toBe('Carlsen, Magnus')
222+
expect(playerLabel('Nakamura', 'black', 'custom')).toBe('Nakamura')
223+
})
224+
225+
it('treats PGN\'s "?" and stored placeholders as no name', () => {
226+
for (const name of ['?', ' ? ', 'None', 'null', '', undefined]) {
227+
expect(pgnNameOr(name, 'x')).toBe('x')
228+
}
229+
})
230+
231+
it('shows an unnamed player by colour, or as Guest in a game played here', () => {
232+
expect(playerLabel('?', 'white', 'custom')).toBe('White')
233+
expect(playerLabel('?', 'black', 'custom')).toBe('Black')
234+
expect(playerLabel('?', 'black')).toBe('Black')
235+
expect(playerLabel('None', 'white', 'play')).toBe('Guest')
236+
})
237+
})

‎src/lib/pgn.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,38 @@ export const pgnPlayerName = (user: User | null | undefined): string => {
6262
/**
6363
* A stored player's name for a PGN, or `fallback` when there's none: games
6464
* played by a visitor who wasn't signed in are stored without a name, or with
65-
* "None".
65+
* "None", and an imported PGN that names nobody comes back with "?" (PGN's
66+
* "unknown").
6667
*/
6768
export const pgnNameOr = (
6869
name: string | null | undefined,
6970
fallback: string,
7071
) => {
7172
const trimmed = name?.trim()
72-
return trimmed && !/^(none|null|undefined)$/i.test(trimmed)
73+
return trimmed && !/^(none|null|undefined|\?)$/i.test(trimmed)
7374
? trimmed
7475
: fallback
7576
}
7677

78+
/**
79+
* A player's name to show, or without one: "Guest" in a game played here (by
80+
* a visitor who wasn't signed in), and their colour in any other (an imported
81+
* game that names nobody).
82+
*/
83+
export const playerLabel = (
84+
name: string | null | undefined,
85+
color: 'white' | 'black',
86+
gameType?: string,
87+
) =>
88+
pgnNameOr(
89+
name,
90+
['play', 'hand', 'brain'].includes(gameType ?? '')
91+
? 'Guest'
92+
: color === 'white'
93+
? 'White'
94+
: 'Black',
95+
)
96+
7797
const sanOf = (parent: GameNode, child: GameNode): string => {
7898
if (child.san) return child.san
7999
const move = child.move

‎src/pages/analysis/[...id].tsx‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import type { DrawBrushes, DrawShape } from 'chessground/draw'
8989
import { MAIA_MODELS } from 'src/constants/common'
9090
import { applyEngineAnalysisData } from 'src/lib/analysis'
9191
import { formatMaiaName } from 'src/lib/maiaNames'
92+
import { playerLabel } from 'src/lib/pgn'
9293
import { cpToWinrate, parseLichessPgn } from 'src/lib'
9394
import {
9495
applyImportedVariations,
@@ -559,22 +560,31 @@ interface Props {
559560

560561
// Stored games' names are already formatted with the game's date (see
561562
// fetchAnalyzedMaiaGame); any other Maia name is taken to be current.
562-
const formatAnalysisPlayerName = (name: string) =>
563-
/^maia_kdd_\d+$/i.test(name) ? formatMaiaName(name) : name
563+
// A player without a name is shown as playerLabel says.
564+
const formatAnalysisPlayerName = (
565+
name: string,
566+
color: 'white' | 'black',
567+
gameType?: string,
568+
) => {
569+
const label = playerLabel(name, color, gameType)
570+
return /^maia_kdd_\d+$/i.test(label) ? formatMaiaName(label) : label
571+
}
564572

565573
// Apart from the page so that it renders only when the game changes: each
566574
// render of <Head> makes Next.js update the document's head, and the page
567575
// re-renders on every engine update.
568576
const AnalysisHead = React.memo(function AnalysisHead({
569577
whitePlayer,
570578
blackPlayer,
579+
gameType,
571580
}: {
572581
whitePlayer?: string
573582
blackPlayer?: string
583+
gameType?: string
574584
}) {
575585
const players =
576586
whitePlayer !== undefined && blackPlayer !== undefined
577-
? `${formatAnalysisPlayerName(whitePlayer)} vs ${formatAnalysisPlayerName(blackPlayer)}`
587+
? `${formatAnalysisPlayerName(whitePlayer, 'white', gameType)} vs ${formatAnalysisPlayerName(blackPlayer, 'black', gameType)}`
578588
: null
579589
return (
580590
<Head>
@@ -1647,7 +1657,11 @@ const Analysis: React.FC<Props> = ({
16471657
/>
16481658
<div className="flex min-w-0 items-center gap-1">
16491659
<p className="truncate text-sm" title={player.name}>
1650-
{formatAnalysisPlayerName(player.name)}
1660+
{formatAnalysisPlayerName(
1661+
player.name,
1662+
index === 0 ? 'white' : 'black',
1663+
analyzedGame.type,
1664+
)}
16511665
</p>
16521666
<span className="shrink-0 text-xs">
16531667
{player.rating ? <>({player.rating})</> : null}
@@ -1682,7 +1696,11 @@ const Analysis: React.FC<Props> = ({
16821696
<div className="h-2 w-2 rounded-full bg-white" />
16831697
</span>
16841698
<span className="truncate font-medium">
1685-
{formatAnalysisPlayerName(analyzedGame.whitePlayer.name)}
1699+
{formatAnalysisPlayerName(
1700+
analyzedGame.whitePlayer.name,
1701+
'white',
1702+
analyzedGame.type,
1703+
)}
16861704
</span>
16871705
{analyzedGame.whitePlayer.rating && (
16881706
<span className="shrink-0 text-primary/60">
@@ -1710,7 +1728,11 @@ const Analysis: React.FC<Props> = ({
17101728
<div className="h-2 w-2 rounded-full border-[0.5px] bg-black" />
17111729
</span>
17121730
<span className="truncate font-medium">
1713-
{formatAnalysisPlayerName(analyzedGame.blackPlayer.name)}
1731+
{formatAnalysisPlayerName(
1732+
analyzedGame.blackPlayer.name,
1733+
'black',
1734+
analyzedGame.type,
1735+
)}
17141736
</span>
17151737
{analyzedGame.blackPlayer.rating && (
17161738
<span className="shrink-0 text-primary/60">
@@ -2571,6 +2593,7 @@ const Analysis: React.FC<Props> = ({
25712593
<AnalysisHead
25722594
whitePlayer={analyzedGame?.whitePlayer.name}
25732595
blackPlayer={analyzedGame?.blackPlayer.name}
2596+
gameType={analyzedGame?.type}
25742597
/>
25752598
<MaiaDownloadPrompt engine={controller.maia} />
25762599
<TreeControllerContext.Provider value={controller}>

0 commit comments

Comments
 (0)