diff --git a/build-avatars.js b/build-avatars.js index ea6b369e..38d781f6 100755 --- a/build-avatars.js +++ b/build-avatars.js @@ -109,6 +109,73 @@ function generateLegacyAvatars () { } } +// // 2024 avatars generation +// async function generateAvatars (part) { +// console.log('Starting generating ' + part) +// const parts = avatarPartsDef[part] +// if (!parts) { +// throw new Error('Missing part\'s conf: ' + part) +// } + +// const inputDir = path.join('./assets/images/avatars/', part) +// const outputDir = path.join('./dist/server/avatars/', part) +// fs.mkdirSync(outputDir, { recursive: true }) + +// function computeFilename (part, count) { +// let a = (1 + (count % parts[part])).toString() +// if (a.length < 2) { a = '0' + a} + +// return path.join( +// inputDir, +// part + '_' + a + '.png' +// ) +// } +// // We can't generate all combinations! It would make 400 000 000 files! +// // So we arbitrary pick some combinations, using some modulus +// const nb = 200 // number of avatars to generate + +// for (let i = 0; i < nb; i++) { +// const ouputFile = path.join( +// outputDir, +// i.toString() + '.png' +// ) +// if (await fs.existsSync(ouputFile)) { +// console.log(`Skipping ${ouputFile}, file already exists`) +// continue +// } + +// const partsToCombine = Object.keys(parts) +// const firstPart = partsToCombine.shift() +// const firstFile = computeFilename(firstPart, i) + +// // We just have to combinate different parts into one file, then output at the wanted size. +// const composites = [] +// let j = 0 +// for (const part of partsToCombine) { +// j++ // introduce an offset so we don't get all empty parts at the same time +// composites.push({ +// input: computeFilename(part, i + (j * 7)) +// }) +// } + +// const buff = await sharp(firstFile) +// .composite(composites) +// .toBuffer() + +// await sharp(buff) +// .resize(60, 60) +// .png({ +// compressionLevel: 9, +// palette: true +// }) +// .toFile(ouputFile) +// } +// } + +const fs = require('fs') +const path = require('path') +const sharp = require('sharp') + // 2024 avatars generation async function generateAvatars (part) { console.log('Starting generating ' + part) @@ -130,48 +197,96 @@ async function generateAvatars (part) { part + '_' + a + '.png' ) } - // We can't generate all combinations! It would make 400 000 000 files! - // So we arbitrary pick some combinations, using some modulus + + // Function to generate "nigbot" files without composites + async function generateNigbotFiles(nb) { + for (let i = 0; i < nb; i++) { + const ouputFile = path.join( + outputDir, + 'nigbot_' + i.toString() + '.png' + ) + if (await fs.existsSync(ouputFile)) { + console.log(`Skipping ${ouputFile}, file already exists`) + continue + } + + const firstPart = 'nigbot' + const firstFile = computeFilename(firstPart, i) + + const buff = await sharp(firstFile) + .resize(60, 60) + .png({ + compressionLevel: 9, + palette: true + }) + .toBuffer() + + await sharp(buff) + .toFile(ouputFile) + } + } + + // Function to generate other files with composites + async function generateOtherFiles(nb) { + for (let i = 0; i < nb; i++) { + const ouputFile = path.join( + outputDir, + i.toString() + '.png' + ) + if (await fs.existsSync(ouputFile)) { + console.log(`Skipping ${ouputFile}, file already exists`) + continue + } + + const partsToCombine = Object.keys(parts) + const firstPart = partsToCombine.shift() + const firstFile = computeFilename(firstPart, i) + + if (firstFile.includes('nigbot')) { + console.log(`Skipping ${firstFile}, file name contains "nigbot"`) + continue + } + + const composites = [] + let j = 0 + for (const part of partsToCombine) { + j++ // introduce an offset so we don't get all empty parts at the same time + const filename = computeFilename(part, i + (j * 7)) + + if (filename.includes('nigbot')) { + console.log(`Skipping ${filename}, file name contains "nigbot"`) + continue + } + + composites.push({ + input: filename + }) + } + + const buff = await sharp(firstFile) + .composite(composites) + .toBuffer() + + await sharp(buff) + .resize(60, 60) + .png({ + compressionLevel: 9, + palette: true + }) + .toFile(ouputFile) + } + } + const nb = 200 // number of avatars to generate - for (let i = 0; i < nb; i++) { - const ouputFile = path.join( - outputDir, - i.toString() + '.png' - ) - if (await fs.existsSync(ouputFile)) { - console.log(`Skipping ${ouputFile}, file already exists`) - continue - } + // Generate "nigbot" files + await generateNigbotFiles(nb) - const partsToCombine = Object.keys(parts) - const firstPart = partsToCombine.shift() - const firstFile = computeFilename(firstPart, i) - - // We just have to combinate different parts into one file, then output at the wanted size. - const composites = [] - let j = 0 - for (const part of partsToCombine) { - j++ // introduce an offset so we don't get all empty parts at the same time - composites.push({ - input: computeFilename(part, i + (j * 7)) - }) - } - - const buff = await sharp(firstFile) - .composite(composites) - .toBuffer() - - await sharp(buff) - .resize(60, 60) - .png({ - compressionLevel: 9, - palette: true - }) - .toFile(ouputFile) - } + // Generate other files with composites + await generateOtherFiles(nb) } + async function generateBotsAvatars () { { // Moderation bot avatar: choosing some parts, and turning it so it is facing left.