Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NB10328 authored and NB10328 committed Nov 27, 2020
1 parent 7f93aa2 commit 60efba1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swgoh-api-swgohgg",
"version": "0.0.4",
"version": "0.0.5",
"description": "SWGoH helper API for swgoh.gg",
"main": "index.js",
"keywords": [
Expand Down
39 changes: 4 additions & 35 deletions swgohgg-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,6 @@ class SwgohGGApi {
* @returns {Map<string, string>} Acronyms map between acronym and full unit name.
*/
static loadAcronyms(fileName) {
if (this.logger) this.logger.info(`loadAcronyms@swgohgg-api: Loading acronyms from ${fileName}`);

// file system access module
const fs = require('fs');

Expand All @@ -428,8 +426,6 @@ class SwgohGGApi {
acronyms.set(acronym.acronym, acronym.name);
});

if (this.logger) this.logger.info(`loadAcronyms@swgohgg-api: loaded acronyms OK (total = ${tempAcronyms.length})`);

return acronyms;
}

Expand All @@ -441,21 +437,7 @@ class SwgohGGApi {
* @returns {number} The number of owned characters derived from player data.
*/
static getUnitTypeCount (player, type) {
if (this.logger) this.logger.debug(`getUnitTypeCount@swgohgg-api: count request for ally code "${player.data.ally_code}" and type "${type}"`);

var result = 0;

// loop over units
for(var i = 0; i < player.units.length; i++) {
/** @type {UnitDetail} */
const unit = player.units[i].data;

// test for characters
if (unit.combat_type == type)
result++;
}

return result;
return player.units.filter(unit => unit.data.combat_type == type).length;
}

/**
Expand Down Expand Up @@ -485,20 +467,7 @@ class SwgohGGApi {
* @returns {boolean} Whether this unit is a GL.
*/
static isGL(unit) {
var result = false;

// loop over zeta abilities
for (var i = 0; i < unit.zeta_abilities.length; i++) {
const abilityName = unit.zeta_abilities[i];

// check for the galatic legend ability
if (abilityName.includes('GALACTICLEGEND')) {
result = true;
break;
}
}

return result;
return unit.zeta_abilities.filter(abilityName => abilityName.includes('GALACTICLEGEND')).length > 0;
}

/**
Expand All @@ -507,7 +476,7 @@ class SwgohGGApi {
* @param {Player} player The player data.
* @returns {PlayerStats} Object with summarized player data.
*/
static getSummaryData(player) {
static getPlayerStatsSummary(player) {
/** @type {PlayerStats} */
var result = {
chars: {
Expand Down Expand Up @@ -535,7 +504,7 @@ class SwgohGGApi {
result.chars.count++;

// test for GL
if (this.isGL(unit.data)) {
if (SwgohGGApi.isGL(unit.data)) {
result.chars.galacticLegendCount++;
}

Expand Down
67 changes: 33 additions & 34 deletions test/swgohgg-api.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/*jshint esversion: 6 */

const SwgohGGApi = require('../swgohgg-api');
const { SwgohGGApi, AbilityTypeEnum, CombatTypeEnum } = require('../swgohgg-api');
const assert = require('assert');

// default api object
const config = require('../config.json');
const { AbilityTypeEnum } = require('../swgohgg-api');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

describe('Static methods', () => {
it('getToken', () => {
assert.strictEqual(SwgohGGApi.SwgohGGApi.getToken('user', 'password'), 'dXNlcjpwYXNzd29yZA==');
assert.strictEqual(SwgohGGApi.getToken('user', 'password'), 'dXNlcjpwYXNzd29yZA==');
});

it ('loadAcronyms', () => {
/** @type {Map<string, string>} */
const acronyms = SwgohGGApi.SwgohGGApi.loadAcronyms('./resources/toon_acronyms.json');
const acronyms = SwgohGGApi.loadAcronyms('./resources/toon_acronyms.json');

assert.strictEqual(typeof acronyms, "object");
assert.strictEqual(acronyms.size > 0, true);
Expand All @@ -24,71 +23,71 @@ describe('Static methods', () => {
it ('getUnitTypeCount/getCharacterCount/getShipCount', () => {
const player = require('./player.232669733.json');

assert.strictEqual(SwgohGGApi.SwgohGGApi.getUnitTypeCount(player, SwgohGGApi.CombatTypeEnum.CombatTypeChar) +
SwgohGGApi.SwgohGGApi.getUnitTypeCount(player, SwgohGGApi.CombatTypeEnum.CombatTypeShip),
assert.strictEqual(SwgohGGApi.getUnitTypeCount(player, CombatTypeEnum.CombatTypeChar) +
SwgohGGApi.getUnitTypeCount(player, CombatTypeEnum.CombatTypeShip),
player.units.length);
assert.strictEqual(SwgohGGApi.SwgohGGApi.getUnitTypeCount(player, SwgohGGApi.CombatTypeEnum.CombatTypeChar),
SwgohGGApi.SwgohGGApi.getCharacterCount(player));
assert.strictEqual(SwgohGGApi.SwgohGGApi.getUnitTypeCount(player, SwgohGGApi.CombatTypeEnum.CombatTypeShip),
SwgohGGApi.SwgohGGApi.getShipCount(player));
assert.strictEqual(SwgohGGApi.getUnitTypeCount(player, CombatTypeEnum.CombatTypeChar),
SwgohGGApi.getCharacterCount(player));
assert.strictEqual(SwgohGGApi.getUnitTypeCount(player, CombatTypeEnum.CombatTypeShip),
SwgohGGApi.getShipCount(player));
});

it('getPlayerUnitFromUnits', () => {
const player = require('./player.232669733.json');

const char = SwgohGGApi.SwgohGGApi.getPlayerUnitFromUnits(player, 'Darth Vader');
const char = SwgohGGApi.getPlayerUnitFromUnits(player, 'Darth Vader');
assert.strictEqual(char.name, 'Darth Vader');
assert.strictEqual(char.base_id, 'VADER');

const ship = SwgohGGApi.SwgohGGApi.getPlayerUnitFromUnits(player, 'Chimaera');
const ship = SwgohGGApi.getPlayerUnitFromUnits(player, 'Chimaera');
assert.strictEqual(ship.name, 'Chimaera');
assert.strictEqual(ship.base_id, 'CAPITALCHIMAERA');
});

it('isGL', () => {
const player = require('./player.973246862.json');

assert.strictEqual(SwgohGGApi.SwgohGGApi.isGL(SwgohGGApi.SwgohGGApi.getPlayerUnitFromUnits(player, 'Darth Vader')), false);
assert.strictEqual(SwgohGGApi.SwgohGGApi.isGL(SwgohGGApi.SwgohGGApi.getPlayerUnitFromUnits(player, 'Supreme Leader Kylo Ren')), true);
assert.strictEqual(SwgohGGApi.isGL(SwgohGGApi.getPlayerUnitFromUnits(player, 'Darth Vader')), false);
assert.strictEqual(SwgohGGApi.isGL(SwgohGGApi.getPlayerUnitFromUnits(player, 'Supreme Leader Kylo Ren')), true);
});

it('getSummaryData', () => {
it('getPlayerStatsSummary', () => {
const player = require('./player.973246862.json');
const playerStats = SwgohGGApi.SwgohGGApi.getSummaryData(player);
const playerStats = SwgohGGApi.getPlayerStatsSummary(player);

assert.strictEqual(playerStats.chars.count, playerStats.chars.levels.reduce((accumulator, currentValue) => accumulator + currentValue));
assert.strictEqual(playerStats.chars.count, playerStats.chars.rarities.reduce((accumulator, currentValue) => accumulator + currentValue));
assert.strictEqual(playerStats.chars.count, playerStats.chars.gear.reduce((accumulator, currentValue) => accumulator + currentValue));
assert.strictEqual(playerStats.chars.galacticLegendCount, 1);
assert.strictEqual(playerStats.chars.count, SwgohGGApi.SwgohGGApi.getCharacterCount(player));
assert.strictEqual(playerStats.chars.count, SwgohGGApi.getCharacterCount(player));

assert.strictEqual(playerStats.ships.count, playerStats.ships.levels.reduce((accumulator, currentValue) => accumulator + currentValue));
assert.strictEqual(playerStats.ships.count, playerStats.ships.rarities.reduce((accumulator, currentValue) => accumulator + currentValue));
assert.strictEqual(playerStats.ships.count, SwgohGGApi.SwgohGGApi.getShipCount(player));
assert.strictEqual(playerStats.ships.count, SwgohGGApi.getShipCount(player));
});

it('getAbilityTypeDescription', () => {
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeBasic), 'Basic');
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeCrew), 'Crew');
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeLeader), 'Leader');
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeSpecial), 'Special');
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeUnique), 'Unique');
assert.strictEqual(SwgohGGApi.SwgohGGApi.getAbilityTypeDescription(9999), 'Unknown ability: 9999');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeBasic), 'Basic');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeCrew), 'Crew');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeLeader), 'Leader');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeSpecial), 'Special');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(AbilityTypeEnum.AbilityTypeUnique), 'Unique');
assert.strictEqual(SwgohGGApi.getAbilityTypeDescription(9999), 'Unknown ability: 9999');
});
});

