UNPKG

dota2-radiant

Version:

Radiant: a small little library/framework for building awesome apps backed by the Dota 2 WebAPI. Utilizes Node with Express

69 lines (56 loc) 1.85 kB
var async = require('async'); var _ = require('underscore'); var stringUtils = require('underscore.string'); var radiant = require('../dota2-radiant'); var app = radiant.app; var MatchesController = { 'index': function (req, res) { // we need to get matches & heroes so we can populate the hero images. async.series([ function (next) { radiant.dazzle.getMatchHistory(function (err, result) { next(null, result.matches); }); }, function (next) { radiant.dazzle.getHeroes(function (err, result) { next(null, result.heroes); }); } ], function (err, context) { var matches = context[0], heroes = context[1]; heroes = formatHeroes(heroes); matches = formatMatches(matches, heroes); res.render('matches', { matches: matches }); }); } }; function formatHeroes(heroes) { _.each(heroes, function (hero) { hero.clean_name = hero.name.replace('npc_dota_hero_', ''); hero.slug = stringUtils.slugify(hero.clean_name); }); return heroes; } function formatMatches(matches, heroes) { _.each(matches, function (match) { var dire = []; var radiant = []; _.each(match.players, function (player) { var hero_id = player.hero_id; var hero = _.findWhere(heroes, { id : hero_id }); player.hero = hero; if (player.player_slot > 4) { dire.push(player); } else { radiant.push(player); } }); match.dire = dire; match.radiant = radiant; }); return matches; } module.exports = exports = MatchesController;