From 26ee9652e68399ee72b31a12f34dcf7fb74c0284 Mon Sep 17 00:00:00 2001 From: aronmal Date: Sun, 3 Sep 2023 18:48:39 +0200 Subject: [PATCH] Rework 'hits' storage --- leaky-ships/src/drizzle/schemas/Tables.ts | 18 ---------- leaky-ships/src/hooks/useGameProps.ts | 42 ++++++---------------- leaky-ships/src/lib/getPayloadFromProps.ts | 1 - leaky-ships/src/lib/utils/helpers.ts | 25 ++++++++++--- leaky-ships/src/lib/zodSchemas.ts | 3 -- leaky-ships/src/routes/api/game/running.ts | 28 +++++---------- 6 files changed, 40 insertions(+), 77 deletions(-) diff --git a/leaky-ships/src/drizzle/schemas/Tables.ts b/leaky-ships/src/drizzle/schemas/Tables.ts index be50e86..e2f417e 100644 --- a/leaky-ships/src/drizzle/schemas/Tables.ts +++ b/leaky-ships/src/drizzle/schemas/Tables.ts @@ -99,16 +99,6 @@ export const ships = pgTable("ship", { .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", { id: text("id").notNull().primaryKey(), 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 }) => ({ user: one(users, { fields: [user_games.userId], references: [users.id] }), game: one(games, { fields: [user_games.gameId], references: [games.id] }), moves: many(moves), ships: many(ships), - hits: many(hits), chats: many(chats), })) diff --git a/leaky-ships/src/hooks/useGameProps.ts b/leaky-ships/src/hooks/useGameProps.ts index adc6bb5..bd5fd4f 100644 --- a/leaky-ships/src/hooks/useGameProps.ts +++ b/leaky-ships/src/hooks/useGameProps.ts @@ -1,17 +1,16 @@ /* eslint-disable solid/reactivity */ import { socket } from "~/lib/socket" -import { GamePropsSchema, GameState, MoveType } from "~/lib/zodSchemas" +import { GamePropsSchema, GameState } from "~/lib/zodSchemas" // import { toast } from "react-toastify" import { createSignal } from "solid-js" import { getPayloadFromProps } from "~/lib/getPayloadFromProps" import { getPayloadwithChecksum } from "~/lib/getPayloadwithChecksum" import { + compileHits, initialUser, initlialMouseCursor, initlialTarget, initlialTargetPreview, - intersectingShip, - targetList, } from "~/lib/utils/helpers" import { EventBarModes, @@ -41,8 +40,11 @@ export const [targetPreview, setTargetPreview] = createSignal( export const [mouseCursor, setMouseCursor] = createSignal(initlialMouseCursor) export const users = { - 0: initialUser(), - 1: initialUser(), + 0: { + ...initialUser(), + hits: () => compileHits(users, 0), + }, + 1: { ...initialUser(), hits: () => compileHits(users, 1) }, forEach(cb: (user: ReturnType, i: 0 | 1) => void) { cb(this[0], 0) cb(this[1], 1) @@ -51,30 +53,10 @@ export const users = { return { 0: cb(this[0], 0), 1: cb(this[1], 1) } }, } +export type Users = typeof users -export function DispatchMove(move: MoveDispatchProps, index: number) { - const list = targetList(move, move.type) - 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 DispatchMove(move: MoveDispatchProps, index: 0 | 1) { + users[index].setMoves((e) => [...e, move]) } export function setShips(ships: ShipProps[], index: number) { 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 { let hash: string | null = null - console.log(newUsers) users.forEach((user, i) => { const newUser = newUsers[i] if (!newUser) return defaultUser(user) @@ -110,7 +91,6 @@ export function setPlayer(newUsers: NewUsers): string | null { user.setChats(newUser.chats) user.setMoves(newUser.moves) user.setShips(newUser.ships) - user.setHits(newUser.hits) }) const body = getPayloadwithChecksum(getPayloadFromProps()) if (!body.hash) { @@ -186,7 +166,6 @@ export function full(newProps: GamePropsSchema) { user.setChats(newUser.chats) user.setMoves(newUser.moves) user.setShips(newUser.ships) - user.setHits(newUser.hits) }) } } @@ -244,5 +223,4 @@ function defaultUser(user: ReturnType) { user.setChats([]) user.setMoves([]) user.setShips([]) - user.setHits([]) } diff --git a/leaky-ships/src/lib/getPayloadFromProps.ts b/leaky-ships/src/lib/getPayloadFromProps.ts index 766b07e..b432809 100644 --- a/leaky-ships/src/lib/getPayloadFromProps.ts +++ b/leaky-ships/src/lib/getPayloadFromProps.ts @@ -27,7 +27,6 @@ export function getPayloadFromProps() { chats: user.chats(), moves: user.moves(), ships: user.ships(), - hits: user.hits(), })), } } diff --git a/leaky-ships/src/lib/utils/helpers.ts b/leaky-ships/src/lib/utils/helpers.ts index 333a0c5..dd4aae5 100644 --- a/leaky-ships/src/lib/utils/helpers.ts +++ b/leaky-ships/src/lib/utils/helpers.ts @@ -1,5 +1,6 @@ import { createSignal } from "solid-js" import { count } from "~/components/Gamefield/Gamefield" +import { Users } from "~/hooks/useGameProps" import type { Hit, IndexedPosition, @@ -11,7 +12,7 @@ import type { TargetList, TargetPreview, } 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) { if (x === 0) return "left" @@ -138,7 +139,6 @@ export function initialUser() { const [chats, setChats] = createSignal([]) const [moves, setMoves] = createSignal([]) const [ships, setShips] = createSignal([]) - const [hits, setHits] = createSignal([]) return { isReady, setIsReady, @@ -154,8 +154,6 @@ export function initialUser() { setMoves, ships, setShips, - hits, - setHits, } } @@ -250,3 +248,22 @@ export function intersectingShip( 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[]) +} diff --git a/leaky-ships/src/lib/zodSchemas.ts b/leaky-ships/src/lib/zodSchemas.ts index 08ff881..a905c9d 100644 --- a/leaky-ships/src/lib/zodSchemas.ts +++ b/leaky-ships/src/lib/zodSchemas.ts @@ -5,7 +5,6 @@ import { chats, gamepins, games, - hits, moves, sessions, ships, @@ -29,7 +28,6 @@ export const verificationTokensSchema = createSelectSchema(verificationTokens) export const gamesSchema = createSelectSchema(games) export const gamepinsSchema = createSelectSchema(gamepins) export const shipsSchema = createSelectSchema(ships) -export const hitsSchema = createSelectSchema(hits) export const movesSchema = createSelectSchema(moves) export const chatsSchema = createSelectSchema(chats) export const user_gamesSchema = createSelectSchema(user_games) @@ -79,7 +77,6 @@ export const PlayerSchema = z chats: ChatSchema.array(), moves: MoveSchema.array(), ships: ShipShema.array(), - hits: HitSchema.array(), }) .nullable() diff --git a/leaky-ships/src/routes/api/game/running.ts b/leaky-ships/src/routes/api/game/running.ts index d2b6b81..ba34ea9 100644 --- a/leaky-ships/src/routes/api/game/running.ts +++ b/leaky-ships/src/routes/api/game/running.ts @@ -57,13 +57,6 @@ export const gameSelects = { orientation: true, }, }, - hits: { - columns: { - x: true, - y: true, - hit: true, - }, - }, user: { columns: { id: true, @@ -111,24 +104,21 @@ export function composeBody( ...props, ...user, })) + const emptyUser = { + id: "", + name: "", + chats: [], + moves: [], + ships: [], + } const composedUsers = { 0: mappedUsers.find((e) => e.index === 0) ?? { index: 0, - id: "", - name: "", - chats: [], - moves: [], - ships: [], - hits: [], + ...emptyUser, }, 1: mappedUsers.find((e) => e.index === 1) ?? { index: 1, - id: "", - name: "", - chats: [], - moves: [], - ships: [], - hits: [], + ...emptyUser, }, } const payload = {