Update the series view command to be in greyscale if the user has not claimed the card (#297)
All checks were successful
Deploy To Stage / build (push) Successful in 10s
Deploy To Stage / deploy (push) Successful in 17s

- Install `Jimp` package so we can manipulate images
- Update the `ImageHelper` class to accept the user id so we can check if a user has claimed the card
- Update the `/series view` command to pass the user id into the ImageHelper

#279

Reviewed-on: #297
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2024-07-12 17:35:12 +01:00 committed by Vylpes
parent 29bb22a819
commit b6f814f895
8 changed files with 575 additions and 15 deletions

View file

@ -2,11 +2,18 @@ import {createCanvas, loadImage} from "canvas";
import path from "path";
import AppLogger from "../client/appLogger";
import {existsSync} from "fs";
import Inventory from "../database/entities/app/Inventory";
import Jimp from "jimp";
interface CardInput {
id: string;
path: string;
}
export default class ImageHelper {
public static async GenerateCardImageGrid(paths: string[]): Promise<Buffer> {
public static async GenerateCardImageGrid(cards: CardInput[], userId?: string): Promise<Buffer> {
const gridWidth = 3;
const gridHeight = Math.ceil(paths.length / gridWidth);
const gridHeight = Math.ceil(cards.length / gridWidth);
const imageWidth = 526;
const imageHeight = 712;
@ -17,17 +24,29 @@ export default class ImageHelper {
const canvas = createCanvas(canvasWidth, canvasHeight);
const ctx = canvas.getContext("2d");
for (let i = 0; i < paths.length; i++) {
const filePath = path.join(process.env.DATA_DIR!, "cards", paths[i]);
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
const filePath = path.join(process.env.DATA_DIR!, "cards", card.path);
const exists = existsSync(filePath);
if (!exists) {
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${paths[i]}`);
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`);
continue;
}
const image = await loadImage(filePath);
const imageData = await Jimp.read(filePath);
if (userId != null) {
const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id);
if (!claimed || claimed.Quantity == 0) {
imageData.greyscale();
}
}
const image = await loadImage(await imageData.getBufferAsync("image/jpeg"));
const x = i % gridWidth;
const y = Math.floor(i / gridWidth);