diff --git a/leaky-ships/hooks/useSocket.ts b/leaky-ships/hooks/useSocket.ts index 83fef0f..8e05676 100644 --- a/leaky-ships/hooks/useSocket.ts +++ b/leaky-ships/hooks/useSocket.ts @@ -62,8 +62,7 @@ function useSocket() { toast.warn("Es gibt Probleme mit der Echtzeitverbindung.", { toastId }) }) - socket.on("gameSetting", (payload, hash, userId) => { - if (userId === session?.user.id) return + socket.on("gameSetting", (payload, hash) => { const newHash = setSetting(payload) if (!newHash || newHash === hash) return console.log("hash", hash, newHash) @@ -74,8 +73,7 @@ function useSocket() { }) socket.on("playerEvent", (event) => { - const { type, i, userId } = event - if (userId === session?.user.id) return + const { type, i } = event let message: string console.log(type) switch (type) { @@ -117,8 +115,7 @@ function useSocket() { }) }) - socket.on("isReady", (payload, userId) => { - if (userId === session?.user.id) return + socket.on("isReady", (payload) => { setIsReady(payload) }) @@ -132,29 +129,14 @@ function useSocket() { } }, [ full, - i, router, session?.user.id, setIsConnected, setIsReady, setPlayer, setSetting, - stateHash, + userStates, ]) - useEffect( - () => - console.log( - i, - isIndex, - userStates[i ?? 0].isConnected, - isConnectedState, - isIndex ? userStates[i].isConnected : isConnectedState, - userStates, - session?.user.id - ), - [i, isIndex, isConnectedState, session?.user.id, userStates] - ) - useEffect(() => console.log("warst", isConnectedState), [isConnectedState]) // useEffect(() => { // if (!isConnected) return diff --git a/leaky-ships/interfaces/NextApiSocket.ts b/leaky-ships/interfaces/NextApiSocket.ts index 61bc954..9fad6c9 100644 --- a/leaky-ships/interfaces/NextApiSocket.ts +++ b/leaky-ships/interfaces/NextApiSocket.ts @@ -27,7 +27,7 @@ export interface ServerToClientEvents { // noArg: () => void // basicEmit: (a: number, b: string, c: Buffer) => void // withAck: (d: string, ) => void - gameSetting: (payload: GameSettings, hash: string, userId: string) => void + gameSetting: (payload: GameSettings, hash: string) => void playerEvent: ( event: | { @@ -35,28 +35,14 @@ export interface ServerToClientEvents { i: number payload: { users: PlayerSchema[] } hash: string - userId: string } | { type: "disconnect" i: number - userId: string } ) => void - isReady: ( - payload: { - i: number - isReady: boolean - }, - userId: string - ) => void - isConnected: ( - payload: { - i: number - isConnected: boolean - }, - userId: string - ) => void + isReady: (payload: { i: number; isReady: boolean }) => void + isConnected: (payload: { i: number; isConnected: boolean }) => void } export interface ClientToServerEvents { diff --git a/leaky-ships/pages/api/ws.ts b/leaky-ships/pages/api/ws.ts index 9b3b894..b9b622a 100644 --- a/leaky-ships/pages/api/ws.ts +++ b/leaky-ships/pages/api/ws.ts @@ -65,12 +65,11 @@ const SocketHandler = async ( socket.data.index = index socket.data.gameId = game.id socket.join(game.id) - io.to(game.id).emit("playerEvent", { + socket.to(game.id).emit("playerEvent", { type: "connect", i: socket.data.index, payload: { users: payload.users }, hash, - userId: socket.data.user?.id ?? "", }) next() @@ -105,12 +104,7 @@ const SocketHandler = async ( const { hash } = composeBody(game) if (!hash) return cb(hash) - io.to(game.id).emit( - "gameSetting", - payload, - hash, - socket.data.user?.id ?? "" - ) + socket.to(game.id).emit("gameSetting", payload, hash) }) socket.on("ping", (count, callback) => { @@ -162,12 +156,11 @@ const SocketHandler = async ( const { payload, hash } = body if (!payload || !hash || socket.data.index === undefined) return cb(false) - io.to(socket.data.gameId).emit("playerEvent", { + socket.to(socket.data.gameId).emit("playerEvent", { type: "leave", i: socket.data.index, payload: { users: payload.users }, hash, - userId: socket.data.user?.id ?? "", }) cb(true) @@ -182,16 +175,12 @@ const SocketHandler = async ( socket.on("isReady", async (isReady) => { if (socket.data.index === undefined || !socket.data.gameId) return - io.to(socket.data.gameId).emit( - "isReady", - { i: socket.data.index, isReady }, - socket.data.user?.id ?? "" - ) - io.to(socket.data.gameId).emit( - "isConnected", - { i: socket.data.index, isConnected: true }, - socket.data.user?.id ?? "" - ) + socket + .to(socket.data.gameId) + .emit("isReady", { i: socket.data.index, isReady }) + socket + .to(socket.data.gameId) + .emit("isConnected", { i: socket.data.index, isConnected: true }) }) socket.on("disconnecting", async () => { @@ -201,10 +190,9 @@ const SocketHandler = async ( socket.request ) if (socket.data.index === undefined || !socket.data.gameId) return - io.to(socket.data.gameId).emit("playerEvent", { + socket.to(socket.data.gameId).emit("playerEvent", { type: "disconnect", i: socket.data.index, - userId: socket.data.user?.id ?? "", }) })