-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphToRooms.ts
131 lines (112 loc) · 3.19 KB
/
graphToRooms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fs from "fs/promises";
interface Graph {
linksTo: Record<string, string[]>;
linkedFrom: Record<string, string[]>;
images: Record<string, []>;
}
async function loadGraph(): Promise<Graph> {
const data = await fs.readFile("graph.json", "utf-8");
return JSON.parse(data);
}
interface Room {
domain: string;
x: number;
y: number;
}
function neighbors(
x: number,
y: number,
empty: boolean,
rooms: Map<string, Room>
) {
const neighbors: [number, number][] = [];
for (const [dx, dy] of [
[0, 1],
// [1, 1],
[1, 0],
// [1, -1],
[0, -1],
// [-1, -1],
[-1, 0],
// [-1, 1],
]) {
const key = `${x + dx},${y + dy}`;
if (empty === (rooms.get(key) === undefined)) {
neighbors.push([x + dx, y + dy]);
}
}
return neighbors;
}
function createRooms(graph: Graph): Map<string, Room> {
const rooms = new Map<string, Room>();
const start = { domain: "breq.dev", x: 0, y: 0 };
const placed = new Set<string>(["breq.dev"]);
const queue: [number, number][] = [...neighbors(0, 0, true, rooms)];
rooms.set("0,0", start);
while (queue.length > 0) {
const current = queue.shift();
if (current === undefined) {
break;
}
const [x, y] = current;
// console.log(`Processing ${x},${y}`);
// find candidates based on filled neighbors
const candidatesCount = new Map<string, number>();
for (const [nx, ny] of neighbors(x, y, false, rooms)) {
const key = `${nx},${ny}`;
const room = rooms.get(key);
graph.linkedFrom[room!.domain]?.forEach((d) => {
candidatesCount.set(d, (candidatesCount.get(d) ?? 0) + 1);
});
graph.linksTo[room!.domain]?.forEach((d) => {
candidatesCount.set(d, (candidatesCount.get(d) ?? 0) + 1);
});
}
// sort candidates by number of links
const candidates = [...candidatesCount.entries()]
.sort(([a], [b]) => graph.linksTo[b]?.length - graph.linksTo[a]?.length)
.sort(([, a], [, b]) => b - a)
.map(([d]) => d)
.filter((d) => !placed.has(d));
// console.log(`Candidates: ${candidates.join(", ")}`);
// place candidate if available
if (candidates.length === 0) {
continue;
}
console.log(`Adding ${candidates[0]} at ${x},${y}`);
rooms.set(`${x},${y}`, { domain: candidates[0], x, y });
placed.add(candidates[0]);
// add vacant neighbor cells to queue
for (const [nx, ny] of neighbors(x, y, true, rooms)) {
if (
!placed.has(`${nx},${ny}`) &&
!queue.find(([qx, qy]) => qx === nx && qy === ny)
) {
queue.push([nx, ny]);
}
}
}
return rooms;
}
async function exportRooms(rooms: Map<string, Room>) {
const data = JSON.stringify(Object.fromEntries(rooms));
await fs.writeFile("rooms.json", data);
}
async function main() {
console.log("Loading graph");
const graph = await loadGraph();
console.log("Graph loaded");
const rooms = createRooms(graph);
console.log(
`Placed ${rooms.size} rooms out of ${
new Set([
...Object.values(graph.linksTo),
...Object.values(graph.linkedFrom),
]).size
}`
);
console.log("Exporting rooms");
await exportRooms(rooms);
console.log("Done!");
}
await main();