Enhance partner search and gift loading functionality in FalukantService and FamilyView
- Added detailed logging for partner search criteria and results in FalukantService to improve debugging and traceability. - Refactored partner search logic to use a dynamic where clause for better readability and maintainability. - Implemented error handling in FamilyView for gift loading, ensuring an empty array is set on failure to load gifts, enhancing user experience.
This commit is contained in:
@@ -3175,29 +3175,54 @@ class FalukantService extends BaseService {
|
||||
}
|
||||
const minTitle = minTitleResult.id;
|
||||
|
||||
// Logging für Debugging
|
||||
console.log(`[createPossiblePartners] Searching for partners:`, {
|
||||
requestingCharacterId,
|
||||
requestingCharacterGender,
|
||||
requestingRegionId,
|
||||
requestingCharacterTitleOfNobility,
|
||||
ownAge
|
||||
});
|
||||
|
||||
const whereClause = {
|
||||
id: { [Op.ne]: requestingCharacterId },
|
||||
gender: { [Op.ne]: requestingCharacterGender },
|
||||
regionId: requestingRegionId,
|
||||
createdAt: { [Op.lt]: new Date(new Date() - 12 * 24 * 60 * 60 * 1000) },
|
||||
titleOfNobility: { [Op.between]: [Math.max(1, requestingCharacterTitleOfNobility - 1), requestingCharacterTitleOfNobility + 1] }
|
||||
};
|
||||
|
||||
// Nur NPCs suchen (userId ist null)
|
||||
whereClause.userId = null;
|
||||
|
||||
console.log(`[createPossiblePartners] Where clause:`, JSON.stringify(whereClause, null, 2));
|
||||
|
||||
const potentialPartners = await FalukantCharacter.findAll({
|
||||
where: {
|
||||
id: { [Op.ne]: requestingCharacterId },
|
||||
gender: { [Op.ne]: requestingCharacterGender },
|
||||
regionId: requestingRegionId,
|
||||
createdAt: { [Op.lt]: new Date(new Date() - 12 * 24 * 60 * 60 * 1000) },
|
||||
titleOfNobility: { [Op.between]: [requestingCharacterTitleOfNobility - 1, requestingCharacterTitleOfNobility + 1] }
|
||||
},
|
||||
where: whereClause,
|
||||
order: [
|
||||
[Sequelize.literal(`ABS((EXTRACT(EPOCH FROM (NOW() - "birthdate")) / 86400) - ${ownAge})`), 'ASC']
|
||||
],
|
||||
limit: 5,
|
||||
});
|
||||
|
||||
console.log(`[createPossiblePartners] Found ${potentialPartners.length} potential partners`);
|
||||
|
||||
if (potentialPartners.length === 0) {
|
||||
console.warn(`[createPossiblePartners] No partners found with criteria. Consider creating NPCs.`);
|
||||
return; // Keine Partner gefunden, aber kein Fehler
|
||||
}
|
||||
|
||||
const proposals = potentialPartners.map(partner => {
|
||||
const age = calcAge(partner.birthdate);
|
||||
return {
|
||||
requesterCharacterId: requestingCharacterId,
|
||||
proposedCharacterId: partner.id,
|
||||
cost: calculateMarriageCost(partner.titleOfNobility, age, minTitle),
|
||||
cost: calculateMarriageCost(partner.titleOfNobility, age),
|
||||
};
|
||||
});
|
||||
|
||||
await MarriageProposal.bulkCreate(proposals);
|
||||
console.log(`[createPossiblePartners] Created ${proposals.length} marriage proposals`);
|
||||
} catch (error) {
|
||||
console.error('Error creating possible partners:', error);
|
||||
throw error;
|
||||
@@ -3250,7 +3275,7 @@ class FalukantService extends BaseService {
|
||||
const myChar = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
||||
if (!myChar) throw new Error('Character not found');
|
||||
|
||||
// 2) Beziehung finden und „anderen“ Character bestimmen
|
||||
// 2) Beziehung finden und „anderen" Character bestimmen
|
||||
const rel = await Relationship.findOne({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
@@ -3263,32 +3288,44 @@ class FalukantService extends BaseService {
|
||||
{ model: FalukantCharacter, as: 'character2', include: [{ model: CharacterTrait, as: 'traits' }] }
|
||||
]
|
||||
});
|
||||
if (!rel) throw new Error('Beziehung nicht gefunden');
|
||||
|
||||
const relatedChar = rel.character1.id === myChar.id ? rel.character2 : rel.character1;
|
||||
// 3) Wenn keine Beziehung gefunden, alle Gifts ohne Filter zurückgeben
|
||||
let relatedTraitIds = [];
|
||||
let relatedMoodId = null;
|
||||
|
||||
// 3) Trait-IDs und Mood des relatedChar
|
||||
const relatedTraitIds = relatedChar.traits.map(t => t.id);
|
||||
const relatedMoodId = relatedChar.moodId;
|
||||
if (rel) {
|
||||
const relatedChar = rel.character1.id === myChar.id ? rel.character2 : rel.character1;
|
||||
// Trait-IDs und Mood des relatedChar
|
||||
relatedTraitIds = relatedChar.traits ? relatedChar.traits.map(t => t.id) : [];
|
||||
relatedMoodId = relatedChar.moodId;
|
||||
}
|
||||
|
||||
// 4) Gifts laden – mit Mood/Trait-Filter nur wenn Beziehung existiert
|
||||
const giftIncludes = [
|
||||
{
|
||||
model: PromotionalGiftMood,
|
||||
as: 'promotionalgiftmoods',
|
||||
attributes: ['mood_id', 'suitability'],
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: PromotionalGiftCharacterTrait,
|
||||
as: 'characterTraits',
|
||||
attributes: ['trait_id', 'suitability'],
|
||||
required: false
|
||||
}
|
||||
];
|
||||
|
||||
// Wenn Beziehung existiert, Filter anwenden
|
||||
if (rel && relatedMoodId) {
|
||||
giftIncludes[0].where = { mood_id: relatedMoodId };
|
||||
}
|
||||
if (rel && relatedTraitIds.length > 0) {
|
||||
giftIncludes[1].where = { trait_id: relatedTraitIds };
|
||||
}
|
||||
|
||||
// 4) Gifts laden – aber nur die passenden Moods und Traits als Unter-Arrays
|
||||
const gifts = await PromotionalGift.findAll({
|
||||
include: [
|
||||
{
|
||||
model: PromotionalGiftMood,
|
||||
as: 'promotionalgiftmoods',
|
||||
attributes: ['mood_id', 'suitability'],
|
||||
where: { mood_id: relatedMoodId },
|
||||
required: false // Gifts ohne Mood-Match bleiben erhalten, haben dann leeres Array
|
||||
},
|
||||
{
|
||||
model: PromotionalGiftCharacterTrait,
|
||||
as: 'characterTraits',
|
||||
attributes: ['trait_id', 'suitability'],
|
||||
where: { trait_id: relatedTraitIds },
|
||||
required: false // Gifts ohne Trait-Match bleiben erhalten
|
||||
}
|
||||
]
|
||||
include: giftIncludes
|
||||
});
|
||||
|
||||
// 5) Rest wie gehabt: Kosten berechnen und zurückgeben
|
||||
|
||||
Reference in New Issue
Block a user