Last active
August 4, 2020 17:47
-
-
Save ilmoralito/260f253e86b7f53a06d21d10031d013c to your computer and use it in GitHub Desktop.
Jornadas
This file contains hidden or 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
const startDate = "2020-08-03"; | |
const matchDays = [1, 2]; | |
const fields = ["Field 1", "Field 2"]; | |
const teams = ["A", "B", "C", "D"]; | |
const numberOfRematches = 3; | |
const getCombinations = (data) => { | |
let output = []; | |
for (let i = 0; i < teams.length - 1; i++) { | |
for (let j = i + 1; j < teams.length; j++) { | |
output = [...output, [data[i], data[j]]]; | |
} | |
} | |
return output; | |
}; | |
const getMatches = (data) => { | |
let matches = []; | |
let i = 0; | |
while (data.length) { | |
for (const [index, entry] of data.entries()) { | |
if (data.length === 1) { | |
matches[i - 1].push(entry); | |
data.splice(index, 1); | |
break; | |
} | |
if (index === 0) { | |
matches.push([entry]); | |
data.splice(index, 1); | |
continue; | |
} | |
const [team1, team2] = entry; | |
const idx = matches[i].findIndex( | |
(value) => value.includes(team1) || value.includes(team2) | |
); | |
if (idx !== -1) { | |
continue; | |
} | |
matches[i].push(entry); | |
data.splice(index, 1); | |
} | |
i++; | |
} | |
return matches; | |
}; | |
const getAllCombinations = (value) => { | |
let output = []; | |
for (const current of Array(numberOfRematches).keys()) { | |
if (current === 0) { | |
output = [...value]; | |
} else { | |
output = [...output, ...value.reverse()]; | |
} | |
} | |
return output; | |
}; | |
const addDays = (date, days) => { | |
const result = new Date(date); | |
result.setDate(result.getDate() + days); | |
return result; | |
}; | |
const getMatchingDays = (startDate, allowedDays, maxCount) => { | |
const matchingDays = []; | |
let current = startDate; | |
while (matchingDays.length < maxCount) { | |
if (allowedDays.find((day) => day === current.getUTCDay())) { | |
matchingDays.push(current); | |
} | |
current = addDays(current, 1); | |
} | |
return matchingDays; | |
}; | |
const getGameDays = (data) => { | |
return data | |
.map((entry, index) => { | |
return { | |
number: index + 1, | |
field: fields[index % fields.length], | |
matches: [...entry], | |
}; | |
}) | |
.map((entry, index) => ({ ...entry, date: matchingDays[index] })); | |
}; | |
const combinations = getCombinations(teams); | |
const allCombinations = getAllCombinations(combinations); | |
const matchingDays = getMatchingDays( | |
new Date(startDate), | |
matchDays, | |
allCombinations.length | |
); | |
const matches = getMatches(allCombinations); | |
console.log(matches); | |
const gameDays = getGameDays(matches); | |
console.dir(gameDays, { depth: null, color: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment