diff --git a/replays/standard_129_3on3_leaver.w3g b/replays/standard_129_3on3_leaver.w3g new file mode 100644 index 0000000..609d824 Binary files /dev/null and b/replays/standard_129_3on3_leaver.w3g differ diff --git a/src/Player.ts b/src/Player.ts index e71e667..19c9916 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -304,7 +304,11 @@ class Player { cleanup (): void { const apmSum = this.actions.timed.reduce((a: number, b: number): number => a + b) - this.apm = Math.round(apmSum / this.actions.timed.length) + if (this.currentTimePlayed === 0) { + this.apm = 0 + } else { + this.apm = Math.round(apmSum / (this.currentTimePlayed / 1000 / 60)) + } this.heroes = reduceHeroes(this.heroCollector) delete this._currentlyTrackedAPM } diff --git a/test/replays.test.ts b/test/replays.test.ts index 7dd65d7..dec7f15 100644 --- a/test/replays.test.ts +++ b/test/replays.test.ts @@ -269,4 +269,16 @@ describe('Replay parsing tests', () => { expect(test.version).toBe('1.31') expect(test.players.length).toBe(1) }) + + it('evaluates APM correctly in a team game with an early leaver', () => { + const test = Parser.parse('./replays/standard_129_3on3_leaver.w3g') + const firstLeftMinute = Math.ceil(test.players[0].currentTimePlayed / 1000 / 60) + const postLeaveBlocks = test.players[0].actions.timed.slice(firstLeftMinute) + const postLeaveApmSum = postLeaveBlocks.reduce((a, b) => a + b) + expect(test.players[0].name).toBe('abmitdirpic') + expect(postLeaveApmSum).toEqual(0) + expect(test.players[0].apm).toEqual(98) + expect(test.players[0].currentTimePlayed).toEqual(4371069) + expect(Parser.msElapsed).toEqual(6433136) + }) })