Skip to content

Commit

Permalink
feat: riven stats from the api
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Apr 30, 2019
1 parent 340c986 commit 77b1ac2
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commands.json

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions src/commands/Ondemand/RivenStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const fetch = require('node-fetch');

const RivenStatEmbed = require('../../embeds/RivenStatEmbed');
const Command = require('../../models/Command.js');
const { apiBase, setupPages } = require('../../CommonFunctions');

/**
* Displays the stats for a warframe
*/
class FrameStats extends Command {
/**
* Constructs a callable command
* @param {Genesis} bot The bot object
*/
constructor(bot) {
super(bot, 'warframe.misc.rivens', 'riven', 'Get current riven sales stats');
this.regex = new RegExp(`^${this.call}\\s?(.+)?`, 'i');
this.usages = [
{
description: 'Get stats for a Weapon riven',
parameters: ['riven name'],
},
];
}

/**
* Run the command
* @param {Message} message Message with a command to handle, reply to,
* or perform an action based on parameters.
* @param {CommandContext} ctx Command Context object, with settings, platform, locale, etc
* @returns {string} success status
*/
async run(message, ctx) {
let weapon = message.strippedContent.match(this.regex)[1];

if (weapon) {
weapon = weapon.trim().toLowerCase();
const results = await fetch(`${apiBase}/${ctx.platform}/rivens/search/${weapon}`).then(data => data.json());
if (Object.keys(results).length > 0) {
const pages = [];

Object.keys(results).forEach((resultKey) => {
pages.push(new RivenStatEmbed(this.bot, results[resultKey], resultKey, ctx.i18n));
});

await setupPages(pages, { message, settings: this.settings, mm: this.messageManager });
return this.messageManager.statuses.SUCCESS;
}
this.messageManager.reply(message, `No results for ${weapon}`, true, true);
return this.messageManager.statuses.FAILURE;
}
this.messageManager.reply(message, 'No query specified', true, true);
return this.messageManager.statuses.FAILURE;
}
}

module.exports = FrameStats;
96 changes: 96 additions & 0 deletions src/embeds/RivenStatEmbed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict';

const BaseEmbed = require('./BaseEmbed.js');

/**
* Generates enemy embeds
*/
class RivenStatEmbed extends BaseEmbed {
/**
* @param {Genesis} bot - An instance of Genesis
* @param {Object} rivenResult details to derive data from
* @param {string} resultKey The query that this search corresponds to
* @param {fn} i18n internationalization function to give string in apprpriate language
*/
constructor(bot, rivenResult, resultKey, i18n) {
super();
this.fields = [];
if(rivenResult.rerolled) {
this.fields.push(...[{
name: i18n`Rerolled`,
value: '\u200B',
inline: false,
}, {
name: i18n`Average Cost`,
value: `${rivenResult.rerolled.avg}`,
inline: true,
}, {
name: i18n`Standard Deviation`,
value: `${rivenResult.rerolled.stddev}`,
inline: true,
}, {
name: i18n`Minimum Cost`,
value: `${rivenResult.rerolled.min}`,
inline: true,
}, {
name: i18n`Maximum Cost`,
value: `${rivenResult.rerolled.max}`,
inline: true,
}, {
name: i18n`Popularity`,
value: `${rivenResult.rerolled.pop}`,
inline: true,
}, {
name: i18n`Median Cost`,
value: `${rivenResult.rerolled.median}`,
inline: true,
}]);
}

if(rivenResult.unrolled) {
this.fields.push(...[{
name: '\u200B',
value: '\u200B',
inline: false,
},{
name: i18n`Unrolled`,
value: '\u200B',
inline: false,
}, {
name: i18n`Average Cost`,
value: `${rivenResult.unrolled.avg}`,
inline: true,
}, {
name: i18n`Standard Deviation`,
value: `${rivenResult.unrolled.stddev}`,
inline: true,
}, {
name: i18n`Minimum Cost`,
value: `${rivenResult.unrolled.min}`,
inline: true,
}, {
name: i18n`Maximum Cost`,
value: `${rivenResult.unrolled.max}`,
inline: true,
}, {
name: i18n`Popularity`,
value: `${rivenResult.unrolled.pop}`,
inline: true,
}, {
name: i18n`Median Cost`,
value: `${rivenResult.unrolled.median}`,
inline: true,
}]);
}

if (!this.fields.length) {
this.description = i18n`No data available`;
}

this.title = resultKey;
this.color = 0x84659F;
this.type = 'rich';
}
}

module.exports = RivenStatEmbed;

0 comments on commit 77b1ac2

Please sign in to comment.