wip : working on game
This commit is contained in:
@@ -10,7 +10,6 @@ const currentGames = computed(() => {
|
|||||||
const games = Object.values(lobbyStore.games).filter((g) =>
|
const games = Object.values(lobbyStore.games).filter((g) =>
|
||||||
g.players.some((p) => p.user.id == userStore.self?.id)
|
g.players.some((p) => p.user.id == userStore.self?.id)
|
||||||
);
|
);
|
||||||
console.log(games);
|
|
||||||
return games;
|
return games;
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
+1
-1
Submodule netcode/events updated: 49adbdec5b...e9d5d8d909
@@ -0,0 +1,3 @@
|
|||||||
|
export const handlers = {
|
||||||
|
join: (event: any) => {},
|
||||||
|
};
|
||||||
+18
-1
@@ -1 +1,18 @@
|
|||||||
<template>Hello</template>
|
<script setup lang="ts">
|
||||||
|
import { useGameStore } from "~/store/game.store";
|
||||||
|
const route = useRoute();
|
||||||
|
const gameId = Number(route.params.id);
|
||||||
|
|
||||||
|
const gameStore = useGameStore();
|
||||||
|
const game = await gameStore.fetchGame(gameId);
|
||||||
|
|
||||||
|
onBeforeMount(() => {
|
||||||
|
gameStore.subscribe(gameId);
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
gameStore.close();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>{{ game }}</template>
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { defineStore } from "pinia";
|
||||||
|
import { subscribe } from "~/netcode";
|
||||||
|
import { GameResponseDTO } from "~/netcode/events/dto/gameresponse.dto";
|
||||||
|
import { handlers } from "~/netcode/handlers/game.handler";
|
||||||
|
|
||||||
|
export const useGameStore = defineStore("game", {
|
||||||
|
state: () => ({
|
||||||
|
game: null as null | GameResponseDTO,
|
||||||
|
eventSource: null as null | EventSource,
|
||||||
|
}),
|
||||||
|
actions: {
|
||||||
|
async subscribe(id: number) {
|
||||||
|
this.eventSource = subscribe(`/games/${id}/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 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;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user