feat : lobby

This commit is contained in:
2024-12-29 23:39:15 +01:00
parent e0ece631d3
commit d92739bbd6
14 changed files with 313 additions and 203 deletions
+84 -54
View File
@@ -1,60 +1,90 @@
import { defineStore } from "pinia";
import { subscribe } from "~/netcode";
import { WordResponseDTO } from "~/netcode/events/dto/wordresponse.dto";
import { handlers } from "~/netcode/handlers/word.handler";
export const useWordStore = defineStore("word", {
state: () => ({
words: {} as Record<string, WordResponseDTO>,
}),
actions: {
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]
state: () => ({
words: {} as Record<string, WordResponseDTO>,
eventSource: null as null | EventSource,
}),
actions: {
async subscribe() {
this.eventSource = subscribe(`/words/subscribe`);
this.eventSource.addEventListener("message", async (message) => {
const data = JSON.parse(message.data);
const type = data.type as string;
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
},
});
async 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];
},
},
});