Skip to content

Commit

Permalink
feat(Replay): add separate .totalApples count
Browse files Browse the repository at this point in the history
  • Loading branch information
hexjelly committed Nov 23, 2020
1 parent 2597f7d commit ba863a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
Binary file added __tests__/assets/replays/appbug.rec
Binary file not shown.
13 changes: 13 additions & 0 deletions __tests__/rec.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,26 @@ describe('Replay', () => {
['rec_valid_1.rec', 2],
['rec_valid_2.rec', 4],
['rec_valid_3.rec', 4],
['appbug.rec', 19],
])('.apples counts correct number of apples: %s', async (fileName, apples) => {
const filePath = `__tests__/assets/replays/${fileName}`;
const file = await readFile(filePath);
const replay = Replay.from(file);
expect(replay.apples).toEqual(apples);
});

test.each([
['rec_valid_1.rec', 2],
['rec_valid_2.rec', 4],
['rec_valid_3.rec', 4],
['appbug.rec', 36],
])('.totalApples counts correct number of apples: %s', async (fileName, apples) => {
const filePath = `__tests__/assets/replays/${fileName}`;
const file = await readFile(filePath);
const replay = Replay.from(file);
expect(replay.totalApples).toEqual(apples);
});

test('.getTime() calculates edge case times correctly', async () => {
const filePath = `__tests__/assets/replays/kelogs06vz.rec`;
const file = await readFile(filePath);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:umd": "webpack",
"build:docs": "typedoc ./src",
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint src --ext .ts",
"coverage": "npm run test -- --coverage",
"prepublishOnly": "npm run build && npm run test"
Expand Down
26 changes: 26 additions & 0 deletions src/rec/Replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,32 @@ export default class Replay {
* @returns Number of apples
*/
get apples(): number {
let apples = 0;

for (const ride of this.rides) {
const touchEvents = ride.events.filter((event) => event.type === 0);
const appleEvents = ride.events.filter((event) => event.type === 4);
const unique = [...new Set(touchEvents.map((event) => event.touchInfo))].map((event, idx) => touchEvents[idx]);

appleEvents.forEach((appleEvent) => {
const touchEventWithSameTime = unique.findIndex((touchEvent) => {
return touchEvent.time === appleEvent.time;
});

if (touchEventWithSameTime > -1) {
unique.splice(touchEventWithSameTime, 1);
apples += 1;
}
});
}

return apples;
}

/**
* Returns the total number of apples collected in the replay (including "apple bugs")
*/
get totalApples(): number {
const apples = this.rides.reduce((apples, ride) => {
apples += ride.events.filter((event) => event.type === 4).length;
return apples;
Expand Down

0 comments on commit ba863a0

Please # to comment.