{"size": 2921, "ext": "js", "max_stars_repo_path": "mastery-point-achievements.js", "max_stars_repo_name": "darthmaim/gw2-scripts", "max_stars_repo_stars_event_min_datetime": "2021-08-05T22:14:40.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-05T22:14:40.000Z", "max_issues_repo_path": "mastery-point-achievements.js", "max_issues_repo_name": "darthmaim/gw2-scripts", "max_issues_repo_issues_event_min_datetime": "2021-10-05T23:27:04.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T12:23:31.000Z", "max_forks_repo_path": "mastery-point-achievements.js", "max_forks_repo_name": "darthmaim/gw2-scripts", "max_forks_repo_forks_event_min_datetime": "2019-05-09T10:51:24.000Z", "max_forks_repo_forks_event_max_datetime": "2019-05-09T10:51:24.000Z"}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": 1, "avg_line_length": 34.3647058824, "max_line_length": 128, "alphanum_fraction": 0.6062992126}
#!/usr/bin/env node

/**
 *
 * This script verifies your account for all locked and unlocked mastery points.
 *
 * Make sure to set your API key first in ./config/local.yml (copy ./config/default.yml).
 * Required API permissions: account, progression.
 *
 * Usage: ./mastery-point-achievements.js
 *
 */

'use strict';

Set.prototype.difference = function(setB) {
    var difference = new Set(this);
    for (var elem of setB) {
        difference.delete(elem);
    }
    return difference;
};

(async function() {
    const config = require('config');
    const api = require('gw2api-client').default();
    api.authenticate(config.get('api_key'));

    const achievements = await api.achievements().all();
    
    const masteriesRegion = new Map();
    const masteries = new Map();
    for (let achievement of achievements) {
        const mastery = achievement.rewards ? achievement.rewards.find(r => r.type === 'Mastery') : null;
        if (!mastery) {
            continue;
        }
        if (!masteries.has(mastery.id)) {
            masteries.set(mastery.id, []);
        }
        if (!masteriesRegion.has(mastery.region)) {
            masteriesRegion.set(mastery.region, []);
        }
        masteriesRegion.get(mastery.region).push(achievement);
        masteries.get(mastery.id).push(achievement);
    }
    
    const [accountAchievements, accountPoints] = await Promise.all([
        api.account().achievements().get(),
        api.account().mastery().points().get()
    ]);
    const unlocked = accountPoints.unlocked;
    const locked = Array.from(new Set(masteries.keys()).difference(new Set(accountPoints.unlocked))).sort();

    const unlockedIncorrectly = unlocked.filter(m => {
        const accountAchievement = accountAchievements.find(a => masteries.has(m) && masteries.get(m).find(m => m.id === a.id));
        return !accountAchievement || !accountAchievement.done;
    });
    const lockedIncorrectly = locked.filter(m => {
        const accountAchievement = accountAchievements.find(a => masteries.has(m) && masteries.get(m).find(m => m.id === a.id));
        return accountAchievement && accountAchievement.done;
    });

    console.log(`Unlocked incorrectly:`);
    for (let mastery of unlockedIncorrectly) {
        const achievement = masteries.get(mastery);
        if (achievement) {
            console.log(` - ${mastery} -> ${achievement.map(a => `${a.name} (${a.id})`).join(', ')}`);
        } else {
            console.log(` - ${mastery} -> unknown achievement`);
        }
    }
    console.log('');

    console.log(`Locked incorrectly:`);
    for (let mastery of lockedIncorrectly) {
        const achievement = masteries.get(mastery);
        if (achievement) {
            console.log(` - ${mastery} -> ${achievement.map(a => `${a.name} (${a.id})`).join(', ')}`);
        } else {
            console.log(` - ${mastery} -> unknown achievement`);
        }
    }
})();
