Skip to content

Commit

Permalink
stars can be distributed up to 896 stars, for some reason, the next i…
Browse files Browse the repository at this point in the history
…teration to 1536 stars breaks
  • Loading branch information
Mogge committed Sep 18, 2024
1 parent 82377bf commit b393de5
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 82 deletions.
2 changes: 1 addition & 1 deletion backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const seedUsers = async () => {
name: 'Räuber Hotzenplotz',
referenceId: faker.string.alphanumeric({ length: 8, casing: 'upper', exclude: 'O' }),
},
...Array.from(new Array(100), () => ({
...Array.from(new Array(891), () => ({
name: faker.person.fullName(),
username: faker.internet.userName(),
referenceId: faker.string.alphanumeric({ length: 8, casing: 'upper', exclude: 'O' }),
Expand Down
125 changes: 120 additions & 5 deletions backend/src/graphql/resolvers/starmap/starmap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,129 @@ describe('distributeStarsToSectorsRecursive', () => {
expect(stars[0].sectorIdx).toBe(0)
})

// ToDo: fix
it('has 13th star in sector 1', () => {
expect(stars[12].sectorIdx).not.toBe(0)
it('has 12th star in sector 11', () => {
expect(stars[11].sectorIdx).toBe(11)
})

it('has 13th star in sector 0', () => {
expect(stars[12].sectorIdx).toBe(0)
})

// ToDo: fix
it('has 14th star in sector 1', () => {
expect(stars[13].sectorIdx).not.toBe(1)
expect(stars[13].sectorIdx).toBe(1)
})
})

describe('distribute 16 stars', () => {
const stars = distributeStarsToSectorsRecursive(16)

it('has 16 stars', () => {
expect(stars).toHaveLength(16)
})

it('has last star in sector 3', () => {
expect(stars[15].sectorIdx).toBe(3)
})
})

describe('distribute 17 stars', () => {
const stars = distributeStarsToSectorsRecursive(17)

it('has 17 stars', () => {
expect(stars).toHaveLength(17)
})

it('has last star in sector 12', () => {
expect(stars[16].sectorIdx).toBe(12)
})
})

describe('distribute 48 stars', () => {
const stars = distributeStarsToSectorsRecursive(48)

it('has 48 stars', () => {
expect(stars).toHaveLength(48)
})

it('has star at index 39 in sector 11', () => {
expect(stars[39].sectorIdx).toBe(11)
})

it('has star at index 40 in sector 0', () => {
expect(stars[40].sectorIdx).toBe(0)
})

it('has the last 4 stars in sector 0, 1, 2, 3', () => {
expect(stars[44].sectorIdx).toBe(0)
expect(stars[45].sectorIdx).toBe(1)
expect(stars[46].sectorIdx).toBe(2)
expect(stars[47].sectorIdx).toBe(3)
})
})

describe('distribute 116 stars', () => {
const stars = distributeStarsToSectorsRecursive(116)

it('has 116 stars', () => {
expect(stars).toHaveLength(116)
})

it('has star at index 48 in sector 28', () => {
expect(stars[48].sectorIdx).toBe(28)
})

it('has star at index 71 in sector 51', () => {
expect(stars[71].sectorIdx).toBe(51)
})

it('has star at index 72 in sector 12', () => {
expect(stars[72].sectorIdx).toBe(12)
})

it('has star at index 88 in sector 4', () => {
expect(stars[88].sectorIdx).toBe(4)
})

it('has star at index 96 in sector 4', () => {
expect(stars[96].sectorIdx).toBe(4)
})

it('has the last 4 stars in sector 0, 1, 2, 3', () => {
expect(stars[112].sectorIdx).toBe(0)
expect(stars[113].sectorIdx).toBe(1)
expect(stars[114].sectorIdx).toBe(2)
expect(stars[115].sectorIdx).toBe(3)
})
})

describe('distribute 256 stars', () => {
const stars = distributeStarsToSectorsRecursive(256)

it('has 256 stars', () => {
expect(stars).toHaveLength(256)
})

it('has the last 4 stars in sector 0, 1, 2, 3', () => {
expect(stars[252].sectorIdx).toBe(0)
expect(stars[253].sectorIdx).toBe(1)
expect(stars[254].sectorIdx).toBe(2)
expect(stars[255].sectorIdx).toBe(3)
})
})

describe('distribute 496 stars', () => {
const stars = distributeStarsToSectorsRecursive(496)

it('has 496 stars', () => {
expect(stars).toHaveLength(496)
})
})

describe('distribute 896 stars', () => {
const stars = distributeStarsToSectorsRecursive(896)

it('has 896 stars', () => {
expect(stars).toHaveLength(896)
})
})
})
142 changes: 66 additions & 76 deletions backend/src/graphql/resolvers/starmap/starmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const fibonacci = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
const sectors: Sector[] = []
const starDistribution: StarDistribution[] = []
const sectorsPerIteration: number[] = []
const starInSector: number[] = []

/*
Expand Down Expand Up @@ -277,47 +278,75 @@ export const getStarDistribution = (): StarDistribution[] => {
}

const getNextSectorIdx = (starsPlaced: number): number => {
// const sectorIterationLength = 4 + MAX_VERTICAL_SECTORS
if (starInSector[starsPlaced] || starInSector[starsPlaced] === 0) {
return starInSector[starsPlaced]
}

const pushAndReturn = (n: number): number => {
starInSector.push(n)
return n
}

if (starsPlaced === 0) {
return pushAndReturn(0)
}

const starDistribution = getStarDistribution()
const sectors = getSectors()
const iteration = starDistribution.findIndex((sD) => sD.starsPlacedInIteration > starsPlaced)

const difference = starDistribution[iteration].starsPlacedInIteration - starsPlaced
let iteration: number = 0

// const length = Math.min(sectorIterationLength, iteration + 1)
/*
console.log({
starsPlaced,
iteration,
difference,
})
*/
if (
(iteration = starDistribution.findIndex((sD) => sD.starsPlacedInIteration === starsPlaced)) > -1
) {
return pushAndReturn(
sectors.findIndex(
(s: Sector) =>
s.iteration === starDistribution[iteration + 1].starsPlacedInSectorIteration.length,
),
)
}

iteration = starDistribution.findIndex((sD) => sD.starsPlacedInIteration > starsPlaced)
const starsAddedInPreviousIterations =
iteration === 0 ? 0 : starDistribution[iteration - 1].starsPlacedInIteration

if (difference === 0) {
return sectors.findLastIndex((s: Sector) => s.iteration === iteration + 1)
const starsToPlace = starsPlaced - starsAddedInPreviousIterations

let sectorIterationIdx = 0
let starsAddedInSectorIterations = 0
const { starsPlacedInSectorIteration } = starDistribution[iteration]
while (starsToPlace > starsAddedInSectorIterations) {
starsAddedInSectorIterations += starsPlacedInSectorIteration[sectorIterationIdx]
sectorIterationIdx++
}

let sectorIdx = 0
let starsAdded = 0
while (difference > starsAdded) {
starsAdded += starDistribution[iteration].starsPlacedInSectorIteration[sectorIdx]
sectorIdx++
/*
console.log({
sectorIdx,
starsAdded,
})
*/
if (starsToPlace === starsAddedInSectorIterations) {
return pushAndReturn(
sectors.findIndex(
(s: Sector) => s.iteration === starsPlacedInSectorIteration.length - sectorIterationIdx,
),
)
}

/*
console.log('result', (
sectors.findIndex((s) => s.iteration === sectorIdx) +
starsAdded - difference
))
*/
const f = fibonacci[sectorIterationIdx - 1]

if (f > 1) {
const starsPlacedInThisSectorIteration = starsPlacedInSectorIteration
.slice(0, sectorIterationIdx - 1)
.reduce((acc, curr) => acc + curr, 0)
const starsToPlcaeInThisSectorIteration = starsToPlace - starsPlacedInThisSectorIteration

const sectorsPerIteration =
getSectorsPerIteration()[starsPlacedInSectorIteration.length - sectorIterationIdx]

return sectors.findIndex((s) => s.iteration === sectorIdx) + starsAdded - difference
if (starsToPlcaeInThisSectorIteration % sectorsPerIteration === 0) {
return pushAndReturn(starInSector[starsPlaced - 1] - sectorsPerIteration + 1)
}
return pushAndReturn(starInSector[starsPlaced - 1] + 1)
}

return pushAndReturn(starInSector[starsPlaced - 1] + 1)
}

export const distributeStarsToSectorsRecursive = (
Expand All @@ -343,8 +372,12 @@ const addRandomStarToSector = (sectorIdx: number, stars: Star[]): Star => {
altitudes.push(s.coordinates.altitude)
})

const azimuthMaxIdx = getMaxDistance(azimuths)
const altitudeMaxIdx = getMaxDistance(altitudes)
const sortFn = (a: number, b: number): number => {
return a - b
}

const azimuthMaxIdx = getMaxDistance(azimuths.sort(sortFn))
const altitudeMaxIdx = getMaxDistance(altitudes.sort(sortFn))

const azimuth = randomBetween(
azimuths[azimuthMaxIdx] + DELTA_MIN,
Expand All @@ -363,46 +396,3 @@ const addRandomStarToSector = (sectorIdx: number, stars: Star[]): Star => {
},
}
}

/*
type StarsInSector = {
sectorIdx: number
coordinates: Coordinates
}
export const randomStarsinSector = (sectorIdx: number, count: number): StarsInSector => {
const starsInSector: StarsInSector = {
sectorIdx,
stars: [],
}
const azimuths: number[] = [
sectors[sectorIdx].bottomLeft.azimuth,
sectors[sectorIdx].topRight.azimuth,
]
const altitudes: number[] = [
sectors[sectorIdx].bottomLeft.altitude,
sectors[sectorIdx].topRight.altitude,
]
for (let n = 0; n < count; n++) {
const azimuthMaxIdx = getMaxDistance(azimuths)
const altitudeMaxIdx = getMaxDistance(altitudes)
const azimuth = randomBetween(
azimuths[azimuthMaxIdx] + DELTA_MIN,
azimuths[azimuthMaxIdx + 1] - DELTA_MIN,
)
const altitude = randomBetween(
altitudes[altitudeMaxIdx] + DELTA_MIN,
altitudes[altitudeMaxIdx + 1] - DELTA_MIN,
)
starsInSector.stars.push({
azimuth,
altitude,
})
azimuths.splice(azimuthMaxIdx, 0, azimuth)
altitudes.splice(altitudeMaxIdx, 0, altitude)
}
return starsInSector
}
*/

0 comments on commit b393de5

Please sign in to comment.