Skip to content

Commit

Permalink
Merge pull request #238 from mkreiser/scoring-settings
Browse files Browse the repository at this point in the history
Add scoring settings to League
  • Loading branch information
mkreiser authored Oct 29, 2023
2 parents 1a7ebf0 + 2e07f8a commit 8feaba3
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
`;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
16 changes: 9 additions & 7 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -1135,7 +1134,10 @@ describe('Client', () => {
name: 'some league',
draftSettings: {},
rosterSettings: {},
scheduleSettings: {}
scheduleSettings: {},
scoringSettings: {
scoringItems: []
}
},
status: {
currentMatchupPeriod: 7,
Expand Down
25 changes: 25 additions & 0 deletions src/league/league.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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.
*/

/**
Expand Down Expand Up @@ -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;
},
{}
)
}
};
}
Expand Down
24 changes: 23 additions & 1 deletion src/league/league.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
}
};
});

Expand Down Expand Up @@ -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
});
});
});
});
});
5 changes: 5 additions & 0 deletions src/player-stats/player-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) => {
Expand Down

0 comments on commit 8feaba3

Please sign in to comment.