Skip to content

Commit 65b5c03

Browse files
authored
refactor: Edge’s ids are strings of the form 'id1,id2' (#99)
where 'id1' is the greatest id between the devices connected ids, while 'id2' is the lowest
1 parent 07125d4 commit 65b5c03

File tree

14 files changed

+2765
-131
lines changed

14 files changed

+2765
-131
lines changed

package-lock.json

Lines changed: 2667 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"inline-source-map": "0.6.3",
2020
"jest": "^29.7.0",
2121
"mini-css-extract-plugin": "^2.9.2",
22+
"npm": "^11.0.0",
2223
"prettier": "^3.3.3",
2324
"style-loader": "4.0.0",
2425
"ts-jest": "^29.2.5",

src/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class GlobalContext {
6262

6363
changeViewGraph(selectedLayer: string) {
6464
const layer = layerFromName(selectedLayer);
65+
urManager.reset();
6566
this.setNetwork(this.datagraph, layer);
6667
}
6768

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function loadAssets(otherPromises: Promise<void>[]) {
183183

184184
pauseButton.onclick = triggerPause;
185185

186-
// (!) For layer abstraction functionality
186+
// Layer abstraction logic
187187
const selectNewLayer = (event: Event) => {
188188
const selectedLayer = (event.target as HTMLSelectElement).value;
189189
console.log(`Layer selected: ${selectedLayer}`);

src/types/devices/device.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TextStyle,
77
Text,
88
} from "pixi.js";
9-
import { EdgeId, ViewGraph } from "./../graphs/viewgraph";
9+
import { ViewGraph } from "./../graphs/viewgraph";
1010
import {
1111
deselectElement,
1212
refreshElement,
@@ -19,7 +19,7 @@ import { Position } from "../common";
1919
import { DeviceInfo } from "../../graphics/renderables/device_info";
2020
import { IpAddress } from "../../packets/ip";
2121
import { DeviceId } from "../graphs/datagraph";
22-
import { DragDeviceMove, EdgeData, AddEdgeMove } from "../undo-redo";
22+
import { DragDeviceMove, AddEdgeMove } from "../undo-redo";
2323
import { Layer } from "./layer";
2424

2525
export { Layer } from "./layer";
@@ -43,7 +43,7 @@ export function layerFromType(type: DeviceType) {
4343
export abstract class Device extends Sprite {
4444
readonly id: DeviceId;
4545
readonly viewgraph: ViewGraph;
46-
connections = new Map<EdgeId, DeviceId>();
46+
connections = new Set<DeviceId>();
4747

4848
highlightMarker: Graphics | null = null; // Marker to indicate selection
4949

@@ -99,19 +99,15 @@ export abstract class Device extends Sprite {
9999
this.addChild(idText); // Add the ID text as a child of the device
100100
}
101101

102-
getConnections(): { edgeId: EdgeId; adyacentId: DeviceId }[] {
103-
return Array.from(this.connections.entries()).map(
104-
([edgeId, adyacentId]) => {
105-
return { edgeId, adyacentId };
106-
},
107-
);
102+
getConnections(): DeviceId[] {
103+
return Array.from(this.connections.values());
108104
}
109105

110-
addConnection(edgeId: EdgeId, adyacentId: DeviceId) {
111-
this.connections.set(edgeId, adyacentId);
106+
addConnection(adyacentId: DeviceId) {
107+
this.connections.add(adyacentId);
112108
}
113109

114-
removeConnection(id: EdgeId) {
110+
removeConnection(id: DeviceId) {
115111
this.connections.delete(id);
116112
}
117113

@@ -145,22 +141,18 @@ export abstract class Device extends Sprite {
145141
this.parent.on("pointerup", onPointerUp);
146142
}
147143

148-
connectTo(adyacentId: number): boolean {
144+
connectTo(adyacentId: DeviceId): boolean {
149145
// Connects both devices with an edge.
150146
// console.log("Entered connectTo");
151147

152148
const edgeId = this.viewgraph.addEdge(this.id, adyacentId);
153149
if (edgeId) {
154150
const adyacentDevice = this.viewgraph.getDevice(adyacentId);
155-
this.addConnection(edgeId, adyacentId);
156-
adyacentDevice.addConnection(edgeId, this.id);
151+
this.addConnection(adyacentId);
152+
adyacentDevice.addConnection(this.id);
157153

158154
// Register move
159-
const moveData: EdgeData = {
160-
edgeId,
161-
connectedNodes: { n1: this.id, n2: adyacentId },
162-
};
163-
const move = new AddEdgeMove(moveData);
155+
const move = new AddEdgeMove({ n1: this.id, n2: adyacentId });
164156
urManager.push(move);
165157

166158
return true;

src/types/devices/host.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createDropdown, DeviceInfo, RightBar } from "../../graphics/right_bar";
77
import { ProgramInfo } from "../../graphics/renderables/device_info";
88
import { sendPacket } from "../packet";
99
import { Ticker } from "pixi.js";
10+
import { DeviceId } from "../graphs/datagraph";
1011
import { Layer } from "./layer";
1112

1213
const DEFAULT_ECHO_DELAY = 250; // ms
@@ -15,7 +16,7 @@ export class Host extends Device {
1516
currentProgram: (ticker: Ticker) => void = undefined;
1617

1718
constructor(
18-
id: number,
19+
id: DeviceId,
1920
viewgraph: ViewGraph,
2021
position: Position,
2122
ip: IpAddress,

src/types/devices/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import RouterImage from "../../assets/router.svg";
44
import { Position } from "../common";
55
import { DeviceInfo, RightBar } from "../../graphics/right_bar";
66
import { IpAddress } from "../../packets/ip";
7+
import { DeviceId } from "../graphs/datagraph";
78

89
export class Router extends Device {
910
constructor(
10-
id: number,
11+
id: DeviceId,
1112
viewgraph: ViewGraph,
1213
position: Position,
1314
ip: IpAddress,

src/types/devices/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { IpAddress } from "../../packets/ip";
2+
import { DeviceId } from "../graphs/datagraph";
23
import { ViewGraph } from "../graphs/viewgraph";
34
import { Device, DeviceType } from "./device";
45
import { Host } from "./host";
56
import { Router } from "./router";
67

78
export interface CreateDevice {
8-
id: number;
9+
id: DeviceId;
910
type: DeviceType;
1011
x: number;
1112
y: number;

src/types/edge.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graphics, Point } from "pixi.js";
2-
import { EdgeId, ViewGraph } from "./graphs/viewgraph";
2+
import { ViewGraph } from "./graphs/viewgraph";
33
import { Device } from "./devices/index"; // Import the Device class
44
import { deselectElement, selectElement, urManager } from "./viewportManager";
55
import { RightBar, StyledInfo } from "../graphics/right_bar";
@@ -14,23 +14,24 @@ export interface EdgeEdges {
1414
}
1515

1616
export class Edge extends Graphics {
17-
id: EdgeId;
18-
connectedNodes: { n1: DeviceId; n2: DeviceId };
17+
connectedNodes: EdgeEdges;
1918
startPos: Point;
2019
endPos: Point;
2120
viewgraph: ViewGraph;
2221
rightbar: RightBar;
2322

23+
static generateConnectionKey(connectedNodes: EdgeEdges): string {
24+
const { n1, n2 } = connectedNodes;
25+
return [n1, n2].sort().join(",");
26+
}
27+
2428
constructor(
25-
id: EdgeId,
2629
connectedNodes: EdgeEdges,
2730
device1: Device,
2831
device2: Device,
2932
viewgraph: ViewGraph,
3033
) {
3134
super();
32-
33-
this.id = id;
3435
this.connectedNodes = connectedNodes;
3536
this.viewgraph = viewgraph;
3637
this.rightbar = RightBar.getInstance();
@@ -98,7 +99,7 @@ export class Edge extends Graphics {
9899
// Method to show the Edge information
99100
showInfo() {
100101
const info = new StyledInfo("Edge Information");
101-
info.addField("Edge ID", this.id.toString());
102+
info.addField("Edge ID", Edge.generateConnectionKey(this.connectedNodes));
102103
info.addField(
103104
"Connected Devices",
104105
`${this.connectedNodes.n1} <=> ${this.connectedNodes.n2}`,
@@ -120,10 +121,7 @@ export class Edge extends Graphics {
120121
"Delete Edge",
121122
() => {
122123
// obtener info
123-
const move = new RemoveEdgeMove({
124-
edgeId: this.id,
125-
connectedNodes: this.connectedNodes,
126-
});
124+
const move = new RemoveEdgeMove(this.connectedNodes);
127125
this.delete();
128126
urManager.push(move);
129127
// registrar movimiento
@@ -136,10 +134,11 @@ export class Edge extends Graphics {
136134
delete() {
137135
// Remove the edge from the viewgraph and datagraph
138136
deselectElement();
139-
this.viewgraph.removeEdge(this.id);
137+
const id = Edge.generateConnectionKey(this.connectedNodes);
138+
this.viewgraph.removeEdge(id);
140139
this.destroy();
141140

142-
console.log(`Edge ${this.id} deleted.`);
141+
console.log(`Edge ${id} deleted.`);
143142
}
144143

145144
public updatePosition(device1: Device, device2: Device) {

0 commit comments

Comments
 (0)