describe('Base methods', () => {
it('Constructor', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);
});
});

describe ('API foundation', () => {

it('fetchRetry(GET)', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const reply = api.fetchRetry('GET', 'https://postman-echo.com/get?foo1=bar1&foo2=bar2', undefined, 3);

Expand All @@ -100,7 +99,7 @@ describe ('API foundation', () => {

it('fetchRetry(POST)', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const reply = api.fetchRetry('POST', 'https://postman-echo.com/post?hand=wave', undefined, 3);

Expand All @@ -111,7 +110,7 @@ describe ('API foundation', () => {

it('fetchRetry(POST payload)', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const reply = api.fetchRetry('POST', 'https://postman-echo.com/post', { id: 10, name: 'John' }, 3);

Expand All @@ -125,7 +124,7 @@ describe ('API foundation', () => {
describe('Local cache', () => {
it('fetchCharacters', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const chars = api.fetchCharacters();

Expand All @@ -136,7 +135,7 @@ describe('Local cache', () => {

it('fetchShips', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const ships = api.fetchShips();

Expand All @@ -147,7 +146,7 @@ describe('Local cache', () => {

it('fetchAbilities', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const abilities = api.fetchAbilities();

Expand All @@ -158,7 +157,7 @@ describe('Local cache', () => {

it('fetchGear', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

const gear = api.fetchGear();

Expand All @@ -171,7 +170,7 @@ describe('Local cache', () => {

it ('buildCache', () => {
const config = require('../config.json');
const api = new SwgohGGApi.SwgohGGApi(config.user, config.password);
const api = new SwgohGGApi(config.user, config.password);

api.buildCache();

Expand Down

0 comments on commit 60efba1

Please sign in to comment.