const fs = require('mz/fs')
const R = require('ramda')
const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './sqlite-latest.sqlite'
  },
  useNullAsDefault: true
})

const getRegions = () => knex('mapRegions')
  .select(['regionID', 'regionName'])
  .reduce((acc, region) => {
    acc[region.regionID] = region.regionName
    return acc
  }, {})

const getJumps = () => knex('mapSolarSystemJumps')
  .select(['fromSolarSystemID', 'toSolarSystemID'])
  .map((jump) => {
    return {
      from: jump.fromSolarSystemID,
      to: jump.toSolarSystemID
    }
  })

const getSolarSystems = (regions) => knex('mapSolarSystems')
  .select([
    'regionID',
    'security',
    'solarSystemID',
    'solarSystemName',
    'x', 'y', 'z'
  ])
  // Skip wormhole systems
  .where('solarSystemID', '<', 31000000)
  .map((solarSystem) => {
    return {
      name: solarSystem.solarSystemName,
      id: solarSystem.solarSystemID,
      security: solarSystem.security,
      region: regions[solarSystem.regionID],
      x: solarSystem.x,
      y: solarSystem.y,
      z: solarSystem.z
    }
  })

const generateJson = async (outputPath, indentation = 2) => {
  const regions = await getRegions()
  const solarSystems = await getSolarSystems(regions)
  const jumps = await getJumps()

  return await fs.writeFile(
    outputPath,
    JSON.stringify(
      {
        // regions,
        solarSystems,
        jumps
      },
      null,
      indentation
    ),
    { encoding: 'utf8' }
  )
}

generateJson('./dist/universe-pretty.json').then(() => console.log('Done!'))

@@ -185,24 +185,1008 @@
 t: true%0A%7D)%0A%0A
+const compareCoord = (coord, point) =%3E (%7B%0A  min: R.min(point%5Bcoord%5D),%0A  max: R.max(point%5Bcoord%5D)%0A%7D)%0A%0Aconst getBounds = R.reduce((acc, point) =%3E R.evolve(%7B%0A    x: compareCoord('x', point),%0A    y: compareCoord('y', point),%0A    z: compareCoord('z', point)%0A  %7D, acc),%0A  %7B%0A    x: %7B%0A      min: Number.POSITIVE_INFINITY,%0A      max: Number.NEGATIVE_INFINITY%0A    %7D,%0A    y: %7B%0A      min: Number.POSITIVE_INFINITY,%0A      max: Number.NEGATIVE_INFINITY%0A    %7D,%0A    z: %7B%0A      min: Number.POSITIVE_INFINITY,%0A      max: Number.NEGATIVE_INFINITY%0A    %7D%0A  %7D)%0A%0Aconst normalizeCoord = (coord, bounds) =%3E (value) =%3E ((value - bounds%5Bcoord%5D.min) / bounds%5Bcoord%5D.range)%0A%0Aconst normalizePoints = (bounds, points) =%3E %7B%0A  bounds.x.range = bounds.x.max - bounds.x.min%0A  bounds.y.range = bounds.y.max - bounds.y.min%0A  bounds.z.range = bounds.z.max - bounds.z.min%0A%0A  return R.map(R.evolve(%7B%0A    x: normalizeCoord('x', bounds),%0A    y: normalizeCoord('y', bounds),%0A    z: normalizeCoord('z', bounds)%0A  %7D), points)%0A%7D%0A%0A
 const getReg
@@ -1213,24 +1213,24 @@
 apRegions')%0A
-
   .select(%5B'
@@ -1808,16 +1808,43 @@
 000000)%0A
+  // Grab the info we want%0A
   .map((
@@ -2108,28 +2108,117 @@
 tem.z%0A    %7D%0A
-
   %7D)
+%0A  // Normalize coordinates%0A  .then(R.converge(normalizePoints, %5BgetBounds, R.identity%5D))
 %0A%0Aconst gene

normalize coordinates again