Skip to content

Commit

Permalink
fixed bugs, still need to pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmineee-li committed Apr 29, 2024
1 parent 8a509c0 commit 22864a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions server/src/achievement/achievement.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('AchievementModule E2E', () => {
console.log = function () {};

achievementService = module.get<AchievementService>(AchievementService);
challengeService = module.get<ChallengeService>(ChallengeService);
prisma = module.get<PrismaService>(PrismaService);
userService = module.get<UserService>(UserService);
eventService = module.get<EventService>(EventService);
Expand Down
57 changes: 47 additions & 10 deletions server/src/achievement/achievement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,40 @@ export class AchievementService {
async checkAchievementProgress(
user: User,
challengeId: string,
isJourney: boolean,
isJourneyCompleted: boolean,
) {
// find challenge corresponding to challengeId
const curChallenge = await this.prisma.challenge.findUniqueOrThrow({
where: { id: challengeId },
include: {linkedEvent: true}
});

const ability = await this.abilityFactory.createForUser(user);


// find all achievements associated with the challenge that are accessible
// by user and have incomplete trackers; joins tracker to resulting query
const achs = await this.prisma.achievement.findMany({
where: {
OR: [
{ linkedEventId: challengeId }, // achievements linked to the specific event of the challenge
// { linkedEventId: challengeId }, // achievements linked to the specific event of the challenge
{ linkedEventId: curChallenge.linkedEventId }, // achievements linked to the specific event of the challenge
{ linkedEventId: null }, // achievements not linked to any specific event
{ achievementType : AchievementType.},
{ locationType : LocationType.ANY },
],
AND: [
accessibleBy(ability, Action.Read).Achievement,
{locationType: curChallenge.location},
// {locationType: curChallenge.location},
],
trackers: {
every: { //
OR: [
{ userId: { not: user.id } }, // Trackers not belonging to the user
{ dateComplete: { not: null } } // Trackers that are completed
]
},
},
},
include: {
trackers: {
Expand Down Expand Up @@ -319,22 +332,46 @@ export class AchievementService {
const journeyOrChalAchShouldProgress =
ach.achievementType ===
AchievementTypeDto.TOTAL_CHALLENGES_OR_JOURNEYS ||
(isJourney &&
(isJourneyCompleted &&
ach.achievementType === AchievementTypeDto.TOTAL_JOURNEYS) ||
(!isJourney &&
(!isJourneyCompleted &&
ach.achievementType === AchievementTypeDto.TOTAL_CHALLENGES);


if (ach.achievementType === AchievementTypeDto.TOTAL_POINTS) {
tracker.progress += curChallenge.points;
const updatedTracker = await this.prisma.achievementTracker.update({
where: { id: tracker.id },
data: {
progress: {
increment: curChallenge.points // increment tracker progress by points of current challenge
}
},
});
if (tracker.progress >= ach.requiredPoints) {
// ach is newly completed; update tracker with completion date
tracker.dateComplete = new Date();
await this.prisma.achievementTracker.update({
where: { id: tracker.id },
data: {
dateComplete: new Date() // add new date
}
});
}
} else if (journeyOrChalAchShouldProgress) {
tracker.progress += 1;
if (tracker.progress >= ach.requiredPoints) {
tracker.dateComplete = new Date();
const updatedTracker = await this.prisma.achievementTracker.update({
where: { id: tracker.id },
data: {
progress: {
increment: 1 // increment tracker progress by 1
}
},
});
if (updatedTracker.progress >= ach.requiredPoints) {
await this.prisma.achievementTracker.update({
where: { id: tracker.id },
data: {
dateComplete: new Date() // add new date
}
});
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/challenge/challenge.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@ export class ChallengeService {
user.id,
);

// check if the challenge is part of a journey
const isJourney =
// check if the completed challenge is completing a journey
const isJourneyCompleted =
(await this.prisma.prevChallenge.count({
where: {
userId: user.id,
challengeId: eventTracker.curChallengeId,
trackerId: eventTracker.id,
},
})) ===
(await this.prisma.eventTracker.count({
where: { id: eventTracker.id }, // CHECK
(await this.prisma.challenge.count({
where: { linkedEventId: eventTracker.eventId },
}));

await this.achievementService.checkAchievementProgress(
user,
challengeId,
isJourney,
isJourneyCompleted,
);

await this.eventService.emitUpdateLeaderPosition({
Expand Down

0 comments on commit 22864a3

Please sign in to comment.