Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianloveswords/4030dad3e7f0b28eeba2 to your computer and use it in GitHub Desktop.
Save brianloveswords/4030dad3e7f0b28eeba2 to your computer and use it in GitHub Desktop.

Milestone Badge Planning

This will be triggered whenever a user earns a badge. Also, assume all relationships for each query are fulfilled.

  1. Find related milestones, relatedMilestones: MilestoneBadges.get({ badgeId: <badgeId> })
  • Return early if there are no related milestones.
  1. Get list of user's badges, userBadges: BadgeInstances.get({ email: <userEmail> })
  2. Get the relative complement of userBadges and relatedMilestones (related milestone badges the user has not earned yet) to make set of unearnedMilestones.
  • Return early if the relative complement set is empty.
  1. Filter unearnedMilestones by eligibility for fulfillment to get eligibleMilestones
  2. Get the intersection between userBadges and support badges of specific milestone from unearnedMilestones.
  3. If the number of items in the intersection set is greater than number of badges required, accept milestone. Otherwise, reject.
  4. Award set of eligibleMilestones (which might be an empty set).

Performance

It actually shouldn't be that bad as there are only two IO operations: getting the milestones related to the initial award, and getting the rest of the user's awarded badges (both require fulfilled relationships, but that's done by JOINs under the hood so each still counts as a single IO operation). Given that the sets will be at most in the hundreds, the rest of the operations should be fairly speedy.

Copy link

ghost commented May 20, 2014

Not sure if this is meant to be implied in the above, but the "milestone badges" in step 4 could be limited to the primaryBadge(s) found in step 2, as opposed to all milestones in existence, right? Unless I am missing something.

@brianloveswords
Copy link
Author

Yep, where I say "milestone badges" below, I mean the ones that were found in the first step. I'll try to make that clearer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment