Skip to content

Commit

Permalink
fix: package json breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Jan 16, 2025
1 parent 7b02a40 commit 8b64d97
Show file tree
Hide file tree
Showing 9 changed files with 1,242 additions and 2,207 deletions.
25 changes: 6 additions & 19 deletions examples/grid-collision/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"name": "topology-example-grid-collision",
"version": "0.2.1-0",
"description": "Topology Protocol Grid Example",
"main": "src/index.ts",
"repository": "https://github.com/topology-foundation/ts-topology.git",
"version": "0.6.0",
"license": "MIT",
"scripts": {
"build": "vite build",
Expand All @@ -14,22 +11,12 @@
"dependencies": {
"@ts-drp/network": "0.6.0",
"@ts-drp/node": "0.6.0",
"@ts-drp/object": "0.6.0",
"assemblyscript": "^0.27.29",
"crypto-browserify": "^3.12.0",
"memfs": "^4.11.1",
"process": "^0.11.10",
"react-spring": "^9.7.4",
"stream-browserify": "^3.0.0",
"ts-node": "^10.9.2",
"uint8arrays": "^5.1.0",
"vm-browserify": "^1.1.2"
"@ts-drp/object": "0.6.0"
},
"devDependencies": {
"@types/node": "^22.5.4",
"ts-loader": "^9.5.1",
"typescript": "^5.5.4",
"vite": "^5.4.9",
"vite-plugin-node-polyfills": "^0.22.0"
"@types/node": "^22.5.4",
"typescript": "^5.5.4",
"vite": "^6.0.2",
"vite-plugin-node-polyfills": "^0.22.0"
}
}
19 changes: 13 additions & 6 deletions examples/grid-collision/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { TopologyNode } from "@topology-foundation/node";
import type { TopologyObject } from "@topology-foundation/object";
import { DRPNode } from "@ts-drp/node";
import type { DRPObject } from "@ts-drp/object";
import { Grid } from "./objects/grid";
import { hslToRgb, rgbToHex, rgbToHsl } from "./util/color";

