From 52de1dbb15f120b1b31cba408332d661729de823 Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Fri, 22 Mar 2024 14:55:02 +0000 Subject: [PATCH 1/4] fix dedicated servers --- src/lib/util/api/index.ts | 11 ++++++++++- src/routes/lobby/+server.ts | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/util/api/index.ts b/src/lib/util/api/index.ts index 58ab377..b5c24e4 100644 --- a/src/lib/util/api/index.ts +++ b/src/lib/util/api/index.ts @@ -21,13 +21,22 @@ export async function useAuth( const decoded: IJwtData = jwt.verify(token, Buffer.from(PUBLIC_KEY), { algorithms: ["RS256"], }) as IJwtData; - const user = await userRepo.findOne({ + let user = await userRepo.findOne({ where: { apiKey: decoded.apiKey, }, loadEagerRelations: eager || false, relations: eager ? ["sentFriendRequests", "receivedFriendRequests"] : [], }); + if (!user) { + user = await userRepo.findOne({ + where: { + id: decoded.id, + }, + loadEagerRelations: eager || false, + relations: eager ? ["sentFriendRequests", "receivedFriendRequests"] : [], + }); + } return user; } const user = await userRepo.findOne({ diff --git a/src/routes/lobby/+server.ts b/src/routes/lobby/+server.ts index e14f286..ab9c55c 100644 --- a/src/routes/lobby/+server.ts +++ b/src/routes/lobby/+server.ts @@ -18,6 +18,7 @@ export async function GET({ request }) { export async function POST({ request, getClientAddress }) { // TODO: per-ip room limit const body: IRoom = await request.json(); + console.log(body, request.headers); /* description may contain "### END DESCRIPTION ###" on its own line. if it does, get all lines after that */ const parsedDescription = body.description?.split("### END DESCRIPTION ###"); const description = parsedDescription?.slice(1)?.join("### END DESCRIPTION ###") || ""; From fcd165e99015c05fae704de4ba46cc61cf742fbb Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Fri, 22 Mar 2024 14:55:27 +0000 Subject: [PATCH 2/4] remove debug logging --- src/routes/lobby/+server.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/routes/lobby/+server.ts b/src/routes/lobby/+server.ts index ab9c55c..bd1b0c6 100644 --- a/src/routes/lobby/+server.ts +++ b/src/routes/lobby/+server.ts @@ -18,7 +18,6 @@ export async function GET({ request }) { export async function POST({ request, getClientAddress }) { // TODO: per-ip room limit const body: IRoom = await request.json(); - console.log(body, request.headers); /* description may contain "### END DESCRIPTION ###" on its own line. if it does, get all lines after that */ const parsedDescription = body.description?.split("### END DESCRIPTION ###"); const description = parsedDescription?.slice(1)?.join("### END DESCRIPTION ###") || ""; @@ -42,7 +41,6 @@ export async function POST({ request, getClientAddress }) { if (!token) return new Response(null, { status: 401 }); // TODO: jwt utils which type and validate automatically const user = await useAuth(token); - console.log(user); if (!user) return new Response(null, { status: 401 }); const borkedIp = getClientAddress(); const room = RoomManager.createRoom({ From 8aaa779c447e0d16f2f4424fef4d89e9e751ccfb Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Fri, 22 Mar 2024 15:11:37 +0000 Subject: [PATCH 3/4] add official suyu listings --- src/lib/server/class/Room.ts | 2 +- src/routes/lobby/+server.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/server/class/Room.ts b/src/lib/server/class/Room.ts index 2852066..5c6fa5e 100644 --- a/src/lib/server/class/Room.ts +++ b/src/lib/server/class/Room.ts @@ -4,7 +4,7 @@ import type { SuyuUser } from "../schema"; import { v4 } from "uuid"; export class RoomManager { - private static rooms: Room[] = []; + static rooms: Room[] = []; static roomTimeout: Record = {}; // room id, last heard from static createRoom(room: IRoomConfig) { const existingRoom = this.rooms.find((r) => r.host.username === room.host.username); diff --git a/src/routes/lobby/+server.ts b/src/routes/lobby/+server.ts index bd1b0c6..ca258a9 100644 --- a/src/routes/lobby/+server.ts +++ b/src/routes/lobby/+server.ts @@ -66,5 +66,11 @@ export async function POST({ request, getClientAddress }) { hasPassword: body.hasPassword || false, }); console.log("Room added:", JSON.stringify(room, null, 2)); + // push every room to the top which starts with `[SUYU OFFICIAL]` and was created with username "suyu" + const suyuRoom = RoomManager.rooms.find((r) => r.roomInfo.name.startsWith("[SUYU OFFICIAL]")); + if (suyuRoom && user.username === "suyu") { + RoomManager.rooms.splice(RoomManager.rooms.indexOf(suyuRoom), 1); + RoomManager.rooms.unshift(suyuRoom); + } return json(room.toJSON()); } From 8c0e56b33a23e77dbde91a1552ed97bc14057bb2 Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Fri, 22 Mar 2024 15:12:34 +0000 Subject: [PATCH 4/4] oops quick bugfix --- src/routes/lobby/+server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/lobby/+server.ts b/src/routes/lobby/+server.ts index ca258a9..bb80a7d 100644 --- a/src/routes/lobby/+server.ts +++ b/src/routes/lobby/+server.ts @@ -68,7 +68,7 @@ export async function POST({ request, getClientAddress }) { console.log("Room added:", JSON.stringify(room, null, 2)); // push every room to the top which starts with `[SUYU OFFICIAL]` and was created with username "suyu" const suyuRoom = RoomManager.rooms.find((r) => r.roomInfo.name.startsWith("[SUYU OFFICIAL]")); - if (suyuRoom && user.username === "suyu") { + if (suyuRoom && suyuRoom.host.username === "suyu") { RoomManager.rooms.splice(RoomManager.rooms.indexOf(suyuRoom), 1); RoomManager.rooms.unshift(suyuRoom); }