Sunday, 5 March 2023

Add another layer of validation for sequencing data

I am creating a matchmaking function where 2 players with the same weight will be paired.

It is currently working now based on the same weight. Now, my target is, to add a sequence per match. For example.

*If player 1 is in 1st match, then player 1's next match should be on the 4th or 6th match. Every match of the player there should be a minimum of 3 and Maximum of 6 gaps before that specific player can be matched again.

I have provided my current output and my target output below.

    const source = [
        {
            entryID: 1,
            entryName: "player1",
            weight: 1900,
            class:[],

        }, 
        {
            entryID: 2,
            entryName: "player1",
            weight: 1900,
            class:[],

        },
        {
            entryID: 3,
            entryName: "player2",
            weight: 1900,
            class:[],
        },
        {
            entryID: 4,
            entryName: "player3",
            weight: 1900,
            class:[],
        },
        {
            entryID: 5,
            entryName: "player4",
            weight: 1900,
            class:[],
        },
        {
            entryID: 6,
            entryName: "player5",
            weight: 1900,
            class:[],
        },
        {
            entryID: 7,
            entryName: "player6",
            weight: 1900,
            class:[],
        },
        {
            entryID: 8,
            entryName: "player7",
            weight: 1900,
            class:[],

        },
        {
            entryID: 9,
            entryName: "player8",
            weight: 1900,
            class:[],
        },
        {
            entryID: 10,
            entryName: "player9",
            weight: 1900,
           class:[],
        },
        {
            entryID: 11,
           entryName: "player10",
            weight: 1900,
            class:[],
        },
        {
            entryID: 12,
           entryName: "player11",
            weight: 1900,
              class:[],
        },
        {
            entryID: 13,
           entryName: "player12",
            weight: 1900,
            class:[],
        },
        {
            entryID: 14,
           entryName: "player1",
            weight: 1900,
            class:[],
        },

    ];

console.log(combine(source))

function combine(data = [], different = 0, maxGroupSize = 2) {
    const groups = [], related = [], sortedData = [...data].sort((a, b) => a.weight - b.weight),
        alreadyInRela = (setX, eName) => {
            let list = [...setX, eName]
            return related.some(rela => list.every(l => rela.has(l)))
        };
    
    sortedData.forEach((el, indx) => {
        let place = groups.findIndex( // find a place in a group forEach element, use indx as track
            g => g.names.size < maxGroupSize              // is the group incomplete ?
                && !g.names.has(el.entryName)             // is entryName not in the group list (names Set) ?
                && (el.weight - g.weight) <= different
                && !alreadyInRela(g.names, el.entryName) // is (entryName + group list) does not already used ?
                && el.class.every(c => !g.usedClasses.has(c)) // check class
        )

        if (place < 0) { // not found -> create new group
            let names = new Set().add(el.entryName)                      // create new group
            groups.push({ names, indxs: [indx], weight: el.weight, usedClasses: new Set(el.class) })  // group constitutive info 
            related.push(names)                                      // keep track of group list
        } else { // find a place in a group
            groups[place].names.add(el.entryName)  // related list is also updated
            el.class.forEach(c => groups[place].usedClasses.add(c)) // add classes
            groups[place].indxs.push(indx)        // add indx to retreive element in sortedData 
        }
    });

    return groups.reduce((r, g, i) => { // build result
        if (g.indxs.length > 1) {
            let key = `${i}_` + g.indxs.map(x => sortedData[x].weight).join('_')
            r[key] = []
            g.indxs.forEach(x => r[key].push(sortedData[x]))
        }
        return r
    }, {})
}

Current result:

{
  0_1900_1900: [{ // 1st match
  class: [],
  entryID: 1,
  entryName: "player1",
  weight: 1900
}, {
  class: [],
  entryID: 3,
  entryName: "player2",
  weight: 1900
}],
  1_1900_1900: [{ // 2nd match (As we can see here, player 1 has been matched again. There should be 3-6 matches before this player can be matched again)
  class: [],
  entryID: 2,
  entryName: "player1",
  weight: 1900
}, {
  class: [],
  entryID: 4,
  entryName: "player3",
  weight: 1900
}],
  2_1900_1900: [{ // 3rd match
  class: [],
  entryID: 5,
  entryName: "player4",
  weight: 1900
}, {
  class: [],
  entryID: 6,
  entryName: "player5",
  weight: 1900
}],
  3_1900_1900: [{ // 4th match
  class: [],
  entryID: 7,
  entryName: "player6",
  weight: 1900
}, {
  class: [],
  entryID: 8,
  entryName: "player7",
  weight: 1900
}],
  4_1900_1900: [{ // 5th match
  class: [],
  entryID: 9,
  entryName: "player8",
  weight: 1900
}, {
  class: [],
  entryID: 10,
  entryName: "player9",
  weight: 1900
}],
  5_1900_1900: [{ // 6th match
  class: [],
  entryID: 11,
  entryName: "player10",
  weight: 1900
}, {
  class: [],
  entryID: 12,
  entryName: "player11",
  weight: 1900
}],
  6_1900_1900: [{ // 7th match
  class: [],
  entryID: 13,
  entryName: "player12",
  weight: 1900
}, {
  class: [],
  entryID: 14,
  entryName: "player1",
  weight: 1900
}]
}

Target result:

{
  0_1900_1900: [{// 1st match
  class: [],
  entryID: 1,
  entryName: "player1",
  weight: 1900
}, {
  class: [],
  entryID: 3,
  entryName: "player2",
  weight: 1900
}],
  1_1900_1900: [{ //2nd match
  class: [],
  entryID: 4,
  entryName: "player3",
  weight: 1900
}, {
  class: [],
  entryID: 5,
  entryName: "player4",
  weight: 1900
}],
  2_1900_1900: [{ //3rd match
  class: [],
  entryID: 6,
  entryName: "player5",
  weight: 1900
}, {
  class: [],
  entryID: 7,
  entryName: "player6",
  weight: 1900
}],
  3_1900_1900: [{ //4th match (As we can see here, there are 2 matches before player 1 got matched again. This is my target output)
  class: [],
  entryID: 2,
  entryName: "player1",
  weight: 1900
}, {
  class: [],
  entryID: 8,
  entryName: "player7",
  weight: 1900
}],
  4_1900_1900: [{ //5th match
  class: [],
  entryID: 9,
  entryName: "player8",
  weight: 1900
}, {
  class: [],
  entryID: 10,
  entryName: "player9",
  weight: 1900
}],
  5_1900_1900: [{ //6th match
  class: [],
  entryID: 11,
  entryName: "player10",
  weight: 1900
}, {
  class: [],
  entryID: 12,
  entryName: "player11",
  weight: 1900
}],
  6_1900_1900: [{ //7th match
  class: [],
  entryID: 14,
  entryName: "player1",
  weight: 1900
}, {
  class: [],
  entryID: 13,
  entryName: "player12",
  weight: 1900
}]
}

Any help will be appreciated. Thank you



from Add another layer of validation for sequencing data

No comments:

Post a Comment