Enhance proposal generation logic in FalukantService: Refactor character selection process to include region and age filters, improving the randomness and relevance of director character proposals. Implement fallback mechanism for character retrieval to ensure availability, enhancing overall proposal generation reliability.
This commit is contained in:
@@ -2223,26 +2223,64 @@ class FalukantService extends BaseService {
|
|||||||
|
|
||||||
async generateProposals(falukantUserId, regionId) {
|
async generateProposals(falukantUserId, regionId) {
|
||||||
try {
|
try {
|
||||||
const threeWeeksAgo = new Date(Date.now() - 21 * 24 * 60 * 60 * 1000);
|
const twentyOneDaysAgo = new Date(Date.now() - 21 * 24 * 60 * 60 * 1000);
|
||||||
|
const relevantRegionIds = await this.getRegionAndParentIds(regionId);
|
||||||
|
|
||||||
|
const employerCharacter = await FalukantCharacter.findOne({
|
||||||
|
where: { userId: falukantUserId },
|
||||||
|
attributes: ['id']
|
||||||
|
});
|
||||||
|
const excludeCharacterId = employerCharacter?.id ?? null;
|
||||||
|
|
||||||
const proposalCount = Math.floor(Math.random() * 3) + 3;
|
const proposalCount = Math.floor(Math.random() * 3) + 3;
|
||||||
|
const usedCharacterIds = new Set();
|
||||||
|
|
||||||
for (let i = 0; i < proposalCount; i++) {
|
for (let i = 0; i < proposalCount; i++) {
|
||||||
const directorCharacter = await FalukantCharacter.findOne({
|
const buildWhere = (includeMinAge = true) => {
|
||||||
where: {
|
const w = {
|
||||||
regionId,
|
regionId: { [Op.in]: relevantRegionIds },
|
||||||
createdAt: { [Op.lt]: threeWeeksAgo },
|
};
|
||||||
},
|
if (includeMinAge) w.birthdate = { [Op.lte]: twentyOneDaysAgo };
|
||||||
|
if (usedCharacterIds.size > 0) w.id = { [Op.notIn]: Array.from(usedCharacterIds) };
|
||||||
|
if (excludeCharacterId) {
|
||||||
|
w[Op.or] = [
|
||||||
|
{ userId: null },
|
||||||
|
{ userId: { [Op.ne]: falukantUserId } }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
};
|
||||||
|
|
||||||
|
let directorCharacter = await FalukantCharacter.findOne({
|
||||||
|
where: buildWhere(true), // birthdate <= 21 Tage her = mind. 21 Tage alt
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: TitleOfNobility,
|
model: TitleOfNobility,
|
||||||
as: 'nobleTitle',
|
as: 'nobleTitle',
|
||||||
attributes: ['level'],
|
attributes: ['level'],
|
||||||
|
required: true
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
order: sequelize.literal('RANDOM()'),
|
order: sequelize.literal('RANDOM()'),
|
||||||
});
|
});
|
||||||
|
if (!directorCharacter) {
|
||||||
|
directorCharacter = await FalukantCharacter.findOne({
|
||||||
|
where: buildWhere(false), // Fallback ohne Altersfilter
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
model: TitleOfNobility,
|
||||||
|
as: 'nobleTitle',
|
||||||
|
attributes: ['level'],
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
],
|
||||||
|
order: sequelize.literal('RANDOM()'),
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!directorCharacter) {
|
if (!directorCharacter) {
|
||||||
throw new Error('No directors available for the region');
|
throw new Error('No directors available for the region');
|
||||||
}
|
}
|
||||||
|
usedCharacterIds.add(directorCharacter.id);
|
||||||
const avgKnowledge = await this.calculateAverageKnowledge(directorCharacter.id);
|
const avgKnowledge = await this.calculateAverageKnowledge(directorCharacter.id);
|
||||||
const proposedIncome = Math.round(
|
const proposedIncome = Math.round(
|
||||||
directorCharacter.nobleTitle.level * Math.pow(1.231, avgKnowledge / 1.5)
|
directorCharacter.nobleTitle.level * Math.pow(1.231, avgKnowledge / 1.5)
|
||||||
|
|||||||
Reference in New Issue
Block a user