groups calcetto con match

by BeNdErR

JavaScript

var teamsPerGroup = 4;
var groupNames = ["A","B","C","D","E","F","G","H"]
var teamsTable =  [{
    name: "A"
}, {
    name: "B"
}, {
    name: "C"
}, {
    name: "D"
}, {
    name: "E"
}, {
    name: "F"
}, {
    name: "G"
}, {
    name: "H"
}, {
    name: "I"
}, {
    name: "L"
}, {
    name: "M"
}, {
    name: "N"
}, {
    name: "O"
}, {
    name: "P"
}, {
    name: "Q"
}, {
    name: "R"
}];

teamsTable = _.shuffle(teamsTable);
function createGroups(teams, teamsPerGroup){
    var groups = [];
    var groupIndex = -1;
    var currGroup = undefined;
    for(var x = 0;  x < teams.length; x++){
        if((x % teamsPerGroup) === 0){
            if (x > 0){
                currGroup.matchList = createMatches(currGroup.teams)
                groups.push(currGroup);
            }
            groupIndex++;
            currGroup = {name : "Group " + groupNames[groupIndex], teams : [], matchList: []};
        }
        currGroup.teams.push(teams[x])
    }
    currGroup.matchList = createMatches(currGroup.teams)
    groups.push(currGroup);
    console.log(groups);
}
createGroups(teamsTable, teamsPerGroup)
function createMatches(teams) {
    var games = [];
    for (var x = 0; x < teams.length; x++) {
        var homeTeam = teams[x].name;
        
        for (var y = x + 1; y < teams.length; y++) {
            
            
            var opponentTeam = teams[y].name;
            var match = {
                team1: homeTeam,
                team2 : opponentTeam,
                scoreTeam1 : 0,
                scoreTeam2 : 0,
                status : "not played"
            }
            games.push(match)
            
        }
    }
    
    return _.shuffle(games);
}