2024-10-04 21:06:02 -05:00
|
|
|
import type { OnlineServer } from "@/lib/types/mh-server";
|
2024-08-07 21:20:17 -05:00
|
|
|
import { Inngest } from "inngest";
|
|
|
|
|
import { serve } from "inngest/next";
|
2024-08-20 21:32:27 -05:00
|
|
|
import { Document, MongoClient, ObjectId, WithId } from "mongodb";
|
2024-10-04 21:06:02 -05:00
|
|
|
import {createReportIssue} from "@/lib/linear";
|
2024-08-07 21:20:17 -05:00
|
|
|
|
|
|
|
|
// Create a client to send and receive events
|
2024-08-10 11:47:53 -05:00
|
|
|
export const inngest = new Inngest({ id: "mhsf" });
|
2024-08-07 21:20:17 -05:00
|
|
|
|
|
|
|
|
// Create an API that serves zero functions
|
|
|
|
|
export default serve({
|
2024-10-04 21:06:02 -05:00
|
|
|
client: inngest,
|
|
|
|
|
functions: [
|
|
|
|
|
inngest.createFunction(
|
|
|
|
|
{ id: "report" },
|
|
|
|
|
{ event: "report-server" },
|
|
|
|
|
async ({ event, step }) => {
|
|
|
|
|
// by the way, I bombed the Discord stuff
|
|
|
|
|
await createReportIssue(event.data.server, event.data.reason, event.data.userId);
|
|
|
|
|
|
|
|
|
|
return { event, body: "Done" }
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
inngest.createFunction(
|
|
|
|
|
{ id: "short-term-data" },
|
|
|
|
|
[{ cron: "*/30 * * * *" }, { event: "test/30-min" }],
|
|
|
|
|
async ({ event, step }) => {
|
|
|
|
|
const mongo = new MongoClient(process.env.MONGO_DB as string);
|
|
|
|
|
try {
|
|
|
|
|
const mh = await step.run("grab-servers-from-api", async () => {
|
|
|
|
|
return await (
|
|
|
|
|
await fetch("https://api.minehut.com/servers", {
|
|
|
|
|
headers: {
|
|
|
|
|
accept: "application/json",
|
|
|
|
|
"accept-language": Math.random().toString(),
|
|
|
|
|
priority: "u=1, i",
|
|
|
|
|
"sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
|
|
|
|
|
"sec-ch-ua-mobile": "?0",
|
|
|
|
|
"sec-ch-ua-platform": '"macOS"',
|
|
|
|
|
"sec-fetch-dest": "empty",
|
|
|
|
|
"sec-fetch-mode": "cors",
|
|
|
|
|
"sec-fetch-site": "cross-site",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Referer: "http://localhost:3000/",
|
|
|
|
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
|
|
|
},
|
|
|
|
|
body: null,
|
|
|
|
|
method: "GET",
|
|
|
|
|
})
|
|
|
|
|
).json();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mha = mongo.db("mhsf").collection("mh");
|
|
|
|
|
const meta = mongo.db("mhsf").collection("meta");
|
|
|
|
|
const dbl = mongo.db("mhsf").collection("history");
|
|
|
|
|
|
|
|
|
|
await mha.insertOne({
|
|
|
|
|
total_players: mh.total_players,
|
|
|
|
|
total_servers: mh.total_servers,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const completed = await step.run("listing-servers", async () => {
|
|
|
|
|
mh.servers.forEach(async (server: OnlineServer, i: number) => {
|
|
|
|
|
const serverFavoritesObject = await meta.findOne({
|
|
|
|
|
server: server.name,
|
|
|
|
|
});
|
|
|
|
|
let favorites = 0;
|
|
|
|
|
if (serverFavoritesObject != undefined)
|
|
|
|
|
favorites = serverFavoritesObject.favorites;
|
|
|
|
|
|
|
|
|
|
await dbl.insertOne({
|
|
|
|
|
player_count: server.playerData.playerCount,
|
|
|
|
|
favorites,
|
|
|
|
|
server: server.name,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
});
|
|
|
|
|
console.log(i, mh.servers.length);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
if (completed == true) {
|
|
|
|
|
return { event, body: "Finished!" };
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await mongo.close();
|
|
|
|
|
|
|
|
|
|
return { event, body: "Cloudflare.. aborting " + e };
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-08-07 21:20:17 -05:00
|
|
|
});
|
2024-08-20 21:32:27 -05:00
|
|
|
|
|
|
|
|
|