Files
pihkaal-me/app/stores/achievements.ts

95 lines
2.3 KiB
TypeScript

import { useLocalStorage } from "@vueuse/core";
const STORAGE_ID = "nds-achievements";
export const ACHIEVEMENTS = [
{ id: "boot", secret: false },
// projects
{ id: "projects_visit", secret: false },
{ id: "projects_view_5", secret: false },
{ id: "projects_open_link", secret: false },
// gallery
{ id: "gallery_visit", secret: false },
// contact
{ id: "contact_visit", secret: false },
{ id: "contact_git_visit", secret: false },
// settings
{ id: "settings_color_change", secret: false },
// snake
{ id: "snake_score_25", secret: false },
// 2048
{ id: "2048_score_512", secret: false },
// taptap
{ id: "taptap_score_20", secret: false },
// secrets
{ id: "settings_color_try_all", secret: true },
{ id: "settings_visit_all", secret: true },
{ id: "contact_36_notifications", secret: true },
] as const;
export type Achievement = (typeof ACHIEVEMENTS)[number]["id"];
export const useAchievementsStore = defineStore("achievements", () => {
const app = useAppStore();
const storage = useLocalStorage(
STORAGE_ID,
{
unlocked: [] as Achievement[],
advancement: {
colors: [app.color.hex],
visitedSettings: [] as string[],
},
},
{ mergeDefaults: true },
);
if (!storage.value.advancement.colors.includes(app.color.hex)) {
storage.value.advancement.colors.push(app.color.hex);
}
const confetti = useConfetti();
const unlock = (name: Achievement) => {
if (storage.value.unlocked.includes(name)) {
return false;
}
storage.value.unlocked.push(name);
const { assets } = useAssets();
assets.audio.messageReceived.play(0.5);
if (storage.value.unlocked.length === ACHIEVEMENTS.length) {
confetti.spawn();
} else {
confetti.spawn(50, 175);
}
return true;
};
const reset = () => {
storage.value = {
unlocked: [],
advancement: {
colors: [app.color.hex],
visitedSettings: [],
},
};
};
return {
unlocked: computed(() => storage.value.unlocked),
advancement: computed(() => storage.value.advancement),
allObtained: computed(
() => storage.value.unlocked.length === ACHIEVEMENTS.length,
),
unlock,
reset,
isUnlocked: computed(
() => (name: Achievement) => storage.value.unlocked.includes(name),
),
};
});