diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2017-04-15 17:07:20 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2017-04-15 17:07:20 +0200 |
| commit | 795126408fc717a11d832f412470597377d71449 (patch) | |
| tree | e81700812c75453d1fcf80c296e0a07b84ae449c /api.js | |
| parent | 0564241327ecc6eb08cd716f510ebb0afbb3eeb3 (diff) | |
| download | discordbot-795126408fc717a11d832f412470597377d71449.tar.gz discordbot-795126408fc717a11d832f412470597377d71449.zip | |
implement basic functionality
Diffstat (limited to 'api.js')
| -rw-r--r-- | api.js | 70 |
1 files changed, 42 insertions, 28 deletions
@@ -11,63 +11,77 @@ const API_FE_URL = process.env.API_FE_URL || "http://vainsocial.dev/bots/api", API_WS_URL = process.env.API_WS_URL || "ws://vainsocial.dev/ws", API_BE_URL = process.env.API_BE_URL || "http://vainsocial.dev/bridge"; -const ws = new WebSocket(API_WS_URL, { perMessageDeflate: false }), - notif = webstomp.over(ws); +const notif = webstomp.over(new WebSocket(API_WS_URL, + { perMessageDeflate: false })); -function connect() { +(function connect() { notif.connect("web", "web", () => console.log("connected to queue"), - (err) => console.error("error connecting to queue", err)); - keepalive(); -} - -function keepalive() { - ws.ping("", false, true); - setTimeout(this, 60000); -} - -ws.on("ready", connect); + (err) => connect() + ); +})(); // TODO use keepalive / connection pool -async function getFE(url) { - return await request({ +function getFE(url) { + return request({ uri: API_FE_URL + url, json: true }); } -async function postBE(url) { - return await request.post({ +function postBE(url) { + return request.post({ uri: API_BE_URL + url, json: true }); } function subscribe(topic, channel) { - notif.subscribe("/topic/" + topic, async (msg) => { - await channel.put(msg.body); + return notif.subscribe("/topic/" + topic, (msg) => { + channel.put(msg.body); msg.ack(); }, {"ack": "client"}); } -module.exports.searchPlayer = async function (name, timeout=30) { - let channel = new Channel(); - - subscribe("player." + name, channel); - await postBE("/player/" + name + "/search"); - setTimeout(() => channel.close(), timeout*1000); - channel.put(""); // initial fetch +// be an async generator +// next() returns player data whenever an update is available +module.exports.searchPlayer = async function (name, timeout=60) { + let channel = new Channel(), + subscription = subscribe("player." + name, channel); + await postBE("/player/" + name + "/update"); + // stop updates after timeout + setTimeout(() => { + channel.close(); + subscription.unsubscribe(); + }, timeout*1000); + channel.put("initial"); // initial fetch let generator = async () => { - let msg = await channel.take(); + let msg; + while (true) { + msg = await channel.take(); + if (["initial", "search_fail", "stats_update", + "matches_update"].indexOf(msg) != -1) + break; + } if (msg == "search_fail") { throw "not found"; } if (msg == Channel.DONE) { throw "exhausted"; } - return await getFE("/player/" + name); + return getFE("/player/" + name); } return { next: generator }; } + +// return matches +module.exports.searchMatches = async function (name) { + return getFE("/player/" + name + "/matches/1.1.1.1"); +} + +// return single match +module.exports.searchMatch = async function (id) { + return getFE("/match/" + id); +} |
