Files
front/netcode/index.ts
T
2024-07-14 14:49:05 +02:00

224 lines
4.9 KiB
TypeScript

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 { data, pending, error, refresh } = await useFetch<User>(
new URL("/whoami", config.public.endpoint).href,
{
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<Game>(
new URL("/games", config.public.endpoint).href,
{
method: "POST",
credentials: "include",
}
);
console.log(res);
navigateTo("/game/" + res._id);
}
export async function startGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
new URL(`games/${game}/start`, config.public.endpoint).href,
{
method: "POST",
credentials: "include",
}
);
}
export async function joinGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
new URL(`games/${game}/join`, config.public.endpoint).href,
{
method: "POST",
credentials: "include",
}
);
}
export async function endTurn(game: string) {
const config = useRuntimeConfig();
await $fetch(new URL(`games/${game}/endTurn`, config.public.endpoint).href, {
method: "POST",
credentials: "include",
});
}
export async function lockCard(game: string, cardId: string) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/lockCard/${cardId}`, config.public.endpoint)
.href,
{
method: "POST",
credentials: "include",
}
);
}
export async function buyCard(game: string, cardId: string) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/buyCard/${cardId}`, config.public.endpoint)
.href,
{
method: "POST",
credentials: "include",
}
);
}
export async function buyGem(game: string) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/buyGem`, config.public.endpoint).href,
{
method: "POST",
credentials: "include",
}
);
}
export async function useToken(
game: string,
tokenId: string,
cardId?: string,
playerId?: string
) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/useToken/${tokenId}`, config.public.endpoint)
.href,
{
method: "POST",
credentials: "include",
body: { cardId, playerId },
}
);
}
export async function lockEffect(game: string, effectId: string) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/lockEffect/${effectId}`, config.public.endpoint)
.href,
{
method: "POST",
credentials: "include",
}
);
}
export async function makeDiscard(
game: string,
playerId: string,
amount: number = 1
) {
const config = useRuntimeConfig();
await $fetch(
new URL(
`games/${game}/turn/makeDiscard/${playerId}`,
config.public.endpoint
).href,
{
method: "POST",
credentials: "include",
}
);
}
export async function damagePlayer(
game: string,
playerId: string,
amount: number = 1
) {
const config = useRuntimeConfig();
await $fetch(
new URL(
`games/${game}/turn/damagePlayer/${playerId}`,
config.public.endpoint
).href,
{
method: "POST",
credentials: "include",
body: { amount },
}
);
}
export async function damageChampion(
game: string,
playerId: string,
cardId: string
) {
const config = useRuntimeConfig();
await $fetch(
new URL(
`games/${game}/turn/damageChampion/${cardId}`,
config.public.endpoint
).href,
{
method: "POST",
credentials: "include",
body: { playerId },
}
);
}
export async function discardCard(game: string, cardId: string) {
const config = useRuntimeConfig();
await $fetch(
new URL(`games/${game}/turn/discardCard/${cardId}`, config.public.endpoint)
.href,
{
method: "POST",
credentials: "include",
}
);
}