leonitousconforti/tinyburg

View on GitHub
apps/trading-site/app/api/random-bitizen/route.ts

Summary

Maintainability
B
4 hrs
Test Coverage
const bitLayers = [
    "skin",
    "eyes",
    "hair",
    "facialhair",
    "glasses",
    "earrings",
    "shirt",
    "pants",
    "tie",
    "hat",
    "shoes",
    "costume",
] as const;

const bitColors = [
    "FFFFFF",
    "000000",
    "333333",
    "666666",
    "999999",
    "CCCCCC",
    "CCCC99",
    "9999CC",
    "666699",
    "660000",
    "663300",
    "996633",
    "003300",
    "003333",
    "003399",
    "000066",
    "330066",
    "660099",
    "990000",
    "993300",
    "CC9900",
    "006600",
    "336666",
    "0033FF",
    "000099",
    "660099",
    "990066",
    "CC0000",
    "CC3300",
    "FFCC00",
    "009900",
    "006666",
    "0066FF",
    "0000CC",
    "663399",
    "CC0099",
    "FF0000",
    "FF3300",
    "FFFF00",
    "00CC00",
    "009999",
    "0099FF",
    "0000FF",
    "9900CC",
    "FF0099",
    "CC3333",
    "FF6600",
    "FFFF33",
    "00FF00",
    "00CCCC",
    "00CCFF",
    "3366FF",
    "9933FF",
    "FF00FF",
    "FF6666",
    "FF6633",
    "FFFF66",
    "66FF66",
    "66CCCC",
    "00FFFF",
    "3399FF",
    "9966FF",
    "FF66FF",
    "FF9999",
    "FF9966",
    "FFFF99",
    "99FF99",
    "66FFCC",
    "99FFFF",
    "66CCFF",
    "9999FF",
    "FF99FF",
    "FFCCCC",
    "FFCC99",
    "FFFFCC",
    "CCFFCC",
    "99FFCC",
    "CCFFFF",
    "99CCFF",
    "CCCCFF",
    "FFCCFF",
    "b86f4c",
    "ae5b33",
    "861912",
    "e39f94",
    "efb296",
    "b66e56",
    "f4c2aa",
    "efcfad",
    "7b5139",
    "a57152",
    "a57745",
    "f2a65e",
    "cd6c03",
    "fff6df",
    "926e5e",
    "6d382e",
];
const randomBitColor = () => bitColors[Math.floor(Math.random() * bitColors.length)];

const skinColors = [
    "b86f4c",
    "ae5b33",
    "861912",
    "e39f94",
    "efb296",
    "b66e56",
    "f4c2aa",
    "efcfad",
    "7b5139",
    "a57152",
    "a57745",
    "f2a65e",
    "cd6c03",
    "fff6df",
    "926e5e",
    "6d382e",
];
const randomSkinColor = () => skinColors[Math.floor(Math.random() * skinColors.length)];

const bitImages = {
    skin: ["head"],
    eyes: ["eyes"],
    facialhair: [null, "hacc0", "hacc1", "hacc2"],
    glasses: [null, "glasses0", "glasses1", "glasses2"],
    hair: ["hair0", "hair1"],
    shirt: ["shirt"],
    pants: ["pants"],
    shoes: ["shoes"],
    hat: [
        null,
        "bhat0",
        "bhat1",
        "bhat2",
        "bhat3",
        "bhat4",
        "bhat5",
        "bhat6",
        "bhat7",
        "bhat8",
        "bhat9",
        "bhat10",
        "bhat11",
        "bhat12",
        "bhat13",
        "bhat14",
        "bhat15",
        "bhat16",
        "fhat0",
        "fhat1",
        "mhat0",
    ],
    tie: [null, "tie"],
    earrings: [null, "earrings"],
    costume: [
        null,
        "airline pilot",
        "aloha",
        "amigo",
        "angel",
        "apron",
        "archaeologist",
        "astronaut",
        "banana",
        "barber",
        "barista",
        "baseball uniform",
        "bear",
        "bee suit",
        "bellhop",
        "bikachu",
        "biker",
        "bitman",
        "black dress",
        "black ranger",
        "blacksmith",
        "blink",
        "blue ranger",
        "blue suit",
        "boxer",
        "bride",
        "brown dress",
        "bunny",
        "burglar",
        "business suit",
        "captain bit",
        "cat suit",
        "caveman",
        "cheerleader",
        "chef",
        "chicken",
        "chinese suit",
        "clown",
        "construction",
        "convict",
        "cossack",
        "cowboy",
        "creepy",
        "cyborg",
        "delivery",
        "detective",
        "devil",
        "dinosaur",
        "disco",
        "diver",
        "doctor",
        "elephant",
        "elf",
        "elvis",
        "engineer",
        "eyepatch",
        "fairy",
        "farmer",
        "fast food uniform",
        "fez",
        "fireman",
        "fishing",
        "football player",
        "french maid",
        "frog suit",
        "gangster",
        "gentleman",
        "ghost",
        "ghosthustler",
        "g-man",
        "gold dress",
        "golfer",
        "green ranger",
        "groom",
        "guard",
        "hazmat",
        "heart",
        "hippie",
        "horse",
        "hotdog",
        "hunter",
        "ice queen",
        "ironbit",
        "jet helmet",
        "karate outfit",
        "king",
        "knight",
        "lab coat",
        "lady",
        "leather jacket",
        "leprechaun",
        "liberty",
        "lumberjack",
        "mad hatter",
        "mad scientist",
        "magician",
        "maid",
        "mapple genius",
        "mime",
        "monkey",
        "monster suit",
        "native american",
        "nerd",
        "ninja",
        "nurse",
        "overalls",
        "panda hat",
        "panda suit",
        "paparazzi",
        "phantom",
        "pharaoh",
        "piggy",
        "pilgrim",
        "pilot headset",
        "pink ranger",
        "pirate",
        "plumber a",
        "plumber b",
        "princess",
        "pumpkin",
        "red baron",
        "red dress",
        "red hat",
        "red ranger",
        "red suit",
        "robot",
        "rockstar",
        "roman",
        "sailor",
        "sanitation",
        "santa",
        "security",
        "shark",
        "snapdog",
        "snowman",
        "soccer",
        "soldier",
        "star captain",
        "super hero",
        "superbit",
        "surgeon",
        "swimmer",
        "toga",
        "tourist",
        "travel agent",
        "trooper",
        "uncle sam",
        "usher",
        "vampire",
        "viking",
        "waiter",
        "white suit",
        "witch",
        "yellow ranger",
        "youngwizard",
        "zombie",
    ],
};

export const GET = async (): Promise<string> => {
    // Create a random bitizen
    const bitizen = bitLayers.map((layer) => ({
        img: bitImages[layer][Math.floor(Math.random() * bitImages[layer].length)],
        color: layer === "skin" ? randomSkinColor() : randomBitColor(),
        type: layer,
    }));

    // Url to fetch the random bitizen
    const fetchParams = bitizen
        .map((bitPart) => `${bitPart.type}=${bitPart.img}&${bitPart.type}Color=${bitPart.color}`)
        .join("&");

    const url = `http://bitbuilder.nimblebit.com/createImage.php?${fetchParams}&backgroundColor=${randomBitColor()}`;
    const res = await fetch(url);
    return res.url.replace("nimblebit.com/share", "s3.amazonaws.com").concat(".png");
};