Skip to content

Commit

Permalink
Auto creation of badges working
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlaj committed Apr 7, 2024
1 parent aa3ba97 commit 6b79ca3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions server/src/controllers/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ router.get("/", verifyAccess, errorHandler(async (req: Request, res: Response) =
}));

const autoGenerateNames = [
"Beginner",
"Intermediate",
"Advanced",
"Grandmaster",
"Master",
"Grandmaster"
"Advanced",
"Intermediate",
"Beginner",
]

interface CategoryData {
Expand Down Expand Up @@ -65,17 +65,19 @@ router.post("/autoGenerate", verifyIsAdmin, errorHandler(async (req: Request, re
let averagePointsPerLevel = Math.max(1, Math.floor(totalPoints / autoGenerateNames.length));

for (let i = autoGenerateNames.length - 1; i >= 0; i--) {
const title = autoGenerateNames[autoGenerateNames.length - i - 1]

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

const allPointBasedBadge = {
name: autoGenerateNames[i] + " Earner",
name: title + " Earner",
basedOn: 'all',
condition: 'points',
threshold: Math.min(totalPoints, (i + 1) * averagePointsPerLevel),
Expand All @@ -86,18 +88,17 @@ router.post("/autoGenerate", verifyIsAdmin, errorHandler(async (req: Request, re
}

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 categoryChallengeThreshold = Math.max(1, Math.floor(categoryData.count / autoGenerateNames.length));
const categoryPointThreshold = Math.max(1, Math.floor(categoryData.points / Math.min(autoGenerateNames.length, 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)));
const numChallengeTypeToCreate = Math.min(Math.max(1, Math.floor(categoryData.count / categoryChallengeThreshold)));
const numPointTypeToCreate = Math.min(Math.max(1, Math.floor(categoryData.points / categoryPointThreshold)));

console.log({ numChallengeTypeToCreate })
console.log({ numPointTypeToCreate })
for (let i = numChallengeTypeToCreate - 1; i >= 0; i--) {
const title = autoGenerateNames[numChallengeTypeToCreate - i - 1]

for (let i = 0; i < numChallengeTypeToCreate; i++) {
const challengeBasedBadge = {
name: autoGenerateNames[i] + " " + categoryName + " Solver",
name: title + " " + categoryName + " Solver",
basedOn: 'category',
category: categoryName,
condition: 'challenges',
Expand All @@ -108,9 +109,11 @@ router.post("/autoGenerate", verifyIsAdmin, errorHandler(async (req: Request, re
badgesToCreate.push(challengeBasedBadge);
}

for (let i = 0; i < numPointTypeToCreate; i++) {
for (let i = numPointTypeToCreate - 1; i >= 0; i--) {
const title = autoGenerateNames[numPointTypeToCreate - i - 1]

const pointBasedBadge = {
name: autoGenerateNames[i] + " " + categoryName + " Earner",
name: title + " " + categoryName + " Earner",
basedOn: 'category',
category: categoryName,
condition: 'points',
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/badgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class BadgeService {
transaction: t
});

const categoryPointCounts = await PointLog.count({
const categoryPointCounts = await PointLog.sum('pointsAwarded', {
where: {
userId,
challengeId: categoryChallengeIds
Expand Down

0 comments on commit 6b79ca3

Please sign in to comment.