remove concepts, finish lobbies/rooms

This commit is contained in:
not-nullptr 2024-03-10 19:42:37 +00:00
parent e498d512b4
commit 6bd72d56de
29 changed files with 235 additions and 822 deletions

View file

@ -1,37 +1,73 @@
import type { IRoom } from "$types/rooms";
import type { IRoom, IRoomConfig, RoomPlayer } from "$types/rooms";
import type { SuyuUser } from "../schema";
import { v4 } from "uuid";
export class RoomManager {
private static rooms: Room[] = [];
static createRoom(room: IRoomConfig) {
const newRoom = new Room(room);
this.rooms.push(newRoom);
return newRoom;
}
static getRooms() {
return this.rooms;
}
static getRoom(id: string) {
return this.rooms.find((room) => room.roomInfo.id === id);
}
static removeRoom(id: string) {
const index = this.rooms.findIndex((room) => room.roomInfo.id === id);
this.rooms.splice(index, 1);
}
}
export class Room {
public roomInfo: IRoom;
public host: SuyuUser;
constructor(
name: string,
description: string,
game: string,
players: SuyuUser[],
maxPlayers: number,
// name: string,
// description: string,
// game: string,
// players: SuyuUser[],
// maxPlayers: number,
// ip: string,
config: IRoomConfig,
) {
const parsed = config.ip.split(":");
this.host = config.host;
this.roomInfo = {
name,
description,
preferredGameName: game,
players,
maxPlayers,
address: "localhost",
externalGuid: "1234",
hasPassword: false,
id: "1234",
name: config.name,
description: config.description,
preferredGameName: config.gameName,
preferredGameId: config.gameId,
players: config.players,
maxPlayers: config.maxPlayers,
address: parsed[0],
externalGuid: v4(),
hasPassword: config.hasPassword,
id: v4(),
netVersion: 1,
owner: "1234",
port: 1234,
preferredGameId: 1234,
owner: this.host.username,
port: parseInt(parsed[1]),
};
}
async addPlayer(user: SuyuUser) {
addPlayer(user: RoomPlayer) {
this.roomInfo.players.push(user);
}
setPlayerList(players: RoomPlayer[]) {
this.roomInfo.players = players;
}
toJSON() {
return this.roomInfo;
}
delete() {
RoomManager.removeRoom(this.roomInfo.id);
}
}

View file

@ -1,15 +1,30 @@
import { userRepo } from "$lib/server/repo";
import type { SuyuUser } from "$lib/server/schema";
import { PUBLIC_KEY } from "$lib/server/secrets";
import type { IJwtData } from "$types/auth";
import cookie from "cookie";
import jwt from "jsonwebtoken";
export async function useAuth(request: Request | string): Promise<SuyuUser | null> {
const apiKey =
typeof request === "string"
? request
: cookie.parse(request.headers.get("cookie") || "").token;
const cookies = cookie.parse(
typeof request !== "string" ? request.headers.get("cookie") || "" : "",
);
const apiKey = typeof request === "string" ? request : cookies.token;
if (!apiKey) {
return null;
}
if (apiKey.startsWith("Bearer ")) {
const token = apiKey.replace("Bearer ", "");
const decoded: IJwtData = jwt.verify(token, Buffer.from(PUBLIC_KEY), {
algorithms: ["RS256"],
}) as IJwtData;
const user = await userRepo.findOne({
where: {
id: decoded.id,
},
});
return user;
}
const user = await userRepo.findOne({
where: {
apiKey,