-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefine.js
45 lines (42 loc) · 1.35 KB
/
refine.js
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
const { ScreepsAPI } = require('screeps-api')
const fs = require('fs').promises
const ROOM_REGEX = /^[EW][0-9]*[05][NS][0-9]*[05]$/
async function run() {
const [,,server,segment] = process.argv
const api = await ScreepsAPI.fromConfig(server)
const files = await fs.readdir('.')
const shardFiles = files.filter(f => f.endsWith('.portals.json'))
for(const file of shardFiles) {
console.log(`Processing ${file}`)
const [shard] = file.split('.')
const data = JSON.parse(await fs.readFile(file, 'utf8'))
const map = new Map()
for(const portal of data) {
const {
room: fromRoom,
destination: {
shard: toShard = shard,
room: toRoom
},
unstableDate
} = portal
if(!fromRoom.match(ROOM_REGEX) || !toRoom.match(ROOM_REGEX)) {
continue
}
const key = `${fromRoom} ${toShard} ${toRoom}`
const rec = [fromRoom, toShard, toRoom]
if (unstableDate) {
rec.push(unstableDate)
}
map.set(key, rec)
}
const raw = JSON.stringify(Array.from(map.values()))
await fs.writeFile(`${shard}.portals.min.json`, raw)
if(server && segment) {
console.log(`Uploading to segment ${segment} on ${shard} of ${server}`)
await api.memory.segment.set(+segment, raw, shard)
}
}
console.log('Done!')
}
run().catch(console.error)