204 lines
4.6 KiB
TypeScript
204 lines
4.6 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 getLobby() {
|
|
const config = useRuntimeConfig();
|
|
const req = await useFetch<Array<Game>>(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<Game[]>;
|
|
}
|
|
|
|
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 joinGame(game: string) {
|
|
const config = useRuntimeConfig();
|
|
const res = await $fetch<Game>(
|
|
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",
|
|
}
|
|
);
|
|
}
|