const node = new TopologyNode();
let topologyObject: TopologyObject;
const node = new DRPNode({
network_config: {
bootstrap_peers: [
'/ip4/0.0.0.0/tcp/50000/ws/p2p/12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ',
],
bootstrap: false
}
});
let topologyObject: DRPObject;
let gridCRO: Grid;
let peers: string[] = [];
let discoveryPeers: string[] = [];
Expand Down Expand Up @@ -216,7 +223,7 @@ async function main() {
);
button_create.addEventListener("click", async () => {
topologyObject = await node.createObject(new Grid());
gridCRO = topologyObject.cro as Grid;
gridCRO = topologyObject.drp as Grid;
createConnectHandlers();
await addUser();
render();
Expand All @@ -233,7 +240,7 @@ async function main() {
undefined,
true,
);
gridCRO = topologyObject.cro as Grid;
gridCRO = topologyObject.drp as Grid;
createConnectHandlers();
await addUser();
render();
Expand Down
99 changes: 53 additions & 46 deletions examples/grid-collision/src/objects/grid.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ActionType,
type CRO,
type DRP,
type Operation,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@topology-foundation/object";
} from "@ts-drp/object";

export class Grid implements CRO {
export class Grid implements DRP {
operations: string[] = ["addUser", "moveUser"];
semanticsType: SemanticsType = SemanticsType.pair;
positions: Map<string, { x: number; y: number }>;
Expand All @@ -22,6 +22,8 @@ export class Grid implements CRO {

private _addUser(userId: string, color: string): void {
const userColorString = `${userId}:${color}`;
// We should set a random coordinate, but here we don't.
// This is to simplify the logic for spawning.
this.positions.set(userColorString, { x: 0, y: 0 });
}

Expand Down Expand Up @@ -59,6 +61,7 @@ export class Grid implements CRO {
);
if (userColorString) {
const position = this.positions.get(userColorString);

if (position) {
const newPos = this._computeNewPosition(position, direction);
this.positions.set(userColorString, newPos);
Expand All @@ -84,53 +87,57 @@ export class Grid implements CRO {
// Here we implement compensation for the location.
// As we operate based on pairwise comparison, there's always only 2 elements.
// First the vertices must be available, and also not of the same node.
if (vertices.length === 2 && vertices[0].nodeId !== vertices[1].nodeId) {
const leftVertex = vertices[0];
const rightVertex = vertices[1];
const leftVertexPosition = leftVertex.operation
? this.getUserPosition(":".concat(leftVertex.operation.value))
: undefined;
const rightVertexPosition = rightVertex.operation
? this.getUserPosition(":".concat(rightVertex.operation.value))
: undefined;
console.log(vertices);
// Let's first handle adding a new user
// if (vertices.length === 2 && vertices[0].nodeId !== vertices[1].nodeId) {
const leftVertex = vertices[0];
const rightVertex = vertices[1];
const leftVertexPosition = leftVertex.operation
? this.getUserPosition(":".concat(leftVertex.operation.value))
: undefined;
const rightVertexPosition = rightVertex.operation
? this.getUserPosition(":".concat(rightVertex.operation.value))
: undefined;
console.log("resolveConflicts vertices:", vertices)

// // Let's first handle adding a new user
// if (
// leftVertex.operation?.type === "addUser" &&
// rightVertex.operation?.type === "addUser" &&
// leftVertex.nodeId !== rightVertex.nodeId
// ) {
// // If the node id is not the same but its considered a conflict, ignore conflict.
// return {action: ActionType.}
// }

// Now handle moving the user
if (
leftVertex.operation?.type === "moveUser" &&
rightVertex.operation?.type === "moveUser" &&
leftVertexPosition &&
rightVertexPosition
) {
const leftVertexNextPosition = this._computeNewPosition(
leftVertexPosition,
leftVertex.operation.value[1],
);
const rightVertexNextPosition = this._computeNewPosition(
rightVertexPosition,
rightVertex.operation.value[1],
);
console.log(
"Positions: ",
leftVertexNextPosition,
rightVertexNextPosition,
);

// If they are going to colide, do nothing so they don't move and thus do not colide.
if (
leftVertex.operation?.type === "addUser" &&
rightVertex.operation?.type === "addUser"
leftVertexNextPosition.x === rightVertexNextPosition.x &&
leftVertexNextPosition.y === rightVertexNextPosition.y
) {
// This basically tells the cro to accept only the ones that comes first.
if (leftVertexPosition) {
return { action: ActionType.DropRight };
}
return { action: ActionType.DropLeft };
}

// Now handle moving the user
if (
leftVertex.operation?.type === "moveUser" &&
rightVertex.operation?.type === "moveUser" &&
leftVertexPosition &&
rightVertexPosition
) {
const leftVertexNextPosition = this._computeNewPosition(
leftVertexPosition,
leftVertex.operation.value[1],
);
const rightVertexNextPosition = this._computeNewPosition(
rightVertexPosition,
rightVertex.operation.value[1],
);

// If they are going to colide, do nothing so they don't move and thus do not colide.
if (
leftVertexNextPosition.x === rightVertexNextPosition.x &&
leftVertexNextPosition.y === rightVertexNextPosition.y
) {
return { action: ActionType.Drop };
}
return { action: ActionType.Drop };
}
}
// }

// If none of the operations match our criteria, they are concurrent
// safe, and thus we don't need to do anything.
Expand Down
2 changes: 1 addition & 1 deletion examples/grid-collision/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
},
resolve: {
alias: {
"@topology-foundation": path.resolve(__dirname, "../../packages"),
"@ts-drp": path.resolve(__dirname, "../../packages"),
},
},
});
2 changes: 1 addition & 1 deletion packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,4 @@ export class DRPNetworkNode {
addCustomMessageHandler(protocol: string | string[], handler: StreamHandler) {
this._node?.handle(protocol, handler);
}
}
}
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.5.3";
export const VERSION = "0.6.0";
4 changes: 4 additions & 0 deletions packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function linearizePairSemantics(
): Operation[] {
const order: Hash[] = hashGraph.topologicalSort(true, origin, subgraph);
const dropped = new Array(order.length).fill(false);
console.log("hashgraph:", hashGraph);
console.log("order:", order);
console.log("subgraph:", subgraph);
const result = [];
// alway remove the first operation
let i = 1;
Expand All @@ -26,6 +29,7 @@ export function linearizePairSemantics(
let j = i + 1;

while (j < order.length) {
console.log("causally related:", anchor, order[j], hashGraph.areCausallyRelatedUsingBitsets(anchor, order[j]));
if (
hashGraph.areCausallyRelatedUsingBitsets(anchor, order[j]) ||
dropped[j]
Expand Down
Loading

0 comments on commit 8b64d97

Please sign in to comment.