v0.1 alpha
This commit is contained in:
Generated
+1109
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "auraclient-api",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "The api for AuraClient which is a Minecraft mod Client",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "tsc && node dist/index.js",
|
||||||
|
"test": "tsc && node test.mjs"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/kleo-dev/auraclient-api.git"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/kleo-dev/auraclient-api/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/kleo-dev/auraclient-api#readme",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/express": "^5.0.0",
|
||||||
|
"@types/mongodb": "^4.0.6",
|
||||||
|
"@types/node": "^22.13.5",
|
||||||
|
"dotenv": "^16.4.7",
|
||||||
|
"express": "^4.21.2",
|
||||||
|
"mongodb": "^6.13.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { MongoClient } from "mongodb";
|
||||||
|
import { Cloak, Player, mongoUrl } from "./utils";
|
||||||
|
|
||||||
|
export const validCloaks: Cloak[] = ["glitch"];
|
||||||
|
|
||||||
|
const mongoClient = new MongoClient(mongoUrl);
|
||||||
|
|
||||||
|
const db = mongoClient.db("saturnclient");
|
||||||
|
|
||||||
|
export const players = db.collection<Player>("players");
|
||||||
+165
@@ -0,0 +1,165 @@
|
|||||||
|
import express from "express";
|
||||||
|
import { players, validCloaks } from "./database";
|
||||||
|
import { Cloak } from "./utils";
|
||||||
|
|
||||||
|
async function getPlayerData(token: string) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`https://api.minecraftservices.com/minecraft/profile`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer " + token,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Invalid session");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { id, name } = await response.json();
|
||||||
|
|
||||||
|
return { uuid: id, name };
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("Failed to validate session");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.get("/", (_, res) => {
|
||||||
|
res.send({
|
||||||
|
version: "0.0.1-alpha.0",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/player/:uuid", async (req, res) => {
|
||||||
|
const player = await players.findOne({
|
||||||
|
uuid: req.params.uuid.replaceAll("-", ""),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
res.send({
|
||||||
|
saturn: false,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`https://api.minecraftservices.com/minecraft/profile/lookup/${player.uuid}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const { name } = await response.json();
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
saturn: true,
|
||||||
|
cloak: player.cloak,
|
||||||
|
name,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/auth", async (req, res) => {
|
||||||
|
const { token } = req.query;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { uuid, name } = await getPlayerData(token as string);
|
||||||
|
|
||||||
|
const player = await players.findOne({ uuid });
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
await players.insertOne({
|
||||||
|
uuid,
|
||||||
|
cloak: "",
|
||||||
|
cloaks: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
success: true,
|
||||||
|
uuid,
|
||||||
|
cloak: "",
|
||||||
|
cloaks: [],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
success: true,
|
||||||
|
uuid,
|
||||||
|
name,
|
||||||
|
cloak: player.cloak,
|
||||||
|
cloaks: player.cloaks,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).send({
|
||||||
|
success: false,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/cloak/:cloak", async (req, res) => {
|
||||||
|
const { cloak } = req.params;
|
||||||
|
const { token } = req.query;
|
||||||
|
|
||||||
|
const { uuid } = await getPlayerData(token as string);
|
||||||
|
|
||||||
|
const player = await players.findOne({ uuid });
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "Player not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validCloaks.includes(cloak as Cloak)) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "Invalid cloak",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!player.cloaks.includes(cloak as Cloak)) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "You don't have this cloak",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await players.updateOne({ uuid }, { $set: { cloak: cloak as Cloak } });
|
||||||
|
|
||||||
|
console.log("Cloak set to " + cloak + " for " + uuid);
|
||||||
|
|
||||||
|
res.send({ success: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/cloak/", async (req, res) => {
|
||||||
|
const { token } = req.query;
|
||||||
|
|
||||||
|
const { uuid } = await getPlayerData(token as string);
|
||||||
|
|
||||||
|
const player = await players.findOne({ uuid });
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "Player not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await players.updateOne({ uuid }, { $set: { cloak: "" } });
|
||||||
|
|
||||||
|
console.log("Cloak set to none for " + uuid);
|
||||||
|
|
||||||
|
res.send({ success: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(3000, () => {
|
||||||
|
console.log(`listening as http://localhost:${port}/`);
|
||||||
|
});
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { config } from "dotenv";
|
||||||
|
config();
|
||||||
|
|
||||||
|
function notFound(): string {
|
||||||
|
throw "'MONGO_URL' not found in .env";
|
||||||
|
}
|
||||||
|
|
||||||
|
export const mongoUrl = process.env.MONGO_URL || notFound();
|
||||||
|
|
||||||
|
export type Cloak = "glitch" | "forrest" | "";
|
||||||
|
|
||||||
|
export interface Player {
|
||||||
|
uuid: string;
|
||||||
|
cloak: Cloak;
|
||||||
|
cloaks: Cloak[];
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
await fetch("http://localhost:3000/player/d362a042-28bd-49e1-a807-ae74dbe8aba9", {
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"removeComments": true,
|
||||||
|
"preserveConstEnums": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"outDir": "dist/"
|
||||||
|
},
|
||||||
|
"include": ["src/**/*", "test.ts"],
|
||||||
|
"exclude": ["**/*.spec.ts"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user