tods-competition-factory 2.2.38 → 2.2.40

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.
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function factoryVersion() {
6
- return '2.2.38';
6
+ return '2.2.40';
7
7
  }
8
8
 
9
9
  const SINGLES_MATCHUP = 'SINGLES';
@@ -1992,8 +1992,15 @@ function getNumber$1(formatstring) {
1992
1992
  }
1993
1993
  function timedSetFormat(matchUpFormatObject) {
1994
1994
  let value = `T${matchUpFormatObject.minutes}`;
1995
- if (matchUpFormatObject.based)
1996
- value += matchUpFormatObject.based;
1995
+ if (matchUpFormatObject.based === 'A') {
1996
+ value += 'A';
1997
+ }
1998
+ else if (matchUpFormatObject.based === 'P') {
1999
+ value += 'P';
2000
+ }
2001
+ if (matchUpFormatObject.tiebreakFormat?.tiebreakTo) {
2002
+ value += `/TB${matchUpFormatObject.tiebreakFormat.tiebreakTo}`;
2003
+ }
1997
2004
  if (matchUpFormatObject.modifier)
1998
2005
  value += `@${matchUpFormatObject.modifier}`;
1999
2006
  return value;
@@ -2005,7 +2012,8 @@ function getSetFormat(matchUpFormatObject, preserveRedundant) {
2005
2012
  if (matchUpFormatObject.setFormat?.timed && matchUpFormatObject.simplified && setLimit === 1) {
2006
2013
  return timedSetFormat(matchUpFormatObject.setFormat);
2007
2014
  }
2008
- const setLimitCode = (setLimit && `${SET}${setLimit}`) || '';
2015
+ const exactlySuffix = exactly && exactly !== 1 ? 'X' : '';
2016
+ const setLimitCode = (setLimit && `${SET}${setLimit}${exactlySuffix}`) || '';
2009
2017
  const setCountValue = stringifySet(matchUpFormatObject.setFormat, preserveRedundant);
2010
2018
  const setCode = (setCountValue && `S:${setCountValue}`) || '';
2011
2019
  const finalSetCountValue = stringifySet(matchUpFormatObject.finalSetFormat, preserveRedundant);
@@ -2079,9 +2087,12 @@ function parse(matchUpFormatCode) {
2079
2087
  }
2080
2088
  function setsMatch(formatstring) {
2081
2089
  const parts = formatstring.split('-');
2082
- const setsCount = getNumber(parts[0].slice(3));
2083
- const bestOf = setsCount === 1 || setsCount % 2 !== 0 ? setsCount : undefined;
2084
- const exactly = setsCount !== 1 && setsCount % 2 === 0 ? setsCount : undefined;
2090
+ const setsPart = parts[0].slice(3);
2091
+ const hasExactlySuffix = setsPart.endsWith('X');
2092
+ const setsCountString = hasExactlySuffix ? setsPart.slice(0, -1) : setsPart;
2093
+ const setsCount = getNumber(setsCountString);
2094
+ const bestOf = hasExactlySuffix && setsCount !== 1 ? undefined : setsCount;
2095
+ const exactly = hasExactlySuffix && setsCount !== 1 ? setsCount : undefined;
2085
2096
  const setFormat = parts && parseSetFormat(parts[1]);
2086
2097
  const finalSetFormat = parts && parseSetFormat(parts[2]);
2087
2098
  const timed = (setFormat && setFormat.timed) || (finalSetFormat && finalSetFormat.timed);
@@ -2099,44 +2110,56 @@ function setsMatch(formatstring) {
2099
2110
  return result;
2100
2111
  }
2101
2112
  function parseSetFormat(formatstring) {
2102
- if (formatstring?.[1] === ':') {
2103
- const parts = formatstring.split(':');
2104
- const setType = setTypes$1[parts[0]];
2105
- const setFormatString = parts[1];
2106
- if (setType && setFormatString) {
2107
- const isTiebreakSet = setFormatString.startsWith('TB');
2108
- if (isTiebreakSet) {
2109
- const tiebreakSet = parseTiebreakFormat(setFormatString);
2110
- if (tiebreakSet === false)
2111
- return false;
2112
- return typeof tiebreakSet === 'object' ? { tiebreakSet } : undefined;
2113
- }
2114
- const timedSet = setFormatString.startsWith('T');
2115
- if (timedSet)
2116
- return parseTimedSet(setFormatString);
2117
- const parts = formatstring.match(/^[FS]:(\d+)([A-Za-z]*)/);
2118
- const NoAD = (parts && isNoAD(parts[2])) || false;
2119
- const validNoAD = !parts?.[2] || NoAD;
2120
- const setTo = parts ? getNumber(parts[1]) : undefined;
2121
- const tiebreakAtValue = parseTiebreakAt(setFormatString);
2122
- const validTiebreakAt = tiebreakAtValue !== false;
2123
- const tiebreakAt = (validTiebreakAt && tiebreakAtValue) || setTo;
2124
- const tiebreakFormat = parseTiebreakFormat(setFormatString.split('/')[1]);
2125
- const validTiebreak = tiebreakFormat !== false;
2126
- const result = { setTo };
2127
- if (NoAD)
2128
- result.NoAD = true;
2129
- if (tiebreakFormat) {
2130
- result.tiebreakFormat = tiebreakFormat;
2131
- result.tiebreakAt = tiebreakAt;
2132
- }
2133
- else {
2134
- result.noTiebreak = true;
2135
- }
2136
- return (setTo && validNoAD && validTiebreak && validTiebreakAt && result) || false;
2137
- }
2113
+ if (formatstring?.[1] !== ':')
2114
+ return undefined;
2115
+ const parts = formatstring.split(':');
2116
+ const setType = setTypes$1[parts[0]];
2117
+ const setFormatString = parts[1];
2118
+ if (!setType || !setFormatString)
2119
+ return undefined;
2120
+ return parseSetFormatString(formatstring, setFormatString);
2121
+ }
2122
+ function parseSetFormatString(formatstring, setFormatString) {
2123
+ if (setFormatString.startsWith('TB')) {
2124
+ return parseTiebreakSetFormat(setFormatString);
2138
2125
  }
2139
- return undefined;
2126
+ if (setFormatString.startsWith('T')) {
2127
+ return parseTimedSet(setFormatString);
2128
+ }
2129
+ return parseStandardSetFormat(formatstring, setFormatString);
2130
+ }
2131
+ function parseTiebreakSetFormat(setFormatString) {
2132
+ const tiebreakSet = parseTiebreakFormat(setFormatString);
2133
+ if (tiebreakSet === false)
2134
+ return false;
2135
+ return typeof tiebreakSet === 'object' ? { tiebreakSet } : undefined;
2136
+ }
2137
+ function parseStandardSetFormat(formatstring, setFormatString) {
2138
+ const parts = /^[FS]:(\d+)([A-Za-z]*)/.exec(formatstring);
2139
+ const NoAD = (parts && isNoAD(parts[2])) || false;
2140
+ const validNoAD = !parts?.[2] || NoAD;
2141
+ const setTo = parts ? getNumber(parts[1]) : undefined;
2142
+ const tiebreakAtValue = parseTiebreakAt(setFormatString);
2143
+ const validTiebreakAt = tiebreakAtValue !== false;
2144
+ const tiebreakAt = (validTiebreakAt && tiebreakAtValue) || setTo;
2145
+ const tiebreakFormat = parseTiebreakFormat(setFormatString.split('/')[1]);
2146
+ const validTiebreak = tiebreakFormat !== false;
2147
+ if (!setTo || !validNoAD || !validTiebreak || !validTiebreakAt)
2148
+ return false;
2149
+ return buildSetFormatResult(setTo, NoAD, tiebreakFormat, tiebreakAt);
2150
+ }
2151
+ function buildSetFormatResult(setTo, NoAD, tiebreakFormat, tiebreakAt) {
2152
+ const result = { setTo };
2153
+ if (NoAD)
2154
+ result.NoAD = true;
2155
+ if (tiebreakFormat) {
2156
+ result.tiebreakFormat = tiebreakFormat;
2157
+ result.tiebreakAt = tiebreakAt;
2158
+ }
2159
+ else {
2160
+ result.noTiebreak = true;
2161
+ }
2162
+ return result;
2140
2163
  }
2141
2164
  function parseTiebreakAt(setFormatString, expectNumber = true) {
2142
2165
  const tiebreakAtValue = setFormatString?.indexOf('@') > 0 && setFormatString.split('@');
@@ -2147,59 +2170,73 @@ function parseTiebreakAt(setFormatString, expectNumber = true) {
2147
2170
  return undefined;
2148
2171
  }
2149
2172
  function parseTiebreakFormat(formatstring) {
2150
- if (formatstring) {
2151
- if (formatstring.startsWith('TB')) {
2152
- const modifier = parseTiebreakAt(formatstring, false);
2153
- const parts = formatstring.match(/^TB(\d+)([A-Za-z]*)/);
2154
- const tiebreakToString = parts?.[1];
2155
- const NoAD = parts && isNoAD(parts[2]);
2156
- const validNoAD = !parts?.[2] || NoAD;
2157
- const tiebreakTo = getNumber(tiebreakToString);
2158
- if (tiebreakTo && validNoAD) {
2159
- const result = { tiebreakTo };
2160
- if (modifier && typeof modifier === 'string' && !isConvertableInteger(modifier)) {
2161
- result.modifier = modifier;
2162
- }
2163
- if (NoAD)
2164
- result.NoAD = true;
2165
- return result;
2166
- }
2167
- else {
2168
- return false;
2169
- }
2170
- }
2171
- else {
2172
- return false;
2173
- }
2173
+ if (!formatstring)
2174
+ return undefined;
2175
+ if (!formatstring.startsWith('TB'))
2176
+ return false;
2177
+ return parseTiebreakDetails(formatstring);
2178
+ }
2179
+ function parseTiebreakDetails(formatstring) {
2180
+ const modifier = parseTiebreakAt(formatstring, false);
2181
+ const parts = /^TB(\d+)([A-Za-z]*)/.exec(formatstring);
2182
+ const tiebreakToString = parts?.[1];
2183
+ const NoAD = parts && isNoAD(parts[2]);
2184
+ const validNoAD = !parts?.[2] || NoAD;
2185
+ const tiebreakTo = getNumber(tiebreakToString);
2186
+ if (!tiebreakTo || !validNoAD)
2187
+ return false;
2188
+ const result = { tiebreakTo };
2189
+ if (modifier && typeof modifier === 'string' && !isConvertableInteger(modifier)) {
2190
+ result.modifier = modifier;
2174
2191
  }
2175
- return undefined;
2192
+ if (NoAD)
2193
+ result.NoAD = true;
2194
+ return result;
2176
2195
  }
2177
2196
  function parseTimedSet(formatstring) {
2178
2197
  const timestring = formatstring.slice(1);
2179
- const parts = timestring.match(/^(\d+)(@?[A-Za-z]*)/);
2198
+ const parts = /^(\d+)([PGA])?(?:\/TB(\d+))?(@?[A-Za-z]*)?/.exec(timestring);
2180
2199
  const minutes = getNumber(parts?.[1]);
2181
2200
  if (!minutes)
2182
2201
  return;
2183
2202
  const setFormat = { timed: true, minutes };
2184
- const based = parts?.[2];
2185
- const validModifier = [undefined, 'P', 'G'].includes(based);
2186
- if (based && !validModifier) {
2187
- const modifier = timestring.match(/^(\d+)(@)([A-Za-z]+)$/)?.[3];
2203
+ const scoringMethod = parts?.[2];
2204
+ if (scoringMethod === 'A') {
2205
+ setFormat.based = 'A';
2206
+ }
2207
+ else if (scoringMethod === 'P') {
2208
+ setFormat.based = 'P';
2209
+ }
2210
+ else if (scoringMethod === 'G') {
2211
+ setFormat.based = 'G';
2212
+ }
2213
+ const setTiebreakTo = parts?.[3];
2214
+ if (setTiebreakTo) {
2215
+ const tiebreakToNumber = getNumber(setTiebreakTo);
2216
+ if (tiebreakToNumber) {
2217
+ setFormat.tiebreakFormat = { tiebreakTo: tiebreakToNumber };
2218
+ }
2219
+ }
2220
+ const legacyModifier = parts?.[4];
2221
+ const validModifier = [undefined, 'P', 'G', ''].includes(legacyModifier);
2222
+ if (legacyModifier && !validModifier) {
2223
+ const modifier = /^(\d+)([PGA])?(?:\/TB\d+)?(@)([A-Za-z]+)$/.exec(timestring)?.[4];
2188
2224
  if (modifier) {
2189
2225
  setFormat.modifier = modifier;
2190
2226
  return setFormat;
2191
2227
  }
2192
2228
  return;
2193
2229
  }
2194
- if (based)
2195
- setFormat.based = parts[2];
2230
+ if (legacyModifier)
2231
+ setFormat.based = legacyModifier;
2196
2232
  return setFormat;
2197
2233
  }
2198
2234
  function isNoAD(formatstring) {
2199
- return formatstring && formatstring.indexOf(NOAD) >= 0;
2235
+ return formatstring?.includes(NOAD);
2200
2236
  }
2201
2237
  function getNumber(formatstring) {
2202
- return !isNaN(Number(formatstring)) ? Number(formatstring) : 0;
2238
+ const num = Number(formatstring);
2239
+ return Number.isNaN(num) ? 0 : num;
2203
2240
  }
2204
2241
 
2205
2242
  function isValidMatchUpFormat({ matchUpFormat }) {
@@ -32114,7 +32151,7 @@ function tidyScore(params) {
32114
32151
  doProcess(processingOrder);
32115
32152
  let isValid = isValidPattern(score);
32116
32153
  if (!isValid) {
32117
- score = incomingScore.toString().replace(/\D/g, '');
32154
+ score = incomingScore.toString().replaceAll(/\D/g, '');
32118
32155
  if (attributes?.removed) {
32119
32156
  attributes.removed = undefined;
32120
32157
  }
@@ -32147,6 +32184,179 @@ var help = {
32147
32184
  tidyScore: tidyScore
32148
32185
  };
32149
32186
 
32187
+ function validateTiebreakOnlySet(winnerScore, loserScore, scoreDiff, tiebreakSetTo, allowIncomplete) {
32188
+ if (allowIncomplete) {
32189
+ return { isValid: true };
32190
+ }
32191
+ if (winnerScore === 0 && loserScore === 0) {
32192
+ return { isValid: false, error: 'Tiebreak-only set requires both scores' };
32193
+ }
32194
+ if (winnerScore < tiebreakSetTo) {
32195
+ return {
32196
+ isValid: false,
32197
+ error: `Tiebreak-only set winner must reach at least ${tiebreakSetTo}, got ${winnerScore}`,
32198
+ };
32199
+ }
32200
+ if (scoreDiff < 2) {
32201
+ return {
32202
+ isValid: false,
32203
+ error: `Tiebreak-only set must be won by at least 2 points, got ${winnerScore}-${loserScore}`,
32204
+ };
32205
+ }
32206
+ if (winnerScore === tiebreakSetTo && loserScore > tiebreakSetTo - 2) {
32207
+ return {
32208
+ isValid: false,
32209
+ error: `Tiebreak-only set at ${tiebreakSetTo}-${loserScore} requires playing past ${tiebreakSetTo}`,
32210
+ };
32211
+ }
32212
+ if (winnerScore > tiebreakSetTo && scoreDiff !== 2) {
32213
+ return {
32214
+ isValid: false,
32215
+ error: `Tiebreak-only set past ${tiebreakSetTo} must be won by exactly 2 points, got ${winnerScore}-${loserScore}`,
32216
+ };
32217
+ }
32218
+ return { isValid: true };
32219
+ }
32220
+ function validateTiebreakSetGames(winnerScore, loserScore, setTo, tiebreakAt) {
32221
+ const expectedWinnerScore = tiebreakAt === setTo ? setTo + 1 : setTo;
32222
+ const expectedLoserScore = tiebreakAt;
32223
+ if (winnerScore !== expectedWinnerScore) {
32224
+ return {
32225
+ isValid: false,
32226
+ error: `Tiebreak set winner must have ${expectedWinnerScore} games, got ${winnerScore}`,
32227
+ };
32228
+ }
32229
+ if (loserScore !== expectedLoserScore) {
32230
+ return {
32231
+ isValid: false,
32232
+ error: `Tiebreak set loser must have ${expectedLoserScore} games, got ${loserScore}`,
32233
+ };
32234
+ }
32235
+ return { isValid: true };
32236
+ }
32237
+ function validateExplicitTiebreakScore(side1TiebreakScore, side2TiebreakScore, tiebreakFormat) {
32238
+ const tbWinnerScore = Math.max(side1TiebreakScore || 0, side2TiebreakScore || 0);
32239
+ const tbLoserScore = Math.min(side1TiebreakScore || 0, side2TiebreakScore || 0);
32240
+ const tbDiff = tbWinnerScore - tbLoserScore;
32241
+ const tbTo = tiebreakFormat.tiebreakTo || 7;
32242
+ if (tbWinnerScore < tbTo) {
32243
+ return {
32244
+ isValid: false,
32245
+ error: `Tiebreak winner must reach ${tbTo} points, got ${tbWinnerScore}`,
32246
+ };
32247
+ }
32248
+ if (tbDiff < 2) {
32249
+ return {
32250
+ isValid: false,
32251
+ error: `Tiebreak must be won by 2 points, got ${tbWinnerScore}-${tbLoserScore}`,
32252
+ };
32253
+ }
32254
+ if (tbLoserScore >= tbTo - 1 && tbDiff > 2) {
32255
+ return {
32256
+ isValid: false,
32257
+ error: `Tiebreak score ${tbWinnerScore}-${tbLoserScore} is invalid`,
32258
+ };
32259
+ }
32260
+ return { isValid: true };
32261
+ }
32262
+ function validateTwoGameMargin(side1Score, side2Score, setTo, tiebreakAt) {
32263
+ if (!tiebreakAt)
32264
+ return { isValid: true };
32265
+ if (side1Score === setTo + 1 && side2Score < setTo - 1) {
32266
+ return {
32267
+ isValid: false,
32268
+ error: `With tiebreak format, if side 1 has ${setTo + 1} games, side 2 must be at least ${setTo - 1}, got ${side2Score}`,
32269
+ };
32270
+ }
32271
+ if (side2Score === setTo + 1 && side1Score < setTo - 1) {
32272
+ return {
32273
+ isValid: false,
32274
+ error: `With tiebreak format, if side 2 has ${setTo + 1} games, side 1 must be at least ${setTo - 1}, got ${side1Score}`,
32275
+ };
32276
+ }
32277
+ return { isValid: true };
32278
+ }
32279
+ function validateRegularSetCompletion(winnerScore, loserScore, scoreDiff, setTo, tiebreakAt) {
32280
+ if (winnerScore < setTo) {
32281
+ return {
32282
+ isValid: false,
32283
+ error: `Set winner must reach ${setTo} games, got ${winnerScore}`,
32284
+ };
32285
+ }
32286
+ const isTiebreakWon = tiebreakAt && winnerScore === setTo && loserScore === tiebreakAt && scoreDiff === 1;
32287
+ if (scoreDiff < 2 && !isTiebreakWon) {
32288
+ return {
32289
+ isValid: false,
32290
+ error: `Set must be won by at least 2 games, got ${winnerScore}-${loserScore}`,
32291
+ };
32292
+ }
32293
+ if (tiebreakAt) {
32294
+ if (loserScore >= tiebreakAt && !isTiebreakWon) {
32295
+ return {
32296
+ isValid: false,
32297
+ error: `When tied at ${tiebreakAt}-${tiebreakAt}, must play tiebreak. Use format like ${tiebreakAt + 1}-${tiebreakAt}(5)`,
32298
+ };
32299
+ }
32300
+ const maxWinnerScore = tiebreakAt === setTo ? setTo + 1 : setTo;
32301
+ if (winnerScore > maxWinnerScore) {
32302
+ return {
32303
+ isValid: false,
32304
+ error: `With tiebreak format, set score cannot exceed ${maxWinnerScore}-${tiebreakAt}. Got ${winnerScore}-${loserScore}`,
32305
+ };
32306
+ }
32307
+ }
32308
+ else if (winnerScore > setTo + 10) {
32309
+ return {
32310
+ isValid: false,
32311
+ error: `Set score ${winnerScore}-${loserScore} exceeds reasonable limits`,
32312
+ };
32313
+ }
32314
+ return { isValid: true };
32315
+ }
32316
+ function parseSetScores(set, isTiebreakOnlyFormat, hasTiebreakScores) {
32317
+ const side1TiebreakScore = set.side1TiebreakScore;
32318
+ const side2TiebreakScore = set.side2TiebreakScore;
32319
+ const side1Score = isTiebreakOnlyFormat && hasTiebreakScores ? side1TiebreakScore : set.side1Score || set.side1 || 0;
32320
+ const side2Score = isTiebreakOnlyFormat && hasTiebreakScores ? side2TiebreakScore : set.side2Score || set.side2 || 0;
32321
+ return { side1Score, side2Score, side1TiebreakScore, side2TiebreakScore };
32322
+ }
32323
+ function validateTiebreakSet(winnerScore, loserScore, setTo, tiebreakAt, side1TiebreakScore, side2TiebreakScore, tiebreakFormat) {
32324
+ if (setTo && tiebreakAt) {
32325
+ const validation = validateTiebreakSetGames(winnerScore, loserScore, setTo, tiebreakAt);
32326
+ if (!validation.isValid)
32327
+ return validation;
32328
+ }
32329
+ const hasExplicitTiebreak = side1TiebreakScore !== undefined || side2TiebreakScore !== undefined;
32330
+ if (hasExplicitTiebreak && tiebreakFormat) {
32331
+ return validateExplicitTiebreakScore(side1TiebreakScore, side2TiebreakScore, tiebreakFormat);
32332
+ }
32333
+ return { isValid: true };
32334
+ }
32335
+ function validateIncompleteSet(winnerScore, loserScore, setTo) {
32336
+ if (setTo && (winnerScore > setTo + 10 || loserScore > setTo + 10)) {
32337
+ return {
32338
+ isValid: false,
32339
+ error: `Set score ${winnerScore}-${loserScore} exceeds expected range for ${setTo}-game sets`,
32340
+ };
32341
+ }
32342
+ return { isValid: true };
32343
+ }
32344
+ function validateRegularSet(scores, setFormat, allowIncomplete) {
32345
+ const { side1: side1Score, side2: side2Score, winner: winnerScore, loser: loserScore, diff: scoreDiff } = scores;
32346
+ const { setTo, tiebreakAt } = setFormat;
32347
+ if (setTo && tiebreakAt) {
32348
+ const marginValidation = validateTwoGameMargin(side1Score, side2Score, setTo, tiebreakAt);
32349
+ if (!marginValidation.isValid)
32350
+ return marginValidation;
32351
+ }
32352
+ if (allowIncomplete) {
32353
+ return validateIncompleteSet(winnerScore, loserScore, setTo);
32354
+ }
32355
+ if (setTo) {
32356
+ return validateRegularSetCompletion(winnerScore, loserScore, scoreDiff, setTo, tiebreakAt);
32357
+ }
32358
+ return { isValid: true };
32359
+ }
32150
32360
  function validateSetScore(set, matchUpFormat, isDecidingSet, allowIncomplete) {
32151
32361
  if (!matchUpFormat)
32152
32362
  return { isValid: true };
@@ -32159,152 +32369,21 @@ function validateSetScore(set, matchUpFormat, isDecidingSet, allowIncomplete) {
32159
32369
  const { setTo, tiebreakAt, tiebreakFormat, tiebreakSet } = setFormat;
32160
32370
  const tiebreakSetTo = tiebreakSet?.tiebreakTo;
32161
32371
  const isTiebreakOnlyFormat = !!tiebreakSetTo && !setTo;
32162
- const side1TiebreakScore = set.side1TiebreakScore;
32163
- const side2TiebreakScore = set.side2TiebreakScore;
32164
- const hasTiebreakScores = side1TiebreakScore !== undefined && side2TiebreakScore !== undefined;
32165
- const side1Score = isTiebreakOnlyFormat && hasTiebreakScores ? side1TiebreakScore : set.side1Score || set.side1 || 0;
32166
- const side2Score = isTiebreakOnlyFormat && hasTiebreakScores ? side2TiebreakScore : set.side2Score || set.side2 || 0;
32372
+ const hasTiebreakScores = set.side1TiebreakScore !== undefined && set.side2TiebreakScore !== undefined;
32373
+ const { side1Score, side2Score, side1TiebreakScore, side2TiebreakScore } = parseSetScores(set, isTiebreakOnlyFormat, hasTiebreakScores);
32167
32374
  const winnerScore = Math.max(side1Score, side2Score);
32168
32375
  const loserScore = Math.min(side1Score, side2Score);
32169
32376
  const scoreDiff = winnerScore - loserScore;
32170
32377
  if (isTiebreakOnlyFormat) {
32171
- if (allowIncomplete) {
32172
- return { isValid: true };
32173
- }
32174
- if (side1Score === 0 && side2Score === 0) {
32175
- return { isValid: false, error: 'Tiebreak-only set requires both scores' };
32176
- }
32177
- if (winnerScore < tiebreakSetTo) {
32178
- return {
32179
- isValid: false,
32180
- error: `Tiebreak-only set winner must reach at least ${tiebreakSetTo}, got ${winnerScore}`,
32181
- };
32182
- }
32183
- if (scoreDiff < 2) {
32184
- return {
32185
- isValid: false,
32186
- error: `Tiebreak-only set must be won by at least 2 points, got ${winnerScore}-${loserScore}`,
32187
- };
32188
- }
32189
- if (winnerScore === tiebreakSetTo && loserScore > tiebreakSetTo - 2) {
32190
- return {
32191
- isValid: false,
32192
- error: `Tiebreak-only set at ${tiebreakSetTo}-${loserScore} requires playing past ${tiebreakSetTo}`,
32193
- };
32194
- }
32195
- if (winnerScore > tiebreakSetTo && scoreDiff !== 2) {
32196
- return {
32197
- isValid: false,
32198
- error: `Tiebreak-only set past ${tiebreakSetTo} must be won by exactly 2 points, got ${winnerScore}-${loserScore}`,
32199
- };
32200
- }
32201
- return { isValid: true };
32378
+ return validateTiebreakOnlySet(winnerScore, loserScore, scoreDiff, tiebreakSetTo, allowIncomplete ?? false);
32202
32379
  }
32203
32380
  const hasExplicitTiebreak = side1TiebreakScore !== undefined || side2TiebreakScore !== undefined;
32204
32381
  const isImplicitTiebreak = setTo && winnerScore === setTo + 1 && loserScore === setTo;
32205
32382
  const hasTiebreak = hasExplicitTiebreak || isImplicitTiebreak;
32206
32383
  if (hasTiebreak) {
32207
- if (setTo && tiebreakAt) {
32208
- const expectedWinnerScore = tiebreakAt === setTo ? setTo + 1 : setTo;
32209
- const expectedLoserScore = tiebreakAt;
32210
- if (winnerScore !== expectedWinnerScore) {
32211
- return {
32212
- isValid: false,
32213
- error: `Tiebreak set winner must have ${expectedWinnerScore} games, got ${winnerScore}`,
32214
- };
32215
- }
32216
- if (loserScore !== expectedLoserScore) {
32217
- return {
32218
- isValid: false,
32219
- error: `Tiebreak set loser must have ${expectedLoserScore} games, got ${loserScore}`,
32220
- };
32221
- }
32222
- }
32223
- if (hasExplicitTiebreak && tiebreakFormat) {
32224
- const tbWinnerScore = Math.max(side1TiebreakScore || 0, side2TiebreakScore || 0);
32225
- const tbLoserScore = Math.min(side1TiebreakScore || 0, side2TiebreakScore || 0);
32226
- const tbDiff = tbWinnerScore - tbLoserScore;
32227
- const tbTo = tiebreakFormat.tiebreakTo || 7;
32228
- if (tbWinnerScore < tbTo) {
32229
- return {
32230
- isValid: false,
32231
- error: `Tiebreak winner must reach ${tbTo} points, got ${tbWinnerScore}`,
32232
- };
32233
- }
32234
- if (tbDiff < 2) {
32235
- return {
32236
- isValid: false,
32237
- error: `Tiebreak must be won by 2 points, got ${tbWinnerScore}-${tbLoserScore}`,
32238
- };
32239
- }
32240
- if (tbLoserScore >= tbTo - 1 && tbDiff > 2) {
32241
- return {
32242
- isValid: false,
32243
- error: `Tiebreak score ${tbWinnerScore}-${tbLoserScore} is invalid`,
32244
- };
32245
- }
32246
- }
32247
- }
32248
- else {
32249
- if (setTo && tiebreakAt) {
32250
- if (side1Score === setTo + 1 && side2Score < setTo - 1) {
32251
- return {
32252
- isValid: false,
32253
- error: `With tiebreak format, if side 1 has ${setTo + 1} games, side 2 must be at least ${setTo - 1}, got ${side2Score}`,
32254
- };
32255
- }
32256
- if (side2Score === setTo + 1 && side1Score < setTo - 1) {
32257
- return {
32258
- isValid: false,
32259
- error: `With tiebreak format, if side 2 has ${setTo + 1} games, side 1 must be at least ${setTo - 1}, got ${side1Score}`,
32260
- };
32261
- }
32262
- }
32263
- if (allowIncomplete) {
32264
- if (setTo && (winnerScore > setTo + 10 || loserScore > setTo + 10)) {
32265
- return {
32266
- isValid: false,
32267
- error: `Set score ${winnerScore}-${loserScore} exceeds expected range for ${setTo}-game sets`,
32268
- };
32269
- }
32270
- return { isValid: true };
32271
- }
32272
- if (setTo && winnerScore < setTo) {
32273
- return {
32274
- isValid: false,
32275
- error: `Set winner must reach ${setTo} games, got ${winnerScore}`,
32276
- };
32277
- }
32278
- const isTiebreakWon = tiebreakAt && winnerScore === setTo && loserScore === tiebreakAt && scoreDiff === 1;
32279
- if (scoreDiff < 2 && !isTiebreakWon) {
32280
- return {
32281
- isValid: false,
32282
- error: `Set must be won by at least 2 games, got ${winnerScore}-${loserScore}`,
32283
- };
32284
- }
32285
- if (tiebreakAt) {
32286
- if (loserScore >= tiebreakAt && !isTiebreakWon) {
32287
- return {
32288
- isValid: false,
32289
- error: `When tied at ${tiebreakAt}-${tiebreakAt}, must play tiebreak. Use format like ${tiebreakAt + 1}-${tiebreakAt}(5)`,
32290
- };
32291
- }
32292
- const maxWinnerScore = tiebreakAt === setTo ? setTo + 1 : setTo;
32293
- if (winnerScore > maxWinnerScore) {
32294
- return {
32295
- isValid: false,
32296
- error: `With tiebreak format, set score cannot exceed ${maxWinnerScore}-${tiebreakAt}. Got ${winnerScore}-${loserScore}`,
32297
- };
32298
- }
32299
- }
32300
- else if (winnerScore > setTo + 10) {
32301
- return {
32302
- isValid: false,
32303
- error: `Set score ${winnerScore}-${loserScore} exceeds reasonable limits`,
32304
- };
32305
- }
32384
+ return validateTiebreakSet(winnerScore, loserScore, setTo, tiebreakAt, side1TiebreakScore, side2TiebreakScore, tiebreakFormat);
32306
32385
  }
32307
- return { isValid: true };
32386
+ return validateRegularSet({ side1: side1Score, side2: side2Score, winner: winnerScore, loser: loserScore, diff: scoreDiff }, { setTo, tiebreakAt }, allowIncomplete);
32308
32387
  }
32309
32388
  function validateMatchUpScore(sets, matchUpFormat, matchUpStatus) {
32310
32389
  if (!sets || sets.length === 0) {
@@ -32383,12 +32462,39 @@ function analyzeScore({ existingMatchUpStatus, matchUpFormat, matchUpStatus, win
32383
32462
  const excessiveSetScore = !setValues.noTiebreak && maxSetScore > setValues.setTo + 1;
32384
32463
  return !excessiveSetScore;
32385
32464
  });
32386
- const calculatedWinningSide = ((!matchUpFormat || maxSetsCount === setsToWin) &&
32387
- maxSetsInstances === 1 &&
32388
- setsWinCounts.indexOf(maxSetsCount) + 1) ||
32389
- undefined;
32465
+ const isAggregateScoring = matchUpScoringFormat?.setFormat?.based === 'A' || matchUpScoringFormat?.finalSetFormat?.based === 'A';
32466
+ let calculatedWinningSide;
32467
+ if (isAggregateScoring && sets.length > 0) {
32468
+ const aggregateTotals = sets.reduce((totals, set) => {
32469
+ if (set.side1Score !== undefined || set.side2Score !== undefined) {
32470
+ totals[0] += set.side1Score ?? 0;
32471
+ totals[1] += set.side2Score ?? 0;
32472
+ }
32473
+ return totals;
32474
+ }, [0, 0]);
32475
+ if (aggregateTotals[0] > aggregateTotals[1]) {
32476
+ calculatedWinningSide = 1;
32477
+ }
32478
+ else if (aggregateTotals[1] > aggregateTotals[0]) {
32479
+ calculatedWinningSide = 2;
32480
+ }
32481
+ else {
32482
+ const tiebreakSet = sets.find((set) => set.side1TiebreakScore !== undefined || set.side2TiebreakScore !== undefined);
32483
+ if (tiebreakSet) {
32484
+ calculatedWinningSide = tiebreakSet.winningSide;
32485
+ }
32486
+ }
32487
+ }
32488
+ else {
32489
+ calculatedWinningSide =
32490
+ ((!matchUpFormat || maxSetsCount === setsToWin) &&
32491
+ maxSetsInstances === 1 &&
32492
+ setsWinCounts.indexOf(maxSetsCount) + 1) ||
32493
+ undefined;
32494
+ }
32390
32495
  const valid = !!(validSets &&
32391
- ((winningSide && winningSideSetsCount > losingSideSetsCount && winningSide === calculatedWinningSide) ||
32496
+ ((winningSide && isAggregateScoring && winningSide === calculatedWinningSide) ||
32497
+ (winningSide && !isAggregateScoring && winningSideSetsCount > losingSideSetsCount && winningSide === calculatedWinningSide) ||
32392
32498
  (winningSide && irregularEnding) ||
32393
32499
  (!winningSide &&
32394
32500
  !calculatedWinningSide &&
@@ -36509,9 +36615,9 @@ function courtGridRows({ courtPrefix = 'C|', minRowsCount, courtsData }) {
36509
36615
  const maxCourtOrder = courtsData?.reduce((order, court) => {
36510
36616
  const matchUps = court.matchUps || [];
36511
36617
  const courtOrder = Math.max(0, ...matchUps.map((m) => m.schedule.courtOrder || 0));
36512
- return courtOrder > order ? courtOrder : order;
36618
+ return Math.max(courtOrder, order);
36513
36619
  }, 1);
36514
- const rowsCount = minRowsCount ? Math.max(minRowsCount, maxCourtOrder) : maxCourtOrder;
36620
+ const rowsCount = Math.max(minRowsCount || 0, maxCourtOrder);
36515
36621
  const rowBuilder = generateRange(0, rowsCount).map((rowIndex) => ({
36516
36622
  matchUps: generateRange(0, courtsData.length).map((courtIndex) => {
36517
36623
  const courtInfo = courtsData[courtIndex];
@@ -37282,24 +37388,22 @@ function competitionScheduleMatchUps(params) {
37282
37388
  ({ drawIds: publishedDrawIds, detailsMap } = getCompetitionPublishedDrawDetails({ tournamentRecords }));
37283
37389
  }
37284
37390
  if (publishedDrawIds?.length) {
37285
- if (!params.contextFilters)
37286
- params.contextFilters = {};
37287
- if (!params.contextFilters?.drawIds) {
37288
- params.contextFilters.drawIds = publishedDrawIds;
37391
+ params.contextFilters ??= {};
37392
+ if (params.contextFilters.drawIds) {
37393
+ params.contextFilters.drawIds = params.contextFilters.drawIds.filter((drawId) => publishedDrawIds.includes(drawId));
37289
37394
  }
37290
37395
  else {
37291
- params.contextFilters.drawIds = params.contextFilters.drawIds.filter((drawId) => publishedDrawIds.includes(drawId));
37396
+ params.contextFilters.drawIds = publishedDrawIds;
37292
37397
  }
37293
37398
  }
37294
37399
  if (tournamentPublishStatus?.eventIds?.length) {
37295
- if (!params.matchUpFilters)
37296
- params.matchUpFilters = {};
37400
+ params.matchUpFilters ??= {};
37297
37401
  if (params.matchUpFilters?.eventIds) {
37298
- if (!params.matchUpFilters.eventIds.length) {
37299
- params.matchUpFilters.eventIds = tournamentPublishStatus.eventIds;
37402
+ if (params.matchUpFilters.eventIds.length) {
37403
+ params.matchUpFilters.eventIds = params.matchUpFilters.eventIds.filter((eventId) => tournamentPublishStatus.eventIds.includes(eventId));
37300
37404
  }
37301
37405
  else {
37302
- params.matchUpFilters.eventIds = params.matchUpFilters.eventIds.filter((eventId) => tournamentPublishStatus.eventIds.includes(eventId));
37406
+ params.matchUpFilters.eventIds = tournamentPublishStatus.eventIds;
37303
37407
  }
37304
37408
  }
37305
37409
  else {
@@ -37307,14 +37411,13 @@ function competitionScheduleMatchUps(params) {
37307
37411
  }
37308
37412
  }
37309
37413
  if (tournamentPublishStatus?.scheduledDates?.length) {
37310
- if (!params.matchUpFilters)
37311
- params.matchUpFilters = {};
37414
+ params.matchUpFilters ??= {};
37312
37415
  if (params.matchUpFilters.scheduledDates) {
37313
- if (!params.matchUpFilters.scheduledDates.length) {
37314
- params.matchUpFilters.scheduledDates = tournamentPublishStatus.scheduledDates;
37416
+ if (params.matchUpFilters.scheduledDates.length) {
37417
+ params.matchUpFilters.scheduledDates = params.matchUpFilters.scheduledDates.filter((scheduledDate) => tournamentPublishStatus.scheduledDates.includes(scheduledDate));
37315
37418
  }
37316
37419
  else {
37317
- params.matchUpFilters.scheduledDates = params.matchUpFilters.scheduledDates.filter((scheduledDate) => tournamentPublishStatus.scheduledDates.includes(scheduledDate));
37420
+ params.matchUpFilters.scheduledDates = tournamentPublishStatus.scheduledDates;
37318
37421
  }
37319
37422
  }
37320
37423
  else {
@@ -37322,8 +37425,7 @@ function competitionScheduleMatchUps(params) {
37322
37425
  }
37323
37426
  }
37324
37427
  if (alwaysReturnCompleted) {
37325
- if (!params.matchUpFilters)
37326
- params.matchUpFilters = {};
37428
+ params.matchUpFilters ??= {};
37327
37429
  if (params.matchUpFilters?.excludeMatchUpStatuses?.length) {
37328
37430
  if (!params.matchUpFilters.excludeMatchUpStatuses.includes(COMPLETED$1)) {
37329
37431
  params.matchUpFilters.excludeMatchUpStatuses.push(COMPLETED$1);
@@ -37390,7 +37492,7 @@ function competitionScheduleMatchUps(params) {
37390
37492
  };
37391
37493
  if (withCourtGridRows) {
37392
37494
  const { rows, courtPrefix } = courtGridRows({
37393
- minRowsCount: minCourtGridRows,
37495
+ minRowsCount: Math.max(minCourtGridRows || 0, dateMatchUps.length || 0),
37394
37496
  courtsData,
37395
37497
  });
37396
37498
  result.courtPrefix = courtPrefix;
@@ -51976,7 +52078,7 @@ function generateTournamentRecord(params) {
51976
52078
  return result;
51977
52079
  }
51978
52080
  if (params.drawProfiles) {
51979
- const result = processDrawProfiles({
52081
+ const result = processDrawProfiles$1({
51980
52082
  allUniqueParticipantIds,
51981
52083
  ratingsParameters: ratingsParameters$1,
51982
52084
  tournamentRecord,
@@ -51988,7 +52090,7 @@ function generateTournamentRecord(params) {
51988
52090
  return result;
51989
52091
  }
51990
52092
  if (params.eventProfiles) {
51991
- const result = processEventProfiles({
52093
+ const result = processEventProfiles$1({
51992
52094
  allUniqueParticipantIds,
51993
52095
  ratingsParameters: ratingsParameters$1,
51994
52096
  tournamentRecord,
@@ -52013,7 +52115,7 @@ function generateTournamentRecord(params) {
52013
52115
  drawIds,
52014
52116
  });
52015
52117
  }
52016
- function processDrawProfiles(params) {
52118
+ function processDrawProfiles$1(params) {
52017
52119
  const { tournamentRecord, drawProfiles, allUniqueParticipantIds, eventIds, drawIds } = params;
52018
52120
  let drawIndex = 0;
52019
52121
  for (const drawProfile of drawProfiles) {
@@ -52045,7 +52147,7 @@ function processDrawProfiles(params) {
52045
52147
  drawIndex += 1;
52046
52148
  }
52047
52149
  }
52048
- function processEventProfiles(params) {
52150
+ function processEventProfiles$1(params) {
52049
52151
  const { eventProfiles, allUniqueParticipantIds, eventIds, drawIds } = params;
52050
52152
  let eventIndex = 0;
52051
52153
  for (const eventProfile of eventProfiles) {
@@ -52372,164 +52474,150 @@ function generatePairParticipantName({ individualParticipantIds, individualParti
52372
52474
  return participantName;
52373
52475
  }
52374
52476
 
52375
- function modifyTournamentRecord(params) {
52376
- const { ratingsParameters: ratingsParameters$1 = ratingsParameters, participantsProfile = {}, matchUpStatusProfile, completeAllMatchUps, autoEntryPositions, hydrateCollections, randomWinningSide, schedulingProfile, tournamentRecord, autoSchedule, eventProfiles, periodLength, venueProfiles, drawProfiles, isMock, uuids, } = params;
52377
- if (!tournamentRecord)
52378
- return { error: MISSING_TOURNAMENT_RECORD };
52379
- const allUniqueParticipantIds = [];
52380
- const eventIds = [];
52381
- const drawIds = [];
52382
- eventProfiles?.forEach((eventProfile) => {
52383
- const event = tournamentRecord.events?.find((event, index) => (eventProfile.eventIndex !== undefined && index === eventProfile.eventIndex) ||
52384
- (eventProfile.eventName && event.eventName === eventProfile.eventName) ||
52385
- (eventProfile.eventId && event.eventId === eventProfile.eventId));
52386
- if (event?.gender) {
52387
- eventProfile.gender = event.gender;
52388
- }
52389
- });
52390
- const participantsCount = tournamentRecord.participants?.length || undefined;
52391
- if (participantsCount && participantsProfile?.idPrefix) {
52392
- participantsProfile.idPrefix = `${participantsProfile.idPrefix}-${participantsCount}`;
52477
+ function processExistingEvent({ event, eventProfile, tournamentRecord, allUniqueParticipantIds, drawIds }) {
52478
+ const { gender, category, eventType } = event;
52479
+ const { drawProfiles, publish } = eventProfile;
52480
+ const eventParticipantType = (isMatchUpEventType(SINGLES)(eventType) && INDIVIDUAL) ||
52481
+ (isMatchUpEventType(DOUBLES)(eventType) && PAIR) ||
52482
+ eventType;
52483
+ if (drawProfiles) {
52484
+ const { stageParticipantsCount, uniqueParticipantsCount, uniqueParticipantStages } = getStageParticipantsCount({
52485
+ drawProfiles,
52486
+ category,
52487
+ gender,
52488
+ });
52489
+ const { uniqueDrawParticipants = [], uniqueParticipantIds = [] } = uniqueParticipantStages
52490
+ ? generateEventParticipants({
52491
+ event: { eventType, category, gender },
52492
+ uniqueParticipantsCount,
52493
+ participantsProfile: eventProfile.participantsProfile,
52494
+ ratingsParameters: eventProfile.ratingsParameters,
52495
+ tournamentRecord,
52496
+ eventProfile,
52497
+ uuids: eventProfile.uuids,
52498
+ })
52499
+ : {};
52500
+ allUniqueParticipantIds.push(...uniqueParticipantIds);
52501
+ const { stageParticipants } = getStageParticipants({
52502
+ targetParticipants: tournamentRecord.participants || [],
52503
+ allUniqueParticipantIds,
52504
+ stageParticipantsCount,
52505
+ eventParticipantType,
52506
+ });
52507
+ let result = generateFlights({
52508
+ uniqueDrawParticipants,
52509
+ autoEntryPositions: eventProfile.autoEntryPositions,
52510
+ stageParticipants,
52511
+ tournamentRecord,
52512
+ drawProfiles,
52513
+ category,
52514
+ gender,
52515
+ event,
52516
+ });
52517
+ if (result.error)
52518
+ return result;
52519
+ result = generateFlightDrawDefinitions({
52520
+ matchUpStatusProfile: eventProfile.matchUpStatusProfile,
52521
+ completeAllMatchUps: eventProfile.completeAllMatchUps,
52522
+ randomWinningSide: eventProfile.randomWinningSide,
52523
+ tournamentRecord,
52524
+ drawProfiles,
52525
+ isMock: eventProfile.isMock,
52526
+ event,
52527
+ });
52528
+ if (result.error)
52529
+ return result;
52530
+ drawIds.push(...result.drawIds);
52393
52531
  }
52394
- const result = addTournamentParticipants({
52395
- startDate: tournamentRecord.startDate,
52396
- participantsProfile,
52397
- tournamentRecord,
52398
- eventProfiles,
52399
- drawProfiles,
52400
- uuids,
52401
- });
52402
- if (!result.success)
52403
- return result;
52404
- if (eventProfiles) {
52405
- let eventIndex = tournamentRecord.events?.length || 0;
52406
- for (const eventProfile of eventProfiles) {
52407
- const { ratingsParameters } = eventProfile;
52408
- const event = tournamentRecord.events?.find((event, index) => (eventProfile.eventIndex !== undefined && index === eventProfile.eventIndex) ||
52409
- (eventProfile.eventName && event.eventName === eventProfile.eventName) ||
52410
- (eventProfile.eventId && event.eventId === eventProfile.eventId));
52411
- if (!event) {
52412
- const result = generateEventWithFlights({
52413
- startDate: tournamentRecord.startDate,
52414
- allUniqueParticipantIds,
52415
- matchUpStatusProfile,
52416
- participantsProfile,
52417
- completeAllMatchUps,
52418
- autoEntryPositions,
52419
- randomWinningSide,
52420
- ratingsParameters,
52421
- tournamentRecord,
52422
- eventProfile,
52423
- eventIndex,
52424
- uuids,
52425
- });
52426
- if (result.error)
52427
- return result;
52428
- const { eventId, drawIds: generatedDrawIds, uniqueParticipantIds } = result;
52429
- if (generatedDrawIds)
52430
- drawIds.push(...generatedDrawIds);
52431
- eventIds.push(eventId);
52432
- if (uniqueParticipantIds?.length)
52433
- allUniqueParticipantIds.push(...uniqueParticipantIds);
52434
- eventIndex += 1;
52435
- }
52436
- else {
52437
- const { gender, category, eventType } = event;
52438
- const { drawProfiles, publish } = eventProfile;
52439
- const eventParticipantType = (isMatchUpEventType(SINGLES)(eventType) && INDIVIDUAL) ||
52440
- (isMatchUpEventType(DOUBLES)(eventType) && PAIR) ||
52441
- eventType;
52442
- if (drawProfiles) {
52443
- const { stageParticipantsCount, uniqueParticipantsCount, uniqueParticipantStages } = getStageParticipantsCount({
52444
- drawProfiles,
52445
- category,
52446
- gender,
52447
- });
52448
- const { uniqueDrawParticipants = [], uniqueParticipantIds = [] } = uniqueParticipantStages
52449
- ? generateEventParticipants({
52450
- event: { eventType, category, gender },
52451
- uniqueParticipantsCount,
52452
- participantsProfile,
52453
- ratingsParameters,
52454
- tournamentRecord,
52455
- eventProfile,
52456
- uuids,
52457
- })
52458
- : {};
52459
- allUniqueParticipantIds.push(...uniqueParticipantIds);
52460
- const { stageParticipants } = getStageParticipants({
52461
- targetParticipants: tournamentRecord.participants || [],
52462
- allUniqueParticipantIds,
52463
- stageParticipantsCount,
52464
- eventParticipantType,
52465
- });
52466
- let result = generateFlights({
52467
- uniqueDrawParticipants,
52468
- autoEntryPositions,
52469
- stageParticipants,
52470
- tournamentRecord,
52471
- drawProfiles,
52472
- category,
52473
- gender,
52474
- event,
52475
- });
52476
- if (result.error)
52477
- return result;
52478
- result = generateFlightDrawDefinitions({
52479
- matchUpStatusProfile,
52480
- completeAllMatchUps,
52481
- randomWinningSide,
52482
- tournamentRecord,
52483
- drawProfiles,
52484
- isMock,
52485
- event,
52486
- });
52487
- if (result.error)
52488
- return result;
52489
- drawIds.push(...result.drawIds);
52490
- }
52491
- if (publish) {
52492
- publishEvent({ tournamentRecord, event });
52493
- }
52494
- }
52495
- }
52532
+ if (publish) {
52533
+ publishEvent({ tournamentRecord, event });
52496
52534
  }
52497
- if (drawProfiles) {
52498
- let drawIndex = (tournamentRecord.events || [])
52499
- .map((event) => event.drawDefinitions?.map(() => 1) || [])
52500
- .flat()
52501
- .reduce((a, b) => a + b, 0);
52502
- for (const drawProfile of drawProfiles) {
52503
- let result = generateEventWithDraw({
52504
- startDate: tournamentRecord.startDate,
52505
- allUniqueParticipantIds,
52506
- matchUpStatusProfile,
52507
- participantsProfile,
52508
- completeAllMatchUps,
52509
- autoEntryPositions,
52510
- hydrateCollections,
52511
- randomWinningSide,
52512
- ratingsParameters: ratingsParameters$1,
52535
+ return SUCCESS;
52536
+ }
52537
+ function findEventByProfile(events, eventProfile) {
52538
+ return events?.find((event, index) => (eventProfile.eventIndex !== undefined && index === eventProfile.eventIndex) ||
52539
+ (eventProfile.eventName && event.eventName === eventProfile.eventName) ||
52540
+ (eventProfile.eventId && event.eventId === eventProfile.eventId));
52541
+ }
52542
+ function processEventProfiles({ eventProfiles, tournamentRecord, allUniqueParticipantIds, eventIds, drawIds, params }) {
52543
+ let eventIndex = tournamentRecord.events?.length || 0;
52544
+ for (const eventProfile of eventProfiles) {
52545
+ const event = findEventByProfile(tournamentRecord.events, eventProfile);
52546
+ if (event) {
52547
+ const result = processExistingEvent({
52548
+ event,
52549
+ eventProfile: { ...eventProfile, ...params },
52513
52550
  tournamentRecord,
52514
- drawProfile,
52515
- drawIndex,
52516
- uuids,
52551
+ allUniqueParticipantIds,
52552
+ drawIds,
52517
52553
  });
52518
52554
  if (result.error)
52519
52555
  return result;
52520
- const { drawId, eventId, event, uniqueParticipantIds } = result;
52521
- result = addEvent({ tournamentRecord, event, internalUse: true });
52556
+ }
52557
+ else {
52558
+ const result = generateEventWithFlights({
52559
+ startDate: tournamentRecord.startDate,
52560
+ allUniqueParticipantIds,
52561
+ matchUpStatusProfile: params.matchUpStatusProfile,
52562
+ participantsProfile: params.participantsProfile,
52563
+ completeAllMatchUps: params.completeAllMatchUps,
52564
+ autoEntryPositions: params.autoEntryPositions,
52565
+ randomWinningSide: params.randomWinningSide,
52566
+ ratingsParameters: eventProfile.ratingsParameters,
52567
+ tournamentRecord,
52568
+ eventProfile,
52569
+ eventIndex,
52570
+ uuids: params.uuids,
52571
+ });
52522
52572
  if (result.error)
52523
52573
  return result;
52524
- if (drawId)
52525
- drawIds.push(drawId);
52574
+ const { eventId, drawIds: generatedDrawIds, uniqueParticipantIds } = result;
52575
+ if (generatedDrawIds)
52576
+ drawIds.push(...generatedDrawIds);
52526
52577
  eventIds.push(eventId);
52527
52578
  if (uniqueParticipantIds?.length)
52528
52579
  allUniqueParticipantIds.push(...uniqueParticipantIds);
52529
- drawIndex += 1;
52580
+ eventIndex += 1;
52530
52581
  }
52531
52582
  }
52532
- const venueIds = venueProfiles?.length ? generateVenues({ tournamentRecord, venueProfiles }) : [];
52583
+ return SUCCESS;
52584
+ }
52585
+ function processDrawProfiles({ drawProfiles, tournamentRecord, allUniqueParticipantIds, eventIds, drawIds, params }) {
52586
+ let drawIndex = (tournamentRecord.events || [])
52587
+ .flatMap((event) => event.drawDefinitions?.map(() => 1) || [])
52588
+ .reduce((a, b) => a + b, 0);
52589
+ for (const drawProfile of drawProfiles) {
52590
+ let result = generateEventWithDraw({
52591
+ startDate: tournamentRecord.startDate,
52592
+ allUniqueParticipantIds,
52593
+ matchUpStatusProfile: params.matchUpStatusProfile,
52594
+ participantsProfile: params.participantsProfile,
52595
+ completeAllMatchUps: params.completeAllMatchUps,
52596
+ autoEntryPositions: params.autoEntryPositions,
52597
+ hydrateCollections: params.hydrateCollections,
52598
+ randomWinningSide: params.randomWinningSide,
52599
+ ratingsParameters: params.ratingsParameters,
52600
+ tournamentRecord,
52601
+ drawProfile,
52602
+ drawIndex,
52603
+ uuids: params.uuids,
52604
+ });
52605
+ if (result.error)
52606
+ return result;
52607
+ const { drawId, eventId, event, uniqueParticipantIds } = result;
52608
+ result = addEvent({ tournamentRecord, event, internalUse: true });
52609
+ if (result.error)
52610
+ return result;
52611
+ if (drawId)
52612
+ drawIds.push(drawId);
52613
+ eventIds.push(eventId);
52614
+ if (uniqueParticipantIds?.length)
52615
+ allUniqueParticipantIds.push(...uniqueParticipantIds);
52616
+ drawIndex += 1;
52617
+ }
52618
+ return SUCCESS;
52619
+ }
52620
+ function applySchedulingProfile({ schedulingProfile, autoSchedule, periodLength, tournamentRecord }) {
52533
52621
  let scheduledRounds;
52534
52622
  let schedulerResult = {};
52535
52623
  if (schedulingProfile?.length) {
@@ -52551,6 +52639,69 @@ function modifyTournamentRecord(params) {
52551
52639
  });
52552
52640
  }
52553
52641
  }
52642
+ return { scheduledRounds, schedulerResult };
52643
+ }
52644
+ function modifyTournamentRecord(params) {
52645
+ const { participantsProfile = {}, schedulingProfile, tournamentRecord, autoSchedule, eventProfiles, periodLength, venueProfiles, drawProfiles, uuids, } = params;
52646
+ if (!tournamentRecord)
52647
+ return { error: MISSING_TOURNAMENT_RECORD };
52648
+ const allUniqueParticipantIds = [];
52649
+ const eventIds = [];
52650
+ const drawIds = [];
52651
+ eventProfiles?.forEach((eventProfile) => {
52652
+ const event = findEventByProfile(tournamentRecord.events, eventProfile);
52653
+ if (event?.gender) {
52654
+ eventProfile.gender = event.gender;
52655
+ }
52656
+ });
52657
+ const participantsCount = tournamentRecord.participants?.length || undefined;
52658
+ if (participantsCount && participantsProfile?.idPrefix) {
52659
+ participantsProfile.idPrefix = `${participantsProfile.idPrefix}-${participantsCount}`;
52660
+ }
52661
+ const result = addTournamentParticipants({
52662
+ startDate: tournamentRecord.startDate,
52663
+ participantsProfile,
52664
+ tournamentRecord,
52665
+ eventProfiles,
52666
+ drawProfiles,
52667
+ uuids,
52668
+ });
52669
+ if (!result.success)
52670
+ return result;
52671
+ if (eventProfiles) {
52672
+ const eventResult = processEventProfiles({
52673
+ eventProfiles,
52674
+ tournamentRecord,
52675
+ allUniqueParticipantIds,
52676
+ eventIds,
52677
+ drawIds,
52678
+ params,
52679
+ });
52680
+ if (eventResult.error)
52681
+ return eventResult;
52682
+ }
52683
+ if (drawProfiles) {
52684
+ const drawResult = processDrawProfiles({
52685
+ drawProfiles,
52686
+ tournamentRecord,
52687
+ allUniqueParticipantIds,
52688
+ eventIds,
52689
+ drawIds,
52690
+ params,
52691
+ });
52692
+ if (drawResult.error)
52693
+ return drawResult;
52694
+ }
52695
+ const venueIds = venueProfiles?.length ? generateVenues({ tournamentRecord, venueProfiles }) : [];
52696
+ const schedulingResult = applySchedulingProfile({
52697
+ schedulingProfile,
52698
+ autoSchedule,
52699
+ periodLength,
52700
+ tournamentRecord,
52701
+ });
52702
+ if (schedulingResult.error)
52703
+ return schedulingResult;
52704
+ const { scheduledRounds, schedulerResult } = schedulingResult;
52554
52705
  const totalParticipantsCount = tournamentRecord.participants.length;
52555
52706
  return {
52556
52707
  totalParticipantsCount,