forked from cmmcleod/coriolis-data
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathfix_bulkhead_ids.js
More file actions
143 lines (118 loc) · 4.03 KB
/
fix_bulkhead_ids.js
File metadata and controls
143 lines (118 loc) · 4.03 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Generate next ID in the sequence (6X, 6Y, 6Z, 70, 71, ..., 7A, 7B, etc.)
function getNextId(currentId) {
const firstChar = currentId.charAt(0);
const secondChar = currentId.charAt(1);
// Handle numeric second character
if (secondChar >= '0' && secondChar <= '9') {
if (secondChar === '9') {
return firstChar + 'A';
}
return firstChar + String.fromCharCode(secondChar.charCodeAt(0) + 1);
}
// Handle alphabetic second character (uppercase)
if (secondChar >= 'A' && secondChar <= 'Y') {
return firstChar + String.fromCharCode(secondChar.charCodeAt(0) + 1);
}
// Handle Z -> increment first character and reset to 0
if (secondChar === 'Z') {
if (firstChar >= '0' && firstChar <= '8') {
return String.fromCharCode(firstChar.charCodeAt(0) + 1) + '0';
}
if (firstChar === '9') {
return 'A0';
}
if (firstChar >= 'A' && firstChar <= 'Y') {
return String.fromCharCode(firstChar.charCodeAt(0) + 1) + '0';
}
if (firstChar === 'Z') {
throw new Error('ID space exhausted!');
}
}
throw new Error(`Invalid ID format: ${currentId}`);
}
function fixBulkheadIds() {
const shipsDir = path.join(__dirname, 'ships');
const shipFiles = fs.readdirSync(shipsDir)
.filter(f => f.endsWith('.json'))
.map(f => path.join(shipsDir, f));
// First pass: collect all used IDs and find duplicates
const idUsage = {};
const shipBulkheadData = {};
shipFiles.forEach(filePath => {
const filename = path.basename(filePath);
const content = fs.readFileSync(filePath, 'utf8');
const shipData = JSON.parse(content);
const shipId = Object.keys(shipData)[0];
const ship = shipData[shipId];
if (!ship.bulkheads || !Array.isArray(ship.bulkheads)) {
return;
}
shipBulkheadData[filename] = {
path: filePath,
shipData,
shipId,
bulkheads: ship.bulkheads
};
ship.bulkheads.forEach((b, idx) => {
if (!idUsage[b.id]) {
idUsage[b.id] = [];
}
idUsage[b.id].push({ filename, index: idx });
});
});
// Find ships with duplicate IDs that need fixing
const shipsToFix = new Set();
Object.keys(idUsage).forEach(id => {
if (idUsage[id].length > 1) {
// Keep first ship with this ID, fix the rest
idUsage[id].slice(1).forEach(usage => {
shipsToFix.add(usage.filename);
});
}
});
console.log(`Found ${shipsToFix.size} ships with duplicate bulkhead IDs that need fixing:\n`);
Array.from(shipsToFix).sort().forEach(ship => console.log(` - ${ship}`));
console.log('');
// Start assigning new IDs from 6X
let nextId = '6X';
const allUsedIds = new Set(Object.keys(idUsage));
// Second pass: fix duplicates
let fixedCount = 0;
Array.from(shipsToFix).sort().forEach(filename => {
const data = shipBulkheadData[filename];
if (!data) return;
console.log(`\nFixing ${filename}...`);
let modified = false;
data.bulkheads.forEach((bulkhead, idx) => {
// Check if this ID is a duplicate
if (idUsage[bulkhead.id] && idUsage[bulkhead.id].length > 1) {
// Check if this is NOT the first usage
const isFirstUsage = idUsage[bulkhead.id][0].filename === filename &&
idUsage[bulkhead.id][0].index === idx;
if (!isFirstUsage) {
const oldId = bulkhead.id;
// Find next unused ID
while (allUsedIds.has(nextId)) {
nextId = getNextId(nextId);
}
bulkhead.id = nextId;
allUsedIds.add(nextId);
console.log(` Bulkhead ${idx}: ${oldId} -> ${nextId}`);
modified = true;
nextId = getNextId(nextId);
}
}
});
if (modified) {
fs.writeFileSync(data.path, JSON.stringify(data.shipData, null, 2) + '\n', 'utf8');
console.log(` ✓ Updated ${filename}`);
fixedCount++;
}
});
console.log(`\n\nCompleted! Fixed ${fixedCount} ship files.`);
console.log(`Next available ID: ${nextId}`);
}
fixBulkheadIds();