-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@osrs-tracker/hiscores): added helper functions for XP calculations
- Loading branch information
1 parent
345df2c
commit efd3dc5
Showing
10 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# OSRS Tracker · [![NPM package](https://img.shields.io/npm/v/@osrs-tracker/hiscores.svg)](https://www.npmjs.com/package/@osrs-tracker/hiscores) [![GitHub license](https://img.shields.io/github/license/osrs-tracker/osrs-tracker-aws.svg)](https://github.com/osrs-tracker/osrs-tracker-aws/blob/master/LICENSE) | ||
|
||
This package contains the logic for parsing OSRS hiscores for all dates since `2023-03-15`. | ||
This package contains the logic for parsing OSRS hiscores for all dates since `2023-03-15`. It also contains some helper | ||
functions for XP calculations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './models/hiscore.model'; | ||
export * from './models/hiscore.enum'; | ||
export * from './parser/parser'; | ||
export * from './models/hiscore.model'; | ||
export * from './parser/parse-order/parse-order'; | ||
export * from './parser/parser'; | ||
export * from './xp/levels'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './models/hiscore.model'; | ||
export * from './models/hiscore.enum'; | ||
export * from './parser/parser'; | ||
export * from './models/hiscore.model'; | ||
export * from './parser/parse-order/parse-order'; | ||
export * from './parser/parser'; | ||
export * from './xp/levels'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export declare function calculateXPForSkillLevel(level: number): number; | ||
export declare function calculateXPToNextLevel(currentXP: number, currentLevel: number): number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function calculateXPForSkillLevel(level) { | ||
let total = 0; | ||
for (let i = 1; i < level; i++) { | ||
total += Math.floor(i + 300 * Math.pow(2, i / 7)); | ||
} | ||
return Math.floor(total / 4); | ||
} | ||
export function calculateXPToNextLevel(currentXP, currentLevel) { | ||
const totalXPNextLevel = calculateXPForSkillLevel(currentLevel + 1); | ||
return totalXPNextLevel - currentXP; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './models/hiscore.model'; | ||
export * from './models/hiscore.enum'; | ||
export * from './parser/parser'; | ||
export * from './models/hiscore.model'; | ||
export * from './parser/parse-order/parse-order'; | ||
export * from './parser/parser'; | ||
export * from './xp/levels'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { calculateXPForSkillLevel, calculateXPToNextLevel } from './levels'; | ||
|
||
describe('calculateTotalXP', () => { | ||
it('should return 0 for level 1', () => { | ||
expect(calculateXPForSkillLevel(1)).toBe(0); | ||
}); | ||
|
||
it('should return 83 for level 2', () => { | ||
expect(calculateXPForSkillLevel(2)).toBe(83); | ||
}); | ||
|
||
it('should return 174 for level 3', () => { | ||
expect(calculateXPForSkillLevel(3)).toBe(174); | ||
}); | ||
|
||
// skip 30 levels | ||
|
||
it('should return 20.224 for level 34', () => { | ||
expect(calculateXPForSkillLevel(34)).toBe(20224); | ||
}); | ||
|
||
it('should return 22.406 for level 35', () => { | ||
expect(calculateXPForSkillLevel(35)).toBe(22406); | ||
}); | ||
|
||
it('should return 24.815 for level 36', () => { | ||
expect(calculateXPForSkillLevel(36)).toBe(24815); | ||
}); | ||
|
||
// skip 30 levels | ||
|
||
it('should return 547.953 for level 67', () => { | ||
expect(calculateXPForSkillLevel(67)).toBe(547953); | ||
}); | ||
|
||
it('should return 605.032 for level 68', () => { | ||
expect(calculateXPForSkillLevel(68)).toBe(605032); | ||
}); | ||
|
||
it('should return 668.051 for level 69', () => { | ||
expect(calculateXPForSkillLevel(69)).toBe(668051); | ||
}); | ||
|
||
// skip 27 levels | ||
|
||
it('should return 10.692.629 for level 97', () => { | ||
expect(calculateXPForSkillLevel(97)).toBe(10692629); | ||
}); | ||
|
||
it('should return 11.805.606 for level 98', () => { | ||
expect(calculateXPForSkillLevel(98)).toBe(11805606); | ||
}); | ||
|
||
it('should return 13.034.431 for level 99', () => { | ||
expect(calculateXPForSkillLevel(99)).toBe(13034431); | ||
}); | ||
}); | ||
|
||
describe('calculateXPToNextLevel', () => { | ||
it('should return correct XP for next level when current XP is 0', () => { | ||
expect(calculateXPToNextLevel(0, 1)).toBe(calculateXPForSkillLevel(2)); | ||
expect(calculateXPToNextLevel(calculateXPForSkillLevel(2), 2)).toBe( | ||
calculateXPForSkillLevel(3) - calculateXPForSkillLevel(2), | ||
); | ||
}); | ||
|
||
it('should return correct XP for next level when current XP is halfway to next level', () => { | ||
const halfwayXP = calculateXPForSkillLevel(2) / 2; | ||
expect(calculateXPToNextLevel(halfwayXP, 1)).toBe(calculateXPForSkillLevel(2) - halfwayXP); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function calculateXPForSkillLevel(level: number): number { | ||
let total = 0; | ||
for (let i = 1; i < level; i++) { | ||
total += Math.floor(i + 300 * Math.pow(2, i / 7)); | ||
} | ||
return Math.floor(total / 4); | ||
} | ||
|
||
export function calculateXPToNextLevel(currentXP: number, currentLevel: number): number { | ||
const totalXPNextLevel = calculateXPForSkillLevel(currentLevel + 1); | ||
return totalXPNextLevel - currentXP; | ||
} |