Enhance NPC creation functionality and validation in Admin module
- Updated AdminController to validate the count parameter, ensuring it is between 1 and 500. - Refactored NPC creation logic in AdminService to create NPCs for each city-title combination, improving efficiency. - Enhanced frontend localization files to reflect changes in count descriptions and validation messages. - Updated CreateNPCView to provide user guidance on count input and display detailed creation results.
This commit is contained in:
@@ -389,13 +389,17 @@ class AdminController {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { regionIds, minAge, maxAge, minTitleId, maxTitleId, count } = req.body;
|
||||
const countValue = parseInt(count) || 1;
|
||||
if (countValue < 1 || countValue > 500) {
|
||||
return res.status(400).json({ error: 'Count must be between 1 and 500' });
|
||||
}
|
||||
const result = await AdminService.createNPCs(userId, {
|
||||
regionIds: regionIds && regionIds.length > 0 ? regionIds : null,
|
||||
minAge: parseInt(minAge) || 0,
|
||||
maxAge: parseInt(maxAge) || 100,
|
||||
minTitleId: parseInt(minTitleId) || 1,
|
||||
maxTitleId: parseInt(maxTitleId) || 19,
|
||||
count: parseInt(count) || 1
|
||||
count: countValue
|
||||
});
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
|
||||
@@ -1156,75 +1156,78 @@ class AdminService {
|
||||
|
||||
const genders = ['male', 'female'];
|
||||
const createdNPCs = [];
|
||||
const totalNPCs = targetRegions.length * titles.length * count;
|
||||
|
||||
// Erstelle NPCs in einer Transaktion
|
||||
// Für jede Stadt-Titel-Kombination wird die angegebene Anzahl erstellt
|
||||
await sequelize.transaction(async (t) => {
|
||||
for (let i = 0; i < count; i++) {
|
||||
// Zufällige Region
|
||||
const region = targetRegions[Math.floor(Math.random() * targetRegions.length)];
|
||||
for (const region of targetRegions) {
|
||||
for (const title of titles) {
|
||||
// Erstelle 'count' NPCs für diese Stadt-Titel-Kombination
|
||||
for (let i = 0; i < count; i++) {
|
||||
// Zufälliges Geschlecht
|
||||
const gender = genders[Math.floor(Math.random() * genders.length)];
|
||||
|
||||
// Zufälliges Geschlecht
|
||||
const gender = genders[Math.floor(Math.random() * genders.length)];
|
||||
// Zufälliger Vorname
|
||||
const firstName = await FalukantPredefineFirstname.findAll({
|
||||
where: { gender },
|
||||
order: sequelize.fn('RANDOM'),
|
||||
limit: 1,
|
||||
transaction: t
|
||||
});
|
||||
if (firstName.length === 0) {
|
||||
throw new Error(`No first names found for gender: ${gender}`);
|
||||
}
|
||||
const fnObj = firstName[0];
|
||||
|
||||
// Zufälliger Vorname
|
||||
const firstName = await FalukantPredefineFirstname.findAll({
|
||||
where: { gender },
|
||||
order: sequelize.fn('RANDOM'),
|
||||
limit: 1,
|
||||
transaction: t
|
||||
});
|
||||
if (firstName.length === 0) {
|
||||
throw new Error(`No first names found for gender: ${gender}`);
|
||||
// Zufälliger Nachname
|
||||
const lastName = await FalukantPredefineLastname.findAll({
|
||||
order: sequelize.fn('RANDOM'),
|
||||
limit: 1,
|
||||
transaction: t
|
||||
});
|
||||
if (lastName.length === 0) {
|
||||
throw new Error('No last names found');
|
||||
}
|
||||
const lnObj = lastName[0];
|
||||
|
||||
// Zufälliges Alter (in Jahren, wird in Tage umgerechnet)
|
||||
const randomAge = Math.floor(Math.random() * (maxAge - minAge + 1)) + minAge;
|
||||
const birthdate = new Date();
|
||||
birthdate.setDate(birthdate.getDate() - randomAge); // 5 Tage = 5 Jahre alt
|
||||
|
||||
// Erstelle den NPC-Charakter (ohne userId = NPC)
|
||||
const npc = await FalukantCharacter.create({
|
||||
userId: null, // Wichtig: null = NPC
|
||||
regionId: region.id,
|
||||
firstName: fnObj.id,
|
||||
lastName: lnObj.id,
|
||||
gender: gender,
|
||||
birthdate: birthdate,
|
||||
titleOfNobility: title.id,
|
||||
health: 100,
|
||||
moodId: 1
|
||||
}, { transaction: t });
|
||||
|
||||
createdNPCs.push({
|
||||
id: npc.id,
|
||||
firstName: fnObj.name,
|
||||
lastName: lnObj.name,
|
||||
gender: gender,
|
||||
age: randomAge,
|
||||
region: region.name,
|
||||
title: title.labelTr
|
||||
});
|
||||
}
|
||||
}
|
||||
const fnObj = firstName[0];
|
||||
|
||||
// Zufälliger Nachname
|
||||
const lastName = await FalukantPredefineLastname.findAll({
|
||||
order: sequelize.fn('RANDOM'),
|
||||
limit: 1,
|
||||
transaction: t
|
||||
});
|
||||
if (lastName.length === 0) {
|
||||
throw new Error('No last names found');
|
||||
}
|
||||
const lnObj = lastName[0];
|
||||
|
||||
// Zufälliges Alter (in Jahren, wird in Tage umgerechnet)
|
||||
const randomAge = Math.floor(Math.random() * (maxAge - minAge + 1)) + minAge;
|
||||
const birthdate = new Date();
|
||||
birthdate.setDate(birthdate.getDate() - randomAge); // 5 Tage = 5 Jahre alt
|
||||
|
||||
// Zufälliger Title
|
||||
const title = titles[Math.floor(Math.random() * titles.length)];
|
||||
|
||||
// Erstelle den NPC-Charakter (ohne userId = NPC)
|
||||
const npc = await FalukantCharacter.create({
|
||||
userId: null, // Wichtig: null = NPC
|
||||
regionId: region.id,
|
||||
firstName: fnObj.id,
|
||||
lastName: lnObj.id,
|
||||
gender: gender,
|
||||
birthdate: birthdate,
|
||||
titleOfNobility: title.id,
|
||||
health: 100,
|
||||
moodId: 1
|
||||
}, { transaction: t });
|
||||
|
||||
createdNPCs.push({
|
||||
id: npc.id,
|
||||
firstName: fnObj.name,
|
||||
lastName: lnObj.name,
|
||||
gender: gender,
|
||||
age: randomAge,
|
||||
region: region.name,
|
||||
title: title.labelTr
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
count: createdNPCs.length,
|
||||
countPerCombination: count,
|
||||
totalCombinations: targetRegions.length * titles.length,
|
||||
npcs: createdNPCs
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user