Step towards full multiplayer compatibility
This commit is contained in:
parent
9895a286a3
commit
53a07b21b0
16 changed files with 207 additions and 104 deletions
|
@ -1,12 +1,15 @@
|
|||
import { Hit, HitDispatch, Target } from "../interfaces/frontend"
|
||||
import { GameSettings } from "@components/Lobby/SettingsFrame/Setting"
|
||||
import { getPayloadwithChecksum } from "@lib/getPayloadwithChecksum"
|
||||
import { socket } from "@lib/socket"
|
||||
import { initlialTarget, initlialTargetPreview } from "@lib/utils/helpers"
|
||||
import {
|
||||
GamePropsSchema,
|
||||
optionalGamePropsSchema,
|
||||
PlayerSchema,
|
||||
} from "@lib/zodSchemas"
|
||||
import { produce } from "immer"
|
||||
import { SetStateAction } from "react"
|
||||
import { toast } from "react-toastify"
|
||||
import { create } from "zustand"
|
||||
import { devtools } from "zustand/middleware"
|
||||
|
@ -16,9 +19,15 @@ const initialState: optionalGamePropsSchema & {
|
|||
isReady: boolean
|
||||
isConnected: boolean
|
||||
}[]
|
||||
hits: Hit[]
|
||||
target: Target
|
||||
targetPreview: Target
|
||||
} = {
|
||||
payload: null,
|
||||
hash: null,
|
||||
hits: [],
|
||||
target: initlialTarget,
|
||||
targetPreview: initlialTargetPreview,
|
||||
userStates: Array.from(Array(2), () => ({
|
||||
isReady: false,
|
||||
isConnected: false,
|
||||
|
@ -28,8 +37,11 @@ const initialState: optionalGamePropsSchema & {
|
|||
export type State = typeof initialState
|
||||
|
||||
export type Action = {
|
||||
setSetting: (settings: GameSettings) => string | null
|
||||
DispatchHits: (action: HitDispatch) => void
|
||||
setTarget: (target: SetStateAction<Target>) => void
|
||||
setTargetPreview: (targetPreview: SetStateAction<Target>) => void
|
||||
setPlayer: (payload: { users: PlayerSchema[] }) => string | null
|
||||
setSetting: (settings: GameSettings) => string | null
|
||||
full: (newProps: GamePropsSchema) => void
|
||||
leave: (cb: () => void) => void
|
||||
setIsReady: (payload: { i: number; isReady: boolean }) => void
|
||||
|
@ -41,6 +53,34 @@ export const useGameProps = create<State & Action>()(
|
|||
devtools(
|
||||
(set) => ({
|
||||
...initialState,
|
||||
DispatchHits: (action) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
switch (action.type) {
|
||||
case "fireMissile":
|
||||
case "htorpedo":
|
||||
case "vtorpedo": {
|
||||
state.hits.push(...action.payload)
|
||||
}
|
||||
}
|
||||
})
|
||||
),
|
||||
setTarget: (target) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
if (typeof target === "function")
|
||||
state.target = target(state.target)
|
||||
else state.target = target
|
||||
})
|
||||
),
|
||||
setTargetPreview: (targetPreview) =>
|
||||
set(
|
||||
produce((state: State) => {
|
||||
if (typeof targetPreview === "function")
|
||||
state.targetPreview = targetPreview(state.target)
|
||||
else state.targetPreview = targetPreview
|
||||
})
|
||||
),
|
||||
setPlayer: (payload) => {
|
||||
let hash: string | null = null
|
||||
set(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue