tods-competition-factory 0.93.2 → 0.96.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.93.2';
252
+ return '0.96.0';
253
253
  }
254
254
 
255
255
  const FORMAT_STANDARD = 'SET3-S:6/TB7'; // Best of 3 Tiebreak Sets to 6
@@ -1321,13 +1321,6 @@ const validTimeString = /^((0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)([.,]
1321
1321
  const dateValidation = /^([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))([ T](0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)?([.,][0-9]{3})?Z?$/;
1322
1322
  const timeValidation = /^([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))?([ T]?(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)?([.,][0-9]{3})?Z?$/;
1323
1323
 
1324
- /*
1325
- import {
1326
- INVALID_DATE,
1327
- INVALID_TIME_ZONE,
1328
- } from '../constants/errorConditionConstants';
1329
- */
1330
-
1331
1324
  function isDateObject(value) {
1332
1325
  if (typeof value !== 'object' || Array.isArray(value)) {
1333
1326
  return false;
@@ -1398,15 +1391,18 @@ function isDate(dateArg) {
1398
1391
  return t && !isNaN(t.valueOf());
1399
1392
  }
1400
1393
  function dateRange(startDt, endDt) {
1401
- const startDate = new Date(startDt);
1402
- const endDate = new Date(endDt);
1394
+ if (!isValidDateString(startDt) || !isValidDateString(endDt)) return [];
1395
+ const startDateString = extractDate(startDt) + 'T00:00';
1396
+ const endDateString = extractDate(endDt) + 'T00:00';
1397
+ const startDate = new Date(startDateString);
1398
+ const endDate = new Date(endDateString);
1403
1399
  const error = isDate(endDate) && isDate(startDate) && isValidDateRange(startDate, endDate) ? false : true;
1404
1400
  const between = [];
1405
1401
  let iterations = 0;
1406
1402
 
1407
1403
  if (!error) {
1408
- const currentDate = offsetDate(startDate);
1409
- const end = offsetDate(endDate);
1404
+ const currentDate = startDate;
1405
+ const end = endDate;
1410
1406
 
1411
1407
  while (currentDate <= end && iterations < 300) {
1412
1408
  iterations += 1; // must be a *new* Date otherwise it is an array of the same object
@@ -1416,10 +1412,10 @@ function dateRange(startDt, endDt) {
1416
1412
  }
1417
1413
  }
1418
1414
 
1419
- return between;
1415
+ return between.map(date => formatDate(date));
1420
1416
 
1421
1417
  function isValidDateRange(minDate, maxDate) {
1422
- return offsetDate(minDate) <= offsetDate(maxDate);
1418
+ return minDate <= maxDate;
1423
1419
  }
1424
1420
  } // matches valid ISO date string
1425
1421
 
@@ -3890,7 +3886,7 @@ function generateTieMatchUpScore({
3890
3886
  tieFormat,
3891
3887
  matchUp
3892
3888
  }) {
3893
- var _tieFormat, _tieFormat2;
3889
+ var _tieFormat, _tieFormat2, _tieFormat3;
3894
3890
 
3895
3891
  if (!Array.isArray(sideAdjustments) || sideAdjustments.length !== 2 || isNaN(sideAdjustments.reduce((a, b) => a + b))) {
3896
3892
  return {
@@ -3914,7 +3910,18 @@ function generateTieMatchUpScore({
3914
3910
  };
3915
3911
  const sideTieValues = [0, 0];
3916
3912
  const tieMatchUps = (matchUp === null || matchUp === void 0 ? void 0 : matchUp.tieMatchUps) || [];
3917
- const collectionDefinitions = ((_tieFormat = tieFormat) === null || _tieFormat === void 0 ? void 0 : _tieFormat.collectionDefinitions) || [];
3913
+ const collectionDefinitions = ((_tieFormat = tieFormat) === null || _tieFormat === void 0 ? void 0 : _tieFormat.collectionDefinitions) || []; // set up to handle groupValue
3914
+
3915
+ const collectionGroups = ((_tieFormat2 = tieFormat) === null || _tieFormat2 === void 0 ? void 0 : _tieFormat2.collectionGroups) || [];
3916
+ const groupValueGroups = Object.assign({}, ...collectionGroups.filter(group => (group === null || group === void 0 ? void 0 : group.groupValue) && (group === null || group === void 0 ? void 0 : group.groupNumber)).map(group => ({
3917
+ [group.groupNumber]: { ...group,
3918
+ allGroupMatchUpsCompleted: true,
3919
+ matchUpsCount: 0,
3920
+ sideWins: [0, 0],
3921
+ values: [0, 0]
3922
+ }
3923
+ })));
3924
+ const groupValueNumbers = Object.keys(groupValueGroups);
3918
3925
 
3919
3926
  for (const collectionDefinition of collectionDefinitions) {
3920
3927
  const collectionMatchUps = tieMatchUps.filter(matchUp => matchUp.collectionId === collectionDefinition.collectionId); // keep track of the values derived from matchUps
@@ -3926,12 +3933,14 @@ function generateTieMatchUpScore({
3926
3933
  const allCollectionMatchUpsCompleted = collectionMatchUps.every(matchUp => completedMatchUpStatuses.includes(matchUp.matchUpStatus));
3927
3934
  const {
3928
3935
  collectionValueProfile,
3936
+ collectionGroupNumber,
3929
3937
  collectionValue,
3930
3938
  matchUpValue,
3931
3939
  winCriteria,
3932
3940
  scoreValue,
3933
3941
  setValue
3934
3942
  } = collectionDefinition;
3943
+ const belongsToValueGroup = collectionGroupNumber && groupValueNumbers.includes(collectionGroupNumber);
3935
3944
 
3936
3945
  if (matchUpValue) {
3937
3946
  collectionMatchUps.forEach(matchUp => {
@@ -3972,11 +3981,12 @@ function generateTieMatchUpScore({
3972
3981
  });
3973
3982
  }
3974
3983
 
3984
+ const sideWins = [0, 0];
3985
+ collectionMatchUps.forEach(matchUp => {
3986
+ if (matchUp.winningSide) sideWins[matchUp.winningSide - 1] += 1;
3987
+ });
3988
+
3975
3989
  if (collectionValue) {
3976
- const sideWins = [0, 0];
3977
- collectionMatchUps.forEach(matchUp => {
3978
- if (matchUp.winningSide) sideWins[matchUp.winningSide - 1] += 1;
3979
- });
3980
3990
  let collectionWinningSide;
3981
3991
 
3982
3992
  if (winCriteria !== null && winCriteria !== void 0 && winCriteria.aggregateValue) {
@@ -3994,12 +4004,63 @@ function generateTieMatchUpScore({
3994
4004
  }, undefined);
3995
4005
  }
3996
4006
 
3997
- if (collectionWinningSide) sideCollectionValues[collectionWinningSide - 1] += collectionValue;
4007
+ if (collectionWinningSide) {
4008
+ if (belongsToValueGroup) {
4009
+ groupValueGroups[collectionGroupNumber].values[collectionWinningSide - 1] += collectionValue;
4010
+ } else {
4011
+ sideCollectionValues[collectionWinningSide - 1] += collectionValue;
4012
+ }
4013
+ }
3998
4014
  } else {
3999
- sideCollectionValues = sideMatchUpValues;
4015
+ if (belongsToValueGroup) {
4016
+ groupValueGroups[collectionGroupNumber].values[0] += sideMatchUpValues[0] || 0;
4017
+ groupValueGroups[collectionGroupNumber].values[1] += sideMatchUpValues[1] || 0;
4018
+ } else {
4019
+ sideCollectionValues = sideMatchUpValues;
4020
+ }
4000
4021
  }
4001
4022
 
4002
- sideCollectionValues.forEach((sideCollectionValue, i) => sideTieValues[i] += sideCollectionValue);
4023
+ if (!belongsToValueGroup) {
4024
+ sideCollectionValues.forEach((sideCollectionValue, i) => sideTieValues[i] += sideCollectionValue);
4025
+ } else {
4026
+ groupValueGroups[collectionGroupNumber].sideWins[0] += sideWins[0] || 0;
4027
+ groupValueGroups[collectionGroupNumber].sideWins[1] += sideWins[1] || 0;
4028
+ groupValueGroups[collectionGroupNumber].allGroupMatchUpsCompleted = groupValueGroups[collectionGroupNumber].allGroupMatchUpsCompleted && allCollectionMatchUpsCompleted;
4029
+ groupValueGroups[collectionGroupNumber].matchUpsCount += collectionMatchUps.length;
4030
+ }
4031
+ } // process each relevant group for groupValue
4032
+
4033
+
4034
+ for (const groupNumber of groupValueNumbers) {
4035
+ const group = groupValueGroups[groupNumber];
4036
+ let {
4037
+ allGroupMatchUpsCompleted,
4038
+ groupValue,
4039
+ matchUpCount,
4040
+ sideWins,
4041
+ winCriteria,
4042
+ values
4043
+ } = group;
4044
+ let groupWinningSide;
4045
+
4046
+ if (winCriteria !== null && winCriteria !== void 0 && winCriteria.aggregateValue) {
4047
+ if (allGroupMatchUpsCompleted && values[0] !== values[1]) {
4048
+ groupWinningSide = values[0] > values[1] ? 1 : 2;
4049
+ }
4050
+ } else if (winCriteria !== null && winCriteria !== void 0 && winCriteria.valueGoal) {
4051
+ groupWinningSide = values.reduce((winningSide, side, i) => {
4052
+ return side >= winCriteria.valueGoal ? i + 1 : winningSide;
4053
+ }, undefined);
4054
+ } else {
4055
+ const winGoal = Math.floor(matchUpCount / 2) + 1;
4056
+ groupWinningSide = sideWins.reduce((winningSide, side, i) => {
4057
+ return side >= winGoal ? i + 1 : winningSide;
4058
+ }, undefined);
4059
+ }
4060
+
4061
+ if (groupWinningSide) {
4062
+ sideTieValues[groupWinningSide - 1] += groupValue;
4063
+ }
4003
4064
  }
4004
4065
 
4005
4066
  const sideScores = sideTieValues.map((sideTieValue, i) => sideTieValue + sideAdjustments[i]);
@@ -4012,7 +4073,7 @@ function generateTieMatchUpScore({
4012
4073
 
4013
4074
  let winningSide;
4014
4075
 
4015
- if ((_tieFormat2 = tieFormat) !== null && _tieFormat2 !== void 0 && _tieFormat2.winCriteria) {
4076
+ if ((_tieFormat3 = tieFormat) !== null && _tieFormat3 !== void 0 && _tieFormat3.winCriteria) {
4016
4077
  const {
4017
4078
  valueGoal,
4018
4079
  aggregateValue
@@ -26710,14 +26771,17 @@ function clearScheduledMatchUps$1({
26710
26771
  tournamentRecord,
26711
26772
  inContext: false
26712
26773
  });
26774
+ let clearedScheduleCount = 0;
26713
26775
 
26714
26776
  for (const matchUp of matchUps) {
26715
26777
  if (relevantMatchUpIds.includes(matchUp.matchUpId)) {
26716
26778
  matchUp.timeItems = (matchUp.timeItems || []).filter(timeItem => ![ASSIGN_COURT, ASSIGN_VENUE, SCHEDULED_DATE, SCHEDULED_TIME].includes(timeItem === null || timeItem === void 0 ? void 0 : timeItem.itemType));
26779
+ clearedScheduleCount += 1;
26717
26780
  }
26718
26781
  }
26719
26782
 
26720
- return { ...SUCCESS
26783
+ return { ...SUCCESS,
26784
+ clearedScheduleCount
26721
26785
  };
26722
26786
  }
26723
26787
 
@@ -43499,63 +43563,180 @@ function setTournamentNotes({
43499
43563
  element: tournamentRecord,
43500
43564
  notes
43501
43565
  });
43502
- } // TODO: check for matchUps that must be unscheduled with change in date
43503
- // TODO: check all courts in all venues for dateAvailability that is outside of tournament date range
43504
-
43505
- function setTournamentStartDate({
43566
+ }
43567
+ function setTournamentCategories({
43506
43568
  tournamentRecord,
43507
- startDate
43569
+ categories
43508
43570
  }) {
43509
43571
  if (!tournamentRecord) return {
43510
43572
  error: MISSING_TOURNAMENT_RECORD
43511
43573
  };
43512
- if (!dateValidation.test(startDate)) return {
43513
- error: INVALID_DATE
43574
+ categories = (categories || []).filter(category => {
43575
+ return category.categoryName && category.type;
43576
+ });
43577
+ tournamentRecord.tournamentCategories = categories;
43578
+ return SUCCESS;
43579
+ }
43580
+
43581
+ function updateCourtAvailability({
43582
+ tournamentRecord
43583
+ }) {
43584
+ if (!tournamentRecord) return {
43585
+ error: MISSING_TOURNAMENT_RECORD
43514
43586
  };
43587
+ const {
43588
+ startDate,
43589
+ endDate
43590
+ } = tournamentRecord;
43591
+ const tournamentDates = dateRange(startDate, endDate);
43592
+ const courts = [];
43515
43593
 
43516
- if (tournamentRecord.startDate && new Date(startDate) > new Date(tournamentRecord.startDate)) ;
43594
+ for (const venue of tournamentRecord.venues || []) {
43595
+ var _venue$courts;
43517
43596
 
43518
- if (tournamentRecord.endDate && new Date(startDate) > new Date(tournamentRecord.endDate)) {
43519
- tournamentRecord.endDate = startDate;
43597
+ if (venue !== null && venue !== void 0 && (_venue$courts = venue.courts) !== null && _venue$courts !== void 0 && _venue$courts.length) courts.push(...venue.courts);
43520
43598
  }
43521
43599
 
43522
- tournamentRecord.startDate = startDate;
43523
- return SUCCESS;
43524
- } // TODO: check for matchUps that must be unscheduled with change in date
43525
- // TODO: check all courts in all venues for dateAvailability that is outside of tournament date range
43600
+ for (const court of courts) {
43601
+ const {
43602
+ startTime,
43603
+ endTime
43604
+ } = court.dateAvailability.reduce((extents, availability) => {
43605
+ const startMinutes = timeStringMinutes(extents.startTime);
43606
+ const endMinutes = timeStringMinutes(extents.endTime);
43607
+ if (availability.startTime && timeStringMinutes(availability.startTime) < startMinutes) extents.startTime = availability.startTime;
43608
+ if (availability.endTime && timeStringMinutes(availability.endTime) > endMinutes) extents.endTime = availability.endTime;
43609
+ return extents;
43610
+ }, {
43611
+ startTime: '08:00',
43612
+ endTime: '18:00'
43613
+ });
43614
+ const updatedDateAvailability = tournamentDates.map(date => {
43615
+ const existing = court.dateAvailability.find(availability => availability.date === date);
43616
+ return existing || {
43617
+ date,
43618
+ startTime,
43619
+ endTime
43620
+ };
43621
+ });
43622
+ court.dateAvailability = updatedDateAvailability;
43623
+ }
43526
43624
 
43527
- function setTournamentEndDate({
43625
+ return { ...SUCCESS
43626
+ };
43627
+ }
43628
+
43629
+ function setTournamentDates({
43528
43630
  tournamentRecord,
43631
+ startDate,
43529
43632
  endDate
43530
43633
  }) {
43634
+ var _removeInvalidSchedul;
43635
+
43531
43636
  if (!tournamentRecord) return {
43532
43637
  error: MISSING_TOURNAMENT_RECORD
43533
43638
  };
43534
- if (!dateValidation.test(endDate)) return {
43639
+ if (startDate && !dateValidation.test(startDate) || endDate && !dateValidation.test(endDate)) return {
43535
43640
  error: INVALID_DATE
43536
43641
  };
43642
+ if (!startDate && !endDate) return {
43643
+ error: MISSING_DATE
43644
+ };
43645
+ if (endDate && startDate && new Date(endDate) < new Date(startDate)) return {
43646
+ error: INVALID_VALUES
43647
+ };
43648
+ let checkScheduling; // if start has moved closer to end or end has moved closer to start, check for scheduling issues
43649
+
43650
+ if (startDate && tournamentRecord.startDate && new Date(startDate) > new Date(tournamentRecord.startDate) || endDate && tournamentRecord.endDate && new Date(endDate) < new Date(tournamentRecord.endDate)) {
43651
+ checkScheduling = true;
43652
+ }
43537
43653
 
43538
- if (tournamentRecord.endDate && new Date(endDate) < new Date(tournamentRecord.endDate)) ;
43654
+ if (startDate) tournamentRecord.startDate = startDate;
43655
+ if (endDate) tournamentRecord.endDate = endDate; // if there is a startDate specified after current endDate, endDate must be set to startDate
43656
+
43657
+ if (startDate && tournamentRecord.endDate && new Date(startDate) > new Date(tournamentRecord.endDate)) {
43658
+ tournamentRecord.endDate = startDate;
43659
+ } // if there is a endDate specified before current startDate, startDate must be set to endDate
43539
43660
 
43540
- if (tournamentRecord.startDate && new Date(endDate) < new Date(tournamentRecord.startDate)) {
43661
+
43662
+ if (endDate && tournamentRecord.startDate && new Date(endDate) < new Date(tournamentRecord.startDate)) {
43541
43663
  tournamentRecord.startDate = endDate;
43542
43664
  }
43543
43665
 
43544
- tournamentRecord.endDate = endDate;
43545
- return SUCCESS;
43546
- }
43547
- function setTournamentCategories({
43666
+ const unscheduledMatchUpIds = checkScheduling && ((_removeInvalidSchedul = removeInvalidScheduling({
43667
+ tournamentRecord
43668
+ })) === null || _removeInvalidSchedul === void 0 ? void 0 : _removeInvalidSchedul.unscheduledMatchUpIds);
43669
+ updateCourtAvailability({
43670
+ tournamentRecord
43671
+ });
43672
+ return { ...SUCCESS,
43673
+ unscheduledMatchUpIds
43674
+ };
43675
+ } // TODO: check all courts in all venues for dateAvailability that is outside of tournament date range
43676
+
43677
+ function setTournamentStartDate({
43548
43678
  tournamentRecord,
43549
- categories
43679
+ startDate
43550
43680
  }) {
43551
- if (!tournamentRecord) return {
43552
- error: MISSING_TOURNAMENT_RECORD
43553
- };
43554
- categories = (categories || []).filter(category => {
43555
- return category.categoryName && category.type;
43681
+ return setTournamentDates({
43682
+ tournamentRecord,
43683
+ startDate
43556
43684
  });
43557
- tournamentRecord.tournamentCategories = categories;
43558
- return SUCCESS;
43685
+ } // TODO: check all courts in all venues for dateAvailability that is outside of tournament date range
43686
+
43687
+ function setTournamentEndDate({
43688
+ tournamentRecord,
43689
+ endDate
43690
+ }) {
43691
+ return setTournamentDates({
43692
+ tournamentRecord,
43693
+ endDate
43694
+ });
43695
+ } // unschedule scheduled matchUps that fall outside of tournament dates
43696
+
43697
+ function removeInvalidScheduling({
43698
+ tournamentRecord
43699
+ }) {
43700
+ const {
43701
+ matchUps
43702
+ } = allTournamentMatchUps({
43703
+ tournamentRecord
43704
+ });
43705
+ const startDate = tournamentRecord.startDate && new Date(tournamentRecord.startDate);
43706
+ const endDate = tournamentRecord.endDate && new Date(tournamentRecord.endDate);
43707
+ const invalidScheduledDates = [];
43708
+ const invalidSchedulingMatchUpIds = [];
43709
+
43710
+ for (const matchUp of matchUps) {
43711
+ const {
43712
+ schedule,
43713
+ matchUpId
43714
+ } = matchUp;
43715
+ if (!schedule) continue;
43716
+
43717
+ if (schedule.scheduledDate) {
43718
+ const scheduledDate = new Date(schedule.scheduledDate);
43719
+
43720
+ if (startDate && scheduledDate < startDate || endDate && scheduledDate > endDate) {
43721
+ invalidSchedulingMatchUpIds.push(matchUpId);
43722
+ if (!invalidScheduledDates.includes(schedule.scheduledDate)) invalidScheduledDates.push(schedule.scheduledDate);
43723
+ }
43724
+ }
43725
+ }
43726
+
43727
+ if (invalidScheduledDates.length) {
43728
+ const result = clearScheduledMatchUps$1({
43729
+ scheduledDates: invalidScheduledDates,
43730
+ tournamentRecord
43731
+ });
43732
+ if (!result.clearedScheduleCount) return {
43733
+ error: 'schedule not cleared'
43734
+ };
43735
+ }
43736
+
43737
+ return {
43738
+ unscheduledMatchUpIds: invalidSchedulingMatchUpIds
43739
+ };
43559
43740
  }
43560
43741
 
43561
43742
  const tournamentGovernor = {
@@ -43565,6 +43746,7 @@ const tournamentGovernor = {
43565
43746
  analyzeDraws,
43566
43747
  setTournamentName,
43567
43748
  setTournamentNotes,
43749
+ setTournamentDates,
43568
43750
  setTournamentEndDate,
43569
43751
  setTournamentStartDate,
43570
43752
  setTournamentCategories,