Remove parentMilestones as a feature. Handle it through database queries instead.

This commit is contained in:
zkldi
2022-04-14 03:10:06 +01:00
parent 0fc677dae0
commit c58aa14cac
9 changed files with 78 additions and 62 deletions
@@ -131,7 +131,6 @@ t.test("#GetRelevantGoals", (t) => {
progressHuman: "NO DATA",
timeSet: Date.now(),
userID: 1,
parentMilestones: [],
}))
);
});
@@ -194,7 +193,6 @@ t.test("#UpdateGoalsForUser", (t) => {
timeAchieved: null,
timeSet: 0,
userID: 1,
parentMilestones: [],
};
t.test("Should correctly update goals when user achieves goal.", async (t) => {
@@ -396,7 +394,6 @@ t.test("#ProcessGoal", (t) => {
lastInteraction: null,
outOf: 5,
outOfHuman: "HARD CLEAR",
parentMilestones: [],
playtype: "SP",
progress: 6,
progressHuman: "EX HARD CLEAR",
+55 -4
View File
@@ -11,6 +11,8 @@ import {
PBScoreDocument,
Playtypes,
GoalSubscriptionDocument,
MilestoneSubscriptionDocument,
MilestoneDocument,
} from "tachi-common";
import { GetFolderChartIDs } from "utils/folder";
import { CreateGoalTitle as CreateGoalName, ValidateGoalChartsAndCriteria } from "./goal-utils";
@@ -283,7 +285,6 @@ export async function ConstructGoal(
export async function SubscribeToGoal(
userID: integer,
goalDocument: GoalDocument,
parentMilestone?: string,
cancelIfAchieved = true
) {
const goalExists = await db.goals.findOne({ goalID: goalDocument.goalID });
@@ -323,9 +324,6 @@ export async function SubscribeToGoal(
lastInteraction: null,
timeAchieved: result.achieved ? Date.now() : null,
timeSet: Date.now(),
// if this goal subscription came from a milestone, add the milestone to
// the list of parents.
parentMilestones: parentMilestone ? [parentMilestone] : [],
game: goalDocument.game,
playtype: goalDocument.playtype,
goalID: goalDocument.goalID,
@@ -337,3 +335,56 @@ export async function SubscribeToGoal(
return goalSub;
}
export function GetMilestonesThatContainGoal(goalID: string) {
return db.milestones.find({
"milestoneData.goalID": goalID,
});
}
/**
* Unsubscribing from a goal may not be legal, because the goal might be part of
* a milestone the user is subscribed to. This function returns all milestones
* and milestoneSubs that a goal is attached to.
*
* If this query matches none, an empty array is returned.
*/
export async function GetBlockingParentMilestoneSubs(
goalSub: GoalSubscriptionDocument
): Promise<(MilestoneSubscriptionDocument & { milestone: MilestoneDocument })[]> {
const blockers = await db["milestone-subs"].aggregate([
{
// find all milestones that this user is subscribed to
$match: {
userID: goalSub.userID,
game: goalSub.game,
playtype: goalSub.playtype,
},
},
{
// look up the parent milestones
$lookup: {
from: "milestones",
localField: "milestoneID",
foreignField: "milestoneID",
as: "parentMilestoneSubs",
},
},
{
// then project it onto the $milestone field. This will be null
// if the milestone has no parent, which we hopefully won't have
// to consider (illegal)
$set: {
milestone: { $arrayElemAt: ["$parentMilestoneSubs", 0] },
},
},
{
// then finally, filter to only milestones that pertain to this goal.
$match: {
"$milestone.milestoneData.goalID": goalSub.goalID,
},
},
]);
return blockers;
}
+3 -33
View File
@@ -230,25 +230,8 @@ export async function SubscribeToMilestone(
// from result.goalResults ourselves.
// evaluating goals is fairly cheap though.
await Promise.all(
result.goals.map(async (goal) => {
const res = await SubscribeToGoal(userID, goal, milestone.milestoneID, false);
// If the user is already subscribed to this goal -- i.e. manually or as part
// of another milestone
// add this milestoneID to the list of parents instead.
if (res === SubscribeFailReasons.ALREADY_SUBSCRIBED) {
await db["goal-subs"].update(
{
userID,
milestoneID: milestone.milestoneID,
},
{
$push: {
parentMilestones: milestone.milestoneID,
},
}
);
}
result.goals.map((goal) => {
SubscribeToGoal(userID, goal, milestone.milestoneID, false);
})
);
@@ -262,20 +245,7 @@ export async function SubscribeToMilestone(
export async function UnsubscribeFromMilestone(userID: integer, milestone: MilestoneDocument) {
const goalIDs = GetGoalIDsFromMilestone(milestone);
// Pull this milestone ID from all of the goalSubscriptions that have it.
// since it's no longer going to be their parent.
await db["goal-subs"].update(
{
goalID: { $in: goalIDs },
userID,
parentMilestones: milestone.milestoneID,
},
{
$pull: {
parentMilestones: milestone.milestoneID,
},
}
);
// TODO COME BACK HERE
// then, remove all of the ones that now have no parent blocking their demise.
// that's pretty morbid, jesus christ.