From 29a8822e6355d290054b4d8ae90ed6aa80f6dda3 Mon Sep 17 00:00:00 2001 From: TobiTenno Date: Wed, 17 Feb 2021 14:36:13 -0600 Subject: [PATCH] feat: parse color palette entries into rows/colums/palettes --- src/Archwing.js | 4 ++-- src/Companion.js | 4 ++-- src/Mech.js | 4 ++-- src/Pixel.js | 35 +++++++++++++++++++++++++++++++++++ src/Warframe.js | 10 +++++----- src/utils.js | 25 +++++++++++++++++++++++++ test/unit/companion.spec.js | 28 +++++++++++++++++++++++----- test/unit/mech.spec.js | 19 +------------------ test/unit/mock.spec.js | 4 ++-- test/unit/pixel.spec.js | 30 ++++++++++++++++++++++++++++++ test/unit/warframe.spec.js | 26 ++++---------------------- 11 files changed, 131 insertions(+), 58 deletions(-) create mode 100644 src/Pixel.js create mode 100644 test/unit/pixel.spec.js diff --git a/src/Archwing.js b/src/Archwing.js index b162f68..f7034b9 100644 --- a/src/Archwing.js +++ b/src/Archwing.js @@ -1,6 +1,6 @@ 'use strict'; -const { items, loadMods } = require('./utils'); +const { items, loadMods, mapColors } = require('./utils'); class WarframeArchwing { constructor(archwing) { @@ -11,7 +11,7 @@ class WarframeArchwing { this.polarized = archwing.polarized; this.upgrades = loadMods(archwing.upgrades); - this.colors = archwing.pricol; + this.colors = mapColors(archwing.pricol); } } diff --git a/src/Companion.js b/src/Companion.js index 30f9283..b61fcfa 100644 --- a/src/Companion.js +++ b/src/Companion.js @@ -1,6 +1,6 @@ 'use strict'; -const { items, loadMods } = require('./utils'); +const { items, loadMods, mapColors } = require('./utils'); class WarframeCompanion { constructor(companion) { @@ -28,7 +28,7 @@ class WarframeCompanion { delete cosmetic.consumeOnBuild; }); } - this.colors = companion.pricol; + this.colors = mapColors(companion.pricol); this.upgrades = loadMods(companion.upgrades); this.type = companion.type; if (companion.itemName) this.name = companion.itemName; diff --git a/src/Mech.js b/src/Mech.js index c23006d..ba1aaf3 100644 --- a/src/Mech.js +++ b/src/Mech.js @@ -1,6 +1,6 @@ 'use strict'; -const { items, loadMods } = require('./utils'); +const { items, loadMods, mapColors } = require('./utils'); module.exports = class WarframeMech { constructor(mech) { @@ -30,7 +30,7 @@ module.exports = class WarframeMech { }); } - this.colors = mech.pricol; + this.colors = mapColors(mech.pricol); this.upgrades = loadMods(mech.upgrades); // ?? no idea what this is diff --git a/src/Pixel.js b/src/Pixel.js new file mode 100644 index 0000000..3cbcd46 --- /dev/null +++ b/src/Pixel.js @@ -0,0 +1,35 @@ +'use strict'; + +const Items = require('warframe-items'); + +const items = new Items(['Skins']).filter((i) => i.hexColours); + +const position = (ind) => ({ + row: (ind % 18) + 1, + col: Math.ceil(ind / 18), +}); + +module.exports = class Pixel { + constructor(hex) { + this.hex = hex; + this.matches = []; + + items.forEach((item) => { + item.hexColours.forEach(({ value }, index) => { + if (value.toLowerCase().includes(hex.toLowerCase())) { + this.matches.push({ + palette: { + name: item.name, + description: item.description, + }, + position: position(index), + }); + } + }); + }); + } + + get palettes() { + return Array.from(new Set(this.matches.map((match) => match.palette.name))); + } +}; diff --git a/src/Warframe.js b/src/Warframe.js index 082b3c1..bd96530 100644 --- a/src/Warframe.js +++ b/src/Warframe.js @@ -1,6 +1,6 @@ 'use strict'; -const { items, loadMods } = require('./utils'); +const { items, loadMods, mapColors } = require('./utils'); class Warframe { constructor(frame) { @@ -35,10 +35,10 @@ class Warframe { this.upgrades = loadMods(frame.upgrades); this.colors = { - syandana: frame.eyecol, - primary: frame.pricol, - sigil: frame.sigcol, - attachments: frame.attcol, + syandana: mapColors(frame.eyecol), + primary: mapColors(frame.pricol), + sigil: mapColors(frame.sigcol), + attachments: mapColors(frame.attcol), }; } } diff --git a/src/utils.js b/src/utils.js index 123d161..950a17b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,6 +4,8 @@ const Items = require('warframe-items'); const items = new Items(); +const Pixel = require('./Pixel'); + function translateFocus(focus = '') { if (focus.includes('Focus/Attack')) { return 'Madurai'; @@ -81,9 +83,32 @@ function loadMods(upgrades = []) { }; } +const exisColo = (color) => (color ? new Pixel(color) : null); + +function mapColors(colors = undefined) { + if (colors) { + return { + primary: exisColo(colors.t0), + secondary: exisColo(colors.t1), + tertiary: exisColo(colors.t2), + accents: exisColo(colors.t3), + emissive: [ + exisColo(colors.m0), + exisColo(colors.m1), + ].filter((c) => c), + energy: [ + exisColo(colors.en), + exisColo(colors.en1), + ].filter((c) => c), + }; + } + return colors; +} + module.exports = { translateFocus, loadMods, translatePolarity, items, + mapColors, }; diff --git a/test/unit/companion.spec.js b/test/unit/companion.spec.js index 5b766b0..8fcb09b 100644 --- a/test/unit/companion.spec.js +++ b/test/unit/companion.spec.js @@ -55,11 +55,29 @@ describe('WarframeCompanion', () => { xp: 548455507, polarized: 7, colors: { - t0: '92B1C6', - t1: '9C5D42', - t2: '9C5D42', - t3: '413E3B', - en: 'F6DC13', + accents: { + hex: '413E3B', + matches: [], + }, + emissive: [], + energy: [ + { + hex: 'F6DC13', + matches: [], + }, + ], + primary: { + hex: '92B1C6', + matches: [], + }, + secondary: { + hex: '9C5D42', + matches: [], + }, + tertiary: { + hex: '9C5D42', + matches: [], + }, }, upgrades: { arcanes: [], mods: [] }, type: 'beast', diff --git a/test/unit/mech.spec.js b/test/unit/mech.spec.js index 0caff1f..2c9b78c 100644 --- a/test/unit/mech.spec.js +++ b/test/unit/mech.spec.js @@ -20,15 +20,6 @@ describe('WarframeMech', () => { uniqueName: '/Lotus/Upgrades/Skins/Necramech/NecraMechCSkin', }, ], - pricol: { - t0: '2E2E2D', - t1: '9FA1A2', - t2: '291D1C', - t3: '243445', - m0: 'EBFF67', - m1: '374B50', - en: 'FF3806', - }, uniqueName: '/Lotus/Powersuits/EntratiMech/NechroTech', upgrades: [], }; @@ -36,15 +27,7 @@ describe('WarframeMech', () => { const mech = new WarframeMech(sampleMech); assert.deepEqual(mech, { - colors: { - en: 'FF3806', - m0: 'EBFF67', - m1: '374B50', - t0: '2E2E2D', - t1: '9FA1A2', - t2: '291D1C', - t3: '243445', - }, + colors: undefined, cosmetics: [ { category: 'Skins', diff --git a/test/unit/mock.spec.js b/test/unit/mock.spec.js index 5f5ff60..589126c 100644 --- a/test/unit/mock.spec.js +++ b/test/unit/mock.spec.js @@ -7,11 +7,11 @@ const example = (number) => require(`../data/exampleData${number}.json`); const ArsenalData = require('../../src/ArsenalParser'); // Testing the main class -describe('ArsenalData', () => { +describe('Mock ArsenalData', () => { describe('#constructor', () => { it('should handle real data', () => { ['1', '2', '3', '4', '5', '6'].forEach((ind) => { - assert.isOk(new ArsenalData(example(ind))); + assert.isOk(new ArsenalData(example(ind)), `${ind} failed`); }); }); }); diff --git a/test/unit/pixel.spec.js b/test/unit/pixel.spec.js new file mode 100644 index 0000000..2c4775b --- /dev/null +++ b/test/unit/pixel.spec.js @@ -0,0 +1,30 @@ +'use strict'; + +const { assert } = require('chai'); + +// Archwing.js testing +const Pixel = require('../../src/Pixel.js'); + +describe('Pixel', () => { + describe('#constructor', () => { + it('should parse and match hex codes', () => { + const sampleCodes = ['2E203D', '9078EA', 'D6A3EC', '49667C', 'E6B0FE', 'E6B0FE', '2E203D']; + sampleCodes.forEach((hex) => { + const pixel = new Pixel(hex); + assert.equal(pixel.hex, hex, 'Hex mismatch'); + assert.isNotEmpty(pixel.matches, 'No matches resolved'); + assert.isNotEmpty(pixel.palettes, 'Real pixels should give palettes'); + }); + }); + + it('should handle being passed an unknown hex', () => { + const fakeHex = '023nva'; + const fakePixel = new Pixel(fakeHex); + + assert.isOk(fakePixel); + assert.equal(fakePixel.hex, fakeHex, 'Hex mismatch'); + assert.isEmpty(fakePixel.matches, 'Match list mismatch'); + assert.isEmpty(fakePixel.palettes, 'Fake hex should not have palettes'); + }); + }); +}); diff --git a/test/unit/warframe.spec.js b/test/unit/warframe.spec.js index fb073cf..e7e5c26 100644 --- a/test/unit/warframe.spec.js +++ b/test/unit/warframe.spec.js @@ -75,34 +75,16 @@ describe('Warframe', () => { upgrades: { arcanes: [], mods: [] }, colors: { syandana: { - t0: 'FF588A', - t1: 'FE1C50', - t2: 'FF0D43', - t3: 'FF5C9F', - en: 'FF70A9', + primary: { hex: 'FF588A', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 2, col: 2 } }] }, secondary: { hex: 'FE1C50', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 8, col: 2 } }] }, tertiary: { hex: 'FF0D43', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 3, col: 2 } }] }, accents: { hex: 'FF5C9F', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 7, col: 2 } }] }, emissive: [], energy: [{ hex: 'FF70A9', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 12, col: 2 } }] }], }, primary: { - t0: 'FF588A', - t1: 'FE1C50', - t2: 'FF0D43', - t3: 'FF5C9F', - m0: 'FF70A9', - m1: 'FF70A9', - en: 'FF70A9', + primary: { hex: 'FF588A', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 2, col: 2 } }] }, secondary: { hex: 'FE1C50', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 8, col: 2 } }] }, tertiary: { hex: 'FF0D43', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 3, col: 2 } }] }, accents: { hex: 'FF5C9F', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 7, col: 2 } }] }, emissive: [{ hex: 'FF70A9', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 12, col: 2 } }] }, { hex: 'FF70A9', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 12, col: 2 } }] }], energy: [{ hex: 'FF70A9', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 12, col: 2 } }] }], }, sigil: { - t0: '3F9FB7', - t1: '808080', - t2: 'BABABA', - t3: '808080', - en: 'FFBA80', + primary: { hex: '3F9FB7', matches: [{ palette: { name: 'Classic', description: 'Unlocks additional color options for Warframe customization.' }, position: { row: 16, col: 2 } }] }, secondary: { hex: '808080', matches: [{ palette: { name: 'Tenno', description: 'Unlocks additional color options reflecting the default colors used on each standard Warframe.' }, position: { row: 10, col: 3 } }] }, tertiary: { hex: 'BABABA', matches: [{ palette: { name: 'Tenno', description: 'Unlocks additional color options reflecting the default colors used on each standard Warframe.' }, position: { row: 12, col: 1 } }, { palette: { name: 'Tenno', description: 'Unlocks additional color options reflecting the default colors used on each standard Warframe.' }, position: { row: 5, col: 4 } }] }, accents: { hex: '808080', matches: [{ palette: { name: 'Tenno', description: 'Unlocks additional color options reflecting the default colors used on each standard Warframe.' }, position: { row: 10, col: 3 } }] }, emissive: [], energy: [{ hex: 'FFBA80', matches: [] }], }, attachments: { - t0: 'FF588A', - t1: 'FE1C50', - t2: 'FF0D43', - t3: 'FF5C9F', - en: 'FF70A9', + primary: { hex: 'FF588A', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 2, col: 2 } }] }, secondary: { hex: 'FE1C50', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 8, col: 2 } }] }, tertiary: { hex: 'FF0D43', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 3, col: 2 } }] }, accents: { hex: 'FF5C9F', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 7, col: 2 } }] }, emissive: [], energy: [{ hex: 'FF70A9', matches: [{ palette: { name: 'Valentine', description: 'Unlocks a collection of lovely colors.' }, position: { row: 12, col: 2 } }] }], }, }, }), 'Invalid parsing of warframe data',