Skip to content

Commit

Permalink
refactor(XPChart): Centralized forumla
Browse files Browse the repository at this point in the history
  • Loading branch information
SobieskiCodes committed Jul 20, 2024
1 parent 02ffe42 commit b5db2f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
16 changes: 2 additions & 14 deletions src/components/XPCurve.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Tooltip,
Legend,
} from 'chart.js';
import { calculateXPForLevel } from '../utils/xpUtils';

// Register the necessary components
ChartJS.register(
Expand All @@ -22,22 +23,9 @@ ChartJS.register(
Legend
);

const calculateXP = (n) => {
if (n <= 30) {
// Polynomial function for levels 1-30
const a = 3e6 / Math.pow(30, 2); // a = 3,000,000 / 900
return a * Math.pow(n, 2);
} else {
// Exponential function for levels 31-100
const C = 3e6; // XP at level 30
const D = (200e6 - C) / Math.pow(70, 2); // Growth rate for levels 31-100
return C + D * Math.pow(n - 30, 2);
}
};

const XPCurve = () => {
const levels = Array.from({ length: 100 }, (_, i) => i + 1);
const xpValues = levels.map(calculateXP);
const xpValues = levels.map(calculateXPForLevel);

const data = {
labels: levels,
Expand Down
2 changes: 1 addition & 1 deletion src/features/player/playerInitialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const initializeInventory = (initialItems, maxSlots) => {
};

const initialItems = [
{ ...items.resource.treasurechest, quantity: 6 },
//{ ...items.resource.treasurechest, quantity: 6 },
];

const loadInitialState = () => {
Expand Down
27 changes: 5 additions & 22 deletions src/features/player/xpCalculator.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
export const calculateLevelFromXP = (xp) => {
const polynomialXP = (n) => Math.round(3e6 / Math.pow(30, 2) * Math.pow(n, 2));
const exponentialXP = (n) => Math.round(3e6 + (200e6 - 3e6) / Math.pow(70, 2) * Math.pow(n - 30, 2));
import { calculateXPForLevel } from '../../utils/xpUtils';

export const calculateLevelFromXP = (xp) => {
let requiredXP = 0;
let previousXP = 0;

for (let level = 1; level <= 100; level++) {
previousXP = requiredXP;
if (level <= 30) {
requiredXP = polynomialXP(level);
} else {
requiredXP = exponentialXP(level);
}
console.log(`Level: ${level}, XP: ${xp}, Previous XP: ${previousXP}, Required XP: ${requiredXP}`);
requiredXP = calculateXPForLevel(level);

if (xp >= previousXP && xp < requiredXP) {
return level;
}
Expand All @@ -21,18 +16,6 @@ export const calculateLevelFromXP = (xp) => {
return 100; // Max level
};






export const getRequiredXPForLevel = (level) => {
const polynomialXP = (n) => Math.round(3e6 / Math.pow(30, 2) * Math.pow(n, 2));
const exponentialXP = (n) => Math.round(3e6 + (200e6 - 3e6) / Math.pow(70, 2) * Math.pow(n - 30, 2));

if (level <= 30) {
return polynomialXP(level);
} else {
return exponentialXP(level);
}
return calculateXPForLevel(level);
};
12 changes: 12 additions & 0 deletions src/utils/xpUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const calculateXPForLevel = (level) => {
if (level <= 30) {
// Polynomial function for levels 1-30
const a = 3e6 / Math.pow(30, 2); // a = 3,000,000 / 900
return Math.round(a * Math.pow(level, 2));
} else {
// Exponential function for levels 31-100
const C = 3e6; // XP at level 30
const D = (200e6 - C) / Math.pow(70, 2); // Growth rate for levels 31-100
return Math.round(C + D * Math.pow(level - 30, 2));
}
};

0 comments on commit b5db2f7

Please sign in to comment.