Teams selection
unknown
javascript
2 years ago
2.2 kB
15
Indexable
function areSameClub(team1, team2) {
return team1.club === team2.club;
}
function hasThreeTeamsFromSameProvince(group, province) {
const provinceCount = group.filter(team => team.province === province).length;
return provinceCount >= 3;
}
function isGroupFull(group) {
return group.length >= 4;
}
function distributeTeams(seedTeams, otherTeams) {
const groups = {
A: [],
B: [],
C: [],
D: []
};
// Assign seed teams to groups
seedTeams.forEach((seedTeam, index) => {
const group = Object.keys(groups)[index];
groups[group].push(seedTeam);
});
// Shuffle otherTeams
otherTeams.sort(() => Math.random() - 0.5);
while (otherTeams.length > 0) {
const currentTeam = otherTeams.pop();
for (const group in groups) {
const province = currentTeam.province;
if (
groups[group].every(team => !areSameClub(currentTeam, team)) &&
!hasThreeTeamsFromSameProvince(groups[group], province) &&
!isGroupFull(groups[group])
) {
groups[group].push(currentTeam);
break;
}
}
}
return groups;
}
// Example
const seedTeams = [
{ name: 'Stechco1', club: 'Stechco' },
{ name: 'VietUnitedFC', club: 'VietUnitedFC' },
{ name: 'CICC', club: 'CICC' },
{ name: 'CalgaryVFC', club: 'CalgaryVFC' }
]
const otherTeams = [
{ name: 'CTC', province: 'Ontario', club: 'CTC' },
{ name: 'FCKingston', province: 'Ontario', club: 'FCKingston' },
{ name: 'KWFC', province: 'Ontario', club: 'KWFC' },
{ name: 'BFC', province: 'Ontario', club: 'BFC' },
{ name: 'YGOfVN', province: 'Ontario', club: 'YGOfVN' },
{ name: 'FCAE', province: 'Ontario', club: 'FCAE' },
{ name: 'LankFC', province: 'Ontario', club: 'LankFC' },
{ name: 'SFC', province: 'Quebec', club: 'SFC' },
{ name: 'VMU', province: 'Quebec', club: 'VMU' },
{ name: 'Stechco2', province: 'Quebec', club: 'Stechco' },
{ name: 'FC3Mien', province: 'Quebec', club: 'FC3Mien' },
{ name: 'RBJunior', province: 'Quebec', club: 'RBJunior' }
]
const result = distributeTeams(seedTeams, otherTeams);
console.log(result);Editor is loading...