From 2b5e99ab2403145538ff525ed147a9eb83c02a56 Mon Sep 17 00:00:00 2001 From: Mike Kreiser Date: Sun, 29 Oct 2023 13:21:26 -0400 Subject: [PATCH 1/4] Create inverted stat map --- src/player-stats/player-stats.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/player-stats/player-stats.js b/src/player-stats/player-stats.js index 70416e0..b4ba8d1 100644 --- a/src/player-stats/player-stats.js +++ b/src/player-stats/player-stats.js @@ -262,6 +262,11 @@ class PlayerStats extends BaseObject { }; } +export const statIdsToAttributes = _.reduce(PlayerStats.responseMap, (acc, value, key) => { + acc[value] = key; + return acc; +}, {}); + export const parsePlayerStats = ({ responseData, constructorParams, usesPoints, seasonId, statKey, statSourceId, statSplitTypeId }) => { From 9614f3691e57dc4ee9850b86c0e07440fc4770b9 Mon Sep 17 00:00:00 2001 From: Mike Kreiser Date: Sun, 29 Oct 2023 13:21:59 -0400 Subject: [PATCH 2/4] Add league scoring settings to League class --- src/client/client.test.js | 16 +++++++++------- src/league/league.js | 25 +++++++++++++++++++++++++ src/league/league.test.js | 24 +++++++++++++++++++++++- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/client/client.test.js b/src/client/client.test.js index 051555d..3fa2317 100644 --- a/src/client/client.test.js +++ b/src/client/client.test.js @@ -1087,20 +1087,21 @@ describe('Client', () => { client = new Client({ leagueId: 213213 }); jest.spyOn(axios, 'get').mockImplementation(); + axios.get.mockReturnValue(q({ + scoringSettings: { + scoringItems: [] + } + })); }); describe('when the seasonId is prior to 2018', () => { test('throws an error', () => { - axios.get.mockReturnValue(q()); - expect(() => client.getLeagueInfo({ seasonId: 2017 })).toThrow(); }); }); describe('when the seasonId is 2018 or after', () => { test('does not throw an error', () => { - axios.get.mockReturnValue(q()); - expect(() => client.getLeagueInfo({ seasonId: 2018 })).not.toThrow(); }); @@ -1111,7 +1112,6 @@ describe('Client', () => { const config = {}; jest.spyOn(client, '_buildAxiosConfig').mockReturnValue(config); - axios.get.mockReturnValue(q()); client.getLeagueInfo({ seasonId }); expect(axios.get).toBeCalledWith(route, config); @@ -1120,7 +1120,6 @@ describe('Client', () => { describe('before the promise resolves', () => { test('does not invoke callback', () => { jest.spyOn(League, 'buildFromServer').mockImplementation(); - axios.get.mockReturnValue(q()); client.getLeagueInfo({ seasonId }); expect(League.buildFromServer).not.toBeCalled(); @@ -1135,7 +1134,10 @@ describe('Client', () => { name: 'some league', draftSettings: {}, rosterSettings: {}, - scheduleSettings: {} + scheduleSettings: {}, + scoringSettings: { + scoringItems: [] + } }, status: { currentMatchupPeriod: 7, diff --git a/src/league/league.js b/src/league/league.js index 1291152..555ee91 100644 --- a/src/league/league.js +++ b/src/league/league.js @@ -3,6 +3,7 @@ import _ from 'lodash'; import BaseObject from '../base-classes/base-object/base-object'; import { slotCategoryIdToPositionMap } from '../constants.js'; +import { statIdsToAttributes } from '../player-stats/player-stats'; /* global DRAFT_TYPE, LINEUP_LOCK_TIMES */ @@ -59,6 +60,7 @@ class League extends BaseObject { * @property {DraftSettings} draftSettings The draft settings of the league. * @property {RosterSettings} rosterSettings The roster settings of the league. * @property {ScheduleSettings} scheduleSettings The schedule settings of the league. + * @property {object} scoringSettings The scoring settings of the league. */ /** @@ -114,6 +116,29 @@ class League extends BaseObject { numberOfPlayoffTeams: responseData.playoffTeamCount }; } + }, + + scoringSettings: { + key: 'scoringSettings', + manualParse: (responseData) => _.reduce( + responseData.scoringItems, + (acc, { points, pointsOverrides, statId }) => { + const key = statIdsToAttributes[statId]; + + if (!key) { + return acc; + } + + if (pointsOverrides) { + acc[key] = _.first(_.values(pointsOverrides)); + } else { + acc[key] = points; + } + + return acc; + }, + {} + ) } }; } diff --git a/src/league/league.test.js b/src/league/league.test.js index 4bee987..4674b87 100644 --- a/src/league/league.test.js +++ b/src/league/league.test.js @@ -47,7 +47,18 @@ describe('League', () => { data = { draftSettings, rosterSettings, - scheduleSettings + scheduleSettings, + scoringSettings: { + scoringItems: [{ + points: 1, statId: 0 + }, { + points: 4, statId: 1 + }, { + points: 6, pointsOverrides: { '16': 9 }, statId: 2 // eslint-disable-line quote-props + }, { + points: 75, statId: 999 + }] + } }; }); @@ -155,5 +166,16 @@ describe('League', () => { ); }); }); + + describe('scoringSettings', () => { + test('maps to object using constants', () => { + const league = League.buildFromServer(data); + expect(league.scoringSettings).toStrictEqual({ + passingAttempts: 1, + passingIncompletions: 9, + passingCompletions: 4 + }); + }); + }); }); }); From ef03880a3130c217e57ea3b4d81a0b9b8e3b3a28 Mon Sep 17 00:00:00 2001 From: Mike Kreiser Date: Sun, 29 Oct 2023 13:25:35 -0400 Subject: [PATCH 3/4] Adjust snapshots to include scoring settings --- .../__snapshots__/integration.test.js.snap | 44 ++++++++++++++++++ .../__snapshots__/integration.test.js.snap | 45 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/integration-tests/2018-season/__snapshots__/integration.test.js.snap b/integration-tests/2018-season/__snapshots__/integration.test.js.snap index 4e30127..598016b 100644 --- a/integration-tests/2018-season/__snapshots__/integration.test.js.snap +++ b/integration-tests/2018-season/__snapshots__/integration.test.js.snap @@ -56424,6 +56424,50 @@ League { "playoffMatchupLength": 2, "regularSeasonMatchupLength": 1, }, + "scoringSettings": { + "defensive0PointsAllowed": 5, + "defensive100To199YardsAllowed": 3, + "defensive14To17PointsAllowed": 1, + "defensive1To6PointsAllowed": 4, + "defensive200To299YardsAllowed": 2, + "defensive28To34PointsAllowed": -1, + "defensive350To399YardsAllowed": -1, + "defensive35To45PointsAllowed": -3, + "defensive400To449YardsAllowed": -3, + "defensive450To499YardsAllowed": -5, + "defensive500To549YardsAllowed": -6, + "defensive7To13PointsAllowed": 3, + "defensiveBlockedKickForTouchdowns": 6, + "defensiveBlockedKicks": 2, + "defensiveFumbles": 2, + "defensiveInterceptions": 2, + "defensiveLessThan100YardsAllowed": 5, + "defensiveOver45PointsAllowed": -5, + "defensiveOver550YardsAllowed": -7, + "defensiveSacks": 1, + "defensiveSafeties": 2, + "fumbleReturnTouchdown": 6, + "interceptionReturnTouchdown": 6, + "kickoffReturnTouchdown": 6, + "lostFumbles": -2, + "madeExtraPoints": 1, + "madeFieldGoalsFrom40To49": 4, + "madeFieldGoalsFrom50Plus": 5, + "madeFieldGoalsFromUnder40": 3, + "missedExtraPoints": -1, + "missedFieldGoals": -1, + "passing2PtConversions": 2, + "passingInterceptions": -2, + "passingTouchdowns": 4, + "passingYards": 0.04, + "puntReturnTouchdown": 6, + "receiving2PtConversions": 2, + "receivingTouchdowns": 6, + "receivingYards": 0.1, + "rushing2PtConversions": 2, + "rushingTouchdowns": 6, + "rushingYards": 0.1, + }, "size": 8, } `; diff --git a/integration-tests/2022-season/__snapshots__/integration.test.js.snap b/integration-tests/2022-season/__snapshots__/integration.test.js.snap index 257cef9..f3ec7ba 100644 --- a/integration-tests/2022-season/__snapshots__/integration.test.js.snap +++ b/integration-tests/2022-season/__snapshots__/integration.test.js.snap @@ -62478,6 +62478,51 @@ League { "playoffMatchupLength": 1, "regularSeasonMatchupLength": 1, }, + "scoringSettings": { + "defensive0PointsAllowed": 5, + "defensive100To199YardsAllowed": 3, + "defensive14To17PointsAllowed": 1, + "defensive1To6PointsAllowed": 4, + "defensive200To299YardsAllowed": 2, + "defensive28To34PointsAllowed": -1, + "defensive350To399YardsAllowed": -1, + "defensive35To45PointsAllowed": -3, + "defensive400To449YardsAllowed": -3, + "defensive450To499YardsAllowed": -5, + "defensive500To549YardsAllowed": -6, + "defensive7To13PointsAllowed": 3, + "defensiveBlockedKickForTouchdowns": 6, + "defensiveBlockedKicks": 3, + "defensiveFumbles": 2, + "defensiveInterceptions": 2, + "defensiveLessThan100YardsAllowed": 5, + "defensiveOver45PointsAllowed": -5, + "defensiveOver550YardsAllowed": -7, + "defensiveSacks": 1, + "defensiveSafeties": 4, + "fumbleReturnTouchdown": 6, + "interceptionReturnTouchdown": 6, + "kickoffReturnTouchdown": 6, + "lostFumbles": -2, + "madeExtraPoints": 1, + "madeFieldGoalsFrom40To49": 4, + "madeFieldGoalsFrom60Plus": 5, + "madeFieldGoalsFromUnder40": 3, + "missedExtraPoints": -1, + "missedFieldGoals": -1, + "passing2PtConversions": 2, + "passingInterceptions": -2, + "passingTouchdowns": 4, + "passingYards": 0.04, + "puntReturnTouchdown": 6, + "receiving2PtConversions": 2, + "receivingReceptions": 0.5, + "receivingTouchdowns": 6, + "receivingYards": 0.1, + "rushing2PtConversions": 2, + "rushingTouchdowns": 6, + "rushingYards": 0.1, + }, "size": 10, } `; From 2e07f8a704ac4b6bebd5f3ee86f17c1cbb1552c7 Mon Sep 17 00:00:00 2001 From: Mike Kreiser Date: Sun, 29 Oct 2023 13:27:53 -0400 Subject: [PATCH 4/4] 1.8.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b525403..2f14b7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "espn-fantasy-football-api", - "version": "1.7.0", + "version": "1.8.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "espn-fantasy-football-api", - "version": "1.7.0", + "version": "1.8.0", "license": "LGPL-3.0-only", "dependencies": { "axios": "^1.5.0", diff --git a/package.json b/package.json index 9dcbfc6..9bfe922 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "espn-fantasy-football-api", - "version": "1.7.0", + "version": "1.8.0", "description": "A Javascript API to connect to ESPN's fantasy football API", "main": "web.js", "files": [