|
|
|
@ -8,20 +8,37 @@ import {BallPiece} from "../components/editor/BallPiece"
|
|
|
|
|
|
|
|
|
|
import {Rack} from "../components/Rack"
|
|
|
|
|
import {PlayerPiece} from "../components/editor/PlayerPiece"
|
|
|
|
|
import {Player, 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 {BALL_ID, BALL_TYPE, CourtObject, Ball} from "../model/tactic/Ball"
|
|
|
|
|
import {BALL_TYPE} from "../model/tactic/CourtObjects"
|
|
|
|
|
import {CourtAction} from "./editor/CourtAction"
|
|
|
|
|
import {BasketCourt} from "../components/editor/BasketCourt"
|
|
|
|
|
import {overlaps} from "../geo/Box"
|
|
|
|
|
import {
|
|
|
|
|
dropBallOnComponent,
|
|
|
|
|
getComponentCollided,
|
|
|
|
|
getRackPlayers,
|
|
|
|
|
moveComponent,
|
|
|
|
|
placeBallAt,
|
|
|
|
|
placeObjectAt,
|
|
|
|
|
placePlayerAt,
|
|
|
|
|
removeBall, updateComponent,
|
|
|
|
|
} from "../editor/TacticContentDomains"
|
|
|
|
|
import {BallState, Player, PlayerInfo, PlayerPhantom, PlayerTeam,} from "../model/tactic/Player"
|
|
|
|
|
import {RackedCourtObject} from "../editor/RackedItems"
|
|
|
|
|
import CourtPlayer from "../components/editor/CourtPlayer"
|
|
|
|
|
import {getActionKind, placeArrow, repositionActionsRelatedTo,} from "../editor/ActionsDomains"
|
|
|
|
|
import ArrowAction from "../components/actions/ArrowAction"
|
|
|
|
|
import {middlePos, ratioWithinBase} from "../geo/Pos"
|
|
|
|
|
import {Action, ActionKind} from "../model/tactic/Action"
|
|
|
|
|
import BallAction from "../components/actions/BallAction"
|
|
|
|
|
import {getOrigin, removePlayer, truncatePlayerPath,} from "../editor/PlayerDomains"
|
|
|
|
|
import {CourtBall} from "../components/editor/CourtBall"
|
|
|
|
|
import {BASE} from "../Constants"
|
|
|
|
|
import {overlaps} from "../geo/Box"
|
|
|
|
|
import {ratioWithinBase} from "../geo/Pos"
|
|
|
|
|
|
|
|
|
|
const ERROR_STYLE: CSSProperties = {
|
|
|
|
|
borderColor: "red",
|
|
|
|
@ -44,17 +61,7 @@ export interface EditorProps {
|
|
|
|
|
courtType: "PLAIN" | "HALF"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* information about a player that is into a rack
|
|
|
|
|
*/
|
|
|
|
|
interface RackedPlayer {
|
|
|
|
|
team: PlayerTeam
|
|
|
|
|
key: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RackedCourtObject = { key: "ball" }
|
|
|
|
|
|
|
|
|
|
export default function Editor({ id, name, courtType, content }: EditorProps) {
|
|
|
|
|
export default function Editor({id, name, courtType, content}: EditorProps) {
|
|
|
|
|
const isInGuestMode = id == -1
|
|
|
|
|
|
|
|
|
|
const storage_content = localStorage.getItem(GUEST_MODE_CONTENT_STORAGE_KEY)
|
|
|
|
@ -80,7 +87,7 @@ export default function Editor({ id, name, courtType, content }: EditorProps) {
|
|
|
|
|
)
|
|
|
|
|
return SaveStates.Guest
|
|
|
|
|
}
|
|
|
|
|
return fetchAPI(`tactic/${id}/save`, { content }).then((r) =>
|
|
|
|
|
return fetchAPI(`tactic/${id}/save`, {content}).then((r) =>
|
|
|
|
|
r.ok ? SaveStates.Ok : SaveStates.Err,
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
@ -89,7 +96,7 @@ export default function Editor({ id, name, courtType, content }: EditorProps) {
|
|
|
|
|
localStorage.setItem(GUEST_MODE_TITLE_STORAGE_KEY, name)
|
|
|
|
|
return true //simulate that the name has been changed
|
|
|
|
|
}
|
|
|
|
|
return fetchAPI(`tactic/${id}/edit/name`, { name }).then(
|
|
|
|
|
return fetchAPI(`tactic/${id}/edit/name`, {name}).then(
|
|
|
|
|
(r) => r.ok,
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
@ -99,11 +106,11 @@ export default function Editor({ id, name, courtType, content }: EditorProps) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function EditorView({
|
|
|
|
|
tactic: { id, name, content: initialContent },
|
|
|
|
|
tactic: {id, name, content: initialContent},
|
|
|
|
|
onContentChange,
|
|
|
|
|
onNameChange,
|
|
|
|
|
courtType,
|
|
|
|
|
}: EditorViewProps) {
|
|
|
|
|
}: EditorViewProps) {
|
|
|
|
|
const isInGuestMode = id == -1
|
|
|
|
|
|
|
|
|
|
const [titleStyle, setTitleStyle] = useState<CSSProperties>({})
|
|
|
|
@ -124,235 +131,36 @@ function EditorView({
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
|
getRackPlayers(PlayerTeam.Allies, content.components),
|
|
|
|
|
() => getRackPlayers(PlayerTeam.Allies, content.components),
|
|
|
|
|
)
|
|
|
|
|
const [opponents, setOpponents] = useState(
|
|
|
|
|
getRackPlayers(PlayerTeam.Opponents, content.components),
|
|
|
|
|
() => getRackPlayers(PlayerTeam.Opponents, content.components),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [objects, setObjects] = useState<RackedCourtObject[]>(
|
|
|
|
|
isBallOnCourt(content) ? [] : [{ key: "ball" }],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
const isBoundsOnCourt = (bounds: DOMRect) => {
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
// check if refBounds overlaps courtBounds
|
|
|
|
|
return overlaps(courtBounds, bounds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onRackPieceDetach = (ref: HTMLDivElement, element: RackedPlayer) => {
|
|
|
|
|
const refBounds = ref.getBoundingClientRect()
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
const { x, y } = ratioWithinBase(refBounds, courtBounds)
|
|
|
|
|
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
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 onRackedObjectDetach = (
|
|
|
|
|
ref: HTMLDivElement,
|
|
|
|
|
rackedObject: RackedCourtObject,
|
|
|
|
|
) => {
|
|
|
|
|
const refBounds = ref.getBoundingClientRect()
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
const { x, y } = ratioWithinBase(refBounds, courtBounds)
|
|
|
|
|
|
|
|
|
|
let courtObject: CourtObject
|
|
|
|
|
|
|
|
|
|
switch (rackedObject.key) {
|
|
|
|
|
case BALL_TYPE:
|
|
|
|
|
const ballObj = content.components.findIndex(
|
|
|
|
|
(o) => o.type == BALL_TYPE,
|
|
|
|
|
)
|
|
|
|
|
const playerCollidedIdx = getComponentCollided(
|
|
|
|
|
refBounds,
|
|
|
|
|
content.components.toSpliced(ballObj, 1),
|
|
|
|
|
() => isBallOnCourt(content) ? [] : [{key: "ball"}],
|
|
|
|
|
)
|
|
|
|
|
if (playerCollidedIdx != -1) {
|
|
|
|
|
onBallDropOnComponent(playerCollidedIdx)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
courtObject = {
|
|
|
|
|
type: BALL_TYPE,
|
|
|
|
|
id: BALL_ID,
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y,
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new Error("unknown court object " + rackedObject.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
components: [...content.components, courtObject],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getComponentCollided = (
|
|
|
|
|
bounds: DOMRect,
|
|
|
|
|
components: TacticComponent[],
|
|
|
|
|
): number | -1 => {
|
|
|
|
|
for (let i = 0; i < components.length; i++) {
|
|
|
|
|
const component = components[i]
|
|
|
|
|
const playerBounds = document
|
|
|
|
|
.getElementById(component.id)!
|
|
|
|
|
.getBoundingClientRect()
|
|
|
|
|
if (overlaps(playerBounds, bounds)) {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateActions(actions: Action[], components: TacticComponent[]) {
|
|
|
|
|
return actions.map((action) => {
|
|
|
|
|
const originHasBall = (
|
|
|
|
|
components.find(
|
|
|
|
|
(p) => p.type == "player" && p.id == action.fromId,
|
|
|
|
|
)! as Player
|
|
|
|
|
).hasBall
|
|
|
|
|
|
|
|
|
|
let type = action.type
|
|
|
|
|
|
|
|
|
|
if (originHasBall && type == ActionKind.MOVE) {
|
|
|
|
|
type = ActionKind.DRIBBLE
|
|
|
|
|
} else if (originHasBall && type == ActionKind.SCREEN) {
|
|
|
|
|
type = ActionKind.SHOOT
|
|
|
|
|
} else if (type == ActionKind.DRIBBLE) {
|
|
|
|
|
type = ActionKind.MOVE
|
|
|
|
|
} else if (type == ActionKind.SHOOT) {
|
|
|
|
|
type = ActionKind.SCREEN
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...action,
|
|
|
|
|
type,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
const [previewAction, setPreviewAction] = useState<Action | null>(null)
|
|
|
|
|
|
|
|
|
|
const onBallDropOnComponent = (collidedComponentIdx: number) => {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
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, components),
|
|
|
|
|
components,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
const courtRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
const onBallMoved = (refBounds: DOMRect) => {
|
|
|
|
|
if (!isBoundsOnCourt(refBounds)) {
|
|
|
|
|
removeCourtBall()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const playerCollidedIdx = getComponentCollided(
|
|
|
|
|
refBounds,
|
|
|
|
|
content.components,
|
|
|
|
|
)
|
|
|
|
|
if (playerCollidedIdx != -1) {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
components: content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? {
|
|
|
|
|
const setActions = (action: SetStateAction<Action[]>) => {
|
|
|
|
|
setContent((c) => ({
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
}
|
|
|
|
|
: c,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
onBallDropOnComponent(playerCollidedIdx)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (content.components.findIndex((o) => o.type == "ball") != -1) {
|
|
|
|
|
return
|
|
|
|
|
actions: typeof action == "function" ? action(c.actions) : action,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
const { x, y } = ratioWithinBase(refBounds, courtBounds)
|
|
|
|
|
const courtObject = {
|
|
|
|
|
type: BALL_TYPE,
|
|
|
|
|
id: BALL_ID,
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y,
|
|
|
|
|
} as Ball
|
|
|
|
|
|
|
|
|
|
let components = content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? {
|
|
|
|
|
const setComponents = (action: SetStateAction<TacticComponent[]>) => {
|
|
|
|
|
setContent((c) => ({
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
}
|
|
|
|
|
: c,
|
|
|
|
|
)
|
|
|
|
|
components = [...components, courtObject]
|
|
|
|
|
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
actions: updateActions(content.actions, components),
|
|
|
|
|
components,
|
|
|
|
|
components:
|
|
|
|
|
typeof action == "function" ? action(c.components) : action,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removePlayer = (player: Player) => {
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
components: replaceOrInsert(content.components, player, false),
|
|
|
|
|
actions: content.actions.filter(
|
|
|
|
|
(a) => a.toId !== player.id && a.fromId !== player.id,
|
|
|
|
|
),
|
|
|
|
|
}))
|
|
|
|
|
const insertRackedPlayer = (player: Player) => {
|
|
|
|
|
let setter
|
|
|
|
|
switch (player.team) {
|
|
|
|
|
case PlayerTeam.Opponents:
|
|
|
|
@ -361,8 +169,8 @@ function EditorView({
|
|
|
|
|
case PlayerTeam.Allies:
|
|
|
|
|
setter = setAllies
|
|
|
|
|
}
|
|
|
|
|
if (player.hasBall) {
|
|
|
|
|
setObjects([{ key: "ball" }])
|
|
|
|
|
if (player.ballState == BallState.HOLDS) {
|
|
|
|
|
setObjects([{key: "ball"}])
|
|
|
|
|
}
|
|
|
|
|
setter((players) => [
|
|
|
|
|
...players,
|
|
|
|
@ -374,26 +182,168 @@ function EditorView({
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removeCourtBall = () => {
|
|
|
|
|
const doMoveBall = (newBounds: DOMRect) => {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
const ballObj = content.components.findIndex(
|
|
|
|
|
(o) => o.type == "ball",
|
|
|
|
|
const {newContent, removed} = placeBallAt(
|
|
|
|
|
newBounds,
|
|
|
|
|
courtBounds(),
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
const components = content.components.map((c) =>
|
|
|
|
|
c.type == "player"
|
|
|
|
|
? ({
|
|
|
|
|
...c,
|
|
|
|
|
hasBall: false,
|
|
|
|
|
} as Player)
|
|
|
|
|
: c,
|
|
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
|
setObjects((objects) => [...objects, {key: "ball"}])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newContent
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const courtBounds = () => courtRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
const renderPlayer = (component: Player | PlayerPhantom) => {
|
|
|
|
|
let info: PlayerInfo
|
|
|
|
|
let canPlaceArrows: boolean
|
|
|
|
|
const isPhantom = component.type == "phantom"
|
|
|
|
|
|
|
|
|
|
if (isPhantom) {
|
|
|
|
|
const origin = getOrigin(component, content.components)
|
|
|
|
|
const path = origin.path!
|
|
|
|
|
// phantoms can only place other arrows if they are the head of the path
|
|
|
|
|
canPlaceArrows = path.items.indexOf(component.id) == path.items.length - 1
|
|
|
|
|
if (canPlaceArrows) {
|
|
|
|
|
// and if their only action is to shoot the ball
|
|
|
|
|
|
|
|
|
|
// list the actions the phantoms does
|
|
|
|
|
const phantomArrows = content.actions.filter(c => c.fromId == component.id)
|
|
|
|
|
canPlaceArrows = phantomArrows.length == 0 || phantomArrows.findIndex(c => c.type != ActionKind.SHOOT) == -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info = {
|
|
|
|
|
id: component.id,
|
|
|
|
|
team: origin.team,
|
|
|
|
|
role: origin.role,
|
|
|
|
|
bottomRatio: component.bottomRatio,
|
|
|
|
|
rightRatio: component.rightRatio,
|
|
|
|
|
ballState: component.ballState,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// a player
|
|
|
|
|
info = component
|
|
|
|
|
// can place arrows only if the
|
|
|
|
|
canPlaceArrows = component.path == null || content.actions.findIndex(p => p.fromId == component.id && p.type != ActionKind.SHOOT) == -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CourtPlayer
|
|
|
|
|
key={component.id}
|
|
|
|
|
className={isPhantom ? "phantom" : "player"}
|
|
|
|
|
playerInfo={info}
|
|
|
|
|
onMoves={() =>
|
|
|
|
|
setActions((actions) =>
|
|
|
|
|
repositionActionsRelatedTo(info.id, courtBounds(), actions),
|
|
|
|
|
)
|
|
|
|
|
components.splice(ballObj, 1)
|
|
|
|
|
return {
|
|
|
|
|
...content,
|
|
|
|
|
components,
|
|
|
|
|
}
|
|
|
|
|
onPositionValidated={(newPos) => {
|
|
|
|
|
setContent((content) =>
|
|
|
|
|
moveComponent(
|
|
|
|
|
newPos,
|
|
|
|
|
component,
|
|
|
|
|
info,
|
|
|
|
|
courtBounds(),
|
|
|
|
|
content,
|
|
|
|
|
|
|
|
|
|
(content) => {
|
|
|
|
|
if (!isPhantom) insertRackedPlayer(component)
|
|
|
|
|
return removePlayer(component, content)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
onRemove={() => {
|
|
|
|
|
setContent((c) => removePlayer(component, c))
|
|
|
|
|
if (!isPhantom) insertRackedPlayer(component)
|
|
|
|
|
}}
|
|
|
|
|
courtRef={courtRef}
|
|
|
|
|
availableActions={(pieceRef) => [
|
|
|
|
|
canPlaceArrows && (
|
|
|
|
|
<ArrowAction
|
|
|
|
|
key={1}
|
|
|
|
|
onHeadMoved={(headPos) => {
|
|
|
|
|
const arrowHeadPos = middlePos(headPos)
|
|
|
|
|
const targetIdx = getComponentCollided(headPos, content.components)
|
|
|
|
|
|
|
|
|
|
setPreviewAction((action) => ({
|
|
|
|
|
...action!,
|
|
|
|
|
segments: [
|
|
|
|
|
{
|
|
|
|
|
next: ratioWithinBase(
|
|
|
|
|
arrowHeadPos,
|
|
|
|
|
courtBounds(),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
type: getActionKind(targetIdx != -1, info.ballState),
|
|
|
|
|
}))
|
|
|
|
|
}}
|
|
|
|
|
onHeadPicked={(headPos) => {
|
|
|
|
|
(document.activeElement as HTMLElement).blur()
|
|
|
|
|
|
|
|
|
|
setPreviewAction({
|
|
|
|
|
type: getActionKind(false, info.ballState),
|
|
|
|
|
fromId: info.id,
|
|
|
|
|
toId: null,
|
|
|
|
|
moveFrom: ratioWithinBase(
|
|
|
|
|
middlePos(
|
|
|
|
|
pieceRef.getBoundingClientRect(),
|
|
|
|
|
),
|
|
|
|
|
courtBounds(),
|
|
|
|
|
),
|
|
|
|
|
segments: [
|
|
|
|
|
{
|
|
|
|
|
next: ratioWithinBase(
|
|
|
|
|
middlePos(headPos),
|
|
|
|
|
courtBounds(),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
onHeadDropped={(headRect) => {
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
let {createdAction, newContent} = placeArrow(
|
|
|
|
|
component,
|
|
|
|
|
courtBounds(),
|
|
|
|
|
headRect,
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
let originNewBallState = component.ballState
|
|
|
|
|
|
|
|
|
|
if (createdAction.type == ActionKind.SHOOT) {
|
|
|
|
|
const targetIdx = newContent.components.findIndex(c => c.id == createdAction.toId)
|
|
|
|
|
newContent = dropBallOnComponent(targetIdx, newContent)
|
|
|
|
|
originNewBallState = BallState.SHOOTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newContent = updateComponent({
|
|
|
|
|
...(newContent.components.find(c => c.id == component.id)! as Player | PlayerPhantom),
|
|
|
|
|
ballState: originNewBallState
|
|
|
|
|
}, newContent)
|
|
|
|
|
return newContent
|
|
|
|
|
})
|
|
|
|
|
setObjects([{ key: "ball" }])
|
|
|
|
|
setPreviewAction(null)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
info.ballState != BallState.NONE && (
|
|
|
|
|
<BallAction
|
|
|
|
|
key={2}
|
|
|
|
|
onDrop={doMoveBall}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
@ -403,7 +353,7 @@ function EditorView({
|
|
|
|
|
Home
|
|
|
|
|
</button>
|
|
|
|
|
<div id="topbar-left">
|
|
|
|
|
<SavingState state={saveState} />
|
|
|
|
|
<SavingState state={saveState}/>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="title-input-div">
|
|
|
|
|
<TitleInput
|
|
|
|
@ -416,7 +366,7 @@ function EditorView({
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="topbar-right" />
|
|
|
|
|
<div id="topbar-right"/>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="edit-div">
|
|
|
|
|
<div id="racks">
|
|
|
|
@ -425,10 +375,19 @@ function EditorView({
|
|
|
|
|
objects={allies}
|
|
|
|
|
onChange={setAllies}
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
overlaps(courtBounds(), div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={(r, e) =>
|
|
|
|
|
setComponents((components) => [
|
|
|
|
|
...components,
|
|
|
|
|
placePlayerAt(
|
|
|
|
|
r.getBoundingClientRect(),
|
|
|
|
|
courtBounds(),
|
|
|
|
|
e,
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onRackPieceDetach}
|
|
|
|
|
render={({ team, key }) => (
|
|
|
|
|
render={({team, key}) => (
|
|
|
|
|
<PlayerPiece
|
|
|
|
|
team={team}
|
|
|
|
|
text={key}
|
|
|
|
@ -443,9 +402,18 @@ function EditorView({
|
|
|
|
|
objects={objects}
|
|
|
|
|
onChange={setObjects}
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
overlaps(courtBounds(), div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={(r, e) =>
|
|
|
|
|
setContent((content) =>
|
|
|
|
|
placeObjectAt(
|
|
|
|
|
r.getBoundingClientRect(),
|
|
|
|
|
courtBounds(),
|
|
|
|
|
e,
|
|
|
|
|
content,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onRackedObjectDetach}
|
|
|
|
|
render={renderCourtObject}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
@ -454,10 +422,19 @@ function EditorView({
|
|
|
|
|
objects={opponents}
|
|
|
|
|
onChange={setOpponents}
|
|
|
|
|
canDetach={(div) =>
|
|
|
|
|
isBoundsOnCourt(div.getBoundingClientRect())
|
|
|
|
|
overlaps(courtBounds(), div.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={(r, e) =>
|
|
|
|
|
setComponents((components) => [
|
|
|
|
|
...components,
|
|
|
|
|
placePlayerAt(
|
|
|
|
|
r.getBoundingClientRect(),
|
|
|
|
|
courtBounds(),
|
|
|
|
|
e,
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
onElementDetached={onRackPieceDetach}
|
|
|
|
|
render={({ team, key }) => (
|
|
|
|
|
render={({team, key}) => (
|
|
|
|
|
<PlayerPiece
|
|
|
|
|
team={team}
|
|
|
|
|
text={key}
|
|
|
|
@ -472,28 +449,85 @@ function EditorView({
|
|
|
|
|
<BasketCourt
|
|
|
|
|
components={content.components}
|
|
|
|
|
actions={content.actions}
|
|
|
|
|
onBallMoved={onBallMoved}
|
|
|
|
|
courtImage={<Court courtType={courtType} />}
|
|
|
|
|
courtRef={courtDivContentRef}
|
|
|
|
|
setActions={(actions) =>
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
actions: actions(content.actions),
|
|
|
|
|
}))
|
|
|
|
|
courtImage={<Court courtType={courtType}/>}
|
|
|
|
|
courtRef={courtRef}
|
|
|
|
|
previewAction={previewAction}
|
|
|
|
|
renderComponent={(component) => {
|
|
|
|
|
if (
|
|
|
|
|
component.type == "player" ||
|
|
|
|
|
component.type == "phantom"
|
|
|
|
|
) {
|
|
|
|
|
return renderPlayer(component)
|
|
|
|
|
}
|
|
|
|
|
if (component.type == BALL_TYPE) {
|
|
|
|
|
return (
|
|
|
|
|
<CourtBall
|
|
|
|
|
key="ball"
|
|
|
|
|
ball={component}
|
|
|
|
|
onPosValidated={doMoveBall}
|
|
|
|
|
onMoves={() =>
|
|
|
|
|
setActions((actions) =>
|
|
|
|
|
repositionActionsRelatedTo(
|
|
|
|
|
component.id,
|
|
|
|
|
courtBounds(),
|
|
|
|
|
actions,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
onRemove={() => {
|
|
|
|
|
setContent((content) =>
|
|
|
|
|
removeBall(content),
|
|
|
|
|
)
|
|
|
|
|
setObjects(objects => [...objects, {key: "ball"}])
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
throw new Error(
|
|
|
|
|
"unknown tactic component " + component,
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
renderAction={(action, i) => (
|
|
|
|
|
<CourtAction
|
|
|
|
|
key={i}
|
|
|
|
|
action={action}
|
|
|
|
|
courtRef={courtDivContentRef}
|
|
|
|
|
courtRef={courtRef}
|
|
|
|
|
onActionDeleted={() => {
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
content = {
|
|
|
|
|
...content,
|
|
|
|
|
actions: content.actions.toSpliced(
|
|
|
|
|
actions:
|
|
|
|
|
content.actions.toSpliced(
|
|
|
|
|
i,
|
|
|
|
|
1,
|
|
|
|
|
),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action.toId == null)
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
const target =
|
|
|
|
|
content.components.find(
|
|
|
|
|
(c) => action.toId == c.id,
|
|
|
|
|
)!
|
|
|
|
|
|
|
|
|
|
if (target.type == "phantom") {
|
|
|
|
|
const origin = getOrigin(
|
|
|
|
|
target,
|
|
|
|
|
content.components,
|
|
|
|
|
)
|
|
|
|
|
if (origin.id != action.fromId) {
|
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
content = truncatePlayerPath(
|
|
|
|
|
origin,
|
|
|
|
|
target,
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
onActionChanges={(a) =>
|
|
|
|
|
setContent((content) => ({
|
|
|
|
@ -507,25 +541,6 @@ function EditorView({
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
onPlayerChange={(player) => {
|
|
|
|
|
const playerBounds = document
|
|
|
|
|
.getElementById(player.id)!
|
|
|
|
|
.getBoundingClientRect()
|
|
|
|
|
if (!isBoundsOnCourt(playerBounds)) {
|
|
|
|
|
removePlayer(player)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
setContent((content) => ({
|
|
|
|
|
...content,
|
|
|
|
|
components: replaceOrInsert(
|
|
|
|
|
content.components,
|
|
|
|
|
player,
|
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
|
}))
|
|
|
|
|
}}
|
|
|
|
|
onPlayerRemove={removePlayer}
|
|
|
|
|
onBallRemove={removeCourtBall}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
@ -537,46 +552,30 @@ function EditorView({
|
|
|
|
|
function isBallOnCourt(content: TacticContent) {
|
|
|
|
|
return (
|
|
|
|
|
content.components.findIndex(
|
|
|
|
|
(c) => (c.type == "player" && c.hasBall) || c.type == BALL_TYPE,
|
|
|
|
|
(c) => (c.type == "player" && c.ballState == BallState.HOLDS) || c.type == BALL_TYPE,
|
|
|
|
|
) != -1
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderCourtObject(courtObject: RackedCourtObject) {
|
|
|
|
|
if (courtObject.key == "ball") {
|
|
|
|
|
return <BallPiece />
|
|
|
|
|
return <BallPiece/>
|
|
|
|
|
}
|
|
|
|
|
throw new Error("unknown racked court object " + courtObject.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Court({ courtType }: { courtType: string }) {
|
|
|
|
|
function Court({courtType}: { courtType: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<div id="court-image-div">
|
|
|
|
|
{courtType == "PLAIN" ? (
|
|
|
|
|
<PlainCourt id="court-image" />
|
|
|
|
|
<PlainCourt id="court-image"/>
|
|
|
|
|
) : (
|
|
|
|
|
<HalfCourt id="court-image" />
|
|
|
|
|
<HalfCourt id="court-image"/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getRackPlayers(
|
|
|
|
|
team: PlayerTeam,
|
|
|
|
|
components: TacticComponent[],
|
|
|
|
|
): RackedPlayer[] {
|
|
|
|
|
return ["1", "2", "3", "4", "5"]
|
|
|
|
|
.filter(
|
|
|
|
|
(role) =>
|
|
|
|
|
components.findIndex(
|
|
|
|
|
(c) =>
|
|
|
|
|
c.type == "player" && c.team == team && c.role == role,
|
|
|
|
|
) == -1,
|
|
|
|
|
)
|
|
|
|
|
.map((key) => ({ team, key }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function debounceAsync<A, B>(
|
|
|
|
|
f: (args: A) => Promise<B>,
|
|
|
|
|
delay = 1000,
|
|
|
|
@ -605,6 +604,7 @@ function useContentState<S>(
|
|
|
|
|
typeof newState === "function"
|
|
|
|
|
? (newState as (state: S) => S)(content)
|
|
|
|
|
: newState
|
|
|
|
|
|
|
|
|
|
if (state !== content) {
|
|
|
|
|
setSavingState(SaveStates.Saving)
|
|
|
|
|
saveStateCallback(state)
|
|
|
|
@ -619,12 +619,3 @@ function useContentState<S>(
|
|
|
|
|
|
|
|
|
|
return [content, setContentSynced, savingState]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replaceOrInsert<A extends TacticComponent>(
|
|
|
|
|
array: A[],
|
|
|
|
|
it: A,
|
|
|
|
|
replace: boolean,
|
|
|
|
|
): A[] {
|
|
|
|
|
const idx = array.findIndex((i) => i.id == it.id)
|
|
|
|
|
return array.toSpliced(idx, 1, ...(replace ? [it] : []))
|
|
|
|
|
}
|
|
|
|
|