diff --git a/app/api/leaderboard/position/route.ts b/app/api/leaderboard/position/route.ts index fc2a03c..7938e61 100644 --- a/app/api/leaderboard/position/route.ts +++ b/app/api/leaderboard/position/route.ts @@ -1,6 +1,5 @@ import { getLeaderboard } from "@/app/leaderboards/leaderboards"; -import { NextResponse } from "next/server"; -import { NextRequest } from "next/server"; +import { NextRequest, NextResponse } from "next/server"; import { client } from "../../mongo"; export const dynamic = "force-dynamic"; @@ -11,15 +10,6 @@ export async function fetchPosition( stat: string, sortDirection: 1 | -1 = -1 ) { - const filter: { [key: string]: { [key: string]: any } } = {}; - const sort: { [key: string]: number } = {}; - - filter[`statistics.${stat}`] = { - $exists: true, - }; - - sort[`statistics.${stat}`] = sortDirection; - const pos = await ( await client ) @@ -27,11 +17,17 @@ export async function fetchPosition( .collection("players") .aggregate([ { - $match: filter, + $match: { + [`statistics.${stat}`]: { + $exists: true, + }, + }, }, { $setWindowFields: { - sortBy: sort, + sortBy: { + [`statistics.${stat}`]: sortDirection, + }, output: { position: { $documentNumber: {}, diff --git a/app/stats/page.tsx b/app/stats/page.tsx new file mode 100644 index 0000000..d858925 --- /dev/null +++ b/app/stats/page.tsx @@ -0,0 +1,99 @@ +import { client } from "../api/mongo"; + +export const revalidate = 60; // Update every minute + +const format = new Intl.DateTimeFormat(undefined, { + dateStyle: "medium", + timeStyle: "long", +}); + +export default async function Page() { + const uniquePlayers = await (await client) + .db("bluedragon") + .collection("players") + .countDocuments(); + + const cosmeticsBought = await ( + await client + ) + .db("bluedragon") + .collection("players") + .aggregate([ + { + $unwind: { + path: "$cosmetics", + preserveNullAndEmptyArrays: false, + }, + }, + { + $count: "count", + }, + ]) + .toArray(); + + const coins = await ( + await client + ) + .db("bluedragon") + .collection("players") + .aggregate([ + { + $group: { + _id: null, + total: { + $sum: "$coins", + }, + }, + }, + { + $project: { + _id: 0, + }, + }, + ]) + .toArray(); + + const statsRecorded = await ( + await client + ) + .db("bluedragon") + .collection("players") + .aggregate([ + { + $addFields: { + numStats: { + $size: { + $objectToArray: "$statistics", + }, + }, + }, + }, + { + $group: { + _id: null, + total: { + $sum: "$numStats", + }, + }, + }, + { + $project: { + _id: 0, + }, + }, + ]) + .toArray(); + + return ( +
+

Stats

+

Last updated: {format.format(new Date())}

+ +
+ ); +}