Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Revert "Introduce general speed functions"
Browse files Browse the repository at this point in the history
This reverts commit 483d3a9.
  • Loading branch information
drgoonic committed Nov 29, 2018
1 parent 483d3a9 commit 228c4d0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 88 deletions.
48 changes: 0 additions & 48 deletions utilities/speed-general-utils.js

This file was deleted.

117 changes: 82 additions & 35 deletions utilities/speedrank-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { google } = require('googleapis');
const {google} = require('googleapis');
const util = require('util');
const fs = require('fs');
const OAuth2Client = google.auth.OAuth2;
Expand All @@ -7,11 +7,6 @@ const titlecase = require('titlecase');
const escapeStringRegexp = require('escape-string-regexp');
const pad = require('pad');

// const path = require('path');
// speedrankPath = path.join(__dirname, 'speed-general-utils.js');
// '..', '..', 'utilities', 'speedrank-utils.js');
const speedrankUtils = require('./speed-general-utils.js');

const SPREADSHEET_ID = '11gTjAkpm4D3uoxnYCN7ZfbiVnKyi7tmm9Vp9HvTkGpw';
const TOKEN_PATH = 'secrets/credentials.json';
const SECRETS_PATH = 'secrets/client_secret.json';
Expand All @@ -27,7 +22,7 @@ async function authorize() {
secrets = await fs.readFileAsync(SECRETS_PATH);
token = await fs.readFileAsync(TOKEN_PATH);
const credentials = JSON.parse(secrets);
const { client_secret, client_id, redirect_uris } = credentials.installed;
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new OAuth2Client(
client_id, client_secret, redirect_uris[0]);
oAuth2Client.setCredentials(JSON.parse(token));
Expand All @@ -37,27 +32,27 @@ async function authorize() {
}
}

