From 7354586ef5867445c3109b6db031f97caf3fe5c0 Mon Sep 17 00:00:00 2001 From: MiguelMLorente Date: Sat, 30 Nov 2024 22:31:16 +0100 Subject: [PATCH] Created piece and placed piece abstractions --- interface/types/ExternalNode.ts | 6 +-- interface/types/InternalNode.ts | 11 ++++++ interface/types/Node.ts | 11 ++---- interface/types/Piece.ts | 69 +++++++++++++++++++++++++++++++++ interface/types/PlacedPiece.ts | 29 ++++++++++++++ 5 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 interface/types/InternalNode.ts create mode 100644 interface/types/Piece.ts create mode 100644 interface/types/PlacedPiece.ts diff --git a/interface/types/ExternalNode.ts b/interface/types/ExternalNode.ts index 14dcbbe..78447d3 100644 --- a/interface/types/ExternalNode.ts +++ b/interface/types/ExternalNode.ts @@ -2,14 +2,14 @@ import { Direction } from "../constants/Direction"; import { Border } from "./Border"; import { Cell } from "./Cell"; import { Exit } from "./Exit"; -import { Node } from "./Node"; -export class ExternalNode extends Node { +export class ExternalNode { public readonly direction: Direction; private border?: Border; + public readonly cell: Cell; constructor(cell: Cell, direction: Direction) { - super(cell); + this.cell = cell; this.direction = direction; } diff --git a/interface/types/InternalNode.ts b/interface/types/InternalNode.ts new file mode 100644 index 0000000..e60434b --- /dev/null +++ b/interface/types/InternalNode.ts @@ -0,0 +1,11 @@ +import { randomUUID } from "crypto"; + +export class InternalNode { + id: string; + type: string; + + constructor() { + this.id = randomUUID(); + this.type = "STATION"; + } +} diff --git a/interface/types/Node.ts b/interface/types/Node.ts index b185ad2..fa5bbe0 100644 --- a/interface/types/Node.ts +++ b/interface/types/Node.ts @@ -1,9 +1,4 @@ -import { Cell } from "./Cell"; +import { ExternalNode } from "./ExternalNode"; +import { InternalNode } from "./InternalNode"; -export abstract class Node { - public readonly cell: Cell; - - constructor(cell: Cell) { - this.cell = cell; - } -} +export type Node = ExternalNode | InternalNode; diff --git a/interface/types/Piece.ts b/interface/types/Piece.ts new file mode 100644 index 0000000..a1de677 --- /dev/null +++ b/interface/types/Piece.ts @@ -0,0 +1,69 @@ +import { Direction } from "../constants/Direction"; +import { TrackType } from "../constants/TrackType"; +import { Cell } from "./Cell"; +import { InternalNode } from "./InternalNode"; +import { PlacedPiece } from "./PlacedPiece"; +import { Node } from "./Node"; + +export interface TrackProps { + startPoint: Direction; + endPoint?: Direction; + isInternal: boolean; + type: TrackType; +} + +export class Piece { + tracks: Set<{ + joinedPoints: { + firstPoint: Direction; + secondPoint: Direction | InternalNode; + }; + type: TrackType; + }>; + internalNodes: Set; + + constructor(hasInternalNode: boolean, trackDefinitions: TrackProps[]) { + const internalNode = new InternalNode(); + this.internalNodes = new Set(); + if (hasInternalNode) { + this.internalNodes.add(internalNode); + } + this.tracks = new Set( + trackDefinitions.map((track) => { + if (!track.isInternal && !track.endPoint) { + throw Error("Missing direction for non-internal track"); + } + return { + joinedPoints: { + firstPoint: track.startPoint, + secondPoint: track.isInternal + ? internalNode + : (track.endPoint as Direction), + }, + type: track.type, + }; + }), + ); + } + + toPlacedPiece(cell: Cell) { + return new PlacedPiece( + new Set( + Array.from(this.tracks).map((track) => { + return { + nodes: { + firstNode: cell.getNodeAt(track.joinedPoints.firstPoint), + secondNode: + track.joinedPoints.secondPoint instanceof Node + ? track.joinedPoints.secondPoint as Node + : cell.getNodeAt(track.joinedPoints.secondPoint as Direction), + }, + type: track.type, + }; + }), + ), + this.internalNodes, + cell, + ); + } +} diff --git a/interface/types/PlacedPiece.ts b/interface/types/PlacedPiece.ts new file mode 100644 index 0000000..e1e83b5 --- /dev/null +++ b/interface/types/PlacedPiece.ts @@ -0,0 +1,29 @@ +import { TrackType } from "../constants/TrackType"; +import { Cell } from "./Cell"; +import { InternalNode } from "./InternalNode"; +import { Node } from "./Node"; + +export class PlacedPiece { + tracks: Set<{ + nodes: { + firstNode: Node; + secondNode: Node; + }; + type: TrackType; + }>; + internalNodes: Set; + cell: Cell; + + constructor( + tracks: Set<{ + nodes: { firstNode: Node; secondNode: Node }; + type: TrackType; + }>, + internalNodes: Set, + cell: Cell, + ) { + this.tracks = tracks; + this.internalNodes = internalNodes; + this.cell = cell; + } +}