使用if语句时的JavaScript执行顺序

andrewjae04

我是这里的新手,对于任何不了解,我深表歉意。我正在尝试使用node.js创建一个家庭使用的个人游戏,并表示为服务器和socket.io,用于客户端之间的通信。我遇到了这个特殊功能的问题,即如果少于5个播放器,它将在套接字消息中包含除最后一个播放器以外的所有播放器。例如,如果有3个玩家,则似乎在完成player3变量分配之前正在运行player4 else语句(用于空白时)。我已经尝试研究它,发现了Promise和async / await,但是我对它的理解还不足以使其适合我的情况,甚至无法应用。这是很多嵌套的if语句。我觉得循环会更好,但不确定如何正确执行。任何帮助和建议,我们将不胜感激。

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);
      }
    });
  }
模具

我希望代码能自我解释。我还没有测试getPlayerPic功能。我用来callback('default')测试。

如果您必须编写更多相同的代码,那么总是两次使它成为您多次运行的函数。该代码将更容易阅读,也更容易发现问题。

// function to get the player picture
function getPlayerPic(player, callback) {
    //return callback('default' + player);
    fs.access('./public/images/players/' + player, function(error) {
        if (error) return callback('default');
        fs.readdir('./public/images/players/' + data.player1, (error, files) => {
            callback(files);
        });
    });
};

// test data
var data = {
    teamNum: 'team1',
    player1: 'p1',
    player2: 'p2',
    player3: 'p3',
    player4: 'blank',
    player5: 'blank',
}

var team1 = {};

if (data.teamNum == 'team1') {
    team1.teamNum = 'team1';

    var players = Object.keys(data).filter(p => p.startsWith('player')).reverse();
    // players = [ 'player1', 'player2', 'player3', 'player4', 'player5' ];


    function next(callback) {
        var p = players.pop(); // pop an item from players
      console.log('p', p);
        if (p && data[p] && data[p] !== 'blank') // if it exists and is not blank
            getPlayerPic(data[p], function(pic){
                team1[p + 'pic'] = pic;
                next(callback);
            });
        else // we are done here
            callback();
    };

    next(function(){
        console.log('sending pics', team1);
        /*
         sending pics { teamNum: 'team1',
           player1pic: 'defaultp1',
           player2pic: 'defaultp2',
           player3pic: 'defaultp3' }
        */
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章