import type { Game, User } from "./interfaces"; export function subscribe(endpoint: string) { const config = useRuntimeConfig(); const evtSource = new EventSource(config.public.endpoint + endpoint, { withCredentials: true, }); return evtSource; } export async function getLobby() { const config = useRuntimeConfig(); const req = await useFetch>(config.public.endpoint + "/games", { credentials: "include", server: false, }); // await req.execute(); await req.execute(); if (req.data.value == null) { throw new Error("Lobby answer is empty"); } return req.data as Ref; } export async function whoami() { const config = useRuntimeConfig(); const { data, pending, error, refresh } = await useFetch( config.public.endpoint + "/whoami", { credentials: "include", } ); if (error.value?.statusCode && [401].includes(error.value.statusCode)) { // await navigateTo(config.public.discordLoginEndpoint, { // external: true, // }); throw new Error("You are not logged in"); } if (error.value) { throw new Error( "Something went wrong while trying to get user information" ); } return data; } export async function createGame() { const config = useRuntimeConfig(); const res = await $fetch(config.public.endpoint + "/games", { method: "POST", credentials: "include", }); console.log(res); navigateTo("/game/" + res._id); } export async function startGame(game: string) { const config = useRuntimeConfig(); const res = await $fetch( config.public.endpoint + `/games/${game}/start`, { method: "POST", credentials: "include", } ); } export async function joinGame(game: string) { const config = useRuntimeConfig(); const res = await $fetch( config.public.endpoint + `/games/${game}/join`, { method: "POST", credentials: "include", } ); } export async function endTurn(game: string) { const config = useRuntimeConfig(); await $fetch(config.public.endpoint + `/games/${game}/endTurn`, { method: "POST", credentials: "include", }); } export async function lockCard(game: string, cardId: string) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/lockCard/${cardId}`, { method: "POST", credentials: "include", } ); } export async function buyCard(game: string, cardId: string) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/buyCard/${cardId}`, { method: "POST", credentials: "include", } ); } export async function buyGem(game: string) { const config = useRuntimeConfig(); await $fetch(config.public.endpoint + `/games/${game}/turn/buyGem`, { method: "POST", credentials: "include", }); } export async function useToken( game: string, tokenId: string, cardId?: string, playerId?: string ) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/useToken/${tokenId}`, { method: "POST", credentials: "include", body: { cardId, playerId }, } ); } export async function lockEffect(game: string, effectId: string) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/lockEffect/${effectId}`, { method: "POST", credentials: "include", } ); } export async function makeDiscard( game: string, playerId: string, amount: number = 1 ) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/makeDiscard/${playerId}`, { method: "POST", credentials: "include", } ); } export async function damagePlayer( game: string, playerId: string, amount: number = 1 ) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/damagePlayer/${playerId}`, { method: "POST", credentials: "include", body: { amount }, } ); } export async function damageChampion( game: string, playerId: string, cardId: string ) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/damageChampion/${cardId}`, { method: "POST", credentials: "include", body: { playerId }, } ); } export async function discardCard(game: string, cardId: string) { const config = useRuntimeConfig(); await $fetch( config.public.endpoint + `/games/${game}/turn/discardCard/${cardId}`, { method: "POST", credentials: "include", } ); }