Skip to content

Commit

Permalink
Replaced removed google CDN handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixRilling committed Nov 11, 2022
1 parent f4584bb commit bd434e6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 78 deletions.
69 changes: 20 additions & 49 deletions src/ygoprodeck/api/ResourceService.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,54 @@
import type { BanlistInfo, Card, EnvironmentConfig } from "@/core/lib";
import { Environment } from "@/core/lib";

export class ResourceService {
static readonly #YGOPRODECK_IMAGE_BASE_URL =
"https://images.ygoprodeck.com/images";
static readonly #CDN_BASE_URL =
"https://storage.googleapis.com/ygoprodeck.com";
static readonly #YGOPRODECK_ICON_BASE_URL =
"https://images.ygoprodeck.com/images/cards/icons";

readonly #environmentConfig: EnvironmentConfig;

constructor(environmentConfig: EnvironmentConfig) {
this.#environmentConfig = environmentConfig;
}

/**
* Gets the version of the card image URL that should be used.
* This may include additional CDN data.
*
* @param initialCardImageUrl Card image URL returned from the ygoprodeck API.
* @return The URL that should be used.
*/
getEffectiveCardImageUrl(initialCardImageUrl: string): string {
if (
this.#environmentConfig.getEnvironment() == Environment.YGOPRODECK
) {
return initialCardImageUrl.replace(
ResourceService.#CDN_BASE_URL + "/pics_small",
ResourceService.#YGOPRODECK_IMAGE_BASE_URL + "/cards_small"
);
}
return initialCardImageUrl;
}

getPlaceholderCardImageUrl(): string {
return this.getEffectiveCardImageUrl(
`${ResourceService.#CDN_BASE_URL}/pics_small/4035199.jpg`
);
return "https://images.ygoprodeck.com/images/cards/4035199.jpg";
}

getTypeImageUrl(card: Card): string {
return `${this.#getAssetBaseUrl()}/${encodeURIComponent(
card.type.name
)}.jpg`;
return `${
ResourceService.#YGOPRODECK_ICON_BASE_URL
}/${encodeURIComponent(card.type.name)}.jpg`;
}

getSubTypeImageUrl(card: Card): string {
return `${this.#getAssetBaseUrl()}/race/${encodeURIComponent(
card.subType
)}.png`;
return `${
ResourceService.#YGOPRODECK_ICON_BASE_URL
}/race/${encodeURIComponent(card.subType)}.png`;
}

getAttributeImageUrl(card: Card): string {
return `${this.#getAssetBaseUrl()}/attributes/${encodeURIComponent(
card.attribute!
)}.jpg`;
return `${
ResourceService.#YGOPRODECK_ICON_BASE_URL
}/attributes/${encodeURIComponent(card.attribute!)}.jpg`;
}

getAtkImageUrl(): string {
return `${this.#getAssetBaseUrl()}/misc/atk.png`;
return `${ResourceService.#YGOPRODECK_ICON_BASE_URL}/misc/atk.png`;
}

getLevelImageUrl(): string {
return `${this.#getAssetBaseUrl()}/misc/level.png`;
return `${ResourceService.#YGOPRODECK_ICON_BASE_URL}/misc/level.png`;
}

getLinkMarkerImageUrl(): string {
return `${this.#getAssetBaseUrl()}/linkarrows/Right.png`;
return `${
ResourceService.#YGOPRODECK_ICON_BASE_URL
}/linkarrows/Right.png`;
}

getBanStateImageUrl(card: Card, format: keyof BanlistInfo): string {
return `${this.#getAssetBaseUrl()}/${encodeURIComponent(
card.banlist[format].name
)}.png`;
}

#getAssetBaseUrl(): string {
return this.#environmentConfig.getEnvironment() ==
Environment.YGOPRODECK
? `${ResourceService.#YGOPRODECK_IMAGE_BASE_URL}/cards/icons`
: `${ResourceService.#CDN_BASE_URL}/assets`;
return `${
ResourceService.#YGOPRODECK_ICON_BASE_URL
}/${encodeURIComponent(card.banlist[format].name)}.png`;
}
}
14 changes: 3 additions & 11 deletions src/ygoprodeck/api/YgoprodeckApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { RawArchetype } from "./mapping/mapArchetype";
import { mapArchetype } from "./mapping/mapArchetype";
import type { Card, CardSet, CardValues, EnvironmentConfig } from "@/core/lib";
import { Environment } from "@/core/lib";
import type { ResourceService } from "./ResourceService";
import type { UnlinkedCard } from "@/ygoprodeck/api/UnlinkedCard";

