slashinfty / tournament-organizer

JavaScript library for running tournaments
https://slashinfty.github.io/tournament-organizer/
MIT License
51 stars 18 forks source link

fix: update players and matches types on TournamentValues #28

Closed lucasfeliciano closed 2 months ago

lucasfeliciano commented 3 months ago

Update players and matches types on TournamentValues

The issue

If we try to reload a tournament based on the json we get some TypeScript errors on the players and matches attributes.

Type '{ id: string; name: string; active: true; value: number; matches: never[]; meta: {}; }' is missing the following properties from type 'Player': values, addMatch, removeMatch, updateMatch ts(2739)

This is due that players and matches attributes on TournamentValues currently expects an instance of Player and Match. Which include the methods.

I believe that is not the intended behaviour since it should expect a serialized json in order to create the instances.

How to reproduce

import TournamentOrganizer from "tournament-organizer";

const t = new TournamentOrganizer();

const tournament = t.reloadTournament({
  id: "Y6tuElkIdxtH",
  name: "Dutch Pauper League",
  status: "setup",
  round: 0,
  players: [
    {
      id: "4luLJp50nru7",
      name: "Lucas",
      active: true,
      value: 0,
      matches: [],
      meta: {},
    },
  ],
  matches: [],
  colored: false,
  sorting: "none",
  scoring: {
    bestOf: 3,
    win: 3,
    draw: 1,
    loss: 0,
    bye: 3,
    tiebreaks: [],
  },
  stageOne: {
    format: "swiss",
    consolation: false,
    rounds: 0,
    initialRound: 1,
    maxPlayers: 0,
  },
  stageTwo: {
    format: null,
    consolation: false,
    advance: { value: 0, method: "all" },
  },
  meta: {},
});

Solution

I've updated the types on TournamentValues to use MatchValues and PlayerValues respectively. And on Tournament interface to use Match and Player types.

Attention

I tried to update the docs, but if I build from my local machine it will reference commits from my forked repository instead of the base repository.

If this solution make sense and you merge it, could you please update the docs for me?

slashinfty commented 3 months ago

As discussed via Discord, I'd prefer for TournamentValues to reflect the actual values of the Tournament class. Since we have SettableTournamentValues, we can just create LoadableTournamentValues as an interface (in the interface folder, adding it to the interface exports, and using it as the type for the Manager loading method).

interface LoadableTournamentValues extends Omit<SettableTournamentValues, 'matches' | 'players'> {
    matches: Array<MatchValues>,
    players: Array<PlayerValues>
}
lucasfeliciano commented 2 months ago

@slashinfty should be good now. I had to define the id type in the interface too. Since we are reloading a tournament, it needs an id. I've squashed the commits