From 45dad54e3eacaa7a6809d27402be8e1e8664553d Mon Sep 17 00:00:00 2001 From: TheSparta Date: Sat, 4 Nov 2023 14:53:08 +0000 Subject: [PATCH] Fixed PlayerService.calculateLevel --- project/src/services/PlayerService.ts | 13 +++++++------ project/tests/services/PlayerService.test.ts | 13 +++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/project/src/services/PlayerService.ts b/project/src/services/PlayerService.ts index 99594998..190ce346 100644 --- a/project/src/services/PlayerService.ts +++ b/project/src/services/PlayerService.ts @@ -60,19 +60,20 @@ export class PlayerService */ public calculateLevel(pmcData: IPmcData): number { - let exp = 0; + let accExp = 0; - for (const level in this.databaseServer.getTables().globals.config.exp.level.exp_table) + for (const [level, { exp }] of this.databaseServer.getTables().globals.config.exp.level.exp_table.entries()) { - if (pmcData.Info.Experience < exp) + accExp += exp; + + if (pmcData.Info.Experience < accExp) { break; } - pmcData.Info.Level = parseInt(level); - exp += this.databaseServer.getTables().globals.config.exp.level.exp_table[level].exp; + pmcData.Info.Level = level + 1; } return pmcData.Info.Level; } -} \ No newline at end of file +} diff --git a/project/tests/services/PlayerService.test.ts b/project/tests/services/PlayerService.test.ts index dc0f9955..66e66d5d 100644 --- a/project/tests/services/PlayerService.test.ts +++ b/project/tests/services/PlayerService.test.ts @@ -21,6 +21,19 @@ describe("PlayerService", () => describe("calculateLevel", () => { + it("should return 1 when player xp is 0", () => + { + const playerProfile = { + Info: { + Experience: 0 // Via wiki: https://escapefromtarkov.fandom.com/wiki/Character_skills#Levels + } + }; + + const result = playerService.calculateLevel(playerProfile as IPmcData); + + expect(result).toBe(1); + }); + it("should return 1 when player xp is 999", () => { const playerProfile = {