2024-10-08 15:32:02 +01:00
|
|
|
import { Elysia } from "elysia";
|
|
|
|
import cors from "@elysiajs/cors";
|
|
|
|
import { decorators } from "elysia-decorators";
|
|
|
|
import { logger } from "@tqman/nice-logger";
|
2024-10-09 01:17:00 +01:00
|
|
|
import { swagger } from "@elysiajs/swagger";
|
|
|
|
import { rateLimit } from "elysia-rate-limit";
|
2024-10-08 16:36:52 +01:00
|
|
|
import { RateLimitError } from "./error/rate-limit-error";
|
2024-10-09 01:17:00 +01:00
|
|
|
import { helmet } from "elysia-helmet";
|
|
|
|
import { etag } from "@bogeychan/elysia-etag";
|
2024-10-08 16:36:52 +01:00
|
|
|
import AppController from "./controller/app.controller";
|
2024-10-09 01:17:00 +01:00
|
|
|
import * as dotenv from "@dotenvx/dotenvx";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
import { Config } from "./common/config";
|
|
|
|
import { setLogLevel } from "@typegoose/typegoose";
|
|
|
|
import PlayerController from "./controller/player.controller";
|
|
|
|
import { PlayerService } from "./service/player.service";
|
2024-10-10 01:22:09 +01:00
|
|
|
import { cron } from "@elysiajs/cron";
|
|
|
|
import { PlayerDocument, PlayerModel } from "./model/player";
|
2024-10-11 03:47:54 +01:00
|
|
|
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
|
2024-10-11 18:47:33 +01:00
|
|
|
import { delay } from "@ssr/common/utils/utils";
|
2024-10-15 04:09:47 +01:00
|
|
|
import { connectScoreSaberWebSocket } from "@ssr/common/websocket/scoresaber-websocket";
|
2024-10-15 19:31:50 +01:00
|
|
|
import ImageController from "./controller/image.controller";
|
2024-10-08 15:32:02 +01:00
|
|
|
|
2024-10-09 01:17:00 +01:00
|
|
|
// Load .env file
|
|
|
|
dotenv.config({
|
2024-10-13 01:14:51 +01:00
|
|
|
logLevel: (await Bun.file(".env").exists()) ? "success" : "warn",
|
2024-10-09 01:17:00 +01:00
|
|
|
path: ".env",
|
|
|
|
override: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await mongoose.connect(Config.mongoUri!); // Connect to MongoDB
|
|
|
|
setLogLevel("DEBUG");
|
2024-10-08 15:32:02 +01:00
|
|
|
|
2024-10-15 04:09:47 +01:00
|
|
|
connectScoreSaberWebSocket({
|
|
|
|
onScore: async score => {
|
|
|
|
await PlayerService.trackScore(score);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const app = new Elysia();
|
2024-10-10 01:22:09 +01:00
|
|
|
app.use(
|
|
|
|
cron({
|
|
|
|
name: "player-statistics-tracker-cron",
|
2024-10-13 01:14:51 +01:00
|
|
|
pattern: "1 0 * * *", // Every day at 00:01
|
|
|
|
timezone: "Europe/London", // UTC time
|
2024-10-10 01:22:09 +01:00
|
|
|
run: async () => {
|
2024-10-12 02:37:08 +01:00
|
|
|
const pages = 20; // top 1000 players
|
2024-10-12 02:36:44 +01:00
|
|
|
const cooldown = 60_000 / 250; // 250 requests per minute
|
2024-10-11 18:47:33 +01:00
|
|
|
|
2024-10-11 18:42:46 +01:00
|
|
|
let toTrack: PlayerDocument[] = await PlayerModel.find({});
|
|
|
|
const toRemoveIds: string[] = [];
|
|
|
|
|
|
|
|
// loop through pages to fetch the top players
|
|
|
|
console.log(`Fetching ${pages} pages of players from ScoreSaber...`);
|
|
|
|
for (let i = 0; i < pages; i++) {
|
|
|
|
const pageNumber = i + 1;
|
|
|
|
console.log(`Fetching page ${pageNumber}...`);
|
|
|
|
const page = await scoresaberService.lookupPlayers(pageNumber);
|
|
|
|
if (page === undefined) {
|
|
|
|
console.log(`Failed to fetch players on page ${pageNumber}, skipping page...`);
|
2024-10-11 18:48:04 +01:00
|
|
|
await delay(cooldown);
|
2024-10-11 18:42:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const player of page.players) {
|
2024-10-12 02:36:44 +01:00
|
|
|
const foundPlayer = await PlayerService.getPlayer(player.id, true, player);
|
2024-10-11 18:42:46 +01:00
|
|
|
await PlayerService.trackScoreSaberPlayer(foundPlayer, player);
|
|
|
|
toRemoveIds.push(foundPlayer.id);
|
|
|
|
}
|
2024-10-11 18:48:04 +01:00
|
|
|
await delay(cooldown);
|
2024-10-11 03:47:54 +01:00
|
|
|
}
|
2024-10-11 18:42:46 +01:00
|
|
|
console.log(`Finished tracking player statistics for ${pages} pages, found ${toRemoveIds.length} players.`);
|
2024-10-11 03:47:54 +01:00
|
|
|
|
2024-10-11 18:42:46 +01:00
|
|
|
// remove all players that have been tracked
|
|
|
|
toTrack = toTrack.filter(player => !toRemoveIds.includes(player.id));
|
2024-10-11 03:47:54 +01:00
|
|
|
|
2024-10-11 18:42:46 +01:00
|
|
|
console.log(`Tracking ${toTrack.length} player statistics...`);
|
|
|
|
for (const player of toTrack) {
|
2024-10-10 01:40:21 +01:00
|
|
|
await PlayerService.trackScoreSaberPlayer(player);
|
2024-10-11 18:47:33 +01:00
|
|
|
await delay(cooldown);
|
2024-10-10 01:22:09 +01:00
|
|
|
}
|
|
|
|
console.log("Finished tracking player statistics.");
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-10-08 15:32:02 +01:00
|
|
|
/**
|
|
|
|
* Custom error handler
|
|
|
|
*/
|
|
|
|
app.onError({ as: "global" }, ({ code, error }) => {
|
|
|
|
// Return default error for type validation
|
|
|
|
if (code === "VALIDATION") {
|
|
|
|
return error.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = "status" in error ? error.status : undefined;
|
|
|
|
return {
|
|
|
|
...((status && { statusCode: status }) || { status: code }),
|
|
|
|
...(error.message != code && { message: error.message }),
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-10-08 16:36:52 +01:00
|
|
|
/**
|
|
|
|
* Enable E-Tags
|
|
|
|
*/
|
|
|
|
app.use(etag());
|
|
|
|
|
2024-10-08 15:32:02 +01:00
|
|
|
/**
|
|
|
|
* Enable CORS
|
|
|
|
*/
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request logger
|
|
|
|
*/
|
|
|
|
app.use(
|
|
|
|
logger({
|
|
|
|
mode: "combined",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-10-08 16:36:52 +01:00
|
|
|
/**
|
|
|
|
* Rate limit (100 requests per minute)
|
|
|
|
*/
|
2024-10-09 01:17:00 +01:00
|
|
|
app.use(
|
|
|
|
rateLimit({
|
|
|
|
scoping: "global",
|
|
|
|
duration: 60 * 1000,
|
|
|
|
max: 100,
|
|
|
|
skip: request => {
|
|
|
|
let [_, path] = request.url.split("/"); // Get the url parts
|
|
|
|
path === "" || (path === undefined && (path = "/")); // If we're on /, the path is undefined, so we set it to /
|
|
|
|
return path === "/"; // ignore all requests to /
|
|
|
|
},
|
|
|
|
errorResponse: new RateLimitError("Too many requests, please try again later"),
|
|
|
|
})
|
|
|
|
);
|
2024-10-08 16:36:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Security settings
|
|
|
|
*/
|
2024-10-09 01:17:00 +01:00
|
|
|
app.use(
|
|
|
|
helmet({
|
|
|
|
hsts: false, // Disable HSTS
|
|
|
|
contentSecurityPolicy: false, // Disable CSP
|
|
|
|
dnsPrefetchControl: true, // Enable DNS prefetch
|
|
|
|
})
|
|
|
|
);
|
2024-10-08 16:36:52 +01:00
|
|
|
|
2024-10-08 15:32:02 +01:00
|
|
|
/**
|
|
|
|
* Controllers
|
|
|
|
*/
|
|
|
|
app.use(
|
|
|
|
decorators({
|
2024-10-15 19:31:50 +01:00
|
|
|
controllers: [AppController, PlayerController, ImageController],
|
2024-10-08 15:32:02 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-10-08 16:36:52 +01:00
|
|
|
/**
|
|
|
|
* Swagger Documentation
|
|
|
|
*/
|
|
|
|
app.use(swagger());
|
|
|
|
|
2024-10-08 15:32:02 +01:00
|
|
|
app.onStart(() => {
|
|
|
|
console.log("Listening on port http://localhost:8080");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(8080);
|