nodejs irc.js
- discard old messages
- make it a proper nodejs pacakge with dependencies
#!/usr/bin/env node | |
var EventSource = require('eventsource'); | |
var cfgPath = require('confortable')('stream.json', process.cwd()); | |
if (!cfgPath) { | |
throw new Error("Could not load stream.json"); | |
} | |
var cfg = require(cfgPath); | |
var ircStream = require('irc-stream')(cfg.irc.server, cfg.irc.name, { | |
userName: 'ActionFPS_Bot', | |
realName: 'actionfps.com', | |
debug: false, | |
channels: cfg.irc.chan | |
}, { conversationMode: true }); | |
function countPlayers(server) | |
{ | |
var playerCount = 0; | |
if('game' in server) | |
{ | |
if('teams' in server['game']) for(var team_index in server['game']['teams']) { | |
var team = server['game']['teams'][team_index]; | |
playerCount += team['players'].length; | |
if('spectators' in team) playerCount += team['spectators'].length; | |
} | |
if('players' in server['game']) | |
playerCount += server['game']['players'].length; | |
if('spectators' in server['game']) | |
playerCount += server['game']['spectators'].length; | |
} | |
return playerCount; | |
} | |
var es = new EventSource(cfg.source); | |
var ses = new EventSource(cfg.servers_source); | |
var servers = new Array(); | |
es.addEventListener("inter", function(event) { | |
var inter = JSON.parse(event.data); | |
console.log(inter['playerName'] + ' ' + inter['serverConnect']); | |
var msg = ''; | |
if(inter['serverConnect'] in servers) | |
{ | |
var server = servers[inter['serverConnect']]; | |
var playerCount = countPlayers(server); | |
msg = inter['playerName'] + ' started an inter and is looking for players on ' + inter['serverName'] + ' (/connect ' + server['connectName'] + '), ' + playerCount + ' players online'; | |
} | |
else | |
msg = inter['playerName'] + ' started an inter and is looking for players on ' + inter['serverName']; | |
ircStream.write({ message: msg, user: cfg.irc.chan }); | |
}); | |
ses.addEventListener("server-status", function(event) { | |
var server = JSON.parse(event.data); | |
//console.log(server['canonicalName']); | |
servers['assaultcube://' + server['canonicalName']] = server; | |
}); |
{ | |
"irc": { | |
"name": "ActionFPS", | |
"server": "irc.gamesurge.net", | |
"chan": [ "#woop-clan", "#paradoxinfinity" ] | |
}, | |
"source": "http://actionfps.com/inters/", | |
"servers_source": "http://actionfps.com/server-updates/" | |
} |