I am a novice here, I apologize for any ignorance. I am trying to create a family feud game for personal use using node.js and express as the server and socket.io for communication between clients. I ran into an issue with this particular function where if there are less than 5 players it will include all but the last one in the socket message. For example if there are 3 players it seem to be running the player4 else statement (for when blank) before completing the player3 variable assignment. I have tried researching it and found promises and async/await but I don't understand it enough to make it work for my situation or if it would even apply. This is a lot of nested if statements. I feel like a loop would be better but not sure how to do it properly. Any help and suggestions are much appreciated.
if (data.teamNum == 'team1') {
team1.teamNum = 'team1';
fs.access('./public/images/players/' + data.player1, function(error) {
if (error) {
console.log(data.player1 + " icons do not exist.")
team1.player1pic = "default"
} else {
console.log(data.player1 + " icons exist.")
fs.readdir('./public/images/players/' + data.player1, (error, files) => {
team1.player1pic = files; // return the number of files
console.log(data.player1 + " has " + team1.player1pic + " pics.");
});
}
if (data.player2 != 'blank') {
fs.access("./public/images/players/" + data.player2, function(error) {
if (error) {
console.log(data.player2 + " icons do not exist.")
team1.player2pic = "default"
} else {
console.log(data.player2 + " icons exist.")
fs.readdir('./public/images/players/' + data.player2, (error, files) => {
team1.player2pic = files; // return the number of files
console.log(data.player2 + " has " + team1.player2pic + " pics.");
});
}
if (data.player3 != 'blank') {
fs.access("./public/images/players/" + data.player3, function(error) {
if (error) {
console.log(data.player3 + " icons do not exist.")
team1.player3pic = "default"
} else {
console.log(data.player3 + " icons exist.")
fs.readdir('./public/images/players/' + data.player3, (error, files) => {
team1.player3pic = files; // return the number of files
console.log(data.player3 + " has " + team1.player3pic + " pics.");
});
}
if (data.player4 != 'blank') {
fs.access("./public/images/players/" + data.player4, function(error) {
if (error) {
console.log(data.player4 + " icons do not exist.")
team1.player4pic = "default"
} else {
console.log(data.player4 + " icons exist.")
fs.readdir('./public/images/players/' + data.player4, (error, files) => {
team1.player4pic = files; // return the number of files
console.log(data.player4 + " has " + team1.player4pic + " pics.");
});
}
if (data.player5 != 'blank') {
fs.access("./public/images/players/" + data.player5, function(error) {
if (error) {
console.log(data.player5 + " icons do not exist.")
team1.player5pic = "default"
} else {
console.log(data.player5 + " icons exist.")
fs.readdir('./public/images/players/' + data.player5, (error, files) => {
team1.player5pic = files; // return the number of files
console.log(data.player5 + " has " + team1.player5pic + " pics.");
console.log('sending pics');
feud.in(data.room).emit('teampics', team1);
});
}
});
} else {
console.log('sending pics');
feud.in(data.room).emit('teampics', team1);
}
});
} else {
console.log('sending pics');
feud.in(data.room).emit('teampics', team1);
}
});
} else {
console.log('sending pics');
feud.in(data.room).emit('teampics', team1);
}
});
} else {
console.log('sending pics');
feud.in(data.room).emit('teampics', team1);
}
});
}
async/await, but on your way to getting there you should rewrite this by a) using a helper function to remove the duplication b) using a recursive approach to do it for 5 players