tods-competition-factory 1.8.16 → 1.8.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2382,7 +2382,7 @@ const matchUpFormatCode = {
2382
2382
  };
2383
2383
 
2384
2384
  function factoryVersion() {
2385
- return "1.8.16";
2385
+ return "1.8.18";
2386
2386
  }
2387
2387
 
2388
2388
  function getObjectTieFormat(obj) {
@@ -4603,8 +4603,8 @@ function findPolicy({
4603
4603
  }
4604
4604
 
4605
4605
  function getMatchUpCompetitiveProfile({
4606
- profileBands,
4607
4606
  tournamentRecord,
4607
+ profileBands,
4608
4608
  matchUp
4609
4609
  }) {
4610
4610
  if (!matchUp)
@@ -15940,8 +15940,8 @@ function allTournamentMatchUps(params) {
15940
15940
  if (!participants) {
15941
15941
  ({ participants, participantMap } = hydrateParticipants({
15942
15942
  participantsProfile,
15943
- policyDefinitions,
15944
15943
  useParticipantMap,
15944
+ policyDefinitions,
15945
15945
  tournamentRecord,
15946
15946
  contextProfile,
15947
15947
  inContext
@@ -54617,6 +54617,272 @@ function getEntryStatusReports({
54617
54617
  };
54618
54618
  }
54619
54619
 
54620
+ function getParticipantStats({
54621
+ withCompetitiveProfiles,
54622
+ opponentParticipantId,
54623
+ withIndividualStats,
54624
+ teamParticipantId,
54625
+ tournamentRecord,
54626
+ withScaleValues,
54627
+ tallyPolicy,
54628
+ matchUps
54629
+ }) {
54630
+ if (!tournamentRecord)
54631
+ return { error: MISSING_TOURNAMENT_RECORD };
54632
+ if (matchUps && !Array.isArray(matchUps))
54633
+ return { error: INVALID_MATCHUP };
54634
+ const participantsProfile = withScaleValues ? { withScaleValues } : void 0;
54635
+ matchUps = matchUps || allTournamentMatchUps({ tournamentRecord, participantsProfile }).matchUps;
54636
+ if (!matchUps?.length)
54637
+ return { error: MISSING_MATCHUPS };
54638
+ const teamParticipantIds = [];
54639
+ if (opponentParticipantId)
54640
+ teamParticipantIds.push(opponentParticipantId);
54641
+ if (teamParticipantId)
54642
+ teamParticipantIds.push(teamParticipantId);
54643
+ const participantFilters = !teamParticipantIds.length ? { participantTypes: [TEAM_PARTICIPANT] } : { participantIds: teamParticipantIds };
54644
+ const teamParticipants = getParticipants$1({ participantFilters, tournamentRecord }).participants ?? [];
54645
+ if (!teamParticipants.every(
54646
+ ({ participantType }) => participantType === TEAM_PARTICIPANT
54647
+ )) {
54648
+ return { error: INVALID_PARTICIPANT_IDS };
54649
+ }
54650
+ if (!teamParticipantIds.length)
54651
+ teamParticipantIds.push(...teamParticipants.map(extractAttributes("participantId")));
54652
+ const participantDetails = /* @__PURE__ */ new Map();
54653
+ const participantStats = /* @__PURE__ */ new Map();
54654
+ const participating = /* @__PURE__ */ new Map();
54655
+ const teamMap = /* @__PURE__ */ new Map();
54656
+ const initStats = (participantId, participantName = "") => participantStats.get(participantId) || participantStats.set(participantId, {
54657
+ participantName,
54658
+ participantId,
54659
+ competitorIds: [],
54660
+ competitiveness: {},
54661
+ matchUpStatuses: {},
54662
+ tiebreaks: [0, 0],
54663
+ matchUps: [0, 0],
54664
+ points: [0, 0],
54665
+ games: [0, 0],
54666
+ sets: [0, 0]
54667
+ }) && participantStats.get(participantId);
54668
+ for (const teamParticipant of teamParticipants) {
54669
+ const { participantId, individualParticipantIds } = teamParticipant;
54670
+ teamMap.set(participantId, individualParticipantIds ?? []);
54671
+ initStats(participantId, teamParticipant.participantName);
54672
+ }
54673
+ if (teamParticipantId && !teamMap.get(teamParticipantId))
54674
+ return decorateResult({
54675
+ result: { error: PARTICIPANT_NOT_FOUND },
54676
+ context: { teamParticipantId }
54677
+ });
54678
+ if (opponentParticipantId && !teamMap.get(opponentParticipantId))
54679
+ return decorateResult({
54680
+ result: { error: PARTICIPANT_NOT_FOUND },
54681
+ context: { opponentParticipantId }
54682
+ });
54683
+ const relevantMatchUps = [];
54684
+ const getSideParticipantIds = (sides) => {
54685
+ const sideParticipantIds = [[], []];
54686
+ for (const side of sides) {
54687
+ const participant = side.participant;
54688
+ if (participant?.participantName) {
54689
+ participantDetails.set(participant.participantId, {
54690
+ participantName: participant.participantName,
54691
+ ratings: participant.ratings
54692
+ });
54693
+ }
54694
+ }
54695
+ const getCompetitorIds = ({ side, individualParticipantIds }) => {
54696
+ return side.participantId && (!individualParticipantIds?.length || individualParticipantIds.includes(side.participantId)) && [
54697
+ side.participantId
54698
+ ] || side.participant?.individualParticipantIds?.length && (!individualParticipantIds?.length || intersection(
54699
+ individualParticipantIds,
54700
+ side.participant?.individualParticipantIds
54701
+ ).length === side.participant?.individualParticipantIds?.length) && side.participant.individualParticipantIds;
54702
+ };
54703
+ if (teamMap.size) {
54704
+ const processSides = (thisTeamId, individualParticipantIds) => {
54705
+ for (const side of sides) {
54706
+ if (!side.participant)
54707
+ continue;
54708
+ const competitorIds = getCompetitorIds({
54709
+ individualParticipantIds,
54710
+ side
54711
+ });
54712
+ if (competitorIds?.length) {
54713
+ const sideNumber = side.sideNumber;
54714
+ if (!sideNumber)
54715
+ continue;
54716
+ const ids = [thisTeamId];
54717
+ if (withIndividualStats)
54718
+ ids.push(...competitorIds);
54719
+ sideParticipantIds[sideNumber - 1] = ids;
54720
+ const stats = participantStats.get(thisTeamId);
54721
+ for (const id of competitorIds.filter(Boolean)) {
54722
+ if (stats && !stats.competitorIds.includes(id))
54723
+ stats.competitorIds.push(id);
54724
+ }
54725
+ }
54726
+ }
54727
+ };
54728
+ if (teamParticipantId) {
54729
+ const processForTeam = !opponentParticipantId || sides.every((side) => {
54730
+ side.participant && (getCompetitorIds({
54731
+ side,
54732
+ individualParticipantIds: teamMap.get(teamParticipantId)
54733
+ }) || getCompetitorIds({
54734
+ side,
54735
+ individualParticipantIds: teamMap.get(opponentParticipantId)
54736
+ }));
54737
+ });
54738
+ if (processForTeam)
54739
+ processSides(teamParticipantId, teamMap.get(teamParticipantId));
54740
+ } else {
54741
+ for (const [thisTeamId, individualParticipantIds] of teamMap) {
54742
+ processSides(thisTeamId, individualParticipantIds);
54743
+ }
54744
+ }
54745
+ } else if (withIndividualStats) {
54746
+ for (const side of sides) {
54747
+ if (!side.participant)
54748
+ continue;
54749
+ const competitorIds = getCompetitorIds({
54750
+ individualParticipantIds: [],
54751
+ side
54752
+ });
54753
+ const sideNumber = side.sideNumber;
54754
+ if (!sideNumber)
54755
+ continue;
54756
+ sideParticipantIds[sideNumber - 1] = competitorIds;
54757
+ }
54758
+ }
54759
+ return sideParticipantIds;
54760
+ };
54761
+ for (const matchUp of matchUps) {
54762
+ if (!isObject(matchUp))
54763
+ return { error: INVALID_MATCHUP };
54764
+ const {
54765
+ matchUpStatus,
54766
+ matchUpFormat,
54767
+ matchUpType,
54768
+ winningSide,
54769
+ score,
54770
+ sides
54771
+ } = matchUp;
54772
+ if (!sides || !score || matchUpType === TEAM_MATCHUP || matchUpStatus === BYE)
54773
+ continue;
54774
+ const sideParticipantIds = getSideParticipantIds(sides);
54775
+ if (!sideParticipantIds.filter(Boolean).length)
54776
+ continue;
54777
+ const competitiveness = withCompetitiveProfiles && winningSide && getMatchUpCompetitiveProfile({ matchUp })?.competitiveness;
54778
+ relevantMatchUps.push(matchUp);
54779
+ const setsTally = countSets({
54780
+ matchUpStatus,
54781
+ matchUpFormat,
54782
+ tallyPolicy,
54783
+ winningSide,
54784
+ score
54785
+ });
54786
+ const gamesTally = countGames({
54787
+ matchUpStatus,
54788
+ matchUpFormat,
54789
+ tallyPolicy,
54790
+ winningSide,
54791
+ score
54792
+ });
54793
+ const { pointsTally, tiebreaksTally } = countPoints({
54794
+ matchUpFormat,
54795
+ score
54796
+ });
54797
+ sideParticipantIds.forEach((ids, index) => {
54798
+ for (const id of ids) {
54799
+ const participantName = participantDetails.get(id)?.participantName;
54800
+ const stats = initStats(id, participantName);
54801
+ if (stats) {
54802
+ const teamSumTally = (stat, tally) => tally.forEach((t, i) => stats[stat][i] += t);
54803
+ const tiebreaks = index ? [...tiebreaksTally].reverse() : tiebreaksTally;
54804
+ const points = index ? [...pointsTally].reverse() : pointsTally;
54805
+ const games = index ? [...gamesTally].reverse() : gamesTally;
54806
+ const sets = index ? [...setsTally].reverse() : setsTally;
54807
+ teamSumTally("tiebreaks", tiebreaks);
54808
+ teamSumTally("points", points);
54809
+ teamSumTally("games", games);
54810
+ teamSumTally("sets", sets);
54811
+ if (winningSide) {
54812
+ const tallyIndex = winningSide - 1 === index ? 0 : 1;
54813
+ stats.matchUps[tallyIndex] += 1;
54814
+ }
54815
+ if (competitiveness) {
54816
+ const attr = competitiveness.toLowerCase();
54817
+ if (!stats.competitiveness[attr])
54818
+ stats.competitiveness[attr] = [0, 0];
54819
+ stats.competitiveness[attr][index] += 1;
54820
+ }
54821
+ if (matchUpStatus) {
54822
+ const attr = matchUpStatus.toLowerCase();
54823
+ if (!stats.matchUpStatuses[attr])
54824
+ stats.matchUpStatuses[attr] = 0;
54825
+ stats.matchUpStatuses[attr] += 1;
54826
+ }
54827
+ }
54828
+ }
54829
+ });
54830
+ }
54831
+ const statsattributes = ["tiebreaks", "matchUps", "points", "games", "sets"];
54832
+ const competitivenessAttributes = ["competitive", "routine", "decisive"];
54833
+ const ratio = /* @__PURE__ */ new Map();
54834
+ const add = (a, b) => (a ?? 0) + (b ?? 0);
54835
+ for (const [participantId, stats] of participantStats.entries()) {
54836
+ for (const attr of statsattributes) {
54837
+ const total = stats[attr].reduce(add);
54838
+ if (total) {
54839
+ const value = stats[attr][0] / total;
54840
+ const accessor = `${attr}Ratio`;
54841
+ const fixedValue = parseFloat(value.toFixed(2));
54842
+ stats[accessor] = fixedValue;
54843
+ participating.set(participantId, true);
54844
+ if (!ratio.has(accessor))
54845
+ ratio.set(accessor, []);
54846
+ ratio.get(accessor)?.push(fixedValue);
54847
+ }
54848
+ }
54849
+ for (const attr of competitivenessAttributes) {
54850
+ const total = stats.competitiveness?.[attr]?.reduce(add);
54851
+ if (total) {
54852
+ const value = stats.competitiveness[attr][0] / total;
54853
+ const accessor = `${attr}Ratio`;
54854
+ const fixedValue = parseFloat(value.toFixed(2));
54855
+ stats[accessor] = fixedValue;
54856
+ }
54857
+ }
54858
+ }
54859
+ if (!teamParticipantId) {
54860
+ const highLowSort = (a, b) => b - a;
54861
+ for (const stats of participantStats.values()) {
54862
+ for (const attr of statsattributes) {
54863
+ const accessor = `${attr}Ratio`;
54864
+ if (typeof stats[accessor] === "number") {
54865
+ const index = ratio.get(accessor)?.sort(highLowSort).indexOf(stats[accessor]);
54866
+ if (typeof index === "number" && index >= 0) {
54867
+ const rankAccessor = `${attr}Rank`;
54868
+ stats[rankAccessor] = index + 1;
54869
+ }
54870
+ }
54871
+ }
54872
+ }
54873
+ }
54874
+ const result = { relevantMatchUps, ...SUCCESS };
54875
+ if (teamParticipantId) {
54876
+ result.teamStats = participantStats.get(teamParticipantId);
54877
+ if (opponentParticipantId)
54878
+ result.opponentStats = participantStats.get(opponentParticipantId);
54879
+ } else {
54880
+ result.participatingTeamsCount = participating.size;
54881
+ }
54882
+ result.allParticipantStats = [...participantStats.values()];
54883
+ return result;
54884
+ }
54885
+
54620
54886
  function getAvgWTN({
54621
54887
  eventType,
54622
54888
  matchUps,
@@ -54867,196 +55133,10 @@ function getPositionManipulations({ extensions }) {
54867
55133
  return extensions?.find(({ name }) => name === AUDIT_POSITION_ACTIONS)?.value?.slice(1);
54868
55134
  }
54869
55135
 
54870
- function getTeamStats({
54871
- opponentParticipantId,
54872
- teamParticipantId,
54873
- tournamentRecord,
54874
- tallyPolicy,
54875
- matchUps
54876
- }) {
54877
- if (!tournamentRecord)
54878
- return { error: MISSING_TOURNAMENT_RECORD };
54879
- if (matchUps && !Array.isArray(matchUps))
54880
- return { error: INVALID_MATCHUP };
54881
- matchUps = matchUps || allTournamentMatchUps({ tournamentRecord }).matchUps;
54882
- if (!matchUps?.length)
54883
- return { error: MISSING_MATCHUPS };
54884
- const teamParticipantIds = [];
54885
- if (opponentParticipantId)
54886
- teamParticipantIds.push(opponentParticipantId);
54887
- if (teamParticipantId)
54888
- teamParticipantIds.push(teamParticipantId);
54889
- const participantFilters = !teamParticipantIds.length ? { participantTypes: [TEAM_PARTICIPANT] } : { participantIds: teamParticipantIds };
54890
- const teamParticipants = getParticipants$1({ participantFilters, tournamentRecord }).participants ?? [];
54891
- if (!teamParticipants.every(
54892
- ({ participantType }) => participantType === TEAM_PARTICIPANT
54893
- )) {
54894
- return { error: INVALID_PARTICIPANT_IDS };
54895
- }
54896
- if (!teamParticipantIds.length)
54897
- teamParticipantIds.push(...teamParticipants.map(extractAttributes("participantId")));
54898
- const teamStats = /* @__PURE__ */ new Map();
54899
- const teamMap = /* @__PURE__ */ new Map();
54900
- for (const teamParticipant of teamParticipants) {
54901
- const { participantId, individualParticipantIds } = teamParticipant;
54902
- teamMap.set(participantId, individualParticipantIds ?? []);
54903
- teamStats.set(participantId, {
54904
- participantName: teamParticipant.participantName ?? "",
54905
- matchUpStatuses: {},
54906
- competitorIds: [],
54907
- tiebreaks: [0, 0],
54908
- matchUps: [0, 0],
54909
- points: [0, 0],
54910
- participantId,
54911
- games: [0, 0],
54912
- sets: [0, 0]
54913
- });
54914
- }
54915
- if (teamParticipantId && !teamMap.get(teamParticipantId))
54916
- return decorateResult({
54917
- result: { error: PARTICIPANT_NOT_FOUND },
54918
- context: { teamParticipantId }
54919
- });
54920
- if (opponentParticipantId && !teamMap.get(opponentParticipantId))
54921
- return decorateResult({
54922
- result: { error: PARTICIPANT_NOT_FOUND },
54923
- context: { opponentParticipantId }
54924
- });
54925
- const relevantMatchUps = [];
54926
- const getTeamParticipantIds = (sides) => {
54927
- const sideTeamParticipantIds = [];
54928
- const isTeamSide = (side, individualParticipantIds) => {
54929
- return !!(side.participantId && individualParticipantIds.includes(side.participantId) || side.participant?.individualParticipantIds?.length && intersection(
54930
- individualParticipantIds,
54931
- side.participant?.individualParticipantIds
54932
- ).length === side.participant?.individualParticipantIds?.length);
54933
- };
54934
- for (const [teamParticipantId2, individualParticipantIds] of teamMap) {
54935
- for (const side of sides) {
54936
- if (!side.participant)
54937
- continue;
54938
- if (isTeamSide(side, individualParticipantIds)) {
54939
- const sideNumber = side.sideNumber;
54940
- if (!sideNumber)
54941
- continue;
54942
- sideTeamParticipantIds[sideNumber - 1] = teamParticipantId2;
54943
- const competitorIds = side.participant?.individualParticipantIds?.length ? side.participant.individualParticipantIds : [side.participant.participantId];
54944
- const stats = teamStats.get(teamParticipantId2);
54945
- for (const id of competitorIds) {
54946
- if (stats && !stats.competitorIds.includes(id))
54947
- stats.competitorIds.push(id);
54948
- }
54949
- }
54950
- }
54951
- }
54952
- return sideTeamParticipantIds;
54953
- };
54954
- for (const matchUp of matchUps) {
54955
- if (!isObject(matchUp))
54956
- return { error: INVALID_MATCHUP };
54957
- const {
54958
- matchUpStatus,
54959
- matchUpFormat,
54960
- matchUpType,
54961
- winningSide,
54962
- score,
54963
- sides
54964
- } = matchUp;
54965
- if (!sides || !score || matchUpType === TEAM_MATCHUP || matchUpStatus === BYE)
54966
- continue;
54967
- const teamParticipantIds2 = getTeamParticipantIds(sides);
54968
- if (!teamParticipantIds2.filter(Boolean).length)
54969
- continue;
54970
- relevantMatchUps.push(matchUp);
54971
- const setsTally = countSets({
54972
- matchUpStatus,
54973
- matchUpFormat,
54974
- tallyPolicy,
54975
- winningSide,
54976
- score
54977
- });
54978
- const gamesTally = countGames({
54979
- matchUpStatus,
54980
- matchUpFormat,
54981
- tallyPolicy,
54982
- winningSide,
54983
- score
54984
- });
54985
- const { pointsTally, tiebreaksTally } = countPoints({
54986
- matchUpFormat,
54987
- score
54988
- });
54989
- teamParticipantIds2.forEach((teamParticipantId2, index) => {
54990
- if (teamParticipantId2) {
54991
- const stats = teamStats.get(teamParticipantId2);
54992
- if (stats) {
54993
- const teamSumTally = (stat, tally) => tally.forEach((t, i) => stats[stat][i] += t);
54994
- const tiebreaks = index ? [...tiebreaksTally].reverse() : tiebreaksTally;
54995
- const points = index ? [...pointsTally].reverse() : pointsTally;
54996
- const games = index ? [...gamesTally].reverse() : gamesTally;
54997
- const sets = index ? [...setsTally].reverse() : setsTally;
54998
- teamSumTally("tiebreaks", tiebreaks);
54999
- teamSumTally("points", points);
55000
- teamSumTally("games", games);
55001
- teamSumTally("sets", sets);
55002
- if (winningSide) {
55003
- const tallyIndex = winningSide - 1 === index ? 0 : 1;
55004
- stats.matchUps[tallyIndex] += 1;
55005
- }
55006
- if (matchUpStatus) {
55007
- if (!stats.matchUpStatuses[matchUpStatus])
55008
- stats.matchUpStatuses[matchUpStatus] = 0;
55009
- stats.matchUpStatuses[matchUpStatus] += 1;
55010
- }
55011
- }
55012
- }
55013
- });
55014
- }
55015
- const attrs = ["tiebreaks", "matchUps", "points", "games", "sets"];
55016
- const ratio = /* @__PURE__ */ new Map();
55017
- const add = (a, b) => (a ?? 0) + (b ?? 0);
55018
- for (const stats of teamStats.values()) {
55019
- for (const attr of attrs) {
55020
- const total = stats[attr].reduce(add);
55021
- if (total) {
55022
- const value = stats[attr][0] / total;
55023
- const accessor = `${attr}Ratio`;
55024
- const fixedValue = parseFloat(value.toFixed(2));
55025
- stats[accessor] = fixedValue;
55026
- if (!ratio.has(accessor))
55027
- ratio.set(accessor, []);
55028
- ratio.get(accessor)?.push(fixedValue);
55029
- }
55030
- }
55031
- }
55032
- const highLowSort = (a, b) => b - a;
55033
- for (const stats of teamStats.values()) {
55034
- for (const attr of attrs) {
55035
- const accessor = `${attr}Ratio`;
55036
- if (typeof stats[accessor] === "number") {
55037
- const index = ratio.get(accessor)?.sort(highLowSort).indexOf(stats[accessor]);
55038
- if (typeof index === "number" && index >= 0) {
55039
- const rankAccessor = `${attr}Rank`;
55040
- stats[rankAccessor] = index + 1;
55041
- }
55042
- }
55043
- }
55044
- }
55045
- const result = { relevantMatchUps, ...SUCCESS };
55046
- if (teamParticipantId) {
55047
- result.teamStats = teamStats.get(teamParticipantId);
55048
- if (opponentParticipantId)
55049
- result.opponentStats = teamStats.get(opponentParticipantId);
55050
- } else {
55051
- result.allTeamStats = [...teamStats.values()];
55052
- }
55053
- return result;
55054
- }
55055
-
55056
55136
  const reportGovernor = {
55057
55137
  getStructureReports,
55058
55138
  getEntryStatusReports,
55059
- getTeamStats
55139
+ getParticipantStats
55060
55140
  };
55061
55141
 
55062
55142
  function setDrawParticipantRepresentativeIds({
@@ -60068,10 +60148,10 @@ function getEvents({
60068
60148
  }
60069
60149
  const processFlight = (drawId, participantIds2) => {
60070
60150
  const processParticipant2 = (participant) => {
60071
- if (participant?.ratings?.[eventType]) {
60151
+ if (eventsMap[eventId].draws?.[drawId] && participant?.ratings?.[eventType]) {
60072
60152
  for (const rating of participant?.ratings?.[eventType] ?? []) {
60073
60153
  const scaleName = rating.scaleName;
60074
- if (!eventsMap[eventId].draws[drawId].ratings[scaleName])
60154
+ if (!eventsMap[eventId].draws[drawId]?.ratings[scaleName])
60075
60155
  eventsMap[eventId].draws[drawId].ratings[scaleName] = [];
60076
60156
  const accessor = ratingsParameters[scaleName]?.accessor;
60077
60157
  if (accessor) {