Initial commit

This commit is contained in:
2025-05-06 12:41:50 +00:00
commit aec7504e67
33 changed files with 15916 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
import { defineStore } from "pinia";
import { subscribe } from "~/netcode";
import { GameResponseDTO } from "~/netcode/events/dto/gameresponse.dto";
import type { ConceptEvent } from "~/netcode/events/events";
import { handlers } from "~/netcode/handlers/game.handler";
export const useGameStore = defineStore("game", {
state: () => ({
game: {} as GameResponseDTO,
currentWord: null as null | string,
eventSource: null as null | EventSource,
}),
actions: {
subscribe(id: number) {
this.eventSource = subscribe(`/games/${id}/subscribe`);
this.eventSource.addEventListener("message", (message) => {
const data = JSON.parse(message.data as string) as ConceptEvent;
const type = data.type;
console.log(type);
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
},
close() {
this.eventSource?.close();
},
async fetchGame(id: number) {
const config = useRuntimeConfig();
const data = await useFetch<GameResponseDTO>(
config.public.endpoint + `/games/` + id,
{
credentials: "include",
immediate: true,
},
);
if (data.data.value) {
this.game = data.data.value;
}
return data.data;
},
async joinGame(id: number) {
const config = useRuntimeConfig();
const data = await $fetch<GameResponseDTO>(
config.public.endpoint + `/games/` + id + "/join",
{
method: "POST",
credentials: "include",
},
);
},
async startGame(id: number) {
const config = useRuntimeConfig();
const data = await $fetch<GameResponseDTO>(
config.public.endpoint + `/games/` + id + "/start",
{
method: "POST",
credentials: "include",
},
);
},
async deleteGame(id: number) {
const config = useRuntimeConfig();
const data = await $fetch<GameResponseDTO>(
config.public.endpoint + `/games/` + id,
{
method: "DELETE",
credentials: "include",
},
);
},
async getCurrentWord(id: number) {
const config = useRuntimeConfig();
const data = await $fetch<string>(
config.public.endpoint + `/games/` + id + "/currentWord",
{
credentials: "include",
},
);
if (data) {
this.currentWord = data;
}
return data;
},
async sendMessage(id: number, message: string) {
const config = useRuntimeConfig();
const data = await $fetch<string>(
config.public.endpoint + `/games/` + id + "/message",
{
method: "POST",
body: {
value: message,
},
credentials: "include",
},
);
},
},
});
+56
View File
@@ -0,0 +1,56 @@
import { defineStore } from "pinia";
import { subscribe } from "~/netcode";
import type { GameResponseDTO } from "~/netcode/events/dto/gameresponse.dto";
import type { ConceptEvent } from "~/netcode/events/events";
import { handlers } from "~/netcode/handlers/lobby.handler";
export const useLobbyStore = defineStore("lobby", {
state: () => ({
games: {} as Record<number, GameResponseDTO>,
eventSource: null as null | EventSource,
}),
actions: {
subscribe() {
this.eventSource = subscribe(`/games/subscribe`);
this.eventSource.addEventListener("message", (message) => {
const data = JSON.parse(message.data as string) as ConceptEvent;
const type = data.type;
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
},
close() {
this.eventSource?.close();
},
async fetchGames() {
const config = useRuntimeConfig();
const data = await useFetch<GameResponseDTO[]>(
config.public.endpoint + `/games/`,
{
credentials: "include",
immediate: true,
},
);
if (data.data.value) {
for (const game of data.data.value) {
this.games[game.id] = game;
}
}
return data.data;
},
async createGame() {
const config = useRuntimeConfig();
const data = await $fetch<GameResponseDTO>(
config.public.endpoint + "/games",
{
method: "POST",
credentials: "include",
},
);
// this.games[data.id] = data;
return data;
},
},
});
+50
View File
@@ -0,0 +1,50 @@
import { defineStore } from "pinia";
import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto";
import { type FetchContext } from "ofetch";
export const useUserStore = defineStore("user", {
state: () => ({
users: {} as Record<string, UserResponseDTO>,
self: null as UserResponseDTO | null,
has_contacted: false,
}),
actions: {
async fetchUser(id: string) {
if (id in this.users) {
return this.users[id];
}
const config = useRuntimeConfig();
const data = await useFetch<UserResponseDTO>(
config.public.endpoint + `/users/` + id,
{
credentials: "include",
immediate: true,
},
);
if (data.data.value) {
this.users[data.data.value.id] = data.data.value;
}
return data.data.value;
},
async whoami() {
const config = useRuntimeConfig();
const req = await useFetch<UserResponseDTO>(
config.public.endpoint + "/whoami",
{
credentials: "include",
server: false,
immediate: true,
onResponse: (data: FetchContext<UserResponseDTO>) => {
if (data.response && data.response.ok && data.response._data) {
this.self = data.response._data;
}
this.has_contacted = true;
},
lazy: true,
},
);
return req;
},
},
});
+91
View File
@@ -0,0 +1,91 @@
import { defineStore } from "pinia";
import { subscribe } from "~/netcode";
import { WordResponseDTO } from "~/netcode/events/dto/wordresponse.dto";
import type { ConceptEvent } from "~/netcode/events/events";
import { handlers } from "~/netcode/handlers/word.handler";
export const useWordStore = defineStore("word", {
state: () => ({
words: {} as Record<string, WordResponseDTO>,
eventSource: null as null | EventSource,
}),
actions: {
subscribe() {
this.eventSource = subscribe(`/words/subscribe`);
this.eventSource.addEventListener("message", (message) => {
const data = JSON.parse(message.data as string) as ConceptEvent;
const type = data.type;
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
},
close() {
this.eventSource?.close();
},
async fetchWordList() {
const config = useRuntimeConfig();
const data = await useFetch<WordResponseDTO[]>(
config.public.endpoint + `/words/all`,
{
credentials: "include",
},
);
if (data.data.value && !data.error.value) {
for (const word of data.data.value) {
this.words[word.id] = word;
}
}
return data;
},
async enableWord(word: WordResponseDTO) {
const config = useRuntimeConfig();
const data = await $fetch<WordResponseDTO>(
config.public.endpoint + `/words/` + word.id + "/enable",
{
credentials: "include",
method: "PUT",
},
);
// word.enabled = true;
// return data;
},
async disableWord(word: WordResponseDTO) {
const config = useRuntimeConfig();
const data = await $fetch<WordResponseDTO>(
config.public.endpoint + `/words/` + word.id + "/disable",
{
credentials: "include",
method: "PUT",
},
);
// word.enabled = false;
// return data;
},
async createWord(value: string) {
const config = useRuntimeConfig();
const data = await $fetch<WordResponseDTO>(
config.public.endpoint + `/words/`,
{
credentials: "include",
body: { value },
method: "POST",
},
);
// this.words[data.id] = data;
// return data;
},
async deleteWord(word: WordResponseDTO) {
const config = useRuntimeConfig();
const data = await $fetch<WordResponseDTO>(
config.public.endpoint + `/words/` + word.id,
{
credentials: "include",
method: "DELETE",
},
);
// delete this.words[word.id];
},
},
});