LineUp-Bot / scrimRenderer.js
Brozy123's picture
Update scrimRenderer.js
eb0c744 verified
Raw History Blame Contribute Delete
2.61 kB
/**
* scrimRenderer.js - Scrimmage Mode Lineup Image Generator
* Displays two teams facing each other on a single pitch
*/
const { createCanvas, loadImage } = require('@napi-rs/canvas');
const { AttachmentBuilder } = require('discord.js');
const { renderLineupImage } = require('./lineupRenderer');
/**
* Render scrimmage lineup - two team images side by side
* Left: Team A (normal formation, attacking upward)
* Right: Team B (mirrored Y-axis - attacking downward, facing Team A)
*/
async function renderScrimmageImage(lineup, title, formation, client) {
const INDIVIDUAL_W = 1180;
const H = 620;
const W = INDIVIDUAL_W * 2 + 5; // Total width with gap
// Pre-fetch all user data
const allUserIds = new Set();
for (const players of lineup.values()) {
players.forEach(id => allUserIds.add(id));
}
const userDataMap = new Map();
if (client) {
await Promise.all(Array.from(allUserIds).map(async (uid) => {
try {
const user = await client.users.fetch(uid);
const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 128 });
const imgBuf = await downloadImage(avatarUrl);
const img = await loadImage(imgBuf);
userDataMap.set(uid, { user, img });
} catch (e) {
userDataMap.set(uid, { user: null, img: null });
}
});
// --- Draw Team A on left half (normal orientation) ---
const canvas = createCanvas(W, H);
const ctx = canvas.getContext('2d');
const imgA = await loadImage(teamABuffer);
// Draw Team A on left half
ctx.drawImage(imgA, 0, 0, INDIVIDUAL_W, INDIVIDUAL_H);
// --- Draw Team B on right half (mirrored Y) ---
ctx.save();
ctx.translate(HALF_W, 0);
ctx.scale(-1, 1);
ctx.drawImage(imgB, -HALF_W, 0, INDIVIDUAL_W, INDIVIDUAL_H);
ctx.restore();
// --- Draw "VS" in the middle ---
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 28px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.fillText('VS', INDIVIDUAL_W + GAP / 2, H / 2);
// --- Footer ---
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, H - 22, W, 22);
ctx.fillStyle = 'rgba(255,255,255,0.6)';
ctx.font = '10px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('Generated by FriendlyBot', W / 2, H - 11);
return canvas.toBuffer('image/png');
}
async function createScrimmageAttachment(lineup, title, formation, client) {
const buffer = await renderScrimmageImage(lineup, title, formation, client);
return new AttachmentBuilder(buffer, { name: 'scrimmage.png' });
}
module.exports = { renderScrimmageImage, createScrimmageAttachment };