Skip to content

Commit

Permalink
feat: add planet texture images in webp format (#50)
Browse files Browse the repository at this point in the history
* feat: add `imageUrl` property to `Planet` model

* refractor: make environment variable more reusable

* feat: populate `imageUrl` property

* fix: resolve issues with invalid url
  • Loading branch information
fabio-nettis authored Apr 22, 2024
1 parent 02002dd commit 919397f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
HISTORY_API_URL: "https://helldivers-b.omnedia.com/api"
API_URL: "https://api.live.prod.thehelldiversgame.com/api"
# stratagem setup
STRATAGEM_IMAGE_URL: "https://vxspqnuarwhjjbxzgauv.supabase.co/storage/v1/object/public/stratagems"
STORAGE_URL: "https://vxspqnuarwhjjbxzgauv.supabase.co/storage/v1/object/public"

on:
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV RATE_LIMIT="200"
ENV DATABASE_URL="file:./database/data.db"
ENV HISTORY_API_URL="https://helldivers-b.omnedia.com/api"
ENV API_URL="https://api.live.prod.thehelldiversgame.com/api"
ENV STRATAGEM_IMAGE_URL="https://vxspqnuarwhjjbxzgauv.supabase.co/storage/v1/object/public/stratagems"
ENV STORAGE_URL="https://vxspqnuarwhjjbxzgauv.supabase.co/storage/v1/object/public"

# install dependencies into temp directory
# this will cache them and speed up future builds
Expand Down
45 changes: 45 additions & 0 deletions prisma/migrations/20240422103412_textures/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Warnings:
- Added the required column `imageUrl` to the `Planet` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Planet" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"index" BIGINT NOT NULL,
"name" TEXT NOT NULL,
"ownerId" INTEGER NOT NULL,
"sectorId" INTEGER NOT NULL,
"health" INTEGER NOT NULL,
"maxHealth" INTEGER NOT NULL,
"imageUrl" TEXT NOT NULL,
"players" INTEGER NOT NULL,
"disabled" BOOLEAN NOT NULL,
"regeneration" INTEGER NOT NULL,
"liberation" REAL NOT NULL,
"liberationRate" REAL NOT NULL,
"liberationState" TEXT NOT NULL,
"initialOwnerId" INTEGER NOT NULL,
"positionX" REAL NOT NULL,
"positionY" REAL NOT NULL,
"globalEventId" INTEGER,
"biomeId" INTEGER NOT NULL,
"statisticId" INTEGER,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Planet_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Planet_sectorId_fkey" FOREIGN KEY ("sectorId") REFERENCES "Sector" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Planet_initialOwnerId_fkey" FOREIGN KEY ("initialOwnerId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Planet_globalEventId_fkey" FOREIGN KEY ("globalEventId") REFERENCES "GlobalEvent" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Planet_biomeId_fkey" FOREIGN KEY ("biomeId") REFERENCES "Biome" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Planet_statisticId_fkey" FOREIGN KEY ("statisticId") REFERENCES "Stats" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Planet" ("biomeId", "createdAt", "disabled", "globalEventId", "health", "id", "index", "initialOwnerId", "liberation", "liberationRate", "liberationState", "maxHealth", "name", "ownerId", "players", "positionX", "positionY", "regeneration", "sectorId", "statisticId", "updatedAt") SELECT "biomeId", "createdAt", "disabled", "globalEventId", "health", "id", "index", "initialOwnerId", "liberation", "liberationRate", "liberationState", "maxHealth", "name", "ownerId", "players", "positionX", "positionY", "regeneration", "sectorId", "statisticId", "updatedAt" FROM "Planet";
DROP TABLE "Planet";
ALTER TABLE "new_Planet" RENAME TO "Planet";
CREATE UNIQUE INDEX "Planet_index_key" ON "Planet"("index");
CREATE UNIQUE INDEX "Planet_statisticId_key" ON "Planet"("statisticId");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ model Planet {
sectorId Int
health Int
maxHealth Int
imageUrl String
players Int
disabled Boolean
regeneration Int
Expand Down
10 changes: 8 additions & 2 deletions src/controllers/stratagems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const getStratagemById = await witCache(async (ctx: Context) => {
}

// slightly transform the data
const imageBaseUrl = process.env.STRATAGEM_IMAGE_URL || "";
const imageBaseUrl = process.env.STORAGE_URL
? `${process.env.STORAGE_URL}/stratagems`
: "";

stratagem.imageUrl = `${imageBaseUrl}${stratagem.imageUrl}`;
(stratagem as any).keys = stratagem.keys.split(",");

Expand All @@ -57,7 +60,10 @@ export const getAllStratagems = await witCache(async (ctx: Context) => {
return ctx.json({
data: stratagems.map(stratagem => {
// slightly transform the data
const imageBaseUrl = process.env.STRATAGEM_IMAGE_URL || "";
const imageBaseUrl = process.env.STORAGE_URL
? `${process.env.STORAGE_URL}/stratagems`
: "";

stratagem.imageUrl = `${imageBaseUrl}${stratagem.imageUrl}`;
(stratagem as any).keys = stratagem.keys.split(",");
return stratagem;
Expand Down
9 changes: 6 additions & 3 deletions src/static/json/planets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"0": {
"name": "Super Earth",
"biome": "unknown",
"effects": ["none"]
"effects": ["none"],
"imageName": "superearth"
},
"1": {
"name": "Klen Dahth II",
Expand Down Expand Up @@ -1197,7 +1198,8 @@
"239": {
"name": "Tien Kwan",
"biome": "icemoss_special",
"effects": ["extreme_cold", "meteor_storms"]
"effects": ["extreme_cold", "meteor_storms"],
"imageName": "tienkwan"
},
"240": {
"name": "Troost",
Expand Down Expand Up @@ -1302,6 +1304,7 @@
"260": {
"name": "Cyberstan",
"biome": "icemoss_special",
"effects": ["tremors"]
"effects": ["tremors"],
"imageName": "cyberstan"
}
}
17 changes: 15 additions & 2 deletions src/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface PlanetEntry {
name: string;
index: number;
biome: string;
imageName: string;
effects: string[];
}

Expand All @@ -47,7 +48,7 @@ export interface EffectEntry extends BiomeEntry {}
export async function prepareForSourceData() {
const [factions, planets, sectors, biomes, effects]: [
NameEntry[],
Array<NameEntry & { biome: string; effects: string[] }>,
Array<NameEntry & { imageUrl: string; biome: string; effects: string[] }>,
SectorEntry[],
Array<Omit<NameEntry, "index"> & { index: string; description: string }>,
Array<Omit<NameEntry, "index"> & { index: string; description: string }>,
Expand Down Expand Up @@ -106,7 +107,18 @@ export async function prepareForSourceData() {

// populate planets
for (const key in planetsJSON) {
planets.push({ ...planetsJSON[key], index: parseInt(key) });
const entry = planetsJSON[key];
const folder = entry.imageName ? "unique" : "biome";
const file = `${entry.imageName ?? entry.biome}.webp`;
const baseImageUrl = process.env.STORAGE_URL
? `${process.env.STORAGE_URL}`
: "";

planets.push({
...planetsJSON[key],
index: parseInt(key),
imageUrl: `${baseImageUrl}/planets/${folder}/${file}`,
});
}

// populate sectors
Expand Down Expand Up @@ -412,6 +424,7 @@ export async function transformAndStoreSourceData() {
disabled: info.disabled,
players: status.players,
maxHealth: info.maxHealth,
imageUrl: planet.imageUrl,
positionX: info.position.x,
positionY: info.position.y,
regeneration: status.regenPerSecond,
Expand Down

0 comments on commit 919397f

Please sign in to comment.