-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨
generateStatCrew
function added to generationGovernor
; te…
…st coverage
- Loading branch information
Showing
5 changed files
with
3,085 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/assemblies/generators/tournamentRecords/generateStatCrew.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { checkRequiredParameters } from '@Helpers/parameters/checkRequiredParameters'; | ||
import { allTournamentMatchUps } from '@Query/matchUps/getAllTournamentMatchUps'; | ||
import { definedAttributes } from '@Tools/definedAttributes'; | ||
import { formatDate } from '@Tools/dateTime'; | ||
import { jsonToXml } from './jsonToXml'; | ||
|
||
// constants | ||
import { TOURNAMENT_RECORD } from '@Constants/attributeConstants'; | ||
import { DOUBLES, SINGLES } from '@Constants/matchUpTypes'; | ||
import { TEAM } from '@Constants/participantConstants'; | ||
import { SUCCESS } from '@Constants/resultConstants'; | ||
|
||
export function generateStatCrew(params) { | ||
const paramsCheck = checkRequiredParameters(params, [{ [TOURNAMENT_RECORD]: true }]); | ||
if (paramsCheck.error) return paramsCheck; | ||
|
||
const { tournamentRecord } = params; | ||
const { tournamentId } = tournamentRecord; | ||
|
||
const teamParticipants = tournamentRecord.participants.filter((participant) => participant.participantType === TEAM); | ||
|
||
const homeTeam = teamParticipants?.find((team) => team.participantRoleResponsibilities?.includes('Home')); | ||
const awayTeams = teamParticipants?.filter((team) => team.participantId !== homeTeam?.participantId); | ||
const neutralGame = !homeTeam ? 'Y' : 'N'; | ||
const date = formatDate(tournamentRecord.startDate, '/', 'MDY'); | ||
|
||
const visname = awayTeams?.[0]?.participantName; | ||
const visid = awayTeams?.[0]?.participantId; | ||
|
||
const homename = homeTeam?.participantName || awayTeams?.[1]?.participantName; | ||
const homeid = homeTeam?.participantId || awayTeams?.[1]?.participantId; | ||
|
||
const matchUps = allTournamentMatchUps({ tournamentRecord }).matchUps; | ||
const singles = matchUps?.filter((matchUp) => matchUp.matchUpType === SINGLES); | ||
const doubles = matchUps?.filter((matchUp) => matchUp.matchUpType === DOUBLES); | ||
|
||
const getScoreAttributes = ({ sets, sideNumber }) => | ||
Object.assign( | ||
{}, | ||
...(sets?.map((set) => { | ||
const { setNumber, side1Score, side2Score, side1TiebreakScore, side2TiebreakScore } = set; | ||
const setKey = `set_${setNumber}`; | ||
const tbKey = `tie_${setNumber}`; | ||
const setValue = sideNumber === 1 ? side1Score : side2Score; | ||
const tieValue = sideNumber === 1 ? side1TiebreakScore : side2TiebreakScore; | ||
return { [setKey]: setValue, [tbKey]: tieValue }; | ||
}) ?? []), | ||
); | ||
|
||
const getParticipantAttibutes = ({ matchUpId, participant }) => { | ||
const name = !participant.individualParticipants ? participant.participantName : undefined; | ||
const name_1 = participant.individualParticipants?.[0]?.participantName; | ||
const name_2 = participant.individualParticipants?.[1]?.participantName; | ||
const vh = matchUpId === homeid ? 'H' : 'V'; | ||
return definedAttributes({ vh, name, name_1, name_2 }); | ||
}; | ||
|
||
const mapMatchUp = (matchUp) => { | ||
const { collectionPosition: match, orderOfFinish: order, matchUpId, matchUpType, sides } = matchUp; | ||
const childArray = sides?.map((side) => { | ||
const { sideNumber, participant } = side; | ||
const scoreAttributes = getScoreAttributes({ sets: matchUp.score?.sets, sideNumber }); | ||
const scoreType = matchUpType === SINGLES ? 'singles_score' : 'doubles_score'; | ||
return { [scoreType]: { ...getParticipantAttibutes({ matchUpId, participant }), ...scoreAttributes } }; | ||
}); | ||
|
||
const matchType = matchUpType === SINGLES ? 'singles_match' : 'doubles_match'; | ||
return { [matchType]: { match, order, childArray } }; | ||
}; | ||
|
||
const json = { | ||
venue: { gameid: tournamentId, date, neutralGame, homeid, homename, visid, visname, officials: {}, rules: {} }, | ||
singles_matches: singles?.map(mapMatchUp), | ||
doubles_matches: doubles?.map(mapMatchUp), | ||
}; | ||
|
||
const xml = jsonToXml({ json, tagName: 'tngame' }); | ||
|
||
return { xml, ...SUCCESS }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export function jsonToXml({ json, tagName }) { | ||
const childArrays = Object.keys(json) | ||
.filter((key) => Array.isArray(json[key])) | ||
.map((key) => ({ key, value: json[key].filter((v) => typeof v === 'object' && Object.keys(v).length === 1) })); | ||
const childObjects = Object.keys(json) | ||
.filter((key) => typeof json[key] === 'object' && !Array.isArray(json[key])) | ||
.map((key) => ({ key, value: json[key] })); | ||
const attributes = Object.keys(json) | ||
.filter((key) => ['string', 'number'].includes(typeof json[key])) | ||
.map((key) => ({ key, value: json[key] })); | ||
|
||
return generateTag({ tagName, attributes, childArrays, childObjects }); | ||
} | ||
|
||
function generateTag(params) { | ||
const { tagName, attributes = [], childArrays = [], childObjects } = params; | ||
const carriageReturn = '\r\n'; | ||
|
||
const arrayChild = (v) => { | ||
const key = Object.keys(v)[0]; | ||
const value = v[key]; | ||
return { key, value }; | ||
}; | ||
const arrayValues = childArrays | ||
?.map(({ key, value }) => generateTag({ tagName: key, childObjects: value.map(arrayChild) })) | ||
.join('\r\n'); | ||
|
||
const childValues = childObjects | ||
?.map(({ key, value }) => jsonToXml({ tagName: key, json: value ?? [] })) | ||
.join('\r\n'); | ||
|
||
const attributeString = attributes?.map(({ key, value }) => `${key}="${value}"`).join(' '); | ||
const attributeValue = attributeString ? ` ${attributeString}` : ''; | ||
|
||
if (tagName === 'childArray') return childValues; | ||
|
||
const objectsTag = childValues?.length ? `${carriageReturn}${childValues}${carriageReturn}` : ''; | ||
const arraysTag = arrayValues?.length ? `${carriageReturn}${arrayValues}${carriageReturn}` : ''; | ||
|
||
return `<${tagName}${attributeValue}>${objectsTag}${arraysTag}</${tagName}>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.