Skip to content

Commit

Permalink
added method for opening name
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Hendrik Helms committed Oct 17, 2022
1 parent 733782f commit d8457c5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/classes/BoardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { possibleMoves, shortToLongColor } from '@/helper/Board';
import type { ChessInstance } from 'chess.js';
import type { Api } from 'chessground/api';
import type { BoardState } from '@/typings/BoardStore';
import type { LichessOpening } from '@/typings/BoardAPI';

/**
* class for modifying and reading data from the board, \
Expand Down Expand Up @@ -119,6 +120,25 @@ export class BoardApi {
this.board.setShapes([]);
}
}

/**
* returns the opening name for the current position
*/
async getOpeningName() {
const movesArr: string[] = [];
const history = this.game.history({ verbose: true });
history.forEach((move) => {
movesArr.push(move.from + move.to);
});

const moves = movesArr.join(',');

const res = await fetch(
`https://explorer.lichess.ovh/masters?play=${moves}`
);
const data: LichessOpening = await res.json();
return data.opening.name;
}
}

export default BoardApi;
37 changes: 37 additions & 0 deletions src/typings/BoardAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface LichessOpening {
white: number;
black: number;
draws: number;
moves: [
{
uci: string;
san: string;
averageRating: number;
white: number;
game: null;
black: number;
draws: number;
}
];
opening: {
eco: string;
name: string;
};
topGames: [
{
id: string;
uci: string;
month: string;
year: number;
winner: 'white' | 'black' | null;
white: {
name: string;
rating: number;
};
black: {
name: string;
rating: number;
};
}
];
}

0 comments on commit d8457c5

Please sign in to comment.