Feat : lobby
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import type { Game, User } from "./interfaces";
|
||||
|
||||
export function subscribe(endpoint: string) {
|
||||
const config = useRuntimeConfig();
|
||||
const evtSource = new EventSource(new URL(endpoint, config.public.endpoint), {
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
return evtSource;
|
||||
}
|
||||
|
||||
export async function getLobby() {
|
||||
const config = useRuntimeConfig();
|
||||
const req = await useFetch<Array<Game>>(
|
||||
new URL("/games", config.public.endpoint).href,
|
||||
{
|
||||
credentials: "include",
|
||||
}
|
||||
);
|
||||
if (req.data.value == null) {
|
||||
throw new Error("Lobby answer is empty");
|
||||
}
|
||||
return req.data as Ref<Game[]>;
|
||||
}
|
||||
|
||||
export async function whoami() {
|
||||
const config = useRuntimeConfig();
|
||||
const req = useFetch<User>(new URL("/whoami", config.public.endpoint).href, {
|
||||
credentials: "include",
|
||||
});
|
||||
if (
|
||||
req.error.value?.statusCode &&
|
||||
[401].includes(req.error.value.statusCode)
|
||||
) {
|
||||
await navigateTo(config.public.discordLoginEndpoint, {
|
||||
external: true,
|
||||
});
|
||||
throw new Error("You are not logged in");
|
||||
}
|
||||
if (req.error.value) {
|
||||
throw new Error(
|
||||
"Something went wrong while trying to get user information"
|
||||
);
|
||||
}
|
||||
return req.data;
|
||||
}
|
||||
|
||||
export function createGame() {}
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { IGameObject } from "../interfaces/generic";
|
||||
|
||||
export abstract class GameObject implements IGameObject {
|
||||
static readonly instances = new Map<string, GameObject>()
|
||||
readonly uuid: string;
|
||||
readonly socket: WebSocket
|
||||
constructor(socket: WebSocket, { uuid }: { uuid: string }) {
|
||||
this.uuid = uuid
|
||||
this.socket = socket
|
||||
GameObject.instances.set(this.uuid, this)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import { GameObject } from "./GameObject";
|
||||
import type { ICLobby } from "../interfaces/client";
|
||||
|
||||
|
||||
|
||||
export class Lobby extends GameObject implements ICLobby {
|
||||
static instance: Lobby
|
||||
constructor(socket: WebSocket, data: { uuid: string, games: Array<string> }) {
|
||||
if (Lobby.instance) { return Lobby.instance }
|
||||
super(socket, data)
|
||||
Lobby.instance = this
|
||||
}
|
||||
|
||||
|
||||
onAddGame() {
|
||||
|
||||
}
|
||||
onRemoveGame() {
|
||||
|
||||
}
|
||||
|
||||
async request_createGame() {
|
||||
throw Error("not implemented")
|
||||
}
|
||||
|
||||
removeGame() {
|
||||
throw Error("not implemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user