1

我的任务是解析最后 100 场 dota2 游戏。我使用来自 jiiin ( https://github.com/jiin/dota2api )的 dota 2 API 和小库

我所有的,这是一个 async.waterfall 调用。像这样:

exports.get = function(req, res, next) {
var playerID = +req.params.id;
var playerCounter = [];
var playerInfo = {
    kills: [],
    deaths: [],
    assists: [],
    last_hits: [],
    denies: [],
    hero_damage: [],
    hero_healing: [],
    gold_spent: [],
    kills_number: 0,
    deaths_number: 0,
    assists_number: 0
};

async.waterfall([

    function getDota2Json(callback) {
        dota.getByAccountID(playerID, function (err, result) {
            callback(err, result);
        });
    },
    function getMatches(result, callback) {
        result.matches.forEach(function (match) {
            callback(null, match.match_id);
        });
    },
    function getMatchInfo(matchID, callback) {
        dota.getMatchDetails(matchID, function (err, result) {
            callback(err, result.players);
        });
    },
    function getCurrentPlayer(players, callback) {
        players.forEach(function (player) {
            if (player.account_id === playerID) {
                callback(null, player);
            }
        });
    },
    function getDamage(player, callback) {
        callback(null,
                player.kills,
                player.deaths,
                player.assists,
                player.last_hits,
                player.denies,
                player.hero_damage,
                player.hero_healing,
                player.gold_spent,
                '1');
    }
], function (err, kills, deaths, assists, last_hits, denies, hero_damage, hero_healing, gold_spent, counter) {
    playerCounter.push(counter);

    playerInfo.kills.push(kills);
    playerInfo.deaths.push(deaths);
    playerInfo.assists.push(assists);

    console.log(playerCounter.length);

    if (playerCounter.length === 100) {

        playerInfo.kills.forEach(function (val) {
            playerInfo.kills_number += val;
        });
        console.log('Last 100 K ' + playerInfo.kills_number);

        playerInfo.deaths.forEach(function (val) {
            playerInfo.deaths_number += val;
        });
        console.log('Last 100 D ' + playerInfo.deaths_number);

        playerInfo.assists.forEach(function (val) {
            playerInfo.assists_number += val;
        });
        console.log('Last 100 A ' + playerInfo.assists_number);


        var magickOpts = [
            "-background", "grey60",
            "-bordercolor", "snow",
            "-border", "6",
            "-fill", "black",
            "-pointsize", "50",
            "label: Dota 2 LAST 100 \n K - D - A " + playerInfo.kills_number + ' - ' + playerInfo.deaths_number + ' - ' + playerInfo.assists_number,
            ""+playerID+".png"
        ];
        var im = spawn('convert', magickOpts);
    }
});

res.end('rdy');

};

我认为这不是好的代码,但我找不到替代方案。Dota API 用 JSON 格式让我玩了 100 场游戏,但我需要在 request.params.id 中使用带有 game_id 的“forEach”获取详细信息,使用 player_id 使用“forEach”获取当前玩家的详细信息。毕竟,我再次将 forEach 用于汇总数据。这是正确的方式?)还是我很愚蠢?)

也许你有一些想法可以更快地做到这一点?可能需要使用 MongoDB (?) 或其他东西。

最后,我生成带有统计信息的简单图像。

谢谢!

4

1 回答 1

1

我觉得你也许可以使用async.each来简化你的一些forEach调用......

这可能是一个小问题,但要获得一个数字数组的总和,您可以使用数组的reduce方法。

playerInfo.kills_number = playerInfo.kills.reduce(function(a, b) {
  return a + b;
});
于 2013-11-02T13:32:52.807 回答