Rework 'hits' storage

This commit is contained in:
aronmal 2023-09-03 18:48:39 +02:00
parent b067747d48
commit 26ee9652e6
Signed by: aronmal
GPG key ID: 816B7707426FC612
6 changed files with 40 additions and 77 deletions

View file

@ -99,16 +99,6 @@ export const ships = pgTable("ship", {
.references(() => user_games.id, { onDelete: "cascade" }), .references(() => user_games.id, { onDelete: "cascade" }),
}) })
export const hits = pgTable("hit", {
id: text("id").notNull().primaryKey(),
x: integer("x").notNull(),
y: integer("y").notNull(),
hit: boolean("hit").notNull(),
user_game_id: text("user_game_id")
.notNull()
.references(() => user_games.id, { onDelete: "cascade" }),
})
export const moves = pgTable("move", { export const moves = pgTable("move", {
id: text("id").notNull().primaryKey(), id: text("id").notNull().primaryKey(),
createdAt: timestamp("created_at").defaultNow(), createdAt: timestamp("created_at").defaultNow(),
@ -181,19 +171,11 @@ export const shipsRelations = relations(ships, ({ one }) => ({
}), }),
})) }))
export const hitsRelations = relations(hits, ({ one }) => ({
userGame: one(user_games, {
fields: [hits.user_game_id],
references: [user_games.id],
}),
}))
export const userGamesRelations = relations(user_games, ({ one, many }) => ({ export const userGamesRelations = relations(user_games, ({ one, many }) => ({
user: one(users, { fields: [user_games.userId], references: [users.id] }), user: one(users, { fields: [user_games.userId], references: [users.id] }),
game: one(games, { fields: [user_games.gameId], references: [games.id] }), game: one(games, { fields: [user_games.gameId], references: [games.id] }),
moves: many(moves), moves: many(moves),
ships: many(ships), ships: many(ships),
hits: many(hits),
chats: many(chats), chats: many(chats),
})) }))

View file

