summaryrefslogtreecommitdiff
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
parent0e5a0b876aa36786430d313f8ded843aa849eb00 (diff)
downloaddiscordbot-a5bd191b194ca97afd777a4bb523981e4855df92.tar.gz
discordbot-a5bd191b194ca97afd777a4bb523981e4855df92.zip
refactor, fix caching
-rw-r--r--api.js60
-rw-r--r--commands/vainsocial/guild_add.js5
-rw-r--r--commands/vainsocial/guild_view.js2
-rw-r--r--responses.js9
-rw-r--r--util.js5
5 files changed, 59 insertions, 22 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();
+}
diff --git a/commands/vainsocial/guild_add.js b/commands/vainsocial/guild_add.js
index 8dc2ab8..b4aa864 100644
--- a/commands/vainsocial/guild_add.js
+++ b/commands/vainsocial/guild_add.js
@@ -62,10 +62,7 @@ Register IGNs to your Guild.
}
// all good, register to self guild
- await api.post("/guild/members", {
- user_token: msg.author.id,
- member_name: user
- });
+ await api.addToGuild(msg.author.id, user);
await progress(`Added ${user}.`, true);
});
await progress(oneLine`
diff --git a/commands/vainsocial/guild_view.js b/commands/vainsocial/guild_view.js
index e692402..835b717 100644
--- a/commands/vainsocial/guild_view.js
+++ b/commands/vainsocial/guild_view.js
@@ -34,7 +34,7 @@ Show a summary of your Guild.
async run(msg, args) {
util.trackAction(msg, "vainsocial-guild-view");
// get this user's guild
- const guild = await api.get("/guild", { user_token: msg.author.id });
+ const guild = await api.getGuild(msg.author.id);
if (guild == undefined) {
await msg.reply("You are not registered in any guilds.");
return;
diff --git a/responses.js b/responses.js
index ef749eb..a2eb83d 100644
--- a/responses.js
+++ b/responses.js
@@ -141,7 +141,8 @@ module.exports.showUser = async (msg, args) => {
}
}
- const waiter = await api.subscribeUpdates(ign, true);
+ const waiter = api.subscribeUpdates(ign);
+ await api.upsearchPlayer(ign);
do {
const [player, matches] = await Promise.all([
api.getPlayer(ign),
@@ -220,7 +221,8 @@ module.exports.showMatch = async (msg, args) => {
}
}
- const waiter = await api.subscribeUpdates(ign, true);
+ const waiter = await api.subscribeUpdates(ign);
+ await api.upsearchPlayer(ign);
do {
const matches = await api.getMatches(ign);
if (index > matches.length) {
@@ -293,7 +295,8 @@ module.exports.showMatches = async (msg, args) => {
}
}
- const waiter = await api.subscribeUpdates(ign, true);
+ const waiter = await api.subscribeUpdates(ign);
+ await api.upsearchPlayer(ign);
do {
response = await respondMatches(msg, ign, response);
responded = true;
diff --git a/util.js b/util.js
index f2e894b..a1deaf7 100644
--- a/util.js
+++ b/util.js
@@ -99,8 +99,9 @@ module.exports.respond = async (msg, data, response) => {
if (data != response.content)
await response.edit(data);
} else {
- if (new Date(response.embeds[0].createdTimestamp).getTime()
- != data.timestamp.getTime())
+ if (response.embeds.length ==0 ||
+ new Date(response.embeds[0].createdTimestamp).getTime()
+ != data.timestamp.getTime())
// TODO how2 edit embed properly?!
await msg.editResponse(response, {
type: "plain",