<commit_before>import Promise from 'bluebird';
import nconf from 'nconf';
import R from 'ramda';
import _request from 'request';

import sentry from './config/sentry';

const request = Promise.promisify(_request);

const help_text = `League of Legends help commands
Accepted positions are **Top**, **Mid**, **ADC**, **Jungle**, and **Support**.

**\`!lol counters\`** \`champ-name\` \`position\`
		Get the top 10 counters for a Champion and Position
**\`!lol skills\`** \`champ-name\` \`position\`
		Get the highest win skills for a Champion and Position
**\`!lol items\`** \`champ-name\` \`position\`
		Get the highest win item sets for a Champion and Position
**\`!lol server-status\`**
		Get the LoL Game and Client server status for all regions`;

// List of possible phrases used for positions
const positions = {
  top: 'Top',
  mid: 'Middle',
  middle: 'Middle',
  jungle: 'Jungle',
  jg: 'Jungle',
  adc: 'ADC',
  support: 'Support',
  sup: 'Support',
  supp: 'Support'
};

function _makeRequest(options) {
  let default_options = {
    qs: {api_key: nconf.get('CHAMPIONGG_API')},
    json: true
  };
  return request(R.merge(default_options, options))
    .then(R.prop('body'))
    .tap(body => {
      if (body.error) throw new Error(body.error);
    });
}

function counters(bot, msg, suffix) {
  let query = suffix.toLowerCase().replace('counters ', '').replace('counter ', '');
  let position = R.last(query.split(' '));
  let champ = query.replace(` ${position}`, '');

  if (position === champ) return bot.sendMessage(msg.channel, 'You didn\'t specifiy a position. Did you mean top, mid, adc, support, or jungle?');

  if (!R.contains(position, R.keys(positions))) return bot.sendMessage(msg.channel, `I don't understand position **${position}**. Did you mean top, mid, adc, support, or jungle?`);
  let gg_position = positions[position];

  let options = {
    url: `http://api.champion.gg/champion/${champ.replace(/[^a-z]/g, '')}/matchup`
  };
  return _makeRequest(options)
    .then(body => {
      let results = R.zipObj(R.pluck('role')(body), body);
      if (!R.contains(gg_position, R.keys(results))) throw new Error(`I don't have counters for ${champ} ${position}.`);
      return results[gg_position];
    })
    .then(R.prop('matchups'))
    .then(R.sortBy(R.prop('winRate')))
    .filter(data => data.games >= 200) // Only show champs where over 200 games have been played.
    .then(R.slice(0, 10))
    .map(data => `**${data.key}** - ${100 - data.winRate}% win rate over ${data.games} games.`)
    .then(R.prepend(`Sure! Here's the top 10 counters for ${champ} ${position}:\n`))
    .then(R.join('\n'))
    .then(text => bot.sendMessage(msg.channel, text))
    .catch(err => {
      sentry.captureError(err);
      bot.sendMessage(msg.channel, `Error: ${err.message}`);
    });
}

// Retrieves all items from Riot, then parses just the names.
function _itemNames(item_data) {
  let options = {
    url: 'https://ddragon.leagueoflegends.com/cdn/5.24.2/data/en_US/item.json',
    json: true
  };<commit_msg>lol sort counters by statScore<commit_after>import Promise from 'bluebird';
import nconf from 'nconf';
import R from 'ramda';
import _request from 'request';

import sentry from './config/sentry';

const request = Promise.promisify(_request);

const help_text = `League of Legends help commands
Accepted positions are **Top**, **Mid**, **ADC**, **Jungle**, and **Support**.

**\`!lol counters\`** \`champ-name\` \`position\`
		Get the top 10 counters for a Champion and Position
**\`!lol skills\`** \`champ-name\` \`position\`
		Get the highest win skills for a Champion and Position
**\`!lol items\`** \`champ-name\` \`position\`
		Get the highest win item sets for a Champion and Position
**\`!lol server-status\`**
		Get the LoL Game and Client server status for all regions`;

// List of possible phrases used for positions
const positions = {
  top: 'Top',
  mid: 'Middle',
  middle: 'Middle',
  jungle: 'Jungle',
  jg: 'Jungle',
  adc: 'ADC',
  support: 'Support',
  sup: 'Support',
  supp: 'Support'
};

function _makeRequest(options) {
  let default_options = {
    qs: {api_key: nconf.get('CHAMPIONGG_API')},
    json: true
  };
  return request(R.merge(default_options, options))
    .then(R.prop('body'))
    .tap(body => {
      if (body.error) throw new Error(body.error);
    });
}

function counters(bot, msg, suffix) {
  let query = suffix.toLowerCase().replace('counters ', '').replace('counter ', '');
  let position = R.last(query.split(' '));
  let champ = query.replace(` ${position}`, '');

  if (position === champ) return bot.sendMessage(msg.channel, 'You didn\'t specifiy a position. Did you mean top, mid, adc, support, or jungle?');

  if (!R.contains(position, R.keys(positions))) return bot.sendMessage(msg.channel, `I don't understand position **${position}**. Did you mean top, mid, adc, support, or jungle?`);
  let gg_position = positions[position];

  let options = {
    url: `http://api.champion.gg/champion/${champ.replace(/[^a-z]/g, '')}/matchup`
  };
  return _makeRequest(options)
    .then(body => {
      let results = R.zipObj(R.pluck('role')(body), body);
      if (!R.contains(gg_position, R.keys(results))) throw new Error(`I don't have counters for ${champ} ${position}.`);
      return results[gg_position];
    })
    .then(R.prop('matchups'))
    .then(R.sortBy(R.prop('statScore')))
    .then(R.slice(0, 10))
    .map(data => `**${data.key}** - ${100 - data.winRate}% win rate over ${data.games} games.`)
    .then(R.prepend(`Sure! Here's the top 10 **statistical** counters for ${champ} ${position}:\n`))
    .then(R.join('\n'))
    .then(text => bot.sendMessage(msg.channel, text))
    .catch(err => {
      sentry.captureError(err);
      bot.sendMessage(msg.channel, `Error: ${err.message}`);
    });
}

// Retrieves all items from Riot, then parses just the names.
function _itemNames(item_data) {
  let options = {
    url: 'https://ddragon.leagueoflegends.com/cdn/5.24.2/data/en_US/item.json',
    json: true
  };
{'fraction_non_alphanumeric': 0.1813488759367194, 'fraction_numerical': 0.005661948376353039, 'mean_word_length': 3.831858407079646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1965168', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2032, 'n_words': 562}