|
|
|
@ -1,38 +1,27 @@
|
|
|
|
|
import {
|
|
|
|
|
CSSProperties,
|
|
|
|
|
Dispatch,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
useCallback,
|
|
|
|
|
useMemo,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
} from "react"
|
|
|
|
|
import {CSSProperties, Dispatch, SetStateAction, useCallback, useMemo, useRef, useState,} from "react"
|
|
|
|
|
import "../style/editor.css"
|
|
|
|
|
import TitleInput from "../components/TitleInput"
|
|
|
|
|
import PlainCourt from "../assets/court/full_court.svg?react"
|
|
|
|
|
import HalfCourt from "../assets/court/half_court.svg?react"
|
|
|
|
|
|
|
|
|
|
import { BallPiece } from "../components/editor/BallPiece"
|
|
|
|
|
import {BallPiece} from "../components/editor/BallPiece"
|
|
|
|
|
|
|
|
|
|
import { Rack } from "../components/Rack"
|
|
|
|
|
import { PlayerPiece } from "../components/editor/PlayerPiece"
|
|
|
|
|
import { Player } from "../model/tactic/Player"
|
|
|
|
|
import {Rack} from "../components/Rack"
|
|
|
|
|
import {PlayerPiece} from "../components/editor/PlayerPiece"
|
|
|
|
|
import {Player, PlayerTeam} from "../model/tactic/Player"
|
|
|
|
|
|
|
|
|
|
import { Tactic, TacticContent } from "../model/tactic/Tactic"
|
|
|
|
|
import { fetchAPI } from "../Fetcher"
|
|
|
|
|
import { PlayerTeam } from "../model/tactic/Player"
|
|
|
|
|
import {Tactic, TacticComponent, TacticContent} from "../model/tactic/Tactic"
|
|
|
|
|
import {fetchAPI} from "../Fetcher"
|
|
|
|
|
|
|
|
|
|
import SavingState, {
|
|
|
|
|
SaveState,
|
|
|
|
|
SaveStates,
|
|
|
|
|
} from "../components/editor/SavingState"
|
|
|
|
|
import SavingState, {SaveState, SaveStates,} from "../components/editor/SavingState"
|
|
|
|
|
|
|
|
|
|
import { CourtObject } from "../model/tactic/Ball"
|
|
|
|
|
import { CourtAction } from "./editor/CourtAction"
|
|
|
|
|
import { BasketCourt } from "../components/editor/BasketCourt"
|
|
|
|
|
import { ratioWithinBase } from "../components/arrows/Pos"
|
|
|
|
|
import { Action, ActionKind } from "../model/tactic/Action"
|
|
|
|
|
import { BASE } from "../Constants"
|
|
|
|
|
import {BALL_ID, BALL_TYPE, CourtObject, Ball} from "../model/tactic/Ball"
|
|
|
|
|
import {CourtAction} from "./editor/CourtAction"
|
|
|
|
|
import {BasketCourt} from "../components/editor/BasketCourt"
|
|
|
|
|
import {Action, ActionKind} from "../model/tactic/Action"
|
|
|
|
|
import {BASE} from "../Constants"
|
|
|
|
|
import {overlaps} from "../geo/Box"
|
|
|
|
|
import {ratioWithinBase} from "../geo/Pos"
|
|
|
|
|
|
|
|
|
|
const ERROR_STYLE: CSSProperties = {
|
|
|
|
|
borderColor: "red",
|
|
|
|
@ -135,10 +124,10 @@ function EditorView({
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
|
getRackPlayers(PlayerTeam.Allies, content.players),
|
|
|
|
|
getRackPlayers(PlayerTeam.Allies, content.components),
|
|
|
|
|
)
|
|
|
|
|
const [opponents, setOpponents] = useState(
|
|
|
|
|
getRackPlayers(PlayerTeam.Opponents, content.players),
|
|
|
|
|
getRackPlayers(PlayerTeam.Opponents, content.components),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [objects, setObjects] = useState<RackedCourtObject[]>(
|
|
|
|
@ -151,15 +140,10 @@ function EditorView({
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
// check if refBounds overlaps courtBounds
|
|
|
|
|
return !(
|
|
|
|
|
bounds.top > courtBounds.bottom ||
|
|
|
|
|
bounds.right < courtBounds.left ||
|
|
|
|
|
bounds.bottom < courtBounds.top ||
|
|
|
|
|
bounds.left > courtBounds.right
|
|
|
|
|
)
|
|
|
|
|
return overlaps(courtBounds, bounds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onPieceDetach = (ref: HTMLDivElement, element: RackedPlayer) => {
|
|
|
|
|
const onRackPieceDetach = (ref: HTMLDivElement, element: RackedPlayer) => {
|
|
|
|
|
const refBounds = ref.getBoundingClientRect()
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
@ -168,23 +152,24 @@ function EditorView({
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
players: [
|
|
|
|
|
...content.players,
|
|
|
|
|
components: [
|
|
|
|
|
...content.components,
|
|
|
|
|
{
|
|
|
|
|
type: "player",
|
|
|
|
|
id: "player-" + element.key + "-" + element.team,
|
|
|
|
|
team: element.team,
|
|
|
|
|
role: element.key,
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
},
|
|
|
|
|
} as Player,
|
|
|
|
|
],
|
|
|
|
|
actions: content.actions,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onObjectDetach = (
|
|
|
|
|
const onRackedObjectDetach = (
|
|
|
|
|
ref: HTMLDivElement,
|
|
|
|
|
rackedObject: RackedCourtObject,
|
|
|
|
|
) => {
|
|
|
|
@ -196,27 +181,22 @@ function EditorView({
|
|
|
|
|
let courtObject: CourtObject
|
|
|
|
|
|
|
|
|
|
switch (rackedObject.key) {
|
|
|
|
|
case "ball":
|
|
|
|
|
const ballObj = content.objects.findIndex(
|
|
|
|
|
(o) => o.type == "ball",
|
|
|
|
|
case BALL_TYPE:
|
|
|
|
|
const ballObj = content.components.findIndex(
|
|
|
|
|
(o) => o.type == BALL_TYPE,
|
|
|
|
|
)
|
|
|
|
|
const playerCollidedIdx = getPlayerCollided(
|
|
|
|
|
const playerCollidedIdx = getComponentCollided(
|
|
|
|
|
refBounds,
|
|
|
|
|
content.players,
|
|
|
|
|
content.components.toSpliced(ballObj, 1),
|
|
|
|
|
)
|
|
|
|
|
if (playerCollidedIdx != -1) {
|
|
|
|
|
onBallDropOnPlayer(playerCollidedIdx)
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
objects: content.objects.toSpliced(ballObj, 1),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
onBallDropOnComponent(playerCollidedIdx)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
courtObject = {
|
|
|
|
|
type: "ball",
|
|
|
|
|
type: BALL_TYPE,
|
|
|
|
|
id: BALL_ID,
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y,
|
|
|
|
|
}
|
|
|
|
@ -229,38 +209,34 @@ function EditorView({
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
objects: [...content.objects, courtObject],
|
|
|
|
|
components: [...content.components, courtObject],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getPlayerCollided = (
|
|
|
|
|
const getComponentCollided = (
|
|
|
|
|
bounds: DOMRect,
|
|
|
|
|
players: Player[],
|
|
|
|
|
components: TacticComponent[],
|
|
|
|
|
): number | -1 => {
|
|
|
|
|
for (let i = 0; i < players.length; i++) {
|
|
|
|
|
const player = players[i]
|
|
|
|
|
for (let i = 0; i < components.length; i++) {
|
|
|
|
|
const component = components[i]
|
|
|
|
|
const playerBounds = document
|
|
|
|
|
.getElementById(player.id)!
|
|
|
|
|
.getElementById(component.id)!
|
|
|
|
|
.getBoundingClientRect()
|
|
|
|
|
const doesOverlap = !(
|
|
|
|
|
bounds.top > playerBounds.bottom ||
|
|
|
|
|
bounds.right < playerBounds.left ||
|
|
|
|
|
bounds.bottom < playerBounds.top ||
|
|
|
|
|
bounds.left > playerBounds.right
|
|
|
|
|
)
|
|
|
|
|
if (doesOverlap) {
|
|
|
|
|
if (overlaps(playerBounds, bounds)) {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateActions(actions: Action[], players: Player[]) {
|
|
|
|
|
function updateActions(actions: Action[], components: TacticComponent[]) {
|
|
|
|
|
return actions.map((action) => {
|
|
|
|
|
const originHasBall = players.find(
|
|
|
|
|
(p) => p.id == action.fromPlayerId,
|
|
|
|
|
)!.hasBall
|
|
|
|
|
const originHasBall = (
|
|
|
|
|
components.find(
|
|
|
|
|
(p) => p.type == "player" && p.id == action.fromId,
|
|
|
|
|
)! as Player
|
|
|
|
|
).hasBall
|
|
|
|
|
|
|
|
|
|
let type = action.type
|
|
|
|
|
|
|
|
|
@ -280,80 +256,101 @@ function EditorView({
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onBallDropOnPlayer = (playerCollidedIdx: number) => {
|
|
|
|
|
const onBallDropOnComponent = (collidedComponentIdx: number) => {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
const ballObj = content.objects.findIndex((o) => o.type == "ball")
|
|
|
|
|
let player = content.players.at(playerCollidedIdx) as Player
|
|
|
|
|
const players = content.players.toSpliced(playerCollidedIdx, 1, {
|
|
|
|
|
...player,
|
|
|
|
|
hasBall: true,
|
|
|
|
|
})
|
|
|
|
|
const ballObj = content.components.findIndex(
|
|
|
|
|
(p) => p.type == BALL_TYPE,
|
|
|
|
|
)
|
|
|
|
|
let component = content.components[collidedComponentIdx]
|
|
|
|
|
if (component.type != "player") {
|
|
|
|
|
return content //do nothing if the ball isn't dropped on a player.
|
|
|
|
|
}
|
|
|
|
|
const components = content.components.toSpliced(
|
|
|
|
|
collidedComponentIdx,
|
|
|
|
|
1,
|
|
|
|
|
{
|
|
|
|
|
...component,
|
|
|
|
|
hasBall: true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
// Maybe the ball is not present on the court as an object component
|
|
|
|
|
// if so, don't bother removing it from the court.
|
|
|
|
|
// This can occur if the user drags and drop the ball from a player that already has the ball
|
|
|
|
|
// to another component
|
|
|
|
|
if (ballObj != -1) {
|
|
|
|
|
components.splice(ballObj, 1)
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
actions: updateActions(content.actions, players),
|
|
|
|
|
players,
|
|
|
|
|
objects: content.objects.toSpliced(ballObj, 1),
|
|
|
|
|
actions: updateActions(content.actions, components),
|
|
|
|
|
components,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onBallDrop = (refBounds: DOMRect) => {
|
|
|
|
|
const onBallMoved = (refBounds: DOMRect) => {
|
|
|
|
|
if (!isBoundsOnCourt(refBounds)) {
|
|
|
|
|
removeCourtBall()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const playerCollidedIdx = getPlayerCollided(refBounds, content.players)
|
|
|
|
|
const playerCollidedIdx = getComponentCollided(
|
|
|
|
|
refBounds,
|
|
|
|
|
content.components,
|
|
|
|
|
)
|
|
|
|
|
if (playerCollidedIdx != -1) {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
players: content.players.map((player) => ({
|
|
|
|
|
...player,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
})),
|
|
|
|
|
components: content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? {
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
}
|
|
|
|
|
: c,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
onBallDropOnPlayer(playerCollidedIdx)
|
|
|
|
|
onBallDropOnComponent(playerCollidedIdx)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (content.objects.findIndex((o) => o.type == "ball") != -1) {
|
|
|
|
|
if (content.components.findIndex((o) => o.type == "ball") != -1) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
const { x, y } = ratioWithinBase(refBounds, courtBounds)
|
|
|
|
|
let courtObject: CourtObject
|
|
|
|
|
|
|
|
|
|
courtObject = {
|
|
|
|
|
type: "ball",
|
|
|
|
|
const courtObject = {
|
|
|
|
|
type: BALL_TYPE,
|
|
|
|
|
id: BALL_ID,
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y,
|
|
|
|
|
}
|
|
|
|
|
} as Ball
|
|
|
|
|
|
|
|
|
|
let components = content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? {
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
}
|
|
|
|
|
: c,
|
|
|
|
|
)
|
|
|
|
|
components = [...components, courtObject]
|
|
|
|
|
|
|
|
|
|
const players = content.players.map((player) => ({
|
|
|
|
|
...player,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
actions: updateActions(content.actions, components),
|
|
|
|
|
components,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
actions: updateActions(content.actions, players),
|
|
|
|
|
players,
|
|
|
|
|
objects: [...content.objects, courtObject],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removePlayer = (player: Player) => {
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
players: toSplicedPlayers(content.players, player, false),
|
|
|
|
|
objects: [...content.objects],
|
|
|
|
|
components: replaceOrInsert(content.components, player, false),
|
|
|
|
|
actions: content.actions.filter(
|
|
|
|
|
(a) =>
|
|
|
|
|
a.toPlayerId !== player.id && a.fromPlayerId !== player.id,
|
|
|
|
|
(a) => a.toId !== player.id && a.fromId !== player.id,
|
|
|
|
|
),
|
|
|
|
|
}))
|
|
|
|
|
let setter
|
|
|
|
@ -379,14 +376,21 @@ function EditorView({
|
|
|
|
|
|
|
|
|
|
const removeCourtBall = () => {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
const ballObj = content.objects.findIndex((o) => o.type == "ball")
|
|
|
|
|
const ballObj = content.components.findIndex(
|
|
|
|
|
(o) => o.type == "ball",
|
|
|
|
|
)
|
|
|
|
|
const components = content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? ({
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
} as Player)
|
|
|
|
|
: c,
|
|
|
|
|
)
|
|
|
|
|
components.splice(ballObj, 1)
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
players: content.players.map((player) => ({
|
|
|
|
|
...player,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
})),
|
|
|
|
|
objects: content.objects.toSpliced(ballObj, 1),
|
|
|
|
|
components,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
setObjects([{ key: "ball" }])
|
|
|
|
@ -423,7 +427,7 @@ function EditorView({
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onPieceDetach}
|
|
|
|
|
onElementDetached={onRackPieceDetach}
|
|
|
|
|
render={({ team, key }) => (
|
|
|
|
|
<PlayerPiece
|
|
|
|
|
team={team}
|
|
|
|
@ -441,7 +445,7 @@ function EditorView({
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onObjectDetach}
|
|
|
|
|
onElementDetached={onRackedObjectDetach}
|
|
|
|
|
render={renderCourtObject}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
@ -452,7 +456,7 @@ function EditorView({
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onPieceDetach}
|
|
|
|
|
onElementDetached={onRackPieceDetach}
|
|
|
|
|
render={({ team, key }) => (
|
|
|
|
|
<PlayerPiece
|
|
|
|
|
team={team}
|
|
|
|
@ -466,16 +470,14 @@ function EditorView({
|
|
|
|
|
<div id="court-div">
|
|
|
|
|
<div id="court-div-bounds">
|
|
|
|
|
<BasketCourt
|
|
|
|
|
players={content.players}
|
|
|
|
|
objects={content.objects}
|
|
|
|
|
components={content.components}
|
|
|
|
|
actions={content.actions}
|
|
|
|
|
onBallMoved={onBallDrop}
|
|
|
|
|
onBallMoved={onBallMoved}
|
|
|
|
|
courtImage={<Court courtType={courtType} />}
|
|
|
|
|
courtRef={courtDivContentRef}
|
|
|
|
|
setActions={(actions) =>
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
players: content.players,
|
|
|
|
|
actions: actions(content.actions),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
@ -515,8 +517,8 @@ function EditorView({
|
|
|
|
|
}
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
players: toSplicedPlayers(
|
|
|
|
|
content.players,
|
|
|
|
|
components: replaceOrInsert(
|
|
|
|
|
content.components,
|
|
|
|
|
player,
|
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
@ -533,10 +535,11 @@ function EditorView({
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isBallOnCourt(content: TacticContent) {
|
|
|
|
|
if (content.players.findIndex((p) => p.hasBall) != -1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return content.objects.findIndex((o) => o.type == "ball") != -1
|
|
|
|
|
return (
|
|
|
|
|
content.components.findIndex(
|
|
|
|
|
(c) => (c.type == "player" && c.hasBall) || c.type == BALL_TYPE,
|
|
|
|
|
) != -1
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderCourtObject(courtObject: RackedCourtObject) {
|
|
|
|
@ -558,12 +561,18 @@ function Court({ courtType }: { courtType: string }) {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRackPlayers(team: PlayerTeam, players: Player[]): RackedPlayer[] {
|
|
|
|
|
|
|
|
|
|
function getRackPlayers(
|
|
|
|
|
team: PlayerTeam,
|
|
|
|
|
components: TacticComponent[],
|
|
|
|
|
): RackedPlayer[] {
|
|
|
|
|
return ["1", "2", "3", "4", "5"]
|
|
|
|
|
.filter(
|
|
|
|
|
(role) =>
|
|
|
|
|
players.findIndex((p) => p.team == team && p.role == role) ==
|
|
|
|
|
-1,
|
|
|
|
|
components.findIndex(
|
|
|
|
|
(c) =>
|
|
|
|
|
c.type == "player" && c.team == team && c.role == role,
|
|
|
|
|
) == -1,
|
|
|
|
|
)
|
|
|
|
|
.map((key) => ({ team, key }))
|
|
|
|
|
}
|
|
|
|
@ -611,14 +620,11 @@ function useContentState<S>(
|
|
|
|
|
return [content, setContentSynced, savingState]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toSplicedPlayers(
|
|
|
|
|
players: Player[],
|
|
|
|
|
player: Player,
|
|
|
|
|
function replaceOrInsert<A extends TacticComponent>(
|
|
|
|
|
array: A[],
|
|
|
|
|
it: A,
|
|
|
|
|
replace: boolean,
|
|
|
|
|
): Player[] {
|
|
|
|
|
const idx = players.findIndex(
|
|
|
|
|
(p) => p.team === player.team && p.role === player.role,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return players.toSpliced(idx, 1, ...(replace ? [player] : []))
|
|
|
|
|
): A[] {
|
|
|
|
|
const idx = array.findIndex((i) => i.id == it.id)
|
|
|
|
|
return array.toSpliced(idx, 1, ...(replace ? [it] : []))
|
|
|
|
|
}
|
|
|
|
|