私の仕事は、最後の 100 の dota2 ゲームを解析することです。dota 2 API と jiiin ( https://github.com/jiin/dota2api )の小さなライブラリを使用しています。
私が持っているすべて、それは 1 つの 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 (?) などを使用する必要があるかもしれません。
最後に、統計を使用して単純な画像を生成します。
ありがとうございました!