Verbessere die Berechnung der Geschenkekosten in FalukantService und füge Tests für die Funktion hinzu

This commit is contained in:
Torsten Schulz (local)
2026-02-14 16:12:07 +01:00
parent a3b550859c
commit be7db6ad96
3 changed files with 82 additions and 13 deletions

View File

@@ -3112,17 +3112,24 @@ class FalukantService extends BaseService {
// 5) Rest wie gehabt: Kosten berechnen und zurückgeben
const lowestTitleOfNobility = await TitleOfNobility.findOne({ order: [['id', 'ASC']] });
return Promise.all(gifts.map(async gift => ({
id: gift.id,
name: gift.name,
cost: await this.getGiftCost(
gift.value,
myChar.titleOfNobility,
lowestTitleOfNobility.id
),
moodsAffects: gift.promotionalgiftmoods, // nur Einträge mit relatedMoodId
charactersAffects: gift.characterTraits // nur Einträge mit relatedTraitIds
})));
// Sicherstellen, dass wir eine gültige titleOfNobility haben (getFalukantUserByHashedId liefert nicht immer das Feld)
let characterTitleOfNobility = myChar.titleOfNobility;
if (characterTitleOfNobility == null && myChar.id) {
const reloadChar = await FalukantCharacter.findOne({ where: { id: myChar.id }, attributes: ['titleOfNobility'] });
characterTitleOfNobility = reloadChar?.titleOfNobility ?? lowestTitleOfNobility?.id ?? 1;
}
return Promise.all(gifts.map(async gift => {
const value = typeof gift.value === 'number' ? gift.value : (gift.value ? Number(gift.value) : 0);
const cost = await this.getGiftCost(value, characterTitleOfNobility, lowestTitleOfNobility?.id ?? 1);
return {
id: gift.id,
name: gift.name,
cost,
moodsAffects: gift.promotionalgiftmoods, // nur Einträge mit relatedMoodId
charactersAffects: gift.characterTraits // nur Einträge mit relatedTraitIds
};
}));
}
async getChildren(hashedUserId) {
@@ -3279,8 +3286,12 @@ class FalukantService extends BaseService {
}
async getGiftCost(value, titleOfNobility, lowestTitleOfNobility) {
const titleLevel = titleOfNobility - lowestTitleOfNobility + 1;
return Math.round(value * Math.pow(1 + titleLevel * 0.3, 1.3) * 100) / 100;
const val = Number(value) || 0;
const title = Number(titleOfNobility) || 1;
const lowest = Number(lowestTitleOfNobility) || 1;
const titleLevel = title - lowest + 1;
const cost = Math.round(val * Math.pow(1 + titleLevel * 0.3, 1.3) * 100) / 100;
return Number.isFinite(cost) ? cost : 0;
}
async getTitlesOfNobility() {