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

`manager.get.currentRound` returns outdated Round with all Matches being `Locked` or `Completed` #180

Open Tandashi opened 1 year ago

Tandashi commented 1 year ago

Currently I am running into the problem that manager.get.currentRound returns a round that only has Locked or Completed matches. This only happens if there are BYEs involved.

Probably similar to: https://github.com/Drarig29/brackets-manager.js/issues/172

Summary

Version: 1.6.3


Example

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({
    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));
  // { id: 0, number: 1, stage_id: 0, group_id: 0 }
  // This should have been the second round because all the current Matches also are in the second round
  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();
Drarig29 commented 1 year ago

Thanks for the reproducible example! This could be a quick fix, but I noticed I have a few other issues related to this.

Like the fact that get.currentRound() should not be a thing anymore, because it's actually possible to have 2 rounds running at the same time: the final and the consolation final of a single elimination match.

And also in double elimination, where a round of the winner bracket can be played a the same time as a round of the loser bracket - although it's not implemented for double elimination yet, but it better be future proof.

Tandashi commented 1 year ago

Would it maybe be useful to implement it similar to get.currentMatches but just for rounds? Meaning get.currentRounds returns an array of all the rounds that can be played. Then the consumer can also sort by number if previous rounds want to be played first for example.

This would also work for both single_elimination and double_elimination.

Drarig29 commented 1 year ago

I don't have the bandwidth to do it currently. PRs are welcome 😉

Tandashi commented 1 year ago

Will make some room for this later today. Feel free to assign this to me if you'd like 😎

Tandashi commented 1 year ago

I just implemented the quick fix for now due to time limitations as well :|

Drarig29 commented 1 year ago

@Tandashi Can you maybe make a local patch on your repo for now?

With https://www.npmjs.com/package/patch-package for example.

Thanks for the PR but I won't have my computer for one week, I took some days off.