Skip to content

Commit fe6175a

Browse files
fix: custom game analysis
1 parent 357ff62 commit fe6175a

3 files changed

Lines changed: 59 additions & 48 deletions

File tree

src/api/analysis.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from 'src/lib'
1616
import { buildUrl } from './utils'
1717
import { AvailableMoves } from 'src/types/puzzle'
18+
import { Chess } from 'chess.ts'
1819

1920
export const fetchWorldChampionshipGameList = async (): Promise<
2021
Map<string, WorldChampionshipGameListEntry[]>
@@ -260,7 +261,10 @@ export const fetchAnalyzedMaiaGame = async (
260261
const gameStates = data['game_states']
261262

262263
const moves = buildMovesListFromGameStates(gameStates)
263-
const tree = buildGameTreeFromMoveList(moves, moves[0].board)
264+
const tree = buildGameTreeFromMoveList(
265+
moves,
266+
moves.length ? moves[0].board : new Chess().fen(),
267+
)
264268

265269
return {
266270
id,

src/components/Analysis/AnalysisGameList.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
205205

206206
fetchMaiaGameList(selected, currentPage)
207207
.then((data) => {
208+
console.log(data)
208209
let parsedGames: MaiaGameListEntry[] = []
209210

210211
if (selected === 'favorites') {
211-
// Handle favorites response format
212212
parsedGames = data.games.map((game: any) => ({
213213
id: game.game_id || game.id,
214214
type: game.game_type || game.type,
@@ -221,11 +221,12 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
221221
} else {
222222
if (selected === 'custom') {
223223
parsedGames = data.games.map((game: any) => ({
224-
id: game.id,
225-
label: game.name || 'Custom Game',
226-
result: '*',
224+
id: game.game_id || game.id,
227225
type: 'custom',
228-
pgn: game.pgn,
226+
label: game.custom_name || 'Custom Game',
227+
result: game.result || '*',
228+
is_favorited: game.is_favorited,
229+
custom_name: game.custom_name,
229230
}))
230231
} else {
231232
const parse = (
@@ -242,7 +243,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
242243
const raw = game.maia_name.replace('_kdd_', ' ')
243244
const maia = raw.charAt(0).toUpperCase() + raw.slice(1)
244245

245-
// Use custom name if available, otherwise generate default label
246246
const defaultLabel =
247247
game.player_color === 'white'
248248
? `You vs. ${maia}`
@@ -279,7 +279,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
279279
},
280280
}))
281281

282-
// Update favoritedGameIds from the actual games data
283282
const favoritedIds = new Set<string>(
284283
parsedGames
285284
.filter((game: any) => game.is_favorited)
@@ -303,7 +302,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
303302
}
304303
}, [selected, currentPage, fetchedCache])
305304

306-
// Separate useEffect for H&B subsections
307305
useEffect(() => {
308306
if (selected === 'hb') {
309307
const gameType = hbSubsection === 'hand' ? 'hand' : 'brain'
@@ -333,7 +331,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
333331
const raw = game.maia_name.replace('_kdd_', ' ')
334332
const maia = raw.charAt(0).toUpperCase() + raw.slice(1)
335333

336-
// Use custom name if available, otherwise generate default label
337334
const defaultLabel =
338335
game.player_color === 'white'
339336
? `You vs. ${maia}`
@@ -368,7 +365,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
368365
},
369366
}))
370367

371-
// Update favoritedGameIds from the actual games data
372368
const favoritedIds = new Set<string>(
373369
parsedGames
374370
.filter((game: any) => game.is_favorited)
@@ -488,7 +484,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
488484
setFavoriteGames(updatedFavorites)
489485
setFavoritedGameIds(new Set(updatedFavorites.map((f) => f.id)))
490486

491-
// Clear favorites cache to force re-fetch
492487
setFetchedCache((prev) => ({
493488
...prev,
494489
favorites: {},
@@ -498,7 +493,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
498493
favorites: {},
499494
}))
500495

501-
// Also clear current section cache to show updated favorite status
502496
if (selected !== 'favorites') {
503497
const currentSection =
504498
selected === 'hb'
@@ -707,6 +701,7 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
707701
const selectedGame = currentId && currentId[0] === game.id
708702
const isFavorited = (game as any).is_favorited || false
709703
const displayName = game.label
704+
// console.log(game)
710705
return (
711706
<div
712707
key={index}

src/contexts/AnalysisListContext.tsx

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface IAnalysisListContext {
1414
analysisPlayList: MaiaGameListEntry[]
1515
analysisHandList: MaiaGameListEntry[]
1616
analysisBrainList: MaiaGameListEntry[]
17+
analysisCustomList: MaiaGameListEntry[]
1718
}
1819

1920
export const AnalysisListContext = React.createContext<IAnalysisListContext>({
@@ -22,6 +23,7 @@ export const AnalysisListContext = React.createContext<IAnalysisListContext>({
2223
analysisPlayList: [],
2324
analysisHandList: [],
2425
analysisBrainList: [],
26+
analysisCustomList: [],
2527
})
2628

2729
export const AnalysisListContextProvider: React.FC<{ children: ReactNode }> = ({
@@ -48,6 +50,9 @@ export const AnalysisListContextProvider: React.FC<{ children: ReactNode }> = ({
4850
const [analysisBrainList, setAnalysisBrainList] = useState<
4951
MaiaGameListEntry[]
5052
>([])
53+
const [analysisCustomList, setAnalysisCustomList] = useState<
54+
MaiaGameListEntry[]
55+
>([])
5156

5257
useEffect(() => {
5358
async function getAndSetData() {
@@ -89,43 +94,49 @@ export const AnalysisListContextProvider: React.FC<{ children: ReactNode }> = ({
8994
const playRequest = fetchMaiaGameList('play', 1)
9095
const handRequest = fetchMaiaGameList('hand', 1)
9196
const brainRequest = fetchMaiaGameList('brain', 1)
92-
93-
Promise.all([playRequest, handRequest, brainRequest]).then((data) => {
94-
const [play, hand, brain] = data
95-
96-
const parse = (
97-
game: {
98-
game_id: string
99-
maia_name: string
100-
result: string
101-
player_color: 'white' | 'black'
102-
},
103-
type: string,
104-
) => {
105-
const raw = game.maia_name.replace('_kdd_', ' ')
106-
const maia = raw.charAt(0).toUpperCase() + raw.slice(1)
107-
108-
return {
109-
id: game.game_id,
110-
label:
111-
game.player_color === 'white'
112-
? `You vs. ${maia}`
113-
: `${maia} vs. You`,
114-
result: game.result,
115-
type,
97+
const customRequest = fetchMaiaGameList('custom', 1)
98+
99+
Promise.all([playRequest, handRequest, brainRequest, customRequest]).then(
100+
(data) => {
101+
const [play, hand, brain, custom] = data
102+
103+
const parse = (
104+
game: {
105+
game_id: string
106+
maia_name: string
107+
result: string
108+
player_color: 'white' | 'black'
109+
},
110+
type: 'play' | 'hand' | 'brain' | 'custom',
111+
) => {
112+
const raw = game.maia_name.replace('_kdd_', ' ')
113+
const maia = raw.charAt(0).toUpperCase() + raw.slice(1)
114+
115+
return {
116+
id: game.game_id,
117+
label:
118+
game.player_color === 'white'
119+
? `You vs. ${maia}`
120+
: `${maia} vs. You`,
121+
result: game.result,
122+
type,
123+
}
116124
}
117-
}
118125

119-
setAnalysisPlayList(
120-
play.games.map((game: never) => parse(game, 'play')),
121-
)
122-
setAnalysisHandList(
123-
hand.games.map((game: never) => parse(game, 'hand')),
124-
)
125-
setAnalysisBrainList(
126-
brain.games.map((game: never) => parse(game, 'brain')),
127-
)
128-
})
126+
setAnalysisPlayList(
127+
play.games.map((game: never) => parse(game, 'play')),
128+
)
129+
setAnalysisHandList(
130+
hand.games.map((game: never) => parse(game, 'hand')),
131+
)
132+
setAnalysisBrainList(
133+
brain.games.map((game: never) => parse(game, 'brain')),
134+
)
135+
setAnalysisCustomList(
136+
custom.games.map((game: never) => parse(game, 'custom')),
137+
)
138+
},
139+
)
129140
}
130141
}, [user?.lichessId])
131142

@@ -137,6 +148,7 @@ export const AnalysisListContextProvider: React.FC<{ children: ReactNode }> = ({
137148
analysisPlayList,
138149
analysisHandList,
139150
analysisBrainList,
151+
analysisCustomList,
140152
}}
141153
>
142154
{children}

0 commit comments

Comments
 (0)