fix: level-up formula uses current level, add xpToNextLevel to API
All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 33s

XP threshold was computed on level+1 (target), making early levels too
steep (283 XP for level 2 instead of 100). Now uses current level:
level 1→2 = 100 XP, level 2→3 = 283 XP, level 10→11 = 3162 XP.

Added xpToNextLevel field to character and combat responses so the
frontend can display accurate progress bars.
This commit is contained in:
2026-03-24 16:02:51 +01:00
parent 6df11f2860
commit 214045c7ce
3 changed files with 18 additions and 6 deletions

View File

@@ -11,6 +11,7 @@ import { LevelThreshold } from './entities/level-threshold.entity';
import { CreateCharacterDto } from './dto/create-character.dto';
import { DistributeStatsDto } from './dto/distribute-stats.dto';
import { User } from '../user/user.entity';
import { xpRequiredForLevel } from '../combat/combat.engine';
const STAT_POOL = 10; // 5 stats × 1 base + 5 points à distribuer
const ENDURANCE_REGEN_MINUTES = 6; // 1 pt d'endurance toutes les 6 min = 10 pts/heure
@@ -35,7 +36,7 @@ export class CharacterService {
return Math.min(character.enduranceSaved + recharge, character.enduranceMax);
}
async create(dto: CreateCharacterDto, user: User): Promise<Character & { enduranceCurrent: number }> {
async create(dto: CreateCharacterDto, user: User) {
const totalStats =
dto.force + dto.agilite + dto.intelligence + dto.chance + dto.vitalite;
@@ -66,10 +67,14 @@ export class CharacterService {
});
const saved = await this.characterRepository.save(character);
return { ...saved, enduranceCurrent: this.calculateEndurance(saved) };
return {
...saved,
enduranceCurrent: this.calculateEndurance(saved),
xpToNextLevel: xpRequiredForLevel(saved.level),
};
}
async findByUser(user: User): Promise<Character & { enduranceCurrent: number }> {
async findByUser(user: User) {
const character = await this.characterRepository.findOne({
where: { userId: user.id },
});
@@ -78,7 +83,11 @@ export class CharacterService {
throw new NotFoundException('Aucun personnage trouvé pour ce joueur');
}
return { ...character, enduranceCurrent: this.calculateEndurance(character) };
return {
...character,
enduranceCurrent: this.calculateEndurance(character),
xpToNextLevel: xpRequiredForLevel(character.level),
};
}
async getEndurance(

View File

@@ -181,9 +181,10 @@ export function applyXpGain(currentLevel: number, currentXp: number, xpEarned: n
let xp = currentXp + xpEarned;
let statPointsGained = 0;
// Chaîne de level up
// Chaîne de level up — seuil basé sur le niveau actuel
// Level 1→2 = 100 XP, Level 2→3 = 283 XP, Level 10→11 = 3162 XP
while (level < 100) {
const required = xpRequiredForLevel(level + 1);
const required = xpRequiredForLevel(level);
if (xp >= required) {
xp -= required;
level++;

View File

@@ -14,6 +14,7 @@ import { CommunityService } from '../community/community.service';
import {
resolveCombat,
applyXpGain,
xpRequiredForLevel,
CombatantStats,
} from './combat.engine';
@@ -237,6 +238,7 @@ export class CombatService {
character: {
level: character.level,
xp: character.xp,
xpToNextLevel: xpRequiredForLevel(character.level),
gold: character.gold,
hpCurrent: character.hpCurrent,
hpMax: character.hpMax,