Skip to content

Instantly share code, notes, and snippets.

@rjackson
Last active December 29, 2015 20:48
Show Gist options
  • Save rjackson/7725639 to your computer and use it in GitHub Desktop.
Save rjackson/7725639 to your computer and use it in GitHub Desktop.
mmstats_updater
// Configure and mv to config.js
var config = {};
config.steam_name = "";
config.steam_user = "";
config.steam_pass = "";
config.steam_guard_code = "";
config.mm_stats_check_interval = 1 * 60 * 1000;
module.exports = config;
var steam = require("steam"),
util = require("util"),
fs = require("fs"),
Db = require('mongodb').Db,
Server = require('mongodb').Server,
dota2 = require("dota2"),
bot = new steam.SteamClient(),
Dota2 = new dota2.Dota2Client(bot, true),
config = require("./config"),
cwd = config.cwd;
var matchmakingStatsInterval;
db = new Db('mmstats', new Server("localhost", 27017, {auto_reconnect: true}), {w: 0});
db.open(function(){});
/* Steam logic */
var onSteamLogOn = function onSteamLogOn(){
bot.setPersonaState(steam.EPersonaState.Busy); // to display your bot's status as "Online"
bot.setPersonaName(config.steam_name); // to change its nickname
util.log("Logged on.");
Dota2.launch();
Dota2.on("ready", function() {
if (!matchmakingStatsInterval) {
matchmakingStatsInterval = setInterval(function(){
Dota2.matchmakingStatsRequest();
}, config.mm_stats_check_interval);
}
});
Dota2.on("unready", function(){
if (matchmakingStatsInterval) {
matchmakingStatsInterval = null;
clearInterval(matchmakingStatsInterval);
}
});
Dota2.on("matchmakingStatsData", function (waitTimesByGroup, searchingPlayersByGroup, disabledGroups, matchmakingStatsResponse) {
db.collection('mm_stats', function(err, mmStatsCollection) {
if(err) throw err;
//console.log(JSON.stringify(matchmakingStatsResponse));
mmStatsCollection.insert(
{"wait_times_by_group": waitTimesByGroup,
"searching_players_by_group": searchingPlayersByGroup,
"disabled_groups": disabledGroups,
"timestamp": (Date.now() / 1000)
}, function(err){ console.log(err); });
});
});
Dota2.on("unhandled", function(kMsg) {
util.log("UNHANDLED MESSAGE " + kMsg);
});
},
onSteamSentry = function onSteamSentry(sentry) {
util.log("Received sentry.");
require('fs').writeFileSync(cwd + 'sentry', sentry);
},
onSteamServers = function onSteamServers(servers) {
util.log("Received servers.");
fs.writeFile(cwd + 'servers', JSON.stringify(servers));
};
var logOnDetails = {
"accountName": config.steam_user,
"password": config.steam_pass
},
sentry = fs.readFileSync(cwd + "sentry");
if (config.steam_guard_code) logOnDetails.authCode = config.steam_guard_code;
if (sentry.length) logOnDetails.shaSentryfile = sentry;
bot.logOn(logOnDetails);
bot.on("loggedOn", onSteamLogOn)
.on('sentry', onSteamSentry)
.on('servers', onSteamServers);
{
"name": "mmstatsgatherer",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node index"
},
"dependencies": {
"steam": ">= 0.6.0",
"vdf": "*",
"deferred": "*",
"dota2": "https://github.com/RJacksonm1/node-dota2/tarball/gcAutoReconnect",
"mongodb": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment