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

`MatchGame` (Status) not updating properly #174

Closed Tandashi closed 1 year ago

Tandashi commented 1 year ago

Note I am using the Prisma SQL DB but seems to also effect the Json DB as well

Setup

(See also Example below)

Assume I create a stage (setup described below). I then update the MatchGames for the first Match with manager.update.matchGame. The score for the first and second MatchGame would be 2 | 0 meaning opponent 1 has won the best of 3 and advances to the next round.

The following I passed to the create Method for creating a stage:

{
      name: 'Some Title',
      tournamentId: 0,
      type: 'single_elimination',
      seeding: [0, 1, 2, 3, 4, null, null, null],
      settings: {
        grandFinal: 'simple',
        balanceByes: true,
        matchesChildCount: 3,
      },
}

The participants have been created beforehand and I am just linking them in the seeding.

After the creation of the Stage: Screenshot 2023-06-29 at 22 22 14

After updating the MatchGames for the first Match: Screenshot 2023-06-29 at 22 15 00

The Issue

Now I have the 1st Match in the Semi Finals which has its Match marked as Ready as well as its MatchGames marked as Ready. This is correct so far but it was also created like this initially. The 2nd Match in the Semi Finals (P2 vs P3) has also the Match marked as Ready (also still correct) but its MatchGames are still marked as Waiting. Meaning the MatchGames have not updated their status but the Match has. Do I have to update in some other way / am I misunderstanding something?

Also the last MatchGame of the first Match in Round 1 is still marked as Ready although the Match is marked as Completed (which is correct since opponent 1 in both occasions has won twice winning the bo3). Maybe that is somehow related to the issue? Further more in the Example the Participant is also null but in the Match both Participants are present.

My current solution is that I prepare the Match by manually marking the MatchGames as Ready when the Match is also marked as Ready. Not sure if that is intended behaviour though.

Example

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

await manager.update.matchGame({
  id: 7, // First Match Game of the 3rd Match in Round 1
  opponent1: { score: 16, result: 'win' },
  opponent2: { score: 12 },
});

await manager.update.matchGame({
  id: 8, // Second Match Game of the 3rd Match in Round 1
  opponent1: { score: 16, result: 'win' },
  opponent2: { score: 12 },
});

const match = await manager.find.match(1, 2, 2); // Second Match in the Semi Finals
console.log(match)
// {
//   id: 6,
//   status: 2,
//   stage_id: 1,
//   group_id: 1,
//   round_id: 2,
//   number: 2,
//   child_count: 3,
//   opponent1: {
//     id: 2,
//     forfeit: undefined,
//     position: undefined,
//     score: undefined,
//     result: undefined
//   },
//   opponent2: {
//     id: 3,
//     forfeit: undefined,
//     position: undefined,
//     score: undefined,
//     result: undefined
//   }
// }

console.log(await manager.find.matchGame({ parent_id: match.id, number: 1 }));
// {
//   id: 16,
//   status: 1, <-- Waiting not Ready
//   stage_id: 1,
//   parent_id: 6,
//   number: 1,
//   opponent1: {
//     id: null,  <-- Also seems to be wrong, maybe causing the Waiting status (Match Results has both Opponents updated correctly though, just didn't propagate the MatchGameResults. There the entry is also null.
//     forfeit: undefined,
//     position: undefined,
//     score: undefined,
//     result: undefined
//   },
//   opponent2: {
//     id: 3,
//     forfeit: undefined,
//     position: undefined,
//     score: undefined,
//     result: undefined
//   }
// }
Drarig29 commented 1 year ago

Now I have the 1st Match in the Semi Finals which has its Match marked as Ready as well as its MatchGames marked as Ready. This is correct so far but it was also created like this initially. The 2nd Match in the Semi Finals (P2 vs P3) has also the Match marked as Ready (also still correct) but its MatchGames are still marked as Waiting. Meaning the MatchGames have not updated their status but the Match has. Do I have to update in some other way / am I misunderstanding something?

It seems to work with the Json DB on my side... 🤔 Note that in my example, all IDs start from 0:

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

const semiFinalMatchId = (await manager.find.match(0, 2, 2)).id; // Second Match in the Semi Finals

await manager.update.matchGame({
    id: 6, // First Match Game of the 3rd Match in Round 1
    opponent1: { score: 16, result: 'win' },
    opponent2: { score: 12 },
});

console.log(await manager.find.matchGame({ parent_id: semiFinalMatchId, number: 1 }));
// {
//     id: 15,
//     number: 1,
//     stage_id: 0,
//     parent_id: 5,
//     status: 1,
//     opponent1: { id: null },
//     opponent2: { id: 2 }
// }

await manager.update.matchGame({
    id: 7, // Second Match Game of the 3rd Match in Round 1
    opponent1: { score: 16, result: 'win' },
    opponent2: { score: 12 },
});

console.log(await manager.find.matchGame({ parent_id: semiFinalMatchId, number: 1 }));
// {
//     id: 15,
//     number: 1,
//     stage_id: 0,
//     parent_id: 5,
//     status: 2,
//     opponent1: { id: 1 },
//     opponent2: { id: 2 }
// }

The part of the code that is responsible to update the match games is this one:

https://github.com/Drarig29/brackets-manager.js/blob/2cafaf82618bbe1f4faaf6069ca56797938a8b47/src/base/updater.ts#L200-L206

Which corresponds to this part of your prisma implementation. Maybe you should debug this part.

Tandashi commented 1 year ago

Yeah with your example it works... Weird must have messed something up when trying with the JsonDatabase to confirm. My bad...

Will debug the issue in the SQL implementation and make a PR for fix it :)

Drarig29 commented 1 year ago

Thank you, best of luck to find the bug 🙏