I am working on a matchmaking system where two players of the same level are matched and joined in one array. My goal is to include a second data in the array for players who do not have a match.
Example: EntryID: “15”, player: ”testing11”, level: ”3”
EntryID: ”nm”, player: ”nm”, level: ”nm”;
In the example provided, when a player does not have a match, another data point with "nm" meaning no match should be included.
I have attached an image depicting my goal for better understanding of the issue and objective. Thank you.
https://i.sstatic.net/FIVmL.png
Script:
let ajaxResult = []; // the pushed data will be saved here
let save_method;
let table;
let base_url = "<?php echo base_url();?>";
let result = [];
var html = "";
// This is where the same level will be matched
const combine = (source) => {
return source.reduce((acc, curr) => {
if (acc[curr.level]) {
const levelArr = acc[curr.level];
const last = levelArr[levelArr.length - 1];
if (last.length === 2) {
levelArr.push([curr])
} else {
last.push(curr)
}
} else {
acc[curr.level] = [
[curr]
];
}
return acc;
}, {})
};
// I am removing duplicates here. Test 1 vs Test 1 should not be possible
function removeDuplicates(result) {
return Object.values(result.reduce((acc, curr) => {
acc[curr.player] = acc[curr.player] || curr;
return acc;
}, {}))
}
const uniquePlayers = removeDuplicates(result);
$(document).ready(function() {
//datatables
table = $("#entry_list1").DataTable({
processing: false,
serverSide: true,
order: [],
searching: false,
paging: false,
info: false,
ajax: {
url: "<?php echo site_url('controller/fetch_players')?>",
type: "POST",
async: true,
dataType: "json",
success: function(data) {
result = combine(removeDuplicates(data.data2));
var keys = Object.keys(result)
// I am creating a textbox depending on the matches above so that I can insert it into the DB. My goal here is to create a textbox for my "no match" player, as the current code only creates textboxes for matched players.
for (var i = 0; i < keys.length; i++) {
result[keys[i]].forEach(function(val) {
val.forEach(function(value, index) {
var entryIDs = index == 0 ? "entryIDM[]" : "entryIDW[]";
var players = index == 0 ? "playerM[]" : "playerW[]";
var levels = index == 0 ? "levelM[]" : "levelW[]";
html += `<input type="text" name="${entryIDs}" value="${value.entryID}">
<input type="text" name="${players}" value="${value.player}">
<input type="text" name="${levels}" value="${value.level}">`;
})
})
}
document.getElementById("result").innerHTML = html;
},
},
"columnDefs": [{
"targets": [0], //first column
"orderable": false, //set not orderable
},
{
"targets": [-1], //last column
"orderable": false, //set not orderable
},
],
});
});
JSON.stringify:
{"draw":"1","recordsTotal":5,"recordsFiltered":5,"data":[["15","testing11","3"],["13","testing8","1"],["4","testing4","2"],["3","testing3","2"],["1","testing1","1"]],"data2":[{"entryID":"15","player":"testing11","level":"3"},{"entryID":"13","player":"testing8","level":"1"},{"entryID":"4","player":"testing4","level":"2"},{"entryID":"3","player":"testing3","level":"2"},{"entryID":"1","player":"testing1","level":"1"}]}