Working ship placement

This commit is contained in:
aronmal 2023-06-11 22:09:36 +02:00
parent 0317a3343c
commit c2af2dffa2
Signed by: aronmal
GPG key ID: 816B7707426FC612
14 changed files with 1155 additions and 281 deletions

View file

@ -1,4 +1,10 @@
import { Hit, HitDispatch, Target } from "../interfaces/frontend"
import {
ActionDispatchProps,
EventBarModes,
Hit,
ShipProps,
Target,
} from "../interfaces/frontend"
import { GameSettings } from "@components/Lobby/SettingsFrame/Setting"
import { getPayloadwithChecksum } from "@lib/getPayloadwithChecksum"
import { socket } from "@lib/socket"
@ -19,12 +25,18 @@ const initialState: optionalGamePropsSchema & {
isReady: boolean
isConnected: boolean
}[]
menu: keyof EventBarModes
mode: number
ships: ShipProps[]
hits: Hit[]
target: Target
targetPreview: Target
} = {
menu: "actions",
mode: 0,
payload: null,
hash: null,
ships: [],
hits: [],
target: initlialTarget,
targetPreview: initlialTargetPreview,
@ -37,7 +49,7 @@ const initialState: optionalGamePropsSchema & {
export type State = typeof initialState
export type Action = {
DispatchHits: (action: HitDispatch) => void
DispatchAction: (props: ActionDispatchProps) => void
setTarget: (target: SetStateAction<Target>) => void
setTargetPreview: (targetPreview: SetStateAction<Target>) => void
setPlayer: (payload: { users: PlayerSchema[] }) => string | null
@ -46,6 +58,8 @@ export type Action = {
leave: (cb: () => void) => void
setIsReady: (payload: { i: number; isReady: boolean }) => void
starting: () => void
addShip: (props: ShipProps) => void
removeShip: (props: ShipProps) => void
setIsConnected: (payload: { i: number; isConnected: boolean }) => void
reset: () => void
}
@ -54,16 +68,16 @@ export const useGameProps = create<State & Action>()(
devtools(
(set) => ({
...initialState,
DispatchHits: (action) =>
DispatchAction: (action) =>
set(
produce((state: State) => {
switch (action.type) {
case "fireMissile":
case "htorpedo":
case "vtorpedo": {
state.hits.push(...action.payload)
}
}
// switch (action.type) {
// case "fireMissile":
// case "htorpedo":
// case "vtorpedo": {
// state.hits.push(...action.payload)
// }
// }
})
),
setTarget: (target) =>
@ -82,6 +96,25 @@ export const useGameProps = create<State & Action>()(
else state.targetPreview = targetPreview
})
),
addShip: (props) =>
set(
produce((state: State) => {
state.ships.push(props)
})
),
removeShip: ({ size, variant, x, y }) =>
set(
produce((state: State) => {
const indexToRemove = state.ships.findIndex(
(ship) =>
ship.size === size &&
ship.variant === variant &&
ship.x === x &&
ship.y === y
)
state.ships.splice(indexToRemove, 1)
})
),
setPlayer: (payload) => {
let hash: string | null = null
set(
@ -103,7 +136,6 @@ export const useGameProps = create<State & Action>()(
return hash
},
setSetting: (settings) => {
const payload = JSON.stringify(settings)
let hash: string | null = null
set(
produce((state: State) => {