@ -1,17 +1,16 @@
/* eslint-disable solid/reactivity */ /* eslint-disable solid/reactivity */
import { socket } from "~/lib/socket" import { socket } from "~/lib/socket"
import { GamePropsSchema, GameState, MoveType } from "~/lib/zodSchemas" import { GamePropsSchema, GameState } from "~/lib/zodSchemas"
// import { toast } from "react-toastify" // import { toast } from "react-toastify"
import { createSignal } from "solid-js" import { createSignal } from "solid-js"
import { getPayloadFromProps } from "~/lib/getPayloadFromProps" import { getPayloadFromProps } from "~/lib/getPayloadFromProps"
import { getPayloadwithChecksum } from "~/lib/getPayloadwithChecksum" import { getPayloadwithChecksum } from "~/lib/getPayloadwithChecksum"
import { import {
compileHits,
initialUser, initialUser,
initlialMouseCursor, initlialMouseCursor,
initlialTarget, initlialTarget,
initlialTargetPreview, initlialTargetPreview,
intersectingShip,
targetList,
} from "~/lib/utils/helpers" } from "~/lib/utils/helpers"
import { import {
EventBarModes, EventBarModes,
@ -41,8 +40,11 @@ export const [targetPreview, setTargetPreview] = createSignal<TargetPreview>(
export const [mouseCursor, setMouseCursor] = export const [mouseCursor, setMouseCursor] =
createSignal<MouseCursor>(initlialMouseCursor) createSignal<MouseCursor>(initlialMouseCursor)
export const users = { export const users = {
0: initialUser(), 0: {
1: initialUser(), ...initialUser(),
hits: () => compileHits(users, 0),
},
1: { ...initialUser(), hits: () => compileHits(users, 1) },
forEach(cb: (user: ReturnType<typeof initialUser>, i: 0 | 1) => void) { forEach(cb: (user: ReturnType<typeof initialUser>, i: 0 | 1) => void) {
cb(this[0], 0) cb(this[0], 0)
cb(this[1], 1) cb(this[1], 1)
@ -51,30 +53,10 @@ export const users = {
return { 0: cb(this[0], 0), 1: cb(this[1], 1) } return { 0: cb(this[0], 0), 1: cb(this[1], 1) }
}, },
} }
export type Users = typeof users
export function DispatchMove(move: MoveDispatchProps, index: number) { export function DispatchMove(move: MoveDispatchProps, index: 0 | 1) {
const list = targetList(move, move.type) users[index].setMoves((e) => [...e, move])
users.forEach((user, i) => {
if (!user) return
if (index === i) {
user.setMoves((e) => [...e, move])
} else {
if (move.type === MoveType.Enum.radar) return
user.setHits((e) => [
...e,
...list.map(({ x, y }) => ({
hit: !!intersectingShip(user.ships(), {
...move,
size: 1,
variant: 0,
}).fields.length,
x,
y,
})),
])
}
})
} }
export function setShips(ships: ShipProps[], index: number) { export function setShips(ships: ShipProps[], index: number) {
users.forEach(({ setShips }, i) => { users.forEach(({ setShips }, i) => {
@ -100,7 +82,6 @@ export function removeShip({ size, variant, x, y }: ShipProps, index: number) {
export function setPlayer(newUsers: NewUsers): string | null { export function setPlayer(newUsers: NewUsers): string | null {
let hash: string | null = null let hash: string | null = null
console.log(newUsers)
users.forEach((user, i) => { users.forEach((user, i) => {
const newUser = newUsers[i] const newUser = newUsers[i]
if (!newUser) return defaultUser(user) if (!newUser) return defaultUser(user)
@ -110,7 +91,6 @@ export function setPlayer(newUsers: NewUsers): string | null {
user.setChats(newUser.chats) user.setChats(newUser.chats)
user.setMoves(newUser.moves) user.setMoves(newUser.moves)
user.setShips(newUser.ships) user.setShips(newUser.ships)
user.setHits(newUser.hits)
}) })
const body = getPayloadwithChecksum(getPayloadFromProps()) const body = getPayloadwithChecksum(getPayloadFromProps())
if (!body.hash) { if (!body.hash) {
@ -186,7 +166,6 @@ export function full(newProps: GamePropsSchema) {
user.setChats(newUser.chats) user.setChats(newUser.chats)
user.setMoves(newUser.moves) user.setMoves(newUser.moves)
user.setShips(newUser.ships) user.setShips(newUser.ships)
user.setHits(newUser.hits)
}) })
} }
} }
@ -244,5 +223,4 @@ function defaultUser(user: ReturnType<typeof initialUser>) {
user.setChats([]) user.setChats([])
user.setMoves([]) user.setMoves([])
user.setShips([]) user.setShips([])
user.setHits([])
} }

View file

@ -27,7 +27,6 @@ export function getPayloadFromProps() {
chats: user.chats(), chats: user.chats(),
moves: user.moves(), moves: user.moves(),
ships: user.ships(), ships: user.ships(),
hits: user.hits(),
})), })),
} }
} }

View file

