1
我的任務是解析最後100個dota2遊戲。我使用jotain的dota 2 API和小型庫(https://github.com/jiin/dota2api)Async.waterfall,foreach和解析JSON node.js.需要一些建議
我所有的,它是一個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場比賽,但我需要爲game_id「forEach」獲取詳細信息,並且需要爲player_id添加「forEach」,以獲取request.params.id中當前玩家的詳細信息。畢竟,我再次使用forEach來獲取摘要數據。這是正確的方式?)或者我真的愚蠢?)
可能你有一些想法來做到這一點更快?可能需要使用MongoDB(?)或其他的東西。
最後,我用統計生成簡單的圖像。
謝謝!