Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions app/api/leaderboard/position/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -11,27 +10,24 @@ 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
)
.db("bluedragon")
.collection("players")
.aggregate([
{
$match: filter,
$match: {
[`statistics.${stat}`]: {
$exists: true,
},
},
},
{
$setWindowFields: {
sortBy: sort,
sortBy: {
[`statistics.${stat}`]: sortDirection,
},
output: {
position: {
$documentNumber: {},
Expand Down
99 changes: 99 additions & 0 deletions app/stats/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<main className="prose mx-auto dark:prose-invert">
<h1>Stats</h1>
<p>Last updated: {format.format(new Date())}</p>
<ul>
<li>Unique players: {uniquePlayers}</li>
<li>Cosmetics bought: {cosmeticsBought[0].count}</li>
<li>Statistic data points: {statsRecorded[0].total}</li>
<li>Coins: {coins[0].total}</li>
</ul>
</main>
);
}