@ -1,5 +1,6 @@
import { createSignal } from "solid-js" import { createSignal } from "solid-js"
import { count } from "~/components/Gamefield/Gamefield" import { count } from "~/components/Gamefield/Gamefield"
import { Users } from "~/hooks/useGameProps"
import type { import type {
Hit, Hit,
IndexedPosition, IndexedPosition,
@ -11,7 +12,7 @@ import type {
TargetList, TargetList,
TargetPreview, TargetPreview,
} from "../../interfaces/frontend" } from "../../interfaces/frontend"
import { ChatSchema, MoveSchema, Orientation } from "../zodSchemas" import { ChatSchema, MoveSchema, MoveType, Orientation } from "../zodSchemas"
export function borderCN(count: number, x: number, y: number) { export function borderCN(count: number, x: number, y: number) {
if (x === 0) return "left" if (x === 0) return "left"
@ -138,7 +139,6 @@ export function initialUser() {
const [chats, setChats] = createSignal<ChatSchema[]>([]) const [chats, setChats] = createSignal<ChatSchema[]>([])
const [moves, setMoves] = createSignal<MoveSchema[]>([]) const [moves, setMoves] = createSignal<MoveSchema[]>([])
const [ships, setShips] = createSignal<ShipProps[]>([]) const [ships, setShips] = createSignal<ShipProps[]>([])
const [hits, setHits] = createSignal<Hit[]>([])
return { return {
isReady, isReady,
setIsReady, setIsReady,
@ -154,8 +154,6 @@ export function initialUser() {
setMoves, setMoves,
ships, ships,
setShips, setShips,
hits,
setHits,
} }
} }
@ -250,3 +248,22 @@ export function intersectingShip(
borders, borders,
} }
} }
export function compileHits(users: Users, i: 0 | 1) {
return users[i === 0 ? 1 : 0].moves().reduce((hits, move) => {
const list = targetList(move, move.type)
if (move.type === MoveType.Enum.radar) return hits
return [
...hits,
...list.map(({ x, y }) => ({
hit: !!intersectingShip(users[i].ships(), {
...move,
size: 1,
variant: 0,
}).fields.length,
x,
y,
})),
]
}, [] as Hit[])
}

View file

@ -5,7 +5,6 @@ import {
chats, chats,
gamepins, gamepins,
games, games,
hits,
moves, moves,
sessions, sessions,
ships, ships,
@ -29,7 +28,6 @@ export const verificationTokensSchema = createSelectSchema(verificationTokens)
export const gamesSchema = createSelectSchema(games) export const gamesSchema = createSelectSchema(games)
export const gamepinsSchema = createSelectSchema(gamepins) export const gamepinsSchema = createSelectSchema(gamepins)
export const shipsSchema = createSelectSchema(ships) export const shipsSchema = createSelectSchema(ships)
export const hitsSchema = createSelectSchema(hits)
export const movesSchema = createSelectSchema(moves) export const movesSchema = createSelectSchema(moves)
export const chatsSchema = createSelectSchema(chats) export const chatsSchema = createSelectSchema(chats)
export const user_gamesSchema = createSelectSchema(user_games) export const user_gamesSchema = createSelectSchema(user_games)
@ -79,7 +77,6 @@ export const PlayerSchema = z
chats: ChatSchema.array(), chats: ChatSchema.array(),
moves: MoveSchema.array(), moves: MoveSchema.array(),
ships: ShipShema.array(), ships: ShipShema.array(),
hits: HitSchema.array(),
}) })
.nullable() .nullable()

View file

@ -57,13 +57,6 @@ export const gameSelects = {
orientation: true, orientation: true,
}, },
}, },
hits: {
columns: {
x: true,
y: true,
hit: true,
},
},
user: { user: {
columns: { columns: {
id: true, id: true,
@ -111,24 +104,21 @@ export function composeBody(
...props, ...props,
...user, ...user,
})) }))
const emptyUser = {
id: "",
name: "",
chats: [],
moves: [],
ships: [],
}
const composedUsers = { const composedUsers = {
0: mappedUsers.find((e) => e.index === 0) ?? { 0: mappedUsers.find((e) => e.index === 0) ?? {
index: 0, index: 0,
id: "", ...emptyUser,
name: "",
chats: [],
moves: [],
ships: [],
hits: [],
}, },
1: mappedUsers.find((e) => e.index === 1) ?? { 1: mappedUsers.find((e) => e.index === 1) ?? {
index: 1, index: 1,
id: "", ...emptyUser,
name: "",
chats: [],
moves: [],
ships: [],
hits: [],
}, },
} }
const payload = { const payload = {