feat : guess word

This commit is contained in:
2024-12-30 20:15:02 +01:00
parent 08daf733d1
commit 9eaf7009c6
12 changed files with 304 additions and 54 deletions
+50 -2
View File
@@ -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",
}
);
},
},
});
+2 -2
View File
@@ -48,8 +48,8 @@ export const useLobbyStore = defineStore("lobby", {
credentials: "include",
}
);
this.games[data.id] = data;
return data;
// this.games[data.id] = data;
// return data;
},
},
});
+43 -34
View File
@@ -2,39 +2,48 @@ import { defineStore } from "pinia";
import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto";
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) => {
this.self = data.response._data
this.has_contacted = true
},
lazy: true
})
return req;
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) => {
if (data.response.ok) {
this.self = data.response._data;
}
this.has_contacted = true;
},
lazy: true,
}
);
return req;
},
},
});
+7 -7
View File
@@ -46,8 +46,8 @@ export const useWordStore = defineStore("word", {
method: "PUT",
}
);
word.enabled = true;
return data;
// word.enabled = true;
// return data;
},
async disableWord(word: WordResponseDTO) {
const config = useRuntimeConfig();
@@ -58,8 +58,8 @@ export const useWordStore = defineStore("word", {
method: "PUT",
}
);
word.enabled = false;
return data;
// word.enabled = false;
// return data;
},
async createWord(value: string) {
@@ -72,8 +72,8 @@ export const useWordStore = defineStore("word", {
method: "POST",
}
);
this.words[data.id] = data;
return data;
// this.words[data.id] = data;
// return data;
},
async deleteWord(word: WordResponseDTO) {
const config = useRuntimeConfig();
@@ -84,7 +84,7 @@ export const useWordStore = defineStore("word", {
method: "DELETE",
}
);
delete this.words[word.id];
// delete this.words[word.id];
},
},
});