tods-competition-factory 0.76.0 → 0.80.0

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.
@@ -249,7 +249,7 @@ const matchUpFormatCode = {
249
249
  };
250
250
 
251
251
  function factoryVersion() {
252
- return '0.76.0';
252
+ return '0.80.0';
253
253
  }
254
254
 
255
255
  const FORMAT_STANDARD = 'SET3-S:6/TB7'; // Best of 3 Tiebreak Sets to 6
@@ -11813,7 +11813,7 @@ function getAllStructureMatchUps({
11813
11813
 
11814
11814
  if (tournamentParticipants && matchUpWithContext.sides) {
11815
11815
  matchUpWithContext.sides.filter(Boolean).forEach(side => {
11816
- var _side$participant;
11816
+ var _side$participant, _side$participant$ind;
11817
11817
 
11818
11818
  if (side.participantId) {
11819
11819
  const participant = findParticipant({
@@ -11834,7 +11834,7 @@ function getAllStructureMatchUps({
11834
11834
  }
11835
11835
  }
11836
11836
 
11837
- if (side !== null && side !== void 0 && (_side$participant = side.participant) !== null && _side$participant !== void 0 && _side$participant.individualParticipantIds) {
11837
+ if (side !== null && side !== void 0 && (_side$participant = side.participant) !== null && _side$participant !== void 0 && (_side$participant$ind = _side$participant.individualParticipantIds) !== null && _side$participant$ind !== void 0 && _side$participant$ind.length) {
11838
11838
  const individualParticipants = side.participant.individualParticipantIds.map(participantId => {
11839
11839
  return findParticipant({
11840
11840
  policyDefinitions: appliedPolicies,
@@ -15670,10 +15670,10 @@ function getMatchUpDependencies({
15670
15670
  const {
15671
15671
  drawDefinition
15672
15672
  } = findEvent({
15673
- tournamentRecord: tournamentRecords[hasTournamentId.tournamentId],
15673
+ tournamentRecord: tournamentRecords[hasTournamentId === null || hasTournamentId === void 0 ? void 0 : hasTournamentId.tournamentId],
15674
15674
  drawId
15675
15675
  });
15676
- addGoesTo({
15676
+ if (drawDefinition) addGoesTo({
15677
15677
  drawDefinition
15678
15678
  });
15679
15679
  }
@@ -22077,7 +22077,8 @@ function addParticipant$1({
22077
22077
  if (!participant.participantId) participant.participantId = UUID();
22078
22078
  if (!tournamentRecord.participants) tournamentRecord.participants = [];
22079
22079
  const {
22080
- participantId
22080
+ participantId,
22081
+ individualParticipantIds
22081
22082
  } = participant;
22082
22083
  const idExists = tournamentRecord.participants.reduce((p, c) => c.participantId === participantId || p, false);
22083
22084
  if (idExists) return {
@@ -22096,6 +22097,14 @@ function addParticipant$1({
22096
22097
  };
22097
22098
  const tournamentParticipants = tournamentRecord.participants || [];
22098
22099
  const tournamentIndividualParticipantIds = tournamentParticipants.filter(tournamentParticipant => tournamentParticipant.participantType === INDIVIDUAL$1).map(individualParticipant => individualParticipant.participantId);
22100
+ if (participantType !== INDIVIDUAL$1 && participant.person) return {
22101
+ error: INVALID_VALUES,
22102
+ person: participant.person
22103
+ };
22104
+ if (individualParticipantIds && !Array.isArray(individualParticipantIds)) return {
22105
+ error: INVALID_VALUES,
22106
+ individualParticipantIds
22107
+ };
22099
22108
 
22100
22109
  if (participantType === PAIR$1) {
22101
22110
  if (participant.person) return {
@@ -22124,8 +22133,8 @@ function addParticipant$1({
22124
22133
  }
22125
22134
 
22126
22135
  const existingPairParticipants = tournamentParticipants.filter(participant => participant.participantType === PAIR$1).map(participant => ({
22127
- participant,
22128
- individualParticipantIds: participant.individualParticipantIds
22136
+ individualParticipantIds: participant.individualParticipantIds,
22137
+ participant
22129
22138
  })); // determine whether a PAIR participant already exists
22130
22139
 
22131
22140
  const existingPairParticipant = participant.participantType === PAIR$1 && existingPairParticipants.find(existingPairParticipant => intersection(existingPairParticipant.individualParticipantIds, participant.individualParticipantIds).length === 2);
@@ -22159,17 +22168,11 @@ function addParticipant$1({
22159
22168
  participant.participantName = participantName;
22160
22169
  participant.name = participantName; // backwards compatabilty
22161
22170
  }
22162
- } else {
22163
- if (participant.person) return {
22164
- error: INVALID_VALUES,
22165
- person: participant.person
22166
- };
22167
- const {
22168
- individualParticipantIds
22169
- } = participant;
22171
+ } else if ([TEAM$1, GROUP].includes(participantType)) {
22172
+ if (!individualParticipantIds) participant.individualParticipantIds = [];
22170
22173
 
22171
- if (individualParticipantIds !== null && individualParticipantIds !== void 0 && individualParticipantIds.length) {
22172
- for (const individualParticipantId of individualParticipantIds) {
22174
+ if (participant.individualParticipantIds.length) {
22175
+ for (const individualParticipantId of participant.individualParticipantIds) {
22173
22176
  if (typeof individualParticipantId !== 'string') {
22174
22177
  return {
22175
22178
  error: INVALID_VALUES,
@@ -22185,6 +22188,10 @@ function addParticipant$1({
22185
22188
  }
22186
22189
  }
22187
22190
  }
22191
+ } else {
22192
+ return {
22193
+ error: INVALID_PARTICIPANT_TYPE
22194
+ };
22188
22195
  }
22189
22196
 
22190
22197
  tournamentRecord.participants.push(participant);
@@ -23375,7 +23382,7 @@ function getTournamentParticipants({
23375
23382
  if (!tournamentRecord.participants) return {
23376
23383
  error: MISSING_PARTICIPANTS
23377
23384
  };
23378
- let tournamentParticipants = tournamentRecord.participants.map(participant => makeDeepCopy(participant, convertExtensions));
23385
+ let tournamentParticipants = tournamentRecord.participants.map(participant => makeDeepCopy(participant, convertExtensions, true));
23379
23386
  if (typeof participantFilters !== 'object') return {
23380
23387
  error: INVALID_OBJECT,
23381
23388
  participantFilters
@@ -23386,7 +23393,9 @@ function getTournamentParticipants({
23386
23393
 
23387
23394
  (_tournamentParticipan = tournamentParticipants) === null || _tournamentParticipan === void 0 ? void 0 : _tournamentParticipan.forEach(participant => {
23388
23395
  if ([PAIR$1, TEAM$1, GROUP].includes(participant.participantType)) {
23389
- participant.individualParticipants = participant.individualParticipantIds.map(participantId => {
23396
+ var _participant$individu;
23397
+
23398
+ participant.individualParticipants = (_participant$individu = participant.individualParticipantIds) === null || _participant$individu === void 0 ? void 0 : _participant$individu.map(participantId => {
23390
23399
  const individualParticipant = tournamentRecord.participants.find(p => p.participantId === participantId);
23391
23400
  return makeDeepCopy(individualParticipant, convertExtensions, true);
23392
23401
  });
@@ -25885,7 +25894,7 @@ function modifyPersonRequests({
25885
25894
 
25886
25895
  /**
25887
25896
  *
25888
- * @param {string[]} scheduleAttributes - attributes by which it is determined that a matchUp as a schedule
25897
+ * @param {string[]} scheduleAttributes - attributes by which it is determined that a matchUp has a schedule
25889
25898
  * @param {boolean} ignoreMatchUpStatuses - array of matchUpStatuses to ignore; defaults to completed
25890
25899
  * @param {object} tournamentRecord - provided automatically by tournamentEngine
25891
25900
  * @param {string[]} scheduleDates - optional - array of schedule dates to be cleared; default is to clear all dates
@@ -25912,27 +25921,32 @@ function clearScheduledMatchUps$1({
25912
25921
 
25913
25922
  if (venueIds.length) scheduleAttributes.push('venueId');
25914
25923
  const {
25915
- matchUps
25924
+ matchUps: inContextMatchUps
25916
25925
  } = allTournamentMatchUps({
25917
25926
  matchUpFilters: {
25918
25927
  scheduledDates
25919
25928
  },
25920
25929
  tournamentRecord
25921
25930
  });
25922
- const relevantMatchUpIds = matchUps.filter(matchUp => !ignoreMatchUpStatuses.includes(matchUp.matchUpStatus) && hasSchedule({
25931
+ const relevantMatchUpIds = inContextMatchUps.filter(matchUp => !ignoreMatchUpStatuses.includes(matchUp.matchUpStatus) && hasSchedule({
25923
25932
  schedule: matchUp.schedule,
25924
25933
  scheduleAttributes
25925
25934
  }) && (!(venueIds !== null && venueIds !== void 0 && venueIds.length) || venueIds.includes(matchUp.schedule.venueId))).map(getMatchUpId);
25926
- const clearedScheduleValues = {
25927
- venueId: '',
25928
- scheduledDate: '',
25929
- scheduledTime: ''
25930
- };
25931
- return bulkScheduleMatchUps$1({
25932
- schedule: clearedScheduleValues,
25933
- matchUpIds: relevantMatchUpIds,
25934
- tournamentRecord
25935
+ const {
25936
+ matchUps
25937
+ } = allTournamentMatchUps({
25938
+ tournamentRecord,
25939
+ inContext: false
25935
25940
  });
25941
+
25942
+ for (const matchUp of matchUps) {
25943
+ if (relevantMatchUpIds.includes(matchUp.matchUpId)) {
25944
+ matchUp.timeItems = (matchUp.timeItems || []).filter(timeItem => ![ASSIGN_COURT, ASSIGN_VENUE, SCHEDULED_DATE, SCHEDULED_TIME].includes(timeItem === null || timeItem === void 0 ? void 0 : timeItem.itemType));
25945
+ }
25946
+ }
25947
+
25948
+ return { ...SUCCESS
25949
+ };
25936
25950
  }
25937
25951
 
25938
25952
  /**
@@ -29243,27 +29257,27 @@ function setTournamentRecord(record, deepCopyOption = true) {
29243
29257
  if (typeof record !== 'object' || Array.isArray(record)) return {
29244
29258
  error: INVALID_OBJECT
29245
29259
  };
29246
- if (!record.tournamentId) return {
29260
+ if (!(record !== null && record !== void 0 && record.tournamentId)) return {
29247
29261
  error: INVALID_VALUES
29248
29262
  };
29249
29263
  return setTournamentRecord$1(deepCopyOption ? makeDeepCopy(record) : record);
29250
29264
  }
29251
29265
  function setState$3(records, deepCopyOption = true) {
29266
+ var _records;
29267
+
29252
29268
  if (typeof records !== 'object') return {
29253
29269
  error: INVALID_OBJECT
29254
29270
  };
29255
29271
 
29256
29272
  if (Array.isArray(records)) {
29257
- const validRecordsArray = records.filter(({
29258
- tournamentId
29259
- }) => tournamentId).length === records.length;
29273
+ const validRecordsArray = records.filter(record => record === null || record === void 0 ? void 0 : record.tournamentId).length === records.length;
29260
29274
  if (!validRecordsArray) return {
29261
29275
  error: INVALID_RECORDS
29262
29276
  };
29263
29277
  records = Object.assign({}, ...records.map(record => ({
29264
29278
  [record.tournamentId]: record
29265
29279
  })));
29266
- } else if (records.tournamentId) {
29280
+ } else if ((_records = records) !== null && _records !== void 0 && _records.tournamentId) {
29267
29281
  records = {
29268
29282
  [records.tournamentId]: records
29269
29283
  };
@@ -37497,7 +37511,7 @@ function getSideRatings({
37497
37511
  var _tournamentParticipan;
37498
37512
 
37499
37513
  const individualParticipantIds = tournamentParticipants === null || tournamentParticipants === void 0 ? void 0 : (_tournamentParticipan = tournamentParticipants.find(participant => participant.participantId === participantId)) === null || _tournamentParticipan === void 0 ? void 0 : _tournamentParticipan.individualParticipantIds;
37500
- return !individualParticipantIds ? DEFAULT_RATING * 2 : individualParticipantIds.map(participantId => adHocRatings[participantId || DEFAULT_RATING]);
37514
+ return !individualParticipantIds ? DEFAULT_RATING * 2 : individualParticipantIds === null || individualParticipantIds === void 0 ? void 0 : individualParticipantIds.map(participantId => adHocRatings[participantId || DEFAULT_RATING]);
37501
37515
  } else {
37502
37516
  return adHocRatings[participantId] || DEFAULT_RATING;
37503
37517
  }
@@ -38382,15 +38396,17 @@ function getValidAlternatesAction({
38382
38396
 
38383
38397
  if (activeDrawPositions.includes(drawPosition)) return {};
38384
38398
  const otherFlightEntries = policyDefinitions === null || policyDefinitions === void 0 ? void 0 : (_policyDefinitions$PO = policyDefinitions[POLICY_TYPE_POSITION_ACTIONS]) === null || _policyDefinitions$PO === void 0 ? void 0 : _policyDefinitions$PO.otherFlightEntries;
38385
- const drawEnteredParticpantIds = (drawDefinition.entries || []).map(({
38399
+ const drawEnteredParticpantIds = (drawDefinition.entries || []).sort((a, b) => (a.entryPosition || 9999) - (b.entryPosition || 9999)).map(({
38386
38400
  participantId
38387
38401
  }) => participantId).filter(Boolean);
38388
38402
  const assignedParticipantIds = positionAssignments.map(assignment => assignment.participantId).filter(Boolean);
38403
+ const availableDrawEnteredParticipantIds = drawEnteredParticpantIds.filter(participantId => !assignedParticipantIds.includes(participantId));
38389
38404
  const eventEntries = event.entries || [];
38390
- const availableAlternatesParticipantIds = eventEntries.filter(entry => entry.entryStatus === ALTERNATE && eligibleEntryStage({
38405
+ const availableEventAlternatesParticipantIds = eventEntries.filter(entry => entry.entryStatus === ALTERNATE && eligibleEntryStage({
38391
38406
  structure,
38392
38407
  entry
38393
38408
  }) && !assignedParticipantIds.includes(entry.participantId)).sort((a, b) => (a.entryPosition || 9999) - (b.entryPosition || 9999)).map(entry => entry.participantId);
38409
+ const availableAlternatesParticipantIds = unique(availableDrawEnteredParticipantIds.concat(availableEventAlternatesParticipantIds));
38394
38410
 
38395
38411
  if (otherFlightEntries) {
38396
38412
  var _flightProfile$flight;
@@ -38400,7 +38416,7 @@ function getValidAlternatesAction({
38400
38416
  } = getFlightProfile({
38401
38417
  event
38402
38418
  });
38403
- const otherFlightEnteredParticipantIds = flightProfile === null || flightProfile === void 0 ? void 0 : (_flightProfile$flight = flightProfile.flights) === null || _flightProfile$flight === void 0 ? void 0 : _flightProfile$flight.filter(flight => flight.drawId !== drawId).map(flight => flight.drawEntries.filter(entry => entry.participantId && !drawEnteredParticpantIds.includes(entry.participantId)).map(({
38419
+ const otherFlightEnteredParticipantIds = flightProfile === null || flightProfile === void 0 ? void 0 : (_flightProfile$flight = flightProfile.flights) === null || _flightProfile$flight === void 0 ? void 0 : _flightProfile$flight.filter(flight => flight.drawId !== drawId).map(flight => flight.drawEntries.filter(entry => entry.participantId && ![WITHDRAWN, UNGROUPED, UNPAIRED].includes(entry.entryStatus) && !drawEnteredParticpantIds.includes(entry.participantId)).map(({
38404
38420
  participantId
38405
38421
  }) => participantId)).flat().filter(Boolean);
38406
38422
 
@@ -40276,8 +40292,8 @@ function addParticipantScaleItem({
40276
40292
  const {
40277
40293
  scaleItem: existingScaleItem
40278
40294
  } = participantScaleItem({
40279
- participant,
40280
- scaleAttributes: scaleItem
40295
+ scaleAttributes: scaleItem,
40296
+ participant
40281
40297
  });
40282
40298
 
40283
40299
  const isUndefined = value => [undefined, null, ''].includes(value);
@@ -40292,10 +40308,10 @@ function addParticipantScaleItem({
40292
40308
  } = scaleItem;
40293
40309
  const itemType = [SCALE, scaleType, eventType, scaleName].join('.');
40294
40310
  const timeItem = {
40295
- itemType,
40296
40311
  itemValue: scaleItem.scaleValue,
40297
40312
  itemDate: scaleItem.scaleDate,
40298
- createdAt
40313
+ createdAt,
40314
+ itemType
40299
40315
  }; // if there is a unique identifier for the scale
40300
40316
 
40301
40317
  if (scaleItem.scaleId) {
@@ -44943,6 +44959,166 @@ function getScaledEntries({
44943
44959
  }
44944
44960
  }
44945
44961
 
44962
+ function prepareStage({
44963
+ inContextDrawMatchUps,
44964
+ matchUpsMap,
44965
+ participants,
44966
+ tournamentRecord,
44967
+ drawDefinition,
44968
+ automated,
44969
+ drawType,
44970
+ drawSize,
44971
+ entries,
44972
+ event,
44973
+ enforcePolicyLimits = true,
44974
+ seedAssignmentProfile,
44975
+ // mainly used by mocksEngine for scenario testing
44976
+ seedByRanking = true,
44977
+ seededParticipants,
44978
+ assignSeedsCount,
44979
+ // used for testing bye placement next to seeds
44980
+ seedingScaleName,
44981
+ seedsCount,
44982
+ stageSequence = 1,
44983
+ stage
44984
+ }) {
44985
+ const eventType = event === null || event === void 0 ? void 0 : event.eventType;
44986
+ const stageEntries = entries.filter(entry => (!entry.entryStage || entry.entryStage === stage) && STRUCTURE_SELECTED_STATUSES.includes(entry.entryStatus));
44987
+ if (seededParticipants) seedsCount = seededParticipants.length;
44988
+ if (seedsCount > drawSize) seedsCount = drawSize;
44989
+ if (seedsCount > stageEntries.length) seedsCount = stageEntries.length;
44990
+ const {
44991
+ structures
44992
+ } = getDrawStructures({
44993
+ drawDefinition,
44994
+ stageSequence,
44995
+ stage
44996
+ });
44997
+ const [structure] = structures;
44998
+ const {
44999
+ structureId
45000
+ } = structure || {};
45001
+ const {
45002
+ seedLimit
45003
+ } = initializeStructureSeedAssignments({
45004
+ participantCount: stageEntries.length,
45005
+ enforcePolicyLimits,
45006
+ tournamentRecord,
45007
+ drawDefinition,
45008
+ structureId,
45009
+ seedsCount,
45010
+ event
45011
+ });
45012
+ if (seedLimit && seedLimit < seedsCount) seedsCount = seedLimit;
45013
+ const enteredParticipantIds = entries.map(getParticipantId);
45014
+
45015
+ if (seededParticipants) {
45016
+ seededParticipants.filter(({
45017
+ participantId
45018
+ }) => enteredParticipantIds.includes(participantId)).filter(seededParticipant => !seededParticipant.seedNumber || seededParticipant.seedNumber <= seededParticipants.length).sort((a, b) => {
45019
+ if (a.seedNumber < b.seedNumber) return -1;
45020
+ if (a.seedNumber < b.seedNumber) return 1;
45021
+ return 0;
45022
+ }).forEach(seededParticipant => {
45023
+ const {
45024
+ participantId,
45025
+ seedNumber,
45026
+ seedValue
45027
+ } = seededParticipant;
45028
+ assignSeed({
45029
+ drawDefinition,
45030
+ participantId,
45031
+ structureId,
45032
+ seedNumber,
45033
+ seedValue
45034
+ });
45035
+ });
45036
+ } else if (event !== null && event !== void 0 && event.category || seedingScaleName) {
45037
+ var _scaledEntries, _scaledEntries2;
45038
+
45039
+ // if no seededParticipants have been defined, seed by seeding scale or ranking scale, if present
45040
+ const {
45041
+ categoryName,
45042
+ ageCategoryCode
45043
+ } = (event === null || event === void 0 ? void 0 : event.category) || {};
45044
+ const seedingScaleAttributes = {
45045
+ scaleType: SEEDING,
45046
+ scaleName: seedingScaleName || categoryName || ageCategoryCode,
45047
+ eventType
45048
+ };
45049
+ let {
45050
+ scaledEntries
45051
+ } = getScaledEntries({
45052
+ scaleAttributes: seedingScaleAttributes,
45053
+ tournamentRecord,
45054
+ entries,
45055
+ stage
45056
+ });
45057
+
45058
+ if (!((_scaledEntries = scaledEntries) !== null && _scaledEntries !== void 0 && _scaledEntries.length) && seedByRanking) {
45059
+ const rankingScaleAttributes = {
45060
+ scaleType: RANKING,
45061
+ scaleName: categoryName || ageCategoryCode,
45062
+ eventType
45063
+ };
45064
+ ({
45065
+ scaledEntries
45066
+ } = getScaledEntries({
45067
+ scaleAttributes: rankingScaleAttributes,
45068
+ tournamentRecord,
45069
+ entries,
45070
+ stage
45071
+ }));
45072
+ }
45073
+
45074
+ const scaledEntriesCount = ((_scaledEntries2 = scaledEntries) === null || _scaledEntries2 === void 0 ? void 0 : _scaledEntries2.length) || 0;
45075
+ if (scaledEntriesCount < seedsCount) seedsCount = scaledEntriesCount;
45076
+ scaledEntries && scaledEntries.filter(({
45077
+ participantId
45078
+ }) => enteredParticipantIds.includes(participantId)).slice(0, assignSeedsCount || seedsCount).forEach((scaledEntry, index) => {
45079
+ const seedNumber = index + 1;
45080
+ const seedValue = (seedAssignmentProfile === null || seedAssignmentProfile === void 0 ? void 0 : seedAssignmentProfile[seedNumber]) || seedNumber; // ?? attach basis of seeding information to seedAssignment ??
45081
+
45082
+ const {
45083
+ participantId
45084
+ } = scaledEntry;
45085
+ assignSeed({
45086
+ drawDefinition,
45087
+ participantId,
45088
+ structureId,
45089
+ seedNumber,
45090
+ seedValue
45091
+ });
45092
+ });
45093
+ }
45094
+
45095
+ let conflicts = [];
45096
+
45097
+ if (automated !== false) {
45098
+ const seedsOnly = typeof automated === 'object' && automated.seedsOnly; // if { seedsOnly: true } then only seeds and an Byes releated to seeded positions are placed
45099
+
45100
+ ({
45101
+ conflicts
45102
+ } = automatedPositioning$1({
45103
+ inContextDrawMatchUps,
45104
+ drawDefinition,
45105
+ participants,
45106
+ structureId,
45107
+ matchUpsMap,
45108
+ seedsOnly,
45109
+ drawType,
45110
+ event
45111
+ }));
45112
+ }
45113
+
45114
+ return {
45115
+ conflicts,
45116
+ structureId,
45117
+ seedsCount,
45118
+ stageEntries
45119
+ };
45120
+ }
45121
+
44946
45122
  /**
44947
45123
  * automated = true, // can be true/false or "truthy" { seedsOnly: true }
44948
45124
  */
@@ -44950,52 +45126,30 @@ function getScaledEntries({
44950
45126
  function generateDrawDefinition(params) {
44951
45127
  const {
44952
45128
  drawType = SINGLE_ELIMINATION,
44953
- enforcePolicyLimits = true,
44954
- finishingPositionNaming,
44955
45129
  ignoreAllowedDrawTypes,
44956
- seedAssignmentProfile,
44957
- // mainly used by mocksEngine for scenario testing
44958
- playoffMatchUpFormat,
44959
- seedByRanking = true,
44960
- qualifyingProfiles,
44961
- seededParticipants,
44962
45130
  policyDefinitions,
44963
- seedingScaleName,
44964
- assignSeedsCount,
44965
- // used for testing bye placement next to seeds
44966
- automated = true,
44967
- seedingProfile,
45131
+ tournamentRecord,
44968
45132
  tieFormatName,
44969
45133
  drawEntries,
44970
- feedPolicy,
44971
- idPrefix,
44972
45134
  drawId,
44973
- isMock,
44974
- uuids
44975
- } = params;
44976
- const {
44977
- tournamentRecord,
44978
45135
  event
44979
- } = params;
44980
- let {
44981
- drawName,
44982
- matchUpType,
44983
- structureOptions
44984
- } = params;
45136
+ } = params; // get participants both for entry validation and for automated placement
45137
+ // automated placement requires them to be "inContext" for avoidance policies to work
45138
+
44985
45139
  const {
44986
45140
  tournamentParticipants: participants
44987
45141
  } = getTournamentParticipants({
44988
45142
  tournamentRecord,
44989
45143
  inContext: true
44990
- });
44991
- const validEntriesTest = event && participants && checkValidEntries({
45144
+ }); // entries participantTypes must correspond with eventType
45145
+ // this is only possible if the event is provided
45146
+
45147
+ const validEntriesResult = event && participants && checkValidEntries({
44992
45148
  event,
44993
45149
  participants
44994
45150
  });
44995
-
44996
- if (validEntriesTest !== null && validEntriesTest !== void 0 && validEntriesTest.error) {
44997
- return validEntriesTest;
44998
- }
45151
+ if (validEntriesResult !== null && validEntriesResult !== void 0 && validEntriesResult.error) return validEntriesResult; // if tournamentRecord is provided, and unless instructed to ignore valid types,
45152
+ // check for restrictions on allowed drawTypes
44999
45153
 
45000
45154
  const allowedDrawTypes = !ignoreAllowedDrawTypes && tournamentRecord && getAllowedDrawTypes({
45001
45155
  tournamentRecord,
@@ -45007,19 +45161,18 @@ function generateDrawDefinition(params) {
45007
45161
  return {
45008
45162
  error: INVALID_DRAW_TYPE
45009
45163
  };
45010
- }
45164
+ } // coersion of drawSize and seedsCount to integers
45165
+
45166
+
45167
+ let drawSize = typeof params.drawSize !== 'number' ? parseInt(params.drawSize || 32) : params.drawSize || 32;
45168
+ let seedsCount = typeof params.seedsCount !== 'number' ? parseInt(params.seedsCount || 0) : params.seedsCount || 0;
45169
+ const eventType = event === null || event === void 0 ? void 0 : event.eventType;
45170
+ const matchUpType = params.matchUpType || eventType; // drawDefinition cannot have both tieFormat and matchUpFormat
45011
45171
 
45012
45172
  let {
45013
- seedsCount,
45014
- drawSize = 32,
45015
45173
  tieFormat,
45016
45174
  matchUpFormat
45017
- } = params; // coersion
45018
-
45019
- if (typeof drawSize !== 'number') drawSize = parseInt(drawSize);
45020
- if (typeof seedsCount !== 'number') seedsCount = parseInt(seedsCount || 0);
45021
- const eventType = event === null || event === void 0 ? void 0 : event.eventType;
45022
- matchUpType = matchUpType || eventType;
45175
+ } = params;
45023
45176
 
45024
45177
  if (matchUpType === TEAM$3 && eventType === TEAM$3) {
45025
45178
  tieFormat = tieFormat || event !== null && event !== void 0 && event.tieFormat || tieFormatName ? typeof tieFormat === 'object' ? tieFormat : tieFormatName ? tieFormatDefaults({
@@ -45029,19 +45182,15 @@ function generateDrawDefinition(params) {
45029
45182
  } else if (!matchUpFormat) {
45030
45183
  tieFormat = undefined;
45031
45184
  matchUpFormat = 'SET3-S:6/TB7';
45032
- }
45185
+ } // ---------------------------------------------------------------------------
45186
+ // Begin construction of drawDefinition
45033
45187
 
45034
- const entries = drawEntries || (event === null || event === void 0 ? void 0 : event.entries) || [];
45035
- const stageEntries = entries.filter(entry => (!entry.entryStage || entry.entryStage === MAIN) && STRUCTURE_SELECTED_STATUSES.includes(entry.entryStatus)); // the reason for this needs to be clarified
45036
-
45037
- if ([ROUND_ROBIN].includes(drawType)) {
45038
- drawSize = stageEntries.length || drawSize;
45039
- }
45040
45188
 
45041
45189
  const drawDefinition = newDrawDefinition({
45042
45190
  drawType,
45043
45191
  drawId
45044
- });
45192
+ }); // if there is a defined matchUpFormat/tieFormat only attach to drawDefinition...
45193
+ // ...when there is not an equivalent definition on the parent event
45045
45194
 
45046
45195
  if (matchUpFormat || tieFormat) {
45047
45196
  let equivalentInScope = matchUpFormat && (event === null || event === void 0 ? void 0 : event.matchUpFormat) === matchUpFormat || (event === null || event === void 0 ? void 0 : event.tieFormat) && tieFormat && JSON.stringify(event.tieFormat) === JSON.stringify(tieFormat); // if an equivalent matchUpFormat or tieFormat is attached to the event
@@ -45065,52 +45214,22 @@ function generateDrawDefinition(params) {
45065
45214
  }
45066
45215
  }
45067
45216
 
45068
- tieFormat = tieFormat || (event === null || event === void 0 ? void 0 : event.tieFormat);
45069
- let result = generateDrawType({
45070
- finishingPositionNaming,
45071
- goesTo: params.goesTo,
45072
- playoffMatchUpFormat,
45073
- qualifyingProfiles,
45074
- structureOptions,
45217
+ let drawTypeResult = generateDrawType({ ...params,
45075
45218
  drawDefinition,
45076
- seedingProfile,
45077
45219
  matchUpFormat,
45078
45220
  matchUpType,
45079
- feedPolicy,
45080
45221
  tieFormat,
45081
- drawSize,
45082
- drawType,
45083
- idPrefix,
45084
- isMock,
45085
- uuids
45086
- });
45087
- if (result.error) return result;
45088
- const {
45089
- matchUpsMap,
45090
- inContextDrawMatchUps
45091
- } = result;
45092
- const {
45093
- structures
45094
- } = getDrawStructures({
45095
- drawDefinition,
45096
- stageSequence: 1,
45097
- stage: MAIN
45222
+ drawSize
45098
45223
  });
45099
- const [structure] = structures;
45100
- const {
45101
- structureId
45102
- } = structure || {};
45224
+ if (drawTypeResult.error) return drawTypeResult; // first attach any policyDefinitions which have been provided
45103
45225
 
45104
45226
  if (typeof policyDefinitions === 'object') {
45105
- for (const policyType of Object.keys(policyDefinitions)) {
45106
- attachPolicies({
45107
- policyDefinitions: {
45108
- [policyType]: policyDefinitions[policyType]
45109
- },
45110
- drawDefinition
45111
- });
45112
- }
45113
- }
45227
+ attachPolicies({
45228
+ policyDefinitions,
45229
+ drawDefinition
45230
+ });
45231
+ } // then check for a seedingPolicy at all levels
45232
+
45114
45233
 
45115
45234
  const {
45116
45235
  policyDefinitions: seedingPolicy
@@ -45119,9 +45238,10 @@ function generateDrawDefinition(params) {
45119
45238
  tournamentRecord,
45120
45239
  drawDefinition,
45121
45240
  event
45122
- }) || {};
45241
+ }) || {}; // if no seeding policy provided and none present at any other level, attach default
45242
+ // this needs to be attached for prepareStage => initializeSeedAssignments
45123
45243
 
45124
- if (!(policyDefinitions !== null && policyDefinitions !== void 0 && policyDefinitions[POLICY_TYPE_SEEDING]) && !(seedingPolicy !== null && seedingPolicy !== void 0 && seedingPolicy[POLICY_TYPE_SEEDING])) {
45244
+ if (!(seedingPolicy !== null && seedingPolicy !== void 0 && seedingPolicy[POLICY_TYPE_SEEDING])) {
45125
45245
  // if there is no seeding policy then use default seeing policy
45126
45246
  attachPolicies({
45127
45247
  drawDefinition,
@@ -45146,7 +45266,11 @@ function generateDrawDefinition(params) {
45146
45266
  drawDefinition,
45147
45267
  policyDefinitions: eventAvoidancePolicy
45148
45268
  });
45149
- }
45269
+ } // add all entries to the draw
45270
+ // TODO: if there are no qualifyingProfiles, skip adding qualifying entries
45271
+
45272
+
45273
+ const entries = drawEntries || (event === null || event === void 0 ? void 0 : event.entries) || [];
45150
45274
 
45151
45275
  for (const entry of entries) {
45152
45276
  // convenience: assume MAIN as entryStage if none provided
@@ -45161,153 +45285,56 @@ function generateDrawDefinition(params) {
45161
45285
  // if entries are taken from event.entries assume stageSpace is not available
45162
45286
  return result;
45163
45287
  }
45164
- }
45288
+ } // temporary until seeding is supported in LUCKY_DRAW
45165
45289
 
45166
- const enteredParticipantIds = entries.map(getParticipantId);
45167
- if (seededParticipants) seedsCount = seededParticipants.length;
45168
- if (seedsCount > drawSize) seedsCount = drawSize;
45169
- if (seedsCount > stageEntries.length) seedsCount = stageEntries.length;
45170
- const {
45171
- seedLimit
45172
- } = initializeStructureSeedAssignments({
45173
- participantCount: stageEntries.length,
45174
- enforcePolicyLimits,
45175
- tournamentRecord,
45290
+
45291
+ if (drawType === LUCKY_DRAW) seedsCount = 0;
45292
+ const structureResult = prepareStage({ ...drawTypeResult,
45293
+ ...params,
45176
45294
  drawDefinition,
45177
- structureId,
45295
+ participants,
45296
+ stage: MAIN,
45178
45297
  seedsCount,
45179
- event
45298
+ drawSize,
45299
+ entries
45180
45300
  });
45181
- if (seedLimit && seedLimit < seedsCount) seedsCount = seedLimit; // temporary until seeding is supported in LUCKY_DRAW
45182
45301
 
45183
- if (drawType === LUCKY_DRAW) seedsCount = 0;
45302
+ if (params.qualifyingProfiles) {
45303
+ console.log(params.qualifyingProfiles);
45304
+ let stageSequence = 1;
45184
45305
 
45185
- if (seededParticipants) {
45186
- seededParticipants.filter(({
45187
- participantId
45188
- }) => enteredParticipantIds.includes(participantId)).filter(seededParticipant => !seededParticipant.seedNumber || seededParticipant.seedNumber <= seededParticipants.length).sort((a, b) => {
45189
- if (a.seedNumber < b.seedNumber) return -1;
45190
- if (a.seedNumber < b.seedNumber) return 1;
45191
- return 0;
45192
- }).forEach(seededParticipant => {
45306
+ for (const qualifyingProfile of params.qualifyingProfiles) {
45193
45307
  const {
45194
- participantId,
45195
- seedNumber,
45196
- seedValue
45197
- } = seededParticipant;
45198
- assignSeed({
45308
+ drawSize,
45309
+ qualifyingRoundNumber,
45310
+ qualifyingPositions
45311
+ } = qualifyingProfile;
45312
+ const qualifyingResult = prepareStage({ ...drawTypeResult,
45313
+ ...params,
45314
+ qualifyingRoundNumber,
45315
+ qualifyingPositions,
45316
+ stage: QUALIFYING,
45199
45317
  drawDefinition,
45200
- participantId,
45201
- structureId,
45202
- seedNumber,
45203
- seedValue
45204
- });
45205
- });
45206
- } else if (event !== null && event !== void 0 && event.category || seedingScaleName) {
45207
- var _scaledEntries, _scaledEntries2;
45208
-
45209
- // if no seededParticipants have been defined, seed by seeding scale or ranking scale, if present
45210
- const {
45211
- categoryName,
45212
- ageCategoryCode
45213
- } = (event === null || event === void 0 ? void 0 : event.category) || {};
45214
- const seedingScaleAttributes = {
45215
- scaleType: SEEDING,
45216
- scaleName: seedingScaleName || categoryName || ageCategoryCode,
45217
- eventType
45218
- };
45219
- let {
45220
- scaledEntries
45221
- } = getScaledEntries({
45222
- scaleAttributes: seedingScaleAttributes,
45223
- tournamentRecord,
45224
- stage: MAIN,
45225
- entries
45226
- });
45227
-
45228
- if (!((_scaledEntries = scaledEntries) !== null && _scaledEntries !== void 0 && _scaledEntries.length) && seedByRanking) {
45229
- const rankingScaleAttributes = {
45230
- scaleType: RANKING,
45231
- scaleName: categoryName || ageCategoryCode,
45232
- eventType
45233
- };
45234
- ({
45235
- scaledEntries
45236
- } = getScaledEntries({
45237
- scaleAttributes: rankingScaleAttributes,
45238
- tournamentRecord,
45239
- stage: MAIN,
45318
+ stageSequence,
45319
+ seedsCount: 0,
45320
+ participants,
45321
+ drawSize,
45240
45322
  entries
45241
- }));
45242
- }
45243
-
45244
- const scaledEntriesCount = ((_scaledEntries2 = scaledEntries) === null || _scaledEntries2 === void 0 ? void 0 : _scaledEntries2.length) || 0;
45245
- if (scaledEntriesCount < seedsCount) seedsCount = scaledEntriesCount;
45246
- scaledEntries && scaledEntries.filter(({
45247
- participantId
45248
- }) => enteredParticipantIds.includes(participantId)).slice(0, assignSeedsCount || seedsCount).forEach((scaledEntry, index) => {
45249
- const seedNumber = index + 1;
45250
- const seedValue = (seedAssignmentProfile === null || seedAssignmentProfile === void 0 ? void 0 : seedAssignmentProfile[seedNumber]) || seedNumber; // ?? attach basis of seeding information to seedAssignment ??
45251
-
45252
- const {
45253
- participantId
45254
- } = scaledEntry;
45255
- assignSeed({
45256
- drawDefinition,
45257
- participantId,
45258
- structureId,
45259
- seedNumber,
45260
- seedValue
45261
45323
  });
45262
- });
45263
- }
45264
-
45265
- let conflicts = [];
45266
-
45267
- if (automated !== false) {
45268
- const seedsOnly = typeof automated === 'object' && automated.seedsOnly; // if { seedsOnly: true } then only seeds and an Byes releated to seeded positions are placed
45269
-
45270
- ({
45271
- conflicts
45272
- } = automatedPositioning$1({
45273
- inContextDrawMatchUps,
45274
- drawDefinition,
45275
- participants,
45276
- structureId,
45277
- matchUpsMap,
45278
- seedsOnly,
45279
- drawType,
45280
- event
45281
- }));
45324
+ stageSequence += 1;
45325
+ console.log({
45326
+ qualifyingResult
45327
+ });
45328
+ }
45282
45329
  }
45283
45330
 
45284
- drawName = drawName || drawType;
45285
- if (drawDefinition) Object.assign(drawDefinition, {
45286
- drawName
45287
- });
45288
- const drawDetails = {
45289
- drawId: drawDefinition.drawId,
45290
- category: event === null || event === void 0 ? void 0 : event.category,
45291
- eventId: event === null || event === void 0 ? void 0 : event.eventId,
45292
- seedingScaleName,
45293
- matchUpType,
45294
- seedsCount,
45295
- automated,
45296
- tieFormat,
45297
- drawSize,
45298
- drawType,
45299
- drawName
45300
- };
45301
- addNotice({
45302
- topic: AUDIT,
45303
- payload: {
45304
- action: 'generateDrawDefinition',
45305
- payload: drawDetails
45306
- }
45307
- });
45308
- return { ...SUCCESS,
45309
- structureId,
45331
+ const conflicts = structureResult.conflicts;
45332
+ const structureId = structureResult.structureId;
45333
+ drawDefinition.drawName = params.drawName || drawType;
45334
+ return {
45310
45335
  drawDefinition,
45336
+ structureId,
45337
+ ...SUCCESS,
45311
45338
  conflicts
45312
45339
  };
45313
45340
  }
@@ -52327,12 +52354,22 @@ function generateEventWithDraw({
52327
52354
  stage
52328
52355
  } = drawProfile;
52329
52356
  const eventType = drawProfile.eventType || drawProfile.matchUpType || SINGLES;
52357
+ const participantType = eventType === DOUBLES ? PAIR$1 : INDIVIDUAL$1;
52330
52358
  const tieFormat = typeof drawProfile.tieFormat === 'object' ? drawProfile.tieFormat : eventType === TEAM$1 ? tieFormatDefaults({
52331
52359
  namedFormat: tieFormatName
52332
52360
  }) : undefined;
52333
52361
  const categoryName = (category === null || category === void 0 ? void 0 : category.categoryName) || (category === null || category === void 0 ? void 0 : category.ageCategoryCode) || (category === null || category === void 0 ? void 0 : category.ratingType);
52334
52362
  let eventName = drawProfile.eventName || categoryName || `Generated ${eventType}`;
52335
52363
  let targetParticipants = (tournamentRecord === null || tournamentRecord === void 0 ? void 0 : tournamentRecord.participants) || [];
52364
+ /*
52365
+ const qualifyingIndividualParticipantsCount =
52366
+ (params.qualifyingProfiles?.reduce(
52367
+ (count, profile) => count + profile.drawSize,
52368
+ 0
52369
+ ) || 0) * (participantType === DOUBLES ? 2 : 1);
52370
+ console.log({ qualifyingIndividualParticipantsCount });
52371
+ */
52372
+
52336
52373
  const participantsCount = !drawProfile.participantsCount || drawProfile.participantsCount > drawSize ? drawSize : drawProfile.participantsCount;
52337
52374
  const eventId = UUID();
52338
52375
  let event = {
@@ -52374,7 +52411,6 @@ function generateEventWithDraw({
52374
52411
  }
52375
52412
 
52376
52413
  const idPrefix = participantsProfile !== null && participantsProfile !== void 0 && participantsProfile.idPrefix ? `D-${drawIndex}-${participantsProfile === null || participantsProfile === void 0 ? void 0 : participantsProfile.idPrefix}` : undefined;
52377
- const participantType = eventType === DOUBLES ? PAIR$1 : INDIVIDUAL$1;
52378
52414
  const {
52379
52415
  participants: unique
52380
52416
  } = generateParticipants({ ...participantsProfile,