-
-
Save timvdalen/41c90d7eac4bdd282d3cf905c2db5629 to your computer and use it in GitHub Desktop.
kamergotchi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var mainloop, checkstatus, care, claim, getOptions, HOST, TOKEN, https = require('https'), sleep = require('sleep'); | |
HOST = 'api.kamergotchi.nl'; | |
TOKEN = ''; | |
getOptions = function (method, path) { | |
var options = { | |
host: HOST, | |
headers: { | |
'x-player-token': TOKEN, | |
'User-Agent': 'Tim van Dalen <tim@code-orange.nl>' | |
}, | |
method: method, | |
path: path | |
}; | |
if (method === 'POST') { | |
options.headers['Content-Type'] = 'application/json'; | |
} | |
return options; | |
}; | |
care = function (type) { | |
var req = https.request(getOptions('POST', '/game/care'), function (res) { | |
res.on('data', function (data) { | |
checkstatus(); | |
}); | |
}); | |
req.on('error', function (e) { | |
console.error(e); | |
}); | |
req.write(JSON.stringify({bar: type})); | |
req.end(); | |
}; | |
claim = function () { | |
var req = https.request(getOptions('POST', '/game/claim'), function (res) { | |
res.on('data', function (data) { | |
console.log("Claimed!"); | |
}); | |
}); | |
req.on('error', function (e) { | |
console.error(e); | |
}); | |
req.end(); | |
}; | |
checkstatus = function () { | |
var req = https.request(getOptions('GET', '/game'), function (res) { | |
res.on('data', function (data) { | |
data = JSON.parse(data); | |
var reset = new Date(data.game.careReset), now = new Date(), scores, min = [null, 101], cur, claimReset = new Date(data.game.claimReset); | |
if (claimReset < now) { | |
claim(); | |
} | |
if ((reset > now && data.game.careLeft > 0) || reset < now) { | |
console.log("We can care +" + data.game.careLeft); | |
scores = data.game.current; | |
for (cur in scores) { | |
if (scores.hasOwnProperty(cur)) { | |
if (scores[cur] < min[1]) { | |
min[0] = cur; | |
min[1] = scores[cur]; | |
} | |
} | |
} | |
care(min[0]); | |
} else { | |
console.log("Sleeping - " + (reset - now) / 1000 + " - seconds"); | |
if ((reset - now) > 0) { | |
sleep.msleep(reset - now); | |
} | |
checkstatus(); | |
} | |
}); | |
}); | |
req.on('error', function (e) { | |
console.error(e); | |
}); | |
req.end(); | |
}; | |
mainloop = function () { | |
checkstatus(); | |
}; | |
mainloop(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment