feat: add gamification hooks, leaderboard UI, and translations

- Added Directus hooks for automatic point awards:
  - Recording creation/publishing (50 points)
  - Recording featured status (100 points bonus)
  - Comments on recordings (5 points)
- Created /leaderboard route with full UI
  - Server-side data loading with authentication guard
  - Responsive design with medal emojis for top 3
  - User stats display (recordings, plays, achievements)
  - Pagination support
  - "How It Works" info section
- Added comprehensive gamification translations
- Time-weighted scoring displayed for rankings
- Automatic achievement checking on point awards

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Valknar XXX
2025-10-28 13:29:34 +01:00
parent 8f09244188
commit 064894b8bb
4 changed files with 332 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ global.require = createRequire(import.meta.url);
import { defineHook } from "@directus/extensions-sdk";
import slugify from "@sindresorhus/slugify";
import ffmpeg from "fluent-ffmpeg";
import { awardPoints, checkAchievements } from "../endpoint/gamification.js";
async function processVideo(
meta,
@@ -32,7 +33,7 @@ async function processVideo(
}
}
export default defineHook(async ({ filter, action }, { services, logger }) => {
export default defineHook(async ({ filter, action }, { services, logger, database, getSchema }) => {
action("files.upload", async (meta, context) => {
await processVideo(meta, context, services, logger);
});
@@ -67,4 +68,78 @@ export default defineHook(async ({ filter, action }, { services, logger }) => {
return payload;
},
);
// =========================================
// GAMIFICATION HOOKS
// =========================================
// Hook: Award points when recording is published
action("items.create", async (meta, { collection, accountability }) => {
if (collection === "sexy_recordings") {
const { payload, key } = meta;
// Award points if recording is published
if (payload.status === "published" && accountability?.user) {
try {
await awardPoints(database, accountability.user, "RECORDING_CREATE", key);
await checkAchievements(database, accountability.user, "recordings");
logger.info(`Awarded RECORDING_CREATE points to user ${accountability.user}`);
} catch (error) {
logger.error("Failed to award recording creation points:", error);
}
}
}
});
// Hook: Award points when recording status changes to published or featured
action("items.update", async (meta, { collection, accountability, schema }) => {
if (collection === "sexy_recordings") {
const { payload, keys } = meta;
try {
const { ItemsService } = services;
const recordingsService = new ItemsService("sexy_recordings", {
schema: await getSchema(),
});
for (const key of keys) {
const recording = await recordingsService.readOne(key);
// Award points if status changed from non-published to published
if (payload.status === "published" && recording.status !== "published" && recording.user_created) {
await awardPoints(database, recording.user_created, "RECORDING_CREATE", key);
await checkAchievements(database, recording.user_created, "recordings");
logger.info(`Awarded RECORDING_CREATE points to user ${recording.user_created}`);
}
// Award bonus points if recording becomes featured
if (payload.featured === true && !recording.featured && recording.user_created) {
await awardPoints(database, recording.user_created, "RECORDING_FEATURED", key);
await checkAchievements(database, recording.user_created, "recordings");
logger.info(`Awarded RECORDING_FEATURED points to user ${recording.user_created}`);
}
}
} catch (error) {
logger.error("Failed to award recording update points:", error);
}
}
});
// Hook: Award points when user creates a comment on a recording
action("comments.create", async (meta, { accountability }) => {
if (!accountability?.user) return;
try {
const { payload } = meta;
// Check if comment is on a recording
if (payload.collection === "sexy_recordings") {
await awardPoints(database, accountability.user, "COMMENT_CREATE");
await checkAchievements(database, accountability.user, "social");
logger.info(`Awarded COMMENT_CREATE points to user ${accountability.user}`);
}
} catch (error) {
logger.error("Failed to award comment points:", error);
}
});
});