Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active July 10, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/77122616d04c185e718f58d5622f9c09 to your computer and use it in GitHub Desktop.
Save donpdonp/77122616d04c185e718f58d5622f9c09 to your computer and use it in GitHub Desktop.
gluon veloren stats
(function() {
setup()
return {
name: "veloren"
}
})
var dbkey = 'veloren'
var blob = {users: 0}
function setup() {
db.get(dbkey, function(json) {
try {
blob = JSON.parse(json)
bot.say(bot.admin_channel, "veloren blob loaded "+JSON.stringify(blob))
} catch(e) {
bot.say(bot.admin_channel, "veloren blob established")
db.set(dbkey, JSON.stringify(blob))
}
})
}
function go(msg) {
if (msg.method == "clocktower") {
var time = new Date(Date.parse(msg.params.time))
if (time.getMinutes() % 30 == 0) {
var counts = count()
compare(counts)
}
}
if(msg.method == "irc.privmsg") {
var slope_match = /^\!veloren(\s+(.+))?$/.exec(msg.params.message)
if(slope_match){
var counts = count()
var report_time = new Date(counts[0]*1000)
var delay_sec = (now - report_time)/1000
var say = "veloren has "+counts[1]+" users online "
if (delay_sec > 60) {
say = say + delay_sec+" seconds ago"
}
bot.say(msg.params.channel, say)
compare(counts)
}
}
}
function count() {
now = new Date()
var start = now.getTime()-1000*60
var end = now.getTime()
var url = 'https://grafana.veloren.net/api/datasources/proxy/1/api/v1/query_range?'+
'query=clients_connected%7Bjob%3D%22veloren-game-server%22%7D+-+on+%28job%29+%28sum+by+%28job%29+%28clients_disconnected%7Bjob%3D%22veloren-game-server%22%7D%29%29&'+
'start='+(start/1000).toFixed(0)+'&end='+(end/1000).toFixed(0)+'&step=1'
//bot.say(msg.params.channel, url)
var data = JSON.parse(http.get(url))
var values = data.data.result[0].values
var counts = values[values.length - 1] // last one
return counts
}
function compare(counts) {
var users = parseInt(counts[1])
var olddate = new Date(blob.date*1000)
if (users > blob.users) {
bot.say("#pdxbots", "veloren new high user count of "+users+"! (was "+blob.users+" on "+olddate+")")
blob.users = users
blob.date = counts[0]
save()
} else {
//bot.say(bot.admin_channel, "veloren users "+users+" is less than high of "+blob.users+" "+olddate)
}
}
function save() {
db.set(dbkey, JSON.stringify(blob))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment