Template
feat : guess word
This commit is contained in:
+50
-2
@@ -5,7 +5,8 @@ import { handlers } from "~/netcode/handlers/game.handler";
|
||||
|
||||
export const useGameStore = defineStore("game", {
|
||||
state: () => ({
|
||||
game: null as null | GameResponseDTO,
|
||||
game: {} as GameResponseDTO,
|
||||
currentWord: null as null | string,
|
||||
eventSource: null as null | EventSource,
|
||||
}),
|
||||
actions: {
|
||||
@@ -14,6 +15,7 @@ export const useGameStore = defineStore("game", {
|
||||
this.eventSource.addEventListener("message", async (message) => {
|
||||
const data = JSON.parse(message.data);
|
||||
const type = data.type as string;
|
||||
console.log(type);
|
||||
if (type in handlers) {
|
||||
handlers[type as keyof typeof handlers](data);
|
||||
}
|
||||
@@ -34,8 +36,54 @@ export const useGameStore = defineStore("game", {
|
||||
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 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",
|
||||
}
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user