Drarig29 / brackets-manager.js

A simple library to manage tournament brackets (round-robin, single elimination, double elimination).
https://drarig29.github.io/brackets-docs
MIT License
245 stars 38 forks source link

fix: `currentRound` returns round with only `Locked` and `Completed` matches #181

Closed Tandashi closed 1 year ago

Tandashi commented 1 year ago

Running the same example provided in the Issue will now yield the following result:

import { BracketsManager } from 'brackets-manager';
import { JsonDatabase } from 'brackets-json-db';

const storage = new JsonDatabase();
const manager = new BracketsManager(storage);

async function main() {
  const stage = await manager.create.stage({
    name: 'Some Title',
    tournamentId: 0,
    type: 'single_elimination',
    seeding: ['P1', 'P2', 'P3', null],
    settings: {
      grandFinal: 'simple',
      balanceByes: true,
      matchesChildCount: 3,
    },
  });

  console.log(await manager.get.currentRound(stage.id));
  // { id: 0, number: 1, stage_id: 0, group_id: 0 }
  console.log(await manager.get.currentMatches(stage.id));
  // [
  //   {
  //     id: 1,
  //     number: 2,
  //     stage_id: 0,
  //     group_id: 0,
  //     round_id: 0,
  //     child_count: 3,
  //     status: 2,
  //     opponent1: { id: 1, position: 2 },
  //     opponent2: { id: 2, position: 3 }
  //   }
  // ]

  await manager.update.match({ id: 1, opponent1: { result: 'win' } });

  console.log(await manager.get.currentRound(stage.id));
  // This is now the expected Round and matches the `round_id`
  // of the current match.
  // { id: 1, number: 2, stage_id: 0, group_id: 0 }
  console.log(await manager.get.currentMatches(stage.id));
  // [
  //   {
  //     id: 2,
  //     number: 1,
  //     stage_id: 0,
  //     group_id: 0,
  //     round_id: 1,
  //     child_count: 3,
  //     status: 2,
  //     opponent1: { id: 0, position: undefined },
  //     opponent2: { id: 1, position: undefined },
  //   },
  // ];
}
main();

Reference #180

Drarig29 commented 1 year ago

Hey @Tandashi!

I prefer not to merge a hotfix, since you already have a temporary patch in your project. Plus, the unit tests are not passing (please don't mind updating them 😉).

So I'll close this PR, since your issue already tracks the problem. Later, I'll create a new PR to fix everything the way I want (as said in https://github.com/Drarig29/brackets-manager.js/issues/180#issuecomment-1630615835) - including the unit tests.

I hope you understand! And sorry for asking, then closing your PR... 😕