Skip to content

Commit

Permalink
feat(@osrs-tracker/hiscores): added helper functions for XP calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekMencke committed Feb 27, 2024
1 parent 345df2c commit efd3dc5
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 8 deletions.
6 changes: 6 additions & 0 deletions @osrs-tracker/hiscores/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.2.0 - 2024/02/27

- Added methods for XP calculation
- `calculateXPForSkillLevel(level: number): number`
- `calculateXPToNextLevel(currentXP: number, currentLevel: number)`

## v0.1.3 - 2023/09/11

- Add scurrius to parser to fix parser.
Expand Down
3 changes: 2 additions & 1 deletion @osrs-tracker/hiscores/README.md
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.
5 changes: 3 additions & 2 deletions @osrs-tracker/hiscores/dist/index.d.ts
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';
5 changes: 3 additions & 2 deletions @osrs-tracker/hiscores/dist/index.js
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';
2 changes: 2 additions & 0 deletions @osrs-tracker/hiscores/dist/xp/levels.d.ts
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;
11 changes: 11 additions & 0 deletions @osrs-tracker/hiscores/dist/xp/levels.js
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;
}
2 changes: 1 addition & 1 deletion @osrs-tracker/hiscores/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@osrs-tracker/hiscores",
"version": "0.1.3",
"version": "0.2.0",
"description": "Logic for parsing OSRS hiscores",
"license": "MIT",
"author": "Freek Mencke",
Expand Down
5 changes: 3 additions & 2 deletions @osrs-tracker/hiscores/src/index.ts
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';
72 changes: 72 additions & 0 deletions @osrs-tracker/hiscores/src/xp/levels.spec.ts
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);
});
});
12 changes: 12 additions & 0 deletions @osrs-tracker/hiscores/src/xp/levels.ts
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;
}

0 comments on commit efd3dc5

Please sign in to comment.