From 31b4764cd5233acb25f4d0027261fe83eaad6ec7 Mon Sep 17 00:00:00 2001 From: schneefux Date: Mon, 8 May 2017 23:15:33 +0200 Subject: rewrite the whole thing (wip) --- views/guild_create.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 views/guild_create.js (limited to 'views/guild_create.js') diff --git a/views/guild_create.js b/views/guild_create.js new file mode 100644 index 0000000..9720191 --- /dev/null +++ b/views/guild_create.js @@ -0,0 +1,48 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const View = require("./view"), + util = require("../util"), + api = require("../api"), + strings = require("../strings"), + oneLine = require("common-tags").oneLine; + +const GuildCreateView = module.exports; + +// match detail view +module.exports = class extends View { + constructor(msg, user_token) { + super(msg); + this.user_token = user_token; + } + + async text() { + return `Guild created. You can now use ${util.usg(this.msg, "vgadd ign1 ign2 ignN")} to add members to your Guild.`; + } + + async help() { + return `*${emoji.symbols.information_source} or ${util.usg(this.msg, "vgview")} to view your Guild*` + } + + async buttons() { + let reactions = {}; + reactions[emoji.symbols.information_source] = async () => { + util.trackAction(this.msg, "reaction-guildview"); + await new GuildOverviewView(this.user_token).respond(); + }; + return reactions; + } + + // TODO move to super class + async respond() { + this.response = await util.respond(this.msg, + await this.text(), this.response); + if (!this.hasButtons) { + await util.reactionButtons(this.response, + await this.buttons()); + this.hasButtons = true; + } + return this.response; + }; +} -- cgit v1.3.1 From 94adb2f9f182e50f6d60aadc70bb8bf8f74f2371 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 9 May 2017 13:42:45 +0200 Subject: more error handling --- commands/vainsocial/guild_create.js | 21 +++++++++++++-------- commands/vainsocial/guild_view.js | 8 +++++++- views/guild.js | 8 +------- views/guild_create.js | 5 +++-- 4 files changed, 24 insertions(+), 18 deletions(-) (limited to 'views/guild_create.js') diff --git a/commands/vainsocial/guild_create.js b/commands/vainsocial/guild_create.js index ad8d0e7..c28fe2a 100644 --- a/commands/vainsocial/guild_create.js +++ b/commands/vainsocial/guild_create.js @@ -48,13 +48,18 @@ Create a Guild with your VainSocial profile as leader. // register a VainSocial Guild to a Discord account async run(msg, args) { util.trackAction(msg, "vainsocial-guild-create"); - // TODO error handling - await api.post("/guild", { - shard_id: args.region, - name: args.name, - identifier: args.tag, - user_token: msg.author.id - }); - await new GuildCreateView(msg, msg.author.id).respond(); + const guildCreateView = new GuildCreateView(msg, msg.author.id); + try { + await api.post("/guild", { + shard_id: args.region, + name: args.name, + identifier: args.tag, + user_token: msg.author.id + }); + } catch (err) { + console.error(err); + return await guildCreateView.error(err.error.err); + } + await guildCreateView.respond(); } }; diff --git a/commands/vainsocial/guild_view.js b/commands/vainsocial/guild_view.js index 7deb66f..472b284 100644 --- a/commands/vainsocial/guild_view.js +++ b/commands/vainsocial/guild_view.js @@ -33,6 +33,12 @@ Show a summary of your Guild. // show Guild details async run(msg, args) { util.trackAction(msg, "vainsocial-guild-view"); - await new GuildOverviewView(msg, msg.author.id).respond(); + const guildOverviewView = new GuildOverviewView(msg); + try { + const guild = await api.getGuild(msg.author.id); + await guildOverviewView.respond(guild); + } catch (err) { + return await guildOverviewView.error(err.error.err); + } } }; diff --git a/views/guild.js b/views/guild.js index 1a4cb78..a5eedef 100644 --- a/views/guild.js +++ b/views/guild.js @@ -12,11 +12,6 @@ const GuildOverviewView = module.exports; // match detail view module.exports = class extends View { - constructor(msg, user_token) { - super(msg); - this.user_token = user_token; - } - async text(members) { // TODO remove when API supports order by fame members = members.sort((m1, m2) => m1.fame < m2.fame); @@ -31,8 +26,7 @@ module.exports = class extends View { return embed; }; - async respond() { - const guild = await api.getGuild(this.user_token); + async respond(guild) { if (guild == undefined) { this.response = await util.respond(this.msg, strings.notRegistered, this.response); diff --git a/views/guild_create.js b/views/guild_create.js index 9720191..8e7be81 100644 --- a/views/guild_create.js +++ b/views/guild_create.js @@ -2,7 +2,8 @@ /* jshint esnext:true */ "use strict"; -const View = require("./view"), +const emoji = require("discord-emoji"), + View = require("./view"), util = require("../util"), api = require("../api"), strings = require("../strings"), @@ -29,7 +30,7 @@ module.exports = class extends View { let reactions = {}; reactions[emoji.symbols.information_source] = async () => { util.trackAction(this.msg, "reaction-guildview"); - await new GuildOverviewView(this.user_token).respond(); + await new GuildOverviewView(this.msg).respond(this.user_token); }; return reactions; } -- cgit v1.3.1 From 0ee8e0e5239e3f76911834da54d2c72a4a149804 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 9 May 2017 18:02:15 +0200 Subject: error handling, migration to template --- api.js | 2 +- commands/vainsocial/guild_add.js | 2 +- commands/vainsocial/guild_update.js | 33 +++++++++++++++++++++++++++------ commands/vainsocial/guild_view.js | 2 ++ commands/vainsocial/me.js | 3 ++- views/guild.js | 11 +++-------- views/guild_create.js | 2 +- views/guild_progress.js | 33 +++++++++++++++++++++++++++++++++ views/guild_update.js | 34 ++++++++++++++++++++++++++++++++++ views/matches.js | 7 +++---- views/player.js | 19 ++++++++----------- views/register.js | 6 ++---- 12 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 views/guild_progress.js create mode 100644 views/guild_update.js (limited to 'views/guild_create.js') diff --git a/api.js b/api.js index 74614c2..c809309 100644 --- a/api.js +++ b/api.js @@ -200,7 +200,7 @@ module.exports.getGuild = async (token) => { // add user to guild module.exports.addToGuild = async (token, member) => { - const membership = await postFE("/guild/members", { + const membership = await api.postFE("/guild/members", { user_token: token, member_name: member }, "guild+" + token); diff --git a/commands/vainsocial/guild_add.js b/commands/vainsocial/guild_add.js index 8474724..8fe952c 100644 --- a/commands/vainsocial/guild_add.js +++ b/commands/vainsocial/guild_add.js @@ -33,7 +33,7 @@ Register IGNs to your Guild. await Promise.map(playersWaiters, async (waiter, idx) => { await api.upsearchPlayer(args[idx]); let success = false; - while (await waiter.next() != undefined) { + while (["stats_update", undefined].indexOf(await waiter.next())) { playersData[args[idx]] = await api.getPlayer(args[idx]); await guildAddView.respond(); success = true; diff --git a/commands/vainsocial/guild_update.js b/commands/vainsocial/guild_update.js index 66e8a5f..7d36b0e 100644 --- a/commands/vainsocial/guild_update.js +++ b/commands/vainsocial/guild_update.js @@ -26,21 +26,42 @@ Update the match history for all your Guild members. // internal / premium: immediately call backend player refresh async run(msg, args) { util.trackAction(msg, "vainsocial-guild-update"); + // obj of ign: player let playersData = {}; - const playersWaiters = args.map((name) => api.subscribeUpdates(name)), - guildUpdateView = new GuildMembersProgressView(msg, playersData); + // collect an array of IGNs + let names, guild; + const guildUpdateView = new GuildMembersProgressView(msg, + playersData); + try { + guild = await api.getGuild(msg.author.id); + names = guild.members.map((m) => m.player.name); + } catch (err) { + console.log(err); + return await guildUpdateView.error(err.error.err); + } + // update all the IGNs + const playersWaiters = names.map((name) => api.subscribeUpdates(name)); // create waiter dict & data dict await Promise.map(playersWaiters, async (waiter, idx) => { - await api.updatePlayer(args[idx]); + await api.updatePlayer(names[idx]); let success = false; - while (await waiter.next() != undefined) { - playersData[args[idx]] = await api.getPlayer(args[idx]); + while (["stats_update", undefined].indexOf(await waiter.next())) { + try { + playersData[names[idx]] = await api.getPlayer(names[idx]); + } catch (err) { + playersData[names[idx]] = undefined; + } await guildUpdateView.respond(); success = true; } }); await guildUpdateView.respond("Your Guild's fame is being updated…"); - await api.calculateGuild(guild.id, msg.author.id); + try { + await api.calculateGuild(guild.id, msg.author.id); + } catch (err) { + console.log(err); + await guildUpdateView.error(err.error.err); + } await guildUpdateView.respond("Your Guild was updated."); } }; diff --git a/commands/vainsocial/guild_view.js b/commands/vainsocial/guild_view.js index 472b284..c8375d4 100644 --- a/commands/vainsocial/guild_view.js +++ b/commands/vainsocial/guild_view.js @@ -5,6 +5,7 @@ const Commando = require("discord.js-commando"), oneLine = require("common-tags").oneLine, util = require("../../util"), + api = require("../../api"), GuildOverviewView = require("../../views/guild"); module.exports = class ViewGuildCommand extends Commando.Command { @@ -38,6 +39,7 @@ Show a summary of your Guild. const guild = await api.getGuild(msg.author.id); await guildOverviewView.respond(guild); } catch (err) { + console.log(err); return await guildOverviewView.error(err.error.err); } } diff --git a/commands/vainsocial/me.js b/commands/vainsocial/me.js index d2afb24..de87b4c 100644 --- a/commands/vainsocial/me.js +++ b/commands/vainsocial/me.js @@ -37,7 +37,8 @@ Store your in game name for quicker access to other commands and for Guild manag const registerView = new RegisterView(msg, args.name); await api.upsearchPlayer(args.name); try { - await api.subscribeUpdates(args.name).next(); + const waiter = api.subscribeUpdates(args.name); + while (await waiter.next() != "stats_update"); await api.setUser(msg.author.id, args.name); } catch (err) { console.log(err); diff --git a/views/guild.js b/views/guild.js index a5eedef..23c2afe 100644 --- a/views/guild.js +++ b/views/guild.js @@ -24,16 +24,11 @@ module.exports = class extends View { "", "vainsocial-guild-view") .setDescription(await this.text(guild.members)); return embed; - }; + } - async respond(guild) { - if (guild == undefined) { - this.response = await util.respond(this.msg, - strings.notRegistered, this.response); - return this.response; - } + async respond(guild, extra="") { this.response = await util.respond(this.msg, await this.embed(guild), this.response); return this.response; - }; + } } diff --git a/views/guild_create.js b/views/guild_create.js index 8e7be81..38720e1 100644 --- a/views/guild_create.js +++ b/views/guild_create.js @@ -38,7 +38,7 @@ module.exports = class extends View { // TODO move to super class async respond() { this.response = await util.respond(this.msg, - await this.text(), this.response); + await this.text() + "\n" + await this.help(), this.response); if (!this.hasButtons) { await util.reactionButtons(this.response, await this.buttons()); diff --git a/views/guild_progress.js b/views/guild_progress.js new file mode 100644 index 0000000..163c627 --- /dev/null +++ b/views/guild_progress.js @@ -0,0 +1,33 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const View = require("./view"), + util = require("../util"), + api = require("../api"), + strings = require("../strings"), + oneLine = require("common-tags").oneLine; + +const GuildMembersProgressView = module.exports; + +module.exports = class extends View { + constructor(msg, players) { + super(msg); + this.players = players; + } + + // players: obj, key=ign, value=player + async text(players) { + return Object.entries(players).map((tuple) => + (tuple[1] == undefined)? + `Loading ${tuple[0]}…` + : `Loaded ${tuple[0]}.` + ).join("\n"); + } + + async respond(extra="") { + this.response = await util.respond(this.msg, + await this.text(this.players) + "\n" + extra, this.response); + return this.response; + }; +} diff --git a/views/guild_update.js b/views/guild_update.js new file mode 100644 index 0000000..cb32dc3 --- /dev/null +++ b/views/guild_update.js @@ -0,0 +1,34 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const View = require("./view"), + util = require("../util"), + api = require("../api"), + strings = require("../strings"), + oneLine = require("common-tags").oneLine; + +const GuildAddView = module.exports; + +// match detail view +module.exports = class extends View { + constructor(msg, players) { + super(msg); + this.players = players; + } + + // players: obj, key=ign, value=player + async text(players) { + return Object.entries(players).map((tuple) => + (tuple[1] == undefined)? + `Loading ${tuple[0]}…` + : `Loaded ${tuple[0]}.` + ).join("\n"); + } + + async respond() { + this.response = await util.respond(this.msg, + await this.text(this.players), this.response); + return this.response; + }; +} diff --git a/views/matches.js b/views/matches.js index 279bf9c..a79654b 100644 --- a/views/matches.js +++ b/views/matches.js @@ -39,8 +39,8 @@ Score | ${emojiScore} \`${Math.floor(100 * participant.stats.impact_score)}%\` const matchesPart = matches.slice(0, MATCH_HISTORY_LEN); // build embed - let embed = util.vainsocialEmbed( - this.ign, "player/" + this.ign, "vainsocial-matches") + let embed = util.vainsocialEmbed(this.ign, + "player/" + this.ign, "vainsocial-matches") .setDescription(`Last ${matchesPart.length} casual and ranked matches.\n` + await this.help()) .setTimestamp(new Date(matchesPart[0].created_at)); @@ -60,9 +60,8 @@ Score | ${emojiScore} \`${Math.floor(100 * participant.stats.impact_score)}%\` let reactions = {}; matchesPart.forEach((m, idx) => reactions[strings.emojiCount[idx]] = async () => { - console.log("react"); util.trackAction(this.msg, "reaction-match", m.match_api_id); - await new MatchView(this.msg, m.match_api_id).respond(); + await new MatchView(this.msg).respond(m.match_api_id); }); return reactions; } diff --git a/views/player.js b/views/player.js index ec5d38b..a481c69 100644 --- a/views/player.js +++ b/views/player.js @@ -72,7 +72,7 @@ ${emoji.symbols["1234"]} or ${util.usg(this.msg, "vh " + this.ign)} for more*`; let reactions = {}; reactions[emoji.symbols.information_source] = async () => { util.trackAction(this.msg, "reaction-match", player.name); - await new MatchView(this.msg, matches[0].match_api_id).respond(); + await new MatchView(this.msg).respond(matches[0].match_api_id); }; reactions[emoji.symbols["1234"]] = async () => { util.trackAction(this.msg, "reaction-matches", player.name); @@ -82,16 +82,13 @@ ${emoji.symbols["1234"]} or ${util.usg(this.msg, "vh " + this.ign)} for more*`; } async respond() { - const [player, matches] = await Promise.all([ - api.getPlayer(this.ign), - api.getMatches(this.ign) - ]); - if (player == undefined) { - this.response = await util.respond(this.msg, - strings.loading(this.ign), this.response); - return this.response; - } - if (matches.length == 0) { + let player, matches; + try { + [player, matches] = await Promise.all([ + api.getPlayer(this.ign), + api.getMatches(this.ign) + ]); + } catch (err) { this.response = await util.respond(this.msg, strings.loading(this.ign), this.response); return this.response; diff --git a/views/register.js b/views/register.js index 9943299..1df3bf1 100644 --- a/views/register.js +++ b/views/register.js @@ -19,7 +19,7 @@ module.exports = class extends View { } async text() { - return `You are now registered at VainSocial, ${this.msg.author.mention}.`; + return `You are now registered at VainSocial, @${this.msg.author.tag}.`; } async help() { return `*${emoji.symbols.repeat} or ${util.usg(this.msg, "v")} to view your profile, ${util.usg(this.msg, "vgcreate")} to create a Guild*` @@ -33,10 +33,8 @@ module.exports = class extends View { return reactions; } async respond() { - console.log("************************"); - console.log(await this.text()); this.response = await util.respond(this.msg, - await this.text(), this.response); + await this.text() + "\n" + await this.help(), this.response); if (!this.hasButtons) { await util.reactionButtons(this.response, await this.buttons()); -- cgit v1.3.1