exports.speedrank = function lookupSpeedrank(
exports.speedrank = function lookupspeedrank(
msg, player, category, secondaryCategory) {
console.log(util.format('.top caller: %s#%s for top %s %s (%s)',
msg.author.username, msg.author.discriminator,
player, category, secondaryCategory));
return new Promise((resolve, reject) => {
const sheets = google.sheets({ version: 'v4' });
const sheets = google.sheets({version: 'v4'});
authorize()
.then((oAuth2Client) => {
category = escapeStringRegexp(category);
secondaryCategory = escapeStringRegexp(secondaryCategory);
category = category.toLowerCase();
const categorySheet = getSheet(category, secondaryCategory);
player = escapeStringRegexp(player);
player = escapeStringRegexp(player);

const request = {
spreadsheetId: SPREADSHEET_ID,
range: categorySheet,
auth: oAuth2Client,
};
sheets.spreadsheets.values.get(request, (err, { data }) => {
sheets.spreadsheets.values.get(request, (err, {data}) => {
if (err) {
if (err.code === 400) {
console.log(err.code);
Expand All @@ -70,10 +65,10 @@ exports.speedrank = function lookupSpeedrank(
console.log(err);
msg.channel.send(
'The bot user has not set up valid Google API credentials yet.')
.then((res) => {
resolve(res);
return;
});
.then((res) => {
resolve(res);
return;
});
}
} else {
// CategoryNames (headers) are fixed for the rank request
Expand All @@ -83,22 +78,25 @@ exports.speedrank = function lookupSpeedrank(
const fightRow = data.values[2];

let fightNames = [];
for (let i = 1; i < fightRow.length; i++) {
if (speedrankUtils.checkCell(fightRow[i] === '')) {
for(let i = 1; i < fightRow.length; i++) {
if (fightRow[i] === undefined ||
fightRow[i] === '' ||
fightRow[i] === null) {
continue;
}
else {
fightNames.push(fightRow[i]);
}
}
}


let contestants = [];
let padLength = 0;
let playerActualName = player;

// Now that we know the fights, let's search each fight column for the player in question
for (let catName of fightNames) {
let categoryRange = speedrankUtils.find(catName, data.values);
for (let catName of fightNames) {
let categoryRange = find(catName, data.values);

//TODO: Get max rows of table
for (let i = 0; i < 500; i++) {
Expand All @@ -114,7 +112,7 @@ exports.speedrank = function lookupSpeedrank(
// entryStartPos gives us the starting cell
// with the contestant name.
const entryStartPos = categoryRange.columnNum - 1;
let cell = speedrankUtils.checkCell(data.values[row][entryStartPos]);
let cell = checkCell(data.values[row][entryStartPos]);
if (catName.length > padLength) {
padLength = catName.length + 1;
console.log(padLength);
Expand All @@ -128,14 +126,14 @@ exports.speedrank = function lookupSpeedrank(
contestant.push(catName);

// Grab the actual rank of this row
contestant.push(speedrankUtils.checkCell(data.values[row][0]));
contestant.push(checkCell(data.values[row][0]));

// The Overall category and Torment sheets places avg time in the third column. Others have them in the second
if (catName === "Overall" || categorySheet === 'Torment') {
contestant.push(speedrankUtils.checkCell(data.values[row][entryStartPos + 2]));
if (catName === "Overall" || categorySheet === 'Torment'){
contestant.push(checkCell(data.values[row][entryStartPos+2]));
}
else {
contestant.push(speedrankUtils.checkCell(data.values[row][entryStartPos + 1]));
contestant.push(checkCell(data.values[row][entryStartPos+1]));
}

contestants.push(contestant);
Expand All @@ -147,9 +145,9 @@ exports.speedrank = function lookupSpeedrank(
outputRankTable(categorySheet, playerActualName,
categoryNames, contestants, padLength);
msg.channel.send(rankTable)
.then((res) => {
.then( (res) => {
resolve(res);
});
});
}
});
});
Expand All @@ -166,29 +164,65 @@ function getSheet(category, secondaryCategory) {
let version = "Overall";

// check for various permutations of "No CSB"
if (['no-csb', 'nocsb', 'no', 'no csb'].includes(secondaryCategory.toLowerCase())) {
if (['no-csb','nocsb','no','no csb'].includes(secondaryCategory.toLowerCase())){
version = 'No CSB';
}

// check for various permutations of fight, e.g. "4 star", "Torment" and convert to sheet name
if (['3', '3star', '3-star', '3 star'].includes(category.toLowerCase())) {
if (['3','3star','3-star','3 star'].includes(category.toLowerCase())){
category = `GL 3* ${version} rankings`;
}
else if (['4', '4star', '4-star', '4 star'].includes(category.toLowerCase())) {
else if (['4','4star','4-star','4 star'].includes(category.toLowerCase())){
category = `GL 4* ${version} rankings`;
}
else if (['5', '5star', '5-star', '5 star'].includes(category.toLowerCase())) {
else if (['5','5star','5-star','5 star'].includes(category.toLowerCase())){
category = `GL 5* ${version} rankings`;
}
else if (['torment', 'tor', 'neotorment', 'neo'].includes(category.toLowerCase())) {
else if (['torment', 'tor', 'neotorment', 'neo'].includes(category.toLowerCase())){
category = 'Torment';
}
else {
else{
category = 'Error';
}

return category;
}
/**
* Finds a value within a given range.
* @see https://stackoverflow.com/questions/10807936/how-do-i-search-google-spreadsheets/10823543#10823543
* @param {String} value The value to find.
* @param {String} data The range to search in using Google's A1 format.
* @return {Object} A range pointing to the first cell containing the value,
* or null if not found.
*/
function find(value, data) {
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
//console.log('Searching:'+data[i][j]);
if (data[i][j] == value) {
const columnName = columnToName(j + 1);
return {row: i + 1, column: columnName, columnNum: j + 1};
}
}
}
return null;
}
/**
* Returns the Google Spreadsheet column name equivalent of a number.
* @param {Integer} columnNumber The column number to look for
* @return {String} columnName
*/
function columnToName(columnNumber) {
let columnName = '';
let modulo;
const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (columnNumber > 0) {
modulo = (columnNumber - 1) % 26;
columnName = alpha.charAt(modulo) + columnName;
columnNumber = Math.floor((columnNumber - modulo)/26);
}
return columnName;
}

/**
* Formats a ranking table for output.
Expand Down Expand Up @@ -233,7 +267,7 @@ function outputCategoryHeader(categoryNames, namePadLength) {
categoryHeader += ' | ';
categoryHeader += pad(categoryNames[2], 5);
categoryHeader += ' | ';

const repeatLength = categoryHeader.length - 1;
categoryHeader += '\n';
// create a series of dashes to indicate header
Expand All @@ -260,8 +294,21 @@ function outputContestants(contestants, namePadLength) {
//time output
formattedContestants += pad(contestants[i][2], 5);
formattedContestants += ' | ';

formattedContestants += '\n';
}
return formattedContestants;
}
/**
* Ensures null cells don't mess with anything.
* @param cell
* @return {String} cell or ''
*/
function checkCell(cell) {
if (cell === undefined || cell === '' || cell === null) {
return '';
}
else{
return cell;
}
}
48 changes: 43 additions & 5 deletions utilities/speedrun-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const pad = require('pad');
const SPREADSHEET_ID = '11gTjAkpm4D3uoxnYCN7ZfbiVnKyi7tmm9Vp9HvTkGpw';
const TOKEN_PATH = 'secrets/credentials.json';
const SECRETS_PATH = 'secrets/client_secret.json';
const speedrankUtils = require('./speed-general-utils.js');

/** authorize():
* Authorizes Google credentials.
Expand Down Expand Up @@ -71,7 +70,7 @@ exports.speedrun = function lookupSpeedrun(
}
} else {
// Find where the secondaryCategory is on the sheet in question
const categoryRange = speedrankUtils.find(secondaryCategory, data.values);
const categoryRange = find(secondaryCategory, data.values);
let categoryNames = [];
// Get the row where the categories lie.
const categoryRow = data.values[categoryRange.row + 1];
Expand All @@ -83,7 +82,9 @@ exports.speedrun = function lookupSpeedrun(
// and keep going until we either hit a '' or undefined.
for
(let i = categoryRange.columnNum - 1; i < categoryRow.length; i++) {
if (speedrankUtils.checkCell(categoryRow[i]) === '') {
if (categoryRow[i] === undefined ||
categoryRow[i] === '' ||
categoryRow[i] === null) {
break;
}
categoryNames.push(categoryRow[i]);
Expand All @@ -109,8 +110,10 @@ exports.speedrun = function lookupSpeedrun(
for (let j = 0;
j < categoryNames.length; j++) {
// If the entry is somehow undefined or null, put in a ''.
let cell = speedrankUtils.checkCell(data.values[row][entryStartPos + j]);

let cell = data.values[row][entryStartPos + j];
if (cell === undefined || cell == null) {
cell = '';
}
if (j === 0 && cell.length === 0) {
// If the name column is empty, then break.
break;
Expand Down Expand Up @@ -176,6 +179,41 @@ function getSheet(category) {
}
return category;
}
/**
* Finds a value within a given range.
* @see https://stackoverflow.com/questions/10807936/how-do-i-search-google-spreadsheets/10823543#10823543
* @param {String} value The value to find.
* @param {String} data The range to search in using Google's A1 format.
* @return {Object} A range pointing to the first cell containing the value,
* or null if not found.
*/
function find(value, data) {
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
if (data[i][j] == value) {
const columnName = columnToName(j + 1);
return {row: i + 1, column: columnName, columnNum: j + 1};
}
}
}
return null;
}
/**
* Returns the Google Spreadsheet column name equivalent of a number.
* @param {Integer} columnNumber The column number to look for
* @return {String} columnName
*/
function columnToName(columnNumber) {
let columnName = '';
let modulo;
const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (columnNumber > 0) {
modulo = (columnNumber - 1) % 26;
columnName = alpha.charAt(modulo) + columnName;
columnNumber = Math.floor((columnNumber - modulo)/26);
}
return columnName;
}

/**
* Formats a ranking table for output.
Expand Down

0 comments on commit 228c4d0

Please sign in to comment.