tods-competition-factory 1.8.15 → 1.8.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2382,7 +2382,7 @@ const matchUpFormatCode = {
2382
2382
  };
2383
2383
 
2384
2384
  function factoryVersion() {
2385
- return "1.8.15";
2385
+ return "1.8.16";
2386
2386
  }
2387
2387
 
2388
2388
  function getObjectTieFormat(obj) {
@@ -3189,11 +3189,11 @@ function countSets({
3189
3189
  score
3190
3190
  }) {
3191
3191
  const setsTally = [0, 0];
3192
- const { sets } = score || {};
3193
- const matchUpWinnerIndex = matchUpWinningSide - 1;
3192
+ const sets = score?.sets;
3193
+ const matchUpWinnerIndex = typeof matchUpWinningSide === "number" && matchUpWinningSide - 1;
3194
3194
  const parsedMatchUpFormat = parse(matchUpFormat);
3195
3195
  const setsToWin = getSetsToWin(parsedMatchUpFormat?.bestOf ?? 1);
3196
- if (matchUpStatus === DEFAULTED && tallyPolicy?.setsCreditForDefaults || matchUpStatus === WALKOVER$2 && tallyPolicy?.setsCreditForWalkovers) {
3196
+ if (typeof matchUpWinnerIndex === "number" && (matchUpStatus === DEFAULTED && tallyPolicy?.setsCreditForDefaults || matchUpStatus === WALKOVER$2 && tallyPolicy?.setsCreditForWalkovers)) {
3197
3197
  setsTally[matchUpWinnerIndex] = setsToWin;
3198
3198
  } else {
3199
3199
  for (const set of sets || []) {
@@ -3202,7 +3202,7 @@ function countSets({
3202
3202
  setsTally[setWinningSide - 1] += 1;
3203
3203
  }
3204
3204
  }
3205
- if (matchUpStatus === RETIRED$1) {
3205
+ if (typeof matchUpWinnerIndex === "number" && matchUpStatus === RETIRED$1) {
3206
3206
  if (+setsTally[1 - matchUpWinnerIndex] === setsToWin)
3207
3207
  setsTally[1 - matchUpWinnerIndex] -= 1;
3208
3208
  if (tallyPolicy?.setsCreditForRetirements)
@@ -3220,13 +3220,13 @@ function countGames({
3220
3220
  const { sets } = score || {};
3221
3221
  if (!sets)
3222
3222
  return [0, 0];
3223
- const matchUpWinnerIndex = matchUpWinningSide - 1;
3223
+ const matchUpWinnerIndex = typeof matchUpWinningSide === "number" && matchUpWinningSide - 1;
3224
3224
  const parsedMatchUpFormat = parse(matchUpFormat);
3225
3225
  const bestOf = parsedMatchUpFormat?.bestOf ?? 1;
3226
3226
  const setsToWin = getSetsToWin(bestOf);
3227
3227
  const tiebreakAt = parsedMatchUpFormat?.setFormat?.tiebreakAt || 0;
3228
3228
  const gamesTally = [[], []];
3229
- if (matchUpStatus === DEFAULTED && tallyPolicy?.gamesCreditForDefaults || matchUpStatus === WALKOVER$2 && tallyPolicy?.gamesCreditForWalkovers) {
3229
+ if (typeof matchUpWinnerIndex === "number" && (matchUpStatus === DEFAULTED && tallyPolicy?.gamesCreditForDefaults || matchUpStatus === WALKOVER$2 && tallyPolicy?.gamesCreditForWalkovers)) {
3230
3230
  const gamesForSet = parsedMatchUpFormat?.setFormat?.setTo || 0;
3231
3231
  const minimumGameWins = setsToWin * gamesForSet;
3232
3232
  gamesTally[matchUpWinnerIndex].push(minimumGameWins);
@@ -3246,7 +3246,7 @@ function countGames({
3246
3246
  }
3247
3247
  });
3248
3248
  }
3249
- if (matchUpStatus === RETIRED$1) {
3249
+ if (matchUpStatus === RETIRED$1 && typeof matchUpWinnerIndex === "number") {
3250
3250
  const whichFormat = sets.length > setsToWin && parsedMatchUpFormat?.finalSetFormat ? "finalSetFormat" : "setFormat";
3251
3251
  const format = parsedMatchUpFormat?.[whichFormat];
3252
3252
  if (isGamesBased(format.based)) {
@@ -3286,10 +3286,14 @@ function countGames({
3286
3286
  gamesTally[1].reduce((a, b) => a + b, 0)
3287
3287
  ];
3288
3288
  }
3289
- function countPoints({ matchUpFormat, score }) {
3290
- const parsedMatchUpFormat = parse(matchUpFormat);
3289
+ function countPoints({
3290
+ matchUpFormat,
3291
+ score
3292
+ }) {
3293
+ const parsedMatchUpFormat = matchUpFormat ? parse(matchUpFormat) : void 0;
3291
3294
  const bestOf = parsedMatchUpFormat?.bestOf ?? 1;
3292
3295
  const setsToWin = getSetsToWin(bestOf);
3296
+ const tiebreaksTally = [0, 0];
3293
3297
  const pointsTally = [0, 0];
3294
3298
  score?.sets?.forEach((set, i) => {
3295
3299
  const setNumber = set.setNumber || i + 1;
@@ -3306,9 +3310,11 @@ function countPoints({ matchUpFormat, score }) {
3306
3310
  pointsTally[0] += ensureInt(set.side1TiebreakScore || 0);
3307
3311
  if (set.side2TiebreakScore)
3308
3312
  pointsTally[1] += ensureInt(set.side2TiebreakScore || 0);
3313
+ if ((set.side1TiebreakScore || set.side2TiebreakScore) && set.winningSide)
3314
+ tiebreaksTally[set.winningSide - 1] += 1;
3309
3315
  }
3310
3316
  });
3311
- return pointsTally;
3317
+ return { pointsTally, tiebreaksTally };
3312
3318
  }
3313
3319
  function getSetsToWin(bestOfGames) {
3314
3320
  return bestOfGames && Math.ceil(bestOfGames / 2) || 1;
@@ -3423,7 +3429,7 @@ function getParticipantResults({
3423
3429
  const winningParticipantId = winningSide && getWinningSideId(matchUp);
3424
3430
  const losingParticipantId = winningSide && getLosingSideId(matchUp);
3425
3431
  if (!winningParticipantId && !losingParticipantId) {
3426
- if (completedMatchUpStatuses.includes(matchUpStatus)) {
3432
+ if (matchUpStatus && completedMatchUpStatuses.includes(matchUpStatus)) {
3427
3433
  const participantIdSide1 = getSideId(matchUp, 0);
3428
3434
  const participantIdSide2 = getSideId(matchUp, 1);
3429
3435
  if (participantIdSide1) {
@@ -3438,10 +3444,10 @@ function getParticipantResults({
3438
3444
  perPlayer = 0;
3439
3445
  for (const tieMatchUp of tieMatchUps) {
3440
3446
  if (tieMatchUp.winningSide) {
3441
- const tieWinningParticipantId = sides.find(
3447
+ const tieWinningParticipantId = sides?.find(
3442
3448
  ({ sideNumber }) => sideNumber === tieMatchUp.winningSide
3443
3449
  )?.participantId;
3444
- const tieLosingParticipantId = sides.find(
3450
+ const tieLosingParticipantId = sides?.find(
3445
3451
  ({ sideNumber }) => sideNumber === tieMatchUp.winningSide
3446
3452
  )?.participantId;
3447
3453
  if (tieWinningParticipantId && tieLosingParticipantId) {
@@ -3560,26 +3566,30 @@ function getParticipantResults({
3560
3566
  }
3561
3567
  }
3562
3568
  if (manualGamesOverride) {
3563
- const side1participantId = sides.find(
3569
+ const side1participantId = sides?.find(
3564
3570
  ({ sideNumber }) => sideNumber === 1
3565
3571
  )?.participantId;
3566
- const side2participantId = sides.find(
3572
+ const side2participantId = sides?.find(
3567
3573
  ({ sideNumber }) => sideNumber === 2
3568
3574
  )?.participantId;
3569
3575
  checkInitializeParticipant(participantResults, side1participantId);
3570
3576
  checkInitializeParticipant(participantResults, side2participantId);
3571
- const gamesWonSide1 = score.sets.reduce(
3572
- (total, set) => total + set.side1Score,
3577
+ const gamesWonSide1 = score?.sets?.reduce(
3578
+ (total, set) => total + (set?.side1Score ?? 0),
3573
3579
  0
3574
3580
  );
3575
- const gamesWonSide2 = score.sets.reduce(
3576
- (total, set) => total + set.side2Score,
3581
+ const gamesWonSide2 = score?.sets?.reduce(
3582
+ (total, set) => total + (set.side2Score ?? 0),
3577
3583
  0
3578
3584
  );
3579
- participantResults[side1participantId].gamesWon += gamesWonSide1;
3580
- participantResults[side2participantId].gamesWon += gamesWonSide2;
3581
- participantResults[side1participantId].gamesLost += gamesWonSide2;
3582
- participantResults[side2participantId].gamesLost += gamesWonSide1;
3585
+ if (side1participantId) {
3586
+ participantResults[side1participantId].gamesWon += gamesWonSide1;
3587
+ participantResults[side1participantId].gamesLost += gamesWonSide2;
3588
+ }
3589
+ if (side2participantId) {
3590
+ participantResults[side2participantId].gamesWon += gamesWonSide2;
3591
+ participantResults[side2participantId].gamesLost += gamesWonSide1;
3592
+ }
3583
3593
  }
3584
3594
  }
3585
3595
  calculatePercentages({
@@ -3706,7 +3716,7 @@ function processMatchUp$1({
3706
3716
  winningSide,
3707
3717
  score
3708
3718
  });
3709
- const pointsTally = countPoints({ score, matchUpFormat });
3719
+ const { pointsTally } = countPoints({ score, matchUpFormat });
3710
3720
  if (winningParticipantId) {
3711
3721
  participantResults[winningParticipantId].setsWon += setsTally[winningSideIndex];
3712
3722
  participantResults[winningParticipantId].setsLost += setsTally[losingSideIndex];
@@ -37986,7 +37996,7 @@ function setState$4(records, deepCopyOption = true) {
37986
37996
  function engineLogging({ result, methodName, elapsed, params, engine }) {
37987
37997
  const devContext = getDevContext();
37988
37998
  const log = { method: methodName };
37989
- const logError = result.error && (devContext.errors === true || Array.isArray(devContext.errors) && devContext.errors.includes(methodName));
37999
+ const logError = result?.error && (devContext.errors === true || Array.isArray(devContext.errors) && devContext.errors.includes(methodName));
37990
38000
  const specifiedMethodParams = Array.isArray(devContext.params) && devContext.params?.includes(methodName);
37991
38001
  const logParams = devContext.params && !Array.isArray(devContext.params) || specifiedMethodParams;
37992
38002
  const exclude = Array.isArray(devContext.exclude) && devContext.exclude.includes(methodName);
@@ -38000,7 +38010,7 @@ function engineLogging({ result, methodName, elapsed, params, engine }) {
38000
38010
  }
38001
38011
  if (Object.keys(log).length > 1)
38002
38012
  console.log(engine, log);
38003
- if (devContext.makeDeepCopy)
38013
+ if (result && devContext.makeDeepCopy)
38004
38014
  result.deepCopyIterations = getDeepCopyIterations();
38005
38015
  }
38006
38016
 
@@ -43536,7 +43546,7 @@ function buildDrawHierarchy({
43536
43546
  const firstRoundDrawPositions = firstRoundPairedDrawPositions.flat(Infinity);
43537
43547
  const secondRoundDrawPositions = secondRoundMatchUps.map((matchUp) => matchUp.drawPositions).flat(Infinity);
43538
43548
  const secondRoundEntries = secondRoundDrawPositions.filter((drawPosition) => !firstRoundDrawPositions.includes(drawPosition)).sort(drawPositionSort);
43539
- const secondRoundEntriySides = secondRoundMatchUps.filter(
43549
+ const secondRoundEntrySides = secondRoundMatchUps.filter(
43540
43550
  (matchUp) => matchUp.drawPositions?.reduce(
43541
43551
  (p, c) => secondRoundEntries.includes(c) || p,
43542
43552
  void 0
@@ -43554,7 +43564,7 @@ function buildDrawHierarchy({
43554
43564
  const missingPairs = secondRoundEntries.map((drawPosition, index) => {
43555
43565
  return [drawPosition, missingDrawPositions[index]].sort(drawPositionSort);
43556
43566
  });
43557
- const entrySides = Object.assign({}, ...secondRoundEntriySides);
43567
+ const entrySides = Object.assign({}, ...secondRoundEntrySides);
43558
43568
  const finishingRound = makeDeepCopy(
43559
43569
  firstRoundMatchUps?.[0].finishingRound,
43560
43570
  false,
@@ -54857,7 +54867,197 @@ function getPositionManipulations({ extensions }) {
54857
54867
  return extensions?.find(({ name }) => name === AUDIT_POSITION_ACTIONS)?.value?.slice(1);
54858
54868
  }
54859
54869
 
54860
- const reportGovernor = { getStructureReports, getEntryStatusReports };
54870
+ function getTeamStats({
54871
+ opponentParticipantId,
54872
+ teamParticipantId,
54873
+ tournamentRecord,
54874
+ tallyPolicy,
54875
+ matchUps
54876
+ }) {
54877
+ if (!tournamentRecord)
54878
+ return { error: MISSING_TOURNAMENT_RECORD };
54879
+ if (matchUps && !Array.isArray(matchUps))
54880
+ return { error: INVALID_MATCHUP };
54881
+ matchUps = matchUps || allTournamentMatchUps({ tournamentRecord }).matchUps;
54882
+ if (!matchUps?.length)
54883
+ return { error: MISSING_MATCHUPS };
54884
+ const teamParticipantIds = [];
54885
+ if (opponentParticipantId)
54886
+ teamParticipantIds.push(opponentParticipantId);
54887
+ if (teamParticipantId)
54888
+ teamParticipantIds.push(teamParticipantId);
54889
+ const participantFilters = !teamParticipantIds.length ? { participantTypes: [TEAM_PARTICIPANT] } : { participantIds: teamParticipantIds };
54890
+ const teamParticipants = getParticipants$1({ participantFilters, tournamentRecord }).participants ?? [];
54891
+ if (!teamParticipants.every(
54892
+ ({ participantType }) => participantType === TEAM_PARTICIPANT
54893
+ )) {
54894
+ return { error: INVALID_PARTICIPANT_IDS };
54895
+ }
54896
+ if (!teamParticipantIds.length)
54897
+ teamParticipantIds.push(...teamParticipants.map(extractAttributes("participantId")));
54898
+ const teamStats = /* @__PURE__ */ new Map();
54899
+ const teamMap = /* @__PURE__ */ new Map();
54900
+ for (const teamParticipant of teamParticipants) {
54901
+ const { participantId, individualParticipantIds } = teamParticipant;
54902
+ teamMap.set(participantId, individualParticipantIds ?? []);
54903
+ teamStats.set(participantId, {
54904
+ participantName: teamParticipant.participantName ?? "",
54905
+ matchUpStatuses: {},
54906
+ competitorIds: [],
54907
+ tiebreaks: [0, 0],
54908
+ matchUps: [0, 0],
54909
+ points: [0, 0],
54910
+ participantId,
54911
+ games: [0, 0],
54912
+ sets: [0, 0]
54913
+ });
54914
+ }
54915
+ if (teamParticipantId && !teamMap.get(teamParticipantId))
54916
+ return decorateResult({
54917
+ result: { error: PARTICIPANT_NOT_FOUND },
54918
+ context: { teamParticipantId }
54919
+ });
54920
+ if (opponentParticipantId && !teamMap.get(opponentParticipantId))
54921
+ return decorateResult({
54922
+ result: { error: PARTICIPANT_NOT_FOUND },
54923
+ context: { opponentParticipantId }
54924
+ });
54925
+ const relevantMatchUps = [];
54926
+ const getTeamParticipantIds = (sides) => {
54927
+ const sideTeamParticipantIds = [];
54928
+ const isTeamSide = (side, individualParticipantIds) => {
54929
+ return !!(side.participantId && individualParticipantIds.includes(side.participantId) || side.participant?.individualParticipantIds?.length && intersection(
54930
+ individualParticipantIds,
54931
+ side.participant?.individualParticipantIds
54932
+ ).length === side.participant?.individualParticipantIds?.length);
54933
+ };
54934
+ for (const [teamParticipantId2, individualParticipantIds] of teamMap) {
54935
+ for (const side of sides) {
54936
+ if (!side.participant)
54937
+ continue;
54938
+ if (isTeamSide(side, individualParticipantIds)) {
54939
+ const sideNumber = side.sideNumber;
54940
+ if (!sideNumber)
54941
+ continue;
54942
+ sideTeamParticipantIds[sideNumber - 1] = teamParticipantId2;
54943
+ const competitorIds = side.participant?.individualParticipantIds?.length ? side.participant.individualParticipantIds : [side.participant.participantId];
54944
+ const stats = teamStats.get(teamParticipantId2);
54945
+ for (const id of competitorIds) {
54946
+ if (stats && !stats.competitorIds.includes(id))
54947
+ stats.competitorIds.push(id);
54948
+ }
54949
+ }
54950
+ }
54951
+ }
54952
+ return sideTeamParticipantIds;
54953
+ };
54954
+ for (const matchUp of matchUps) {
54955
+ if (!isObject(matchUp))
54956
+ return { error: INVALID_MATCHUP };
54957
+ const {
54958
+ matchUpStatus,
54959
+ matchUpFormat,
54960
+ matchUpType,
54961
+ winningSide,
54962
+ score,
54963
+ sides
54964
+ } = matchUp;
54965
+ if (!sides || !score || matchUpType === TEAM_MATCHUP || matchUpStatus === BYE)
54966
+ continue;
54967
+ const teamParticipantIds2 = getTeamParticipantIds(sides);
54968
+ if (!teamParticipantIds2.filter(Boolean).length)
54969
+ continue;
54970
+ relevantMatchUps.push(matchUp);
54971
+ const setsTally = countSets({
54972
+ matchUpStatus,
54973
+ matchUpFormat,
54974
+ tallyPolicy,
54975
+ winningSide,
54976
+ score
54977
+ });
54978
+ const gamesTally = countGames({
54979
+ matchUpStatus,
54980
+ matchUpFormat,
54981
+ tallyPolicy,
54982
+ winningSide,
54983
+ score
54984
+ });
54985
+ const { pointsTally, tiebreaksTally } = countPoints({
54986
+ matchUpFormat,
54987
+ score
54988
+ });
54989
+ teamParticipantIds2.forEach((teamParticipantId2, index) => {
54990
+ if (teamParticipantId2) {
54991
+ const stats = teamStats.get(teamParticipantId2);
54992
+ if (stats) {
54993
+ const teamSumTally = (stat, tally) => tally.forEach((t, i) => stats[stat][i] += t);
54994
+ const tiebreaks = index ? [...tiebreaksTally].reverse() : tiebreaksTally;
54995
+ const points = index ? [...pointsTally].reverse() : pointsTally;
54996
+ const games = index ? [...gamesTally].reverse() : gamesTally;
54997
+ const sets = index ? [...setsTally].reverse() : setsTally;
54998
+ teamSumTally("tiebreaks", tiebreaks);
54999
+ teamSumTally("points", points);
55000
+ teamSumTally("games", games);
55001
+ teamSumTally("sets", sets);
55002
+ if (winningSide) {
55003
+ const tallyIndex = winningSide - 1 === index ? 0 : 1;
55004
+ stats.matchUps[tallyIndex] += 1;
55005
+ }
55006
+ if (matchUpStatus) {
55007
+ if (!stats.matchUpStatuses[matchUpStatus])
55008
+ stats.matchUpStatuses[matchUpStatus] = 0;
55009
+ stats.matchUpStatuses[matchUpStatus] += 1;
55010
+ }
55011
+ }
55012
+ }
55013
+ });
55014
+ }
55015
+ const attrs = ["tiebreaks", "matchUps", "points", "games", "sets"];
55016
+ const ratio = /* @__PURE__ */ new Map();
55017
+ const add = (a, b) => (a ?? 0) + (b ?? 0);
55018
+ for (const stats of teamStats.values()) {
55019
+ for (const attr of attrs) {
55020
+ const total = stats[attr].reduce(add);
55021
+ if (total) {
55022
+ const value = stats[attr][0] / total;
55023
+ const accessor = `${attr}Ratio`;
55024
+ const fixedValue = parseFloat(value.toFixed(2));
55025
+ stats[accessor] = fixedValue;
55026
+ if (!ratio.has(accessor))
55027
+ ratio.set(accessor, []);
55028
+ ratio.get(accessor)?.push(fixedValue);
55029
+ }
55030
+ }
55031
+ }
55032
+ const highLowSort = (a, b) => b - a;
55033
+ for (const stats of teamStats.values()) {
55034
+ for (const attr of attrs) {
55035
+ const accessor = `${attr}Ratio`;
55036
+ if (typeof stats[accessor] === "number") {
55037
+ const index = ratio.get(accessor)?.sort(highLowSort).indexOf(stats[accessor]);
55038
+ if (typeof index === "number" && index >= 0) {
55039
+ const rankAccessor = `${attr}Rank`;
55040
+ stats[rankAccessor] = index + 1;
55041
+ }
55042
+ }
55043
+ }
55044
+ }
55045
+ const result = { relevantMatchUps, ...SUCCESS };
55046
+ if (teamParticipantId) {
55047
+ result.teamStats = teamStats.get(teamParticipantId);
55048
+ if (opponentParticipantId)
55049
+ result.opponentStats = teamStats.get(opponentParticipantId);
55050
+ } else {
55051
+ result.allTeamStats = [...teamStats.values()];
55052
+ }
55053
+ return result;
55054
+ }
55055
+
55056
+ const reportGovernor = {
55057
+ getStructureReports,
55058
+ getEntryStatusReports,
55059
+ getTeamStats
55060
+ };
54861
55061
 
54862
55062
  function setDrawParticipantRepresentativeIds({
54863
55063
  representativeParticipantIds,