Files
front/netcode/index.ts
T
legonzaur 29cd28953d
release-tag / release-image (push) Successful in 51s
Feat : add surrender button
2024-08-27 13:08:15 +02:00

228 lines
5.2 KiB
TypeScript

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 whoami() {
const config = useRuntimeConfig();
const req = await useFetch<User>(config.public.endpoint + "/whoami", {
credentials: "include",
server: false,
});
await req.execute();
if (req.error.value && req.error.value.statusCode == 401) {
// 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 async function createGame() {
const config = useRuntimeConfig();
const res = await $fetch<Game>(config.public.endpoint + "/games", {
method: "POST",
credentials: "include",
});
navigateTo(`/game?id=${res._id}`);
}
export async function startGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
config.public.endpoint + `/games/${game}/start`,
{
method: "POST",
credentials: "include",
}
);
}
export async function deleteGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(config.public.endpoint + `/games/${game}`, {
method: "DELETE",
credentials: "include",
});
}
export async function joinGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
config.public.endpoint + `/games/${game}/join`,
{
method: "POST",
credentials: "include",
}
);
}
export async function leaveGame(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
config.public.endpoint + `/games/${game}/leave`,
{
method: "POST",
credentials: "include",
}
);
}
export async function surrender(game: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
config.public.endpoint + `/games/${game}/surrender`,
{
method: "POST",
credentials: "include",
}
);
}
export async function kickPlayer(game: string, playerId: string) {
const config = useRuntimeConfig();
const res = await $fetch<Game>(
config.public.endpoint + `/games/${game}/kick/${playerId}`,
{
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",
}
);
}