summaryrefslogtreecommitdiff
path: root/views/player.js
blob: a481c695718877e0d18586f9ec2899bef38f3ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/node
/* jshint esnext:true */
"use strict";

const emoji = require("discord-emoji"),
    View = require("./view"),
    MatchView = require("./match"),
    MatchesView = require("./matches"),
    util = require("../util"),
    api = require("../api"),
    strings = require("../strings"),
    oneLine = require("common-tags").oneLine;

const PREVIEW = process.env.PREVIEW != "false",
    ROOTURL = (PREVIEW? "https://preview.vainsocial.com/":"https://vainsocial.com/");

const PlayerView = module.exports;

// player profile
module.exports = class extends View {
    constructor(msg, ign) {
        super(msg);
        this.ign = ign;
    }

    async text(player) {
        const stats = oneLine`
                Win Rate | \`${Math.round(100 *
                player.currentSeries.reduce((t, s) => t + s.wins, 0) /
                player.currentSeries.reduce((t, s) => t + s.played, 0)
            )}%\`
            `,
            total_kda = oneLine`
                Total KDA | \`${player.stats.kills}\` / \`${player.stats.deaths}\` / \`${player.stats.assists}\`
            `;
        let best_hero = "",
            picks = "";
        if (player.best_hero.length > 0)
            best_hero = oneLine`
                Best | \`${player.best_hero[0].name}\`
            `;
        if (player.picks.length > 0) {
            const hero = await api.mapActor(player.picks[0].actor);
            picks = oneLine`Favorite | \`${hero}\`, \`${player.picks[0].hero_pick} picks\``;
        }
        return `
${stats}
${total_kda}
${best_hero}
${picks}
        `;
    }

    async embed(player, matches) {
        return util.vainsocialEmbed(`${player.name} - ${player.shard_id}`, "player/" + player.name, "vainsocial-user")
            .setThumbnail(ROOTURL + "images/game/skill_tiers/" +
                matches[0].skill_tier + ".png")
            .setDescription("")
            .addField(strings.profile, await this.text(player), true)
            .addField(strings.lastMatch, await new MatchesView().text(matches[0]) +
                "\n" + await this.help(), true)
            .setTimestamp(new Date(matches[0].created_at));
    };

    async help() {
        return oneLine`
*${emoji.symbols.information_source} or ${util.usg(this.msg, "vm " + this.ign)} for detail,
${emoji.symbols["1234"]} or ${util.usg(this.msg, "vh " + this.ign)} for more*`;
    }

    async buttons(player, matches) {
        let reactions = {};
        reactions[emoji.symbols.information_source] = async () => {
            util.trackAction(this.msg, "reaction-match", player.name);
            await new MatchView(this.msg).respond(matches[0].match_api_id);
        };
        reactions[emoji.symbols["1234"]] = async () => {
            util.trackAction(this.msg, "reaction-matches", player.name);
            await new MatchesView(this.msg, player.name).respond();
        };
        return reactions;
    }

    async respond() {
        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;
        }
        this.response = await util.respond(this.msg,
            await this.embed(player, matches), this.response);
        if (!this.hasButtons) {
            await util.reactionButtons(this.response,
                await this.buttons(player, matches));
            this.hasButtons = true;
        }
        return this.response;
    }
}