Skip to content

Commit

Permalink
feat(Replay): add .apples to show collected apples (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexjelly authored Jun 21, 2020
1 parent 10aa958 commit 5544d3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions __tests__/rec.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,15 @@ describe('Replay', () => {
expect(replay.toBuffer()).toEqual(file);
},
);

test.each([
['rec_valid_1.rec', 2],
['rec_valid_2.rec', 4],
['rec_valid_3.rec', 4],
])('.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);
});
});
13 changes: 13 additions & 0 deletions src/rec/Replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,17 @@ export default class Replay {
time: Math.round(maxEventTime),
};
}

/**
* Returns the number of apples collected in the replay.
*
* @returns Number of apples
*/
get apples(): number {
const apples = this.rides.reduce((apples, ride) => {
apples += ride.events.filter((event) => event.type === 4).length;
return apples;
}, 0);
return apples;
}
}

0 comments on commit 5544d3a

Please sign in to comment.