Added userState (isReady and isConnection)
This commit is contained in:
parent
29cb4a279d
commit
63fc8c56bf
13 changed files with 311 additions and 358 deletions
|
@ -1,28 +1,39 @@
|
|||
import { GameSettings } from "@components/Lobby/SettingsFrame/Setting"
|
||||
import { getPayloadwithChecksum } from "@lib/getPayloadwithChecksum"
|
||||
import { socket } from "@lib/socket"
|
||||
import { GamePropsSchema, PlayerSchema } from "@lib/zodSchemas"
|
||||
import { PlayerN } from "@prisma/client"
|
||||
import {
|
||||
GamePropsSchema,
|
||||
optionalGamePropsSchema,
|
||||
PlayerSchema,
|
||||
} from "@lib/zodSchemas"
|
||||
import { produce } from "immer"
|
||||
import { toast } from "react-toastify"
|
||||
import { create } from "zustand"
|
||||
import { devtools } from "zustand/middleware"
|
||||
|
||||
const initialState: GamePropsSchema & {
|
||||
queue: { payload: string; hash: string }[]
|
||||
} = { payload: null, hash: null, queue: [] }
|
||||
const initialState: optionalGamePropsSchema & {
|
||||
userStates: {
|
||||
isReady: boolean
|
||||
isConnected: boolean
|
||||
}[]
|
||||
} = {
|
||||
payload: null,
|
||||
hash: null,
|
||||
userStates: Array.from(Array(2), () => ({
|
||||
isReady: false,
|
||||
isConnected: false,
|
||||
})),
|
||||
}
|
||||
|
||||
export type State = typeof initialState
|
||||
|
||||
export type Action = {
|
||||
setSetting: (settings: GameSettings) => string | null
|
||||
setPlayer: (payload: {
|
||||
player1?: PlayerSchema
|
||||
player2?: PlayerSchema
|
||||
}) => string | null
|
||||
setPlayer: (payload: { users: PlayerSchema[] }) => string | null
|
||||
full: (newProps: GamePropsSchema) => void
|
||||
leave: (cb: () => void) => void
|
||||
setIsReady: (payload: { index: PlayerN; isReady: boolean }) => void
|
||||
setIsReady: (payload: { i: number; isReady: boolean }) => void
|
||||
setIsConnected: (payload: { i: number; isConnected: boolean }) => void
|
||||
reset: () => void
|
||||
}
|
||||
|
||||
|
@ -35,7 +46,7 @@ export const useGameProps = create<State & Action>()(
|
|||
set(
|
||||
produce((state: State) => {
|
||||
if (!state.payload) return
|
||||
Object.assign(state.payload, payload)
|
||||
state.payload.users = payload.users
|
||||
const body = getPayloadwithChecksum(state.payload)
|
||||
if (!body.hash) {
|
||||
toast.warn("Something is wrong... ", {
|
||||
|
@ -55,10 +66,6 @@ export const useGameProps = create<State & Action>()(
|
|||
let hash: string | null = null
|
||||
set(
|
||||
produce((state: State) => {
|
||||
const length = state.queue.length
|
||||
state.queue.filter((e) => e.payload !== payload || e.hash !== hash)
|
||||
if (state.queue.length !== length) return
|
||||
|
||||
if (!state.payload?.game) return
|
||||
Object.assign(state.payload.game, settings)
|
||||
const body = getPayloadwithChecksum(state.payload)
|
||||
|
@ -71,7 +78,6 @@ export const useGameProps = create<State & Action>()(
|
|||
}
|
||||
hash = body.hash
|
||||
state.hash = hash
|
||||
state.queue.push({ payload, hash })
|
||||
})
|
||||
)
|
||||
return hash
|
||||
|
@ -106,14 +112,19 @@ export const useGameProps = create<State & Action>()(
|
|||
cb()
|
||||
})
|
||||
},
|
||||
setIsReady: ({ index, isReady }) =>
|
||||
setIsReady: ({ i, isReady }) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
if (!state.payload) return
|
||||
const player = state.payload[index]
|
||||
if (!player) return
|
||||
player.isReady = isReady
|
||||
state.payload[index] = player
|
||||
state.userStates[i].isReady = isReady
|
||||
state.userStates[i].isConnected = true
|
||||
})
|
||||
),
|
||||
setIsConnected: ({ i, isConnected }) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
state.userStates[i].isConnected = isConnected
|
||||
if (isConnected) return
|
||||
state.userStates[i].isReady = false
|
||||
})
|
||||
),
|
||||
reset: () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue