summaryrefslogtreecommitdiff
path: root/api.js
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2017-05-05 18:03:04 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2017-05-05 18:03:04 +0200
commita5bd191b194ca97afd777a4bb523981e4855df92 (patch)
tree8242a790df39813187b8ba84d5985e90620a0890 /api.js
parent0e5a0b876aa36786430d313f8ded843aa849eb00 (diff)
downloaddiscordbot-a5bd191b194ca97afd777a4bb523981e4855df92.tar.gz
discordbot-a5bd191b194ca97afd777a4bb523981e4855df92.zip
refactor, fix caching
Diffstat (limited to 'api.js')
-rw-r--r--api.js60
1 files changed, 48 insertions, 12 deletions
diff --git a/api.js b/api.js
index 598b1eb..9416486 100644
--- a/api.js
+++ b/api.js
@@ -50,12 +50,15 @@ async function getFE(url, params={}, ttl=60, cachekey=undefined) {
forever: true
});
} catch (err) {
+ // TODO sort errors, loggly
return undefined;
}
}, { ttl: ttl });
}
-async function postFE(url, params={}) {
+// send a POST and optionally bust cache
+async function postFE(url, params={}, cachekey=undefined) {
+ if (cachekey) cache.del(cachekey);
return await request.post(API_FE_URL + url, {
form: params,
json: true,
@@ -63,9 +66,6 @@ async function postFE(url, params={}) {
});
}
-module.exports.get = getFE;
-module.exports.post = postFE;
-
function postBE(url) {
return request.post({
uri: API_BE_URL + url,
@@ -74,6 +74,10 @@ function postBE(url) {
});
}
+module.exports.get = getFE;
+module.exports.post = postFE;
+module.exports.backend = postBE;
+
function subscribe(topic, channel) {
return notif.subscribe("/topic/" + topic, (msg) => {
channel.put(msg.body);
@@ -118,17 +122,10 @@ module.exports.getGamers = async () =>
// be an async iterator
// next() returns promises that are awaited until there is an update
-module.exports.subscribeUpdates = async (name, refresh=true, timeout=UPDATE_TIMEOUT) => {
+module.exports.subscribeUpdates = (name, timeout=UPDATE_TIMEOUT) => {
const channel = new Channel(),
subscription = subscribe("player." + name, channel);
- if (refresh) {
- // trigger backend
- if (await module.exports.getPlayer(name) == undefined)
- await module.exports.searchPlayer(name);
- else await module.exports.updatePlayer(name);
- }
-
// stop updates after timeout
setTimeout(() => channel.close(), timeout*1000);
@@ -164,6 +161,13 @@ module.exports.updatePlayer = (name) =>
module.exports.getPlayer = (name) =>
getFE("/player/" + name, {}, 60, "player+" + name);
+// search or update a player
+module.exports.upsearchPlayer = async (name) => {
+ if (await module.exports.getPlayer(name) == undefined)
+ await module.exports.searchPlayer(name);
+ else await module.exports.updatePlayer(name);
+}
+
// return matches
module.exports.getMatches = async (name) => {
const data = await getFE("/player/" + name + "/matches/2.3.1.1", {},
@@ -175,3 +179,35 @@ module.exports.getMatches = async (name) => {
// return single match
module.exports.getMatch = (id) =>
getFE("/match/" + id, {}, 60 * 60);
+
+// return a guild
+module.exports.getGuild = (token) =>
+ getFE("/guild", { user_token: token }, 60, "guild+" + token);
+// TODO! cache guilds by guild id, not by user token
+
+// add user to guild
+module.exports.addToGuild = async (token, member) => {
+ const membership = await postFE("/guild/members", {
+ user_token: token,
+ member_name: member
+ }, "guild+" + token);
+ cache.del("guild+" + token);
+ return membership;
+}
+
+// recalc fame, block until timeout or points update
+module.exports.calculateGuild = async (id, token) => {
+ const channel = new Channel(),
+ subscription = subscribe("global", channel);
+ await module.exports.backend("/team/" + id + "/crunch");
+
+ // stop updates after timeout
+ setTimeout(() => channel.close(), UPDATE_TIMEOUT*1000);
+
+ let msg;
+ do msg = await channel.take();
+ while([Channel.DONE, "points_update"].indexOf(msg) == -1);
+
+ if (msg == "points_update") cache.del("guild+" + token);
+ subscription.unsubscribe();
+}