Skip to content

Add exportMap #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/services/importMap/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,49 @@ const getMapFromFile = async (filePath) => {
return rooms
}

const exportMap = async (config) => {
const { common: { storage: { env, db } } } = config
if (!config.mongo) {
throw new Error('screepsmod-mongo required for map imports')
}
log('Exporting map')

// We want to pause the server just in case
const wasPaused = await env.get(env.keys.MAIN_LOOP_PAUSED)
if (!wasPaused) {
await env.set(env.keys.MAIN_LOOP_PAUSED, '1')
}

const roomNames = (await db.rooms.find({}, { _id: true })).map(r => r._id)
const shard = await env.get(env.keys.SHARD_NAME)
const date = new Date()
const desc = `${shard}:${date.getUTCFullYear()}-${date.getUTCMonth()}`
let count = 0
const allowedObjTypes = ['controller', 'source', 'mineral', 'extractor', 'keeperLair', 'deposit', 'portal']
const objectOrTypeSpec = allowedObjTypes.map(t => ({ type: t }))
const rooms = await Promise.all(roomNames.map(async (roomName) => {
const roomData = await db.rooms.findOne({ _id: roomName }, { projection: { _id: false } })
const objects = await db['rooms.objects'].find({ room: roomName, $or: objectOrTypeSpec })
const terrain = await db['rooms.terrain'].findOne({ room: roomName })
const room = {
room: roomName,
terrain: terrain.terrain,
objects: objects
}
Object.assign(room, roomData)
count++
return room
}))

const fileName = path.join(process.env.ASSET_DIR, `mapExport-${Number(date)}.json`)
await fs.writeFile(fileName, JSON.stringify({ description: desc, rooms }))

if (!wasPaused) {
await env.set(env.keys.MAIN_LOOP_PAUSED, '0')
}
return logResult(`Exported ${count} rooms to ${fileName}`)
}

module.exports = (config) => {
Object.assign(config.utils, {
async importMap (urlOrMapId) {
Expand All @@ -177,6 +220,9 @@ module.exports = (config) => {
const rooms = await getMapFromFile(filePath)
await loadRooms(config, filePath, rooms)
return logFinish()
},
async exportMap () {
return exportMap(config)
}
})

Expand All @@ -185,4 +231,7 @@ module.exports = (config) => {

config.utils.importMapFile._help =
'importMapFile(filePath) - import a map from a json file'

config.utils.exportMap._help =
'exportMap() - export the map to a json file in the assets directory'
}