interface CardInfoOptions {
Expand Down Expand Up @@ -50,14 +49,9 @@ export class YgoprodeckApiService {
static readonly #HTTP_STATUS_NO_MATCHES = 400;

readonly #environmentConfig: EnvironmentConfig;
readonly #resourceService: ResourceService;

constructor(
environmentConfig: EnvironmentConfig,
resourceService: ResourceService
) {
constructor(environmentConfig: EnvironmentConfig) {
this.#environmentConfig = environmentConfig;
this.#resourceService = resourceService;
}

async getSingleCard(
Expand All @@ -81,7 +75,7 @@ export class YgoprodeckApiService {
return null;
}
// If a match is found, we take the very first item (best match).
return mapCard(data.data[0], this.#resourceService);
return mapCard(data.data[0]);
}

async getCards(options: CardInfoOptions): Promise<UnlinkedCard[]> {
Expand All @@ -107,9 +101,7 @@ export class YgoprodeckApiService {
assertStatusOk(res);
return res.json() as Promise<PaginatedResponse<RawCard[]>>;
});
}).then((data) =>
data.map((rawCard) => mapCard(rawCard, this.#resourceService))
);
}).then((data) => data.map(mapCard));
}

#putCardInfoParams(
Expand Down
19 changes: 5 additions & 14 deletions src/ygoprodeck/api/mapping/mapCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
Vendor,
} from "@/core/lib";
import { DefaultBanState, DefaultVendor, Format } from "@/core/lib";
import type { ResourceService } from "../ResourceService";
import type {
CardSetAppearance,
UnlinkedCard,
Expand Down Expand Up @@ -126,19 +125,14 @@ const mapCardSets = (rawCard: RawCard): CardSetAppearance[] => {
});
};

const mapImage = (
rawCard: RawCard,
resourceService: ResourceService
): CardImage | null => {
const mapImage = (rawCard: RawCard): CardImage | null => {
if (rawCard.card_images == null) {
return null;
}
const image = rawCard.card_images[0];
return {
url: resourceService.getEffectiveCardImageUrl(image.image_url),
urlSmall: resourceService.getEffectiveCardImageUrl(
image.image_url_small
),
url: image.image_url,
urlSmall: image.image_url_small,
};
};

Expand Down Expand Up @@ -174,10 +168,7 @@ const mapRelease = (miscInfo: RawMiscInfo | null): ReleaseInfo => {
};
};

export const mapCard = (
rawCard: RawCard,
resourceService: ResourceService
): UnlinkedCard => {
export const mapCard = (rawCard: RawCard): UnlinkedCard => {
const miscInfo: RawMiscInfo | null =
rawCard.misc_info != null ? rawCard.misc_info[0] : null;
return {
Expand All @@ -196,7 +187,7 @@ export const mapCard = (
linkMarkers: rawCard.linkmarkers ?? null,

sets: mapCardSets(rawCard),
image: mapImage(rawCard, resourceService),
image: mapImage(rawCard),
prices: mapPrices(rawCard),

betaName: miscInfo?.beta_name ?? null,
Expand Down
5 changes: 1 addition & 4 deletions src/ygoprodeck/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const createYgoprodeckModule = (
environmentConfig: EnvironmentConfig
): YgoprodeckModule => {
const resourceService = new ResourceService(environmentConfig);
const ygoprodeckApiService = new YgoprodeckApiService(
environmentConfig,
resourceService
);
const ygoprodeckApiService = new YgoprodeckApiService(environmentConfig);
const ygoprodeckService = new YgoprodeckService(
ygoprodeckApiService,
environmentConfig
Expand Down

0 comments on commit bd434e6

Please sign in to comment.