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" }),
})
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),
}))

View file

@ -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<TargetPreview>(
export const [mouseCursor, setMouseCursor] =
createSignal<MouseCursor>(initlialMouseCursor)
export const users = {
0: initialUser(),
1: initialUser(),
0: {
...initialUser(),
hits: () => compileHits(users, 0),
},
1: { ...initialUser(), hits: () => compileHits(users, 1) },
forEach(cb: (user: ReturnType<typeof initialUser>, 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<typeof initialUser>) {
user.setChats([])
user.setMoves([])
user.setShips([])
user.setHits([])
}

View file

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

View file

@ -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<ChatSchema[]>([])
const [moves, setMoves] = createSignal<MoveSchema[]>([])
const [ships, setShips] = createSignal<ShipProps[]>([])
const [hits, setHits] = createSignal<Hit[]>([])
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[])
}

View file

@ -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()

View file

@ -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 = {