Skip to content

chess-centre/players

Repository files navigation

The Chess Centre

GitHub CircleCI Coverage Status

Chess Players

Utility for accessing chess player data

GitHub tag (latest by date)

Intro

This NodeJS package is provided to help developers quickly build applications or components which want to use the published FIDE chess player rating list data.

Use case

An example of this would be a React component which renders a players profile name country rating where you would expose this data on your backend server, via a REST or GraphQL API.

This might look something like this:

import React, { useEffect, useState } from 'react';

export default const PlayerProfile = props => {
       const { id as FIDE_ID } = props;
       const [player, updatePlayer] = useState({});
       const getPlayer = async () => updatePlayer(() => { ...await fetch(`/player/${FIDE_ID}`));
       
       useEffect(() => await getPlayer(), []);

       return (<div>
            <h1>Fide Profile</h1>
            <div>Name: {player.name}</div>
            <div>Rating: {player.rating}</div>
            <div>Country: {player.country}</div>
       </div>);
}
// App.js -> <PlayerProfile id="123456" />

Sample

Getting started

npm install chess-players

or

yarn add chess-players

Examples (typescript)

Full list

import Fide, { Player } from './fide';

(async () => {
  const fide = new Fide();
  const players: Array<Player> = await fide.getPlayers();

  console.log(`Players: ${players.length}`); // OR res.send();
})();

player count

Top players

import Fide, { Player } from './fide';

(async () => {
  const fide = new Fide();
  const players: Array<Player> = await fide.getPlayers();

  const topTen = players
    .sort((a: Player, b: Player) => b.rating - a.rating)
    .slice(0, 10)
    .reduce(
      (players: any, player: any) => 
      [...players, { name: player.name, rating: player.rating, nationality: player.country }],
      [],
    );

  console.log(topTen); // OR res.send();
})();

player top ten

Specific player

import Fide, { Player } from './fide';

(async () => {
  const fide = new Fide();
  const players: Array<Player> = await fide.getPlayers();

  const player = players.find(player => player.fideid === 418250)

  console.log(player); // OR res.send();
})();

player top ten

Legacy lists

import Fide, { Player, Options } from './fide';

(async () => {
  const fide = new Fide();
  const config: Options = {
    ratingType: "rapid",
    month: "jan",
    year: 2019
  }
  const players: Array<Player> = await fide.getPreviousPlayersList(config);
})();

Memoized (cached API calls)

import Fide from './fide';

(async () => {
  const fide = new Fide();
  console.time('players');
  await fide.getPlayers();
  console.timeEnd('players');

  console.time('players-memoized');
  await fide.getPlayers();
  console.timeEnd('players-memoized');
})();

player memoized

License

MIT