Skip to content

Commit

Permalink
Potentially correct calculations for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlaj committed Apr 7, 2024
1 parent 5ed5c23 commit aa3ba97
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions server/src/controllers/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,59 +61,66 @@ router.post("/autoGenerate", verifyIsAdmin, errorHandler(async (req: Request, re
const IMAGE_URL = '/images/badges/default.png';

const badgesToCreate: any[] = [];
let averageChallengesPerLevel = Math.floor(challengeCount / autoGenerateNames.length);
let averagePointsPerLevel = Math.floor(totalPoints / autoGenerateNames.length);
let averageChallengesPerLevel = Math.max(1, Math.floor(challengeCount / autoGenerateNames.length));
let averagePointsPerLevel = Math.max(1, Math.floor(totalPoints / autoGenerateNames.length));

for (let i = autoGenerateNames.length - 1; i >= 0; i--) {
// General badges for "all" categories
const allChallengeBasedBadge = {
name: autoGenerateNames[i] + " Solver",
basedOn: 'all',
condition: 'challenges',
threshold: (i + 1) + averageChallengesPerLevel,
threshold: Math.min(challengeCount, (i + 1) * averageChallengesPerLevel),
imageUrl: IMAGE_URL
};

const allPointBasedBadge = {
name: autoGenerateNames[i] + " Earner",
basedOn: 'all',
condition: 'points',
threshold: (i + 1) * averagePointsPerLevel,
threshold: Math.min(totalPoints, (i + 1) * averagePointsPerLevel),
imageUrl: IMAGE_URL
};

for (let [categoryName, categoryData] of categoryMap) {
const categoryChallengeThreshold = Math.floor(categoryData.count / autoGenerateNames.length);
const categoryPointThreshold = Math.floor(categoryData.points / autoGenerateNames.length);

if (i * categoryChallengeThreshold < categoryData.count) {
const categoryChallengeBasedBadge = {
name: `${autoGenerateNames[i]} ${categoryName} Solver`,
basedOn: 'category',
condition: 'challenges',
category: categoryName,
threshold: categoryData.count,
imageUrl: IMAGE_URL
};
badgesToCreate.push(categoryChallengeBasedBadge);
break;
}

if (i * categoryPointThreshold < categoryData.points) {
const categoryPointBasedBadge = {
name: `${autoGenerateNames[i]} ${categoryName} Earner`,
basedOn: 'category',
condition: 'points',
category: categoryName,
threshold: categoryData.points,
imageUrl: IMAGE_URL
};
badgesToCreate.push(categoryPointBasedBadge);
break;
}
badgesToCreate.push(allChallengeBasedBadge, allPointBasedBadge);
}

for (let [categoryName, categoryData] of categoryMap) {
const categoryChallengeThreshold = Math.max(1, Math.floor(categoryData.count));
const categoryPointThreshold = Math.max(1, Math.floor(categoryData.points / categoryData.count));

const numChallengeTypeToCreate = Math.min(autoGenerateNames.length, Math.max(1, Math.floor(categoryData.count / autoGenerateNames.length)));
const numPointTypeToCreate = Math.min(autoGenerateNames.length, Math.max(1, Math.floor(categoryData.points / autoGenerateNames.length)));

console.log({ numChallengeTypeToCreate })
console.log({ numPointTypeToCreate })

for (let i = 0; i < numChallengeTypeToCreate; i++) {
const challengeBasedBadge = {
name: autoGenerateNames[i] + " " + categoryName + " Solver",
basedOn: 'category',
category: categoryName,
condition: 'challenges',
threshold: Math.min(categoryData.count, (i + 1) * categoryChallengeThreshold),
imageUrl: IMAGE_URL
};

badgesToCreate.push(challengeBasedBadge);
}

badgesToCreate.push(allChallengeBasedBadge, allPointBasedBadge);
};
for (let i = 0; i < numPointTypeToCreate; i++) {
const pointBasedBadge = {
name: autoGenerateNames[i] + " " + categoryName + " Earner",
basedOn: 'category',
category: categoryName,
condition: 'points',
threshold: Math.min(categoryData.points, (i + 1) * categoryPointThreshold),
imageUrl: IMAGE_URL
};

badgesToCreate.push(pointBasedBadge);
}
}

res.json(await Badge.bulkCreate(badgesToCreate));
}));
Expand Down

0 comments on commit aa3ba97

Please sign in to comment.