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/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..bb80a7d 100644 --- a/src/routes/lobby/+server.ts +++ b/src/routes/lobby/+server.ts @@ -41,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({ @@ -67,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 && suyuRoom.host.username === "suyu") { + RoomManager.rooms.splice(RoomManager.rooms.indexOf(suyuRoom), 1); + RoomManager.rooms.unshift(suyuRoom); + } return json(room.toJSON()); }