Game settings and better socket performance
- Added game settings - Reworked GamePropsSchema for only relevant information -> Prisma schema and zod object - Imporved toast notifications - No duplicate socket connections anymore - Using now Zustand for the gameProps instead of React Context & State
This commit is contained in:
parent
12295b316f
commit
61ae4b901d
31 changed files with 652 additions and 350 deletions
58
leaky-ships/hooks/useGameProps.ts
Normal file
58
leaky-ships/hooks/useGameProps.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { GameSettings } from "@components/Lobby/SettingsFrame/Setting"
|
||||
import { GamePropsSchema } from "@lib/zodSchemas"
|
||||
import { produce } from "immer"
|
||||
import { create } from "zustand"
|
||||
import { devtools } from "zustand/middleware"
|
||||
|
||||
const initialState: GamePropsSchema = { payload: null, hash: null }
|
||||
|
||||
type State = GamePropsSchema
|
||||
|
||||
type Action = {
|
||||
setSetting: (by: GameSettings) => void
|
||||
full: (newProps: GamePropsSchema) => void
|
||||
reset: () => void
|
||||
}
|
||||
|
||||
export const useGameProps = create<State & Action>()(
|
||||
devtools(
|
||||
(set) => ({
|
||||
...initialState,
|
||||
setSetting: (settings) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
if (!state.payload?.game) return
|
||||
Object.assign(state.payload.game, settings)
|
||||
})
|
||||
),
|
||||
full: (newGameProps) =>
|
||||
set((state) => {
|
||||
if (state.hash === newGameProps.hash) {
|
||||
console.log("Everything up to date.")
|
||||
} else {
|
||||
console.log("Update was needed.", state.hash, newGameProps.hash)
|
||||
|
||||
if (
|
||||
state.payload?.game?.id &&
|
||||
state.payload?.game?.id !== newGameProps.payload?.game?.id
|
||||
) {
|
||||
console.warn(
|
||||
"Different gameId detected on update: ",
|
||||
state.payload?.game?.id,
|
||||
newGameProps.payload?.game?.id
|
||||
)
|
||||
}
|
||||
|
||||
return newGameProps
|
||||
}
|
||||
return state
|
||||
}),
|
||||
reset: () => {
|
||||
set(initialState)
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "gameState",
|
||||
}
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue