Skip to content

Commit

Permalink
feat: world manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
aleortega committed Apr 3, 2024
1 parent 410ba8d commit e716690
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/entities/CheckScenes/RoadCoordinates.json

Large diffs are not rendered by default.

54 changes: 53 additions & 1 deletion src/entities/CheckScenes/task/taskRunnerSqs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { randomUUID } from "crypto"

import AWS from "aws-sdk"
import env from "decentraland-gatsby/dist/utils/env"
import fetch from "node-fetch"
import { retry } from "radash"

import CategoryModel from "../../Category/model"
import { DecentralandCategories } from "../../Category/types"
import PlaceModel from "../../Place/model"
Expand All @@ -14,7 +19,7 @@ import {
} from "../../Slack/utils"
import CheckScenesModel from "../model"
import { CheckSceneLogsTypes } from "../types"
import { getWorldAbout } from "../utils"
import { calculateWorldManifestPositions, getWorldAbout } from "../utils"
import { DeploymentToSqs } from "./consumer"
import {
ProcessEntitySceneResult,
Expand Down Expand Up @@ -43,6 +48,50 @@ const placesAttributes: Array<keyof PlaceAttributes> = [
"textsearch",
]

const ACCESS_KEY = env("AWS_ACCESS_KEY")
const ACCESS_SECRET = env("AWS_ACCESS_SECRET")
const BUCKET_HOSTNAME = env("BUCKET_HOSTNAME")
const BUCKET_NAME = env("AWS_BUCKET_NAME", "")

async function updateWorldManifest() {
const s3 = new AWS.S3({
accessKeyId: ACCESS_KEY,
secretAccessKey: ACCESS_SECRET,
})

const signedUrl = await retry({ times: 10, delay: 100 }, async () => {
const responseUrl = s3.getSignedUrl("putObject", {
Bucket: BUCKET_NAME,
Key: `WorldManifest.json`,
Expires: 60 * 1000,
ContentType: "application/json",
ACL: "public-read",
CacheControl: "no-store, no-cache, must-revalidate, proxy-revalidate",
})

const url = new URL(responseUrl)
if (url.searchParams.size === 0) {
throw new Error("Invalid AWS response")
}

if (BUCKET_HOSTNAME) {
url.hostname = BUCKET_HOSTNAME
}

return url.toString()
})

const worldManifestPositions = await calculateWorldManifestPositions()

return await fetch(signedUrl, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(worldManifestPositions),
})
}

export async function taskRunnerSqs(job: DeploymentToSqs) {
const contentEntityScene = await processEntityId(job)

Expand Down Expand Up @@ -228,6 +277,9 @@ export async function taskRunnerSqs(job: DeploymentToSqs) {

await CheckScenesModel.createMany(placesToDisable)
}

// do not await so it is done on background
updateWorldManifest()
}

async function getValidCategories(creatorTags: string[]) {
Expand Down
23 changes: 23 additions & 0 deletions src/entities/CheckScenes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ContentServer from "decentraland-gatsby/dist/utils/api/ContentServer"

import areSamePositions from "../../utils/array/areSamePositions"
import { PlaceAttributes } from "../Place/types"
import PlacePositionModel from "../PlacePosition/model"
import roadCoordinates from "./RoadCoordinates.json"
import { DeploymentTrackAttributes, WorldAbout } from "./types"

/** @deprecated */
Expand Down Expand Up @@ -88,3 +90,24 @@ export async function getWorldAbout(
const worldContentServer = await ContentServer.getInstanceFrom(url)
return worldContentServer.fetch(`/world/${worldName}/about`)
}

export async function calculateWorldManifestPositions() {
const occupiedPositions = await PlacePositionModel.find({})
const emptyPositions = []

for (let x = -150; x <= 150; x++) {
for (let y = -150; y <= 150; y++) {
const position = [x.toString(), y.toString()]
if (
!occupiedPositions.some((place) =>
areSamePositions(place.positions, position)
) &&
!areSamePositions(roadCoordinates, position)
) {
emptyPositions.push(position)
}
}
}

return { occupiedPositions, emptyPositions, roadPositions: roadCoordinates }
}

0 comments on commit e716690

Please sign in to comment.