Spaces:
Runtime error
Runtime error
File size: 14,188 Bytes
2344c18 3c22986 2344c18 3c22986 2344c18 3c22986 2344c18 3c22986 2344c18 3c22986 2344c18 7141569 2344c18 ad88c31 2344c18 ad88c31 039c42e e315404 2344c18 7141569 2344c18 e315404 2344c18 e315404 2344c18 e315404 2344c18 e315404 2344c18 e315404 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 ad88c31 2344c18 e8ca29e 2344c18 ad88c31 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | /**
* lineupRenderer.js - Football Pitch Lineup Image Generator
* Uses @napi-rs/canvas to render a professional football pitch
* with player Discord avatars and position labels.
*/
const { createCanvas, loadImage } = require('@napi-rs/canvas');
const { AttachmentBuilder } = require('discord.js');
const https = require('https');
const http = require('http');
// Position coordinates on a 900x600 pitch for each formation
// Coordinates are [x, y] as percentage of canvas width/height
const POSITION_COORDS = {
// Goalkeeper
'GK': [50, 90],
// Defenders
'CB': [50, 72], 'CB1': [38, 72], 'CB2': [62, 72], 'CB3': [30, 72], 'CB4': [70, 72],
'LCB': [35, 72], 'RCB': [65, 72],
'LB': [15, 65], 'RB': [85, 65],
'LWB': [10, 55], 'RWB': [90, 55],
// Midfielders
'CM': [50, 50], 'CM1': [35, 50], 'CM2': [65, 50], 'CM3': [20, 50], 'CM4': [80, 50],
'LCM': [30, 50], 'RCM': [70, 50],
'CDM': [50, 58], 'CDM1': [40, 58], 'CDM2': [60, 58],
'CAM': [50, 38], 'CAM1': [40, 38], 'CAM2': [60, 38],
'LM': [12, 50], 'RM': [88, 50],
// Forwards
'LW': [18, 30], 'RW': [82, 30],
'LF': [30, 30], 'RF': [70, 30],
'ST': [50, 20], 'ST1': [38, 20], 'ST2': [62, 20], 'ST3': [50, 15],
'CF': [50, 32],
};
/**
* Download an image from a URL and return as a Buffer
*/
function downloadImage(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
client.get(url, { timeout: 5000 }, (res) => {
// Follow redirects
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return downloadImage(res.headers.location).then(resolve).catch(reject);
}
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => resolve(Buffer.concat(chunks)));
res.on('error', reject);
}).on('error', reject);
});
}
/**
* Draw a rounded rectangle
*/
function roundRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
/**
* Draw the football pitch background
*/
function drawPitch(ctx, W, H) {
// Main grass gradient
const grassGrad = ctx.createLinearGradient(0, 0, 0, H);
grassGrad.addColorStop(0, '#1a7a3a');
grassGrad.addColorStop(0.5, '#1e8c42');
grassGrad.addColorStop(1, '#1a7a3a');
ctx.fillStyle = grassGrad;
ctx.fillRect(0, 0, W, H);
// Grass stripe pattern
const stripeCount = 12;
const stripeH = H / stripeCount;
for (let i = 0; i < stripeCount; i++) {
if (i % 2 === 0) {
ctx.fillStyle = 'rgba(255,255,255,0.04)';
ctx.fillRect(0, i * stripeH, W, stripeH);
}
}
// Pitch outline
const pad = 30;
ctx.strokeStyle = 'rgba(255,255,255,0.85)';
ctx.lineWidth = 2.5;
ctx.strokeRect(pad, pad, W - pad * 2, H - pad * 2);
// Center line
ctx.beginPath();
ctx.moveTo(pad, H / 2);
ctx.lineTo(W - pad, H / 2);
ctx.stroke();
// Center circle
ctx.beginPath();
ctx.arc(W / 2, H / 2, 60, 0, Math.PI * 2);
ctx.stroke();
// Center dot
ctx.fillStyle = 'rgba(255,255,255,0.85)';
ctx.beginPath();
ctx.arc(W / 2, H / 2, 4, 0, Math.PI * 2);
ctx.fill();
// Top penalty box (attacking end)
const boxW = 220;
const boxH = 80;
ctx.strokeRect((W - boxW) / 2, pad, boxW, boxH);
// Top small box
const sBoxW = 120;
const sBoxH = 35;
ctx.strokeRect((W - sBoxW) / 2, pad, sBoxW, sBoxH);
// Top penalty arc
ctx.beginPath();
ctx.arc(W / 2, pad + boxH, 35, 0, Math.PI);
ctx.stroke();
// Bottom penalty box (GK end)
ctx.strokeRect((W - boxW) / 2, H - pad - boxH, boxW, boxH);
// Bottom small box
ctx.strokeRect((W - sBoxW) / 2, H - pad - sBoxH, sBoxW, sBoxH);
// Bottom penalty arc
ctx.beginPath();
ctx.arc(W / 2, H - pad - boxH, 35, Math.PI, Math.PI * 2);
ctx.stroke();
// Corner arcs
const corners = [[pad, pad], [W - pad, pad], [pad, H - pad], [W - pad, H - pad]];
const cornerAngles = [[0, Math.PI / 2], [Math.PI / 2, Math.PI], [3 * Math.PI / 2, 2 * Math.PI], [Math.PI, 3 * Math.PI / 2]];
corners.forEach(([cx, cy], i) => {
ctx.beginPath();
ctx.arc(cx, cy, 12, cornerAngles[i][0], cornerAngles[i][1]);
ctx.stroke();
});
}
/**
* Render the lineup as a PNG image buffer
* @param {Map} lineup - Map of posKey -> [userId, ...]
* @param {string} title - Title to display on the image
* @param {Array} formation - Array of [posKey, posLabel]
* @param {object} client - Discord client for fetching user data
* @param {boolean} flipY - If true, flip Y coordinates (for opposing team view)
* @returns {Promise<Buffer>} PNG image buffer
*/
async function renderLineupImage(lineup, title, formation, client, flipY = false) {
const PITCH_W = 900;
const SIDEBAR_W = 280;
const W = PITCH_W + SIDEBAR_W;
const H = 620;
// 1. Pre-fetch all user data and avatars in parallel to speed up rendering
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 the content ---
const canvas = createCanvas(W, H);
const ctx = canvas.getContext('2d');
// 1. Draw the sidebar background
ctx.fillStyle = '#2B2D31'; // Discord dark gray
ctx.fillRect(0, 0, SIDEBAR_W, H);
// Draw sidebar header
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 20px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.fillText('SUBSTITUTES', SIDEBAR_W / 2, 30);
ctx.strokeStyle = '#5865F2';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(20, 45);
ctx.lineTo(SIDEBAR_W - 20, 45);
ctx.stroke();
// 2. Draw the pitch on the right
ctx.save();
ctx.translate(SIDEBAR_W, 0);
drawPitch(ctx, PITCH_W, H);
ctx.restore();
// 3. Draw title bar over the pitch
ctx.save();
ctx.translate(SIDEBAR_W, 0);
const titleGrad = ctx.createLinearGradient(0, 0, PITCH_W, 0);
titleGrad.addColorStop(0, 'rgba(0,0,0,0.7)');
titleGrad.addColorStop(0.5, 'rgba(0,0,0,0.85)');
titleGrad.addColorStop(1, 'rgba(0,0,0,0.7)');
ctx.fillStyle = titleGrad;
ctx.fillRect(0, 0, PITCH_W, 36);
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 16px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.fillText(`LINEUP — ${title.toUpperCase()}`, PITCH_W / 2, 24);
ctx.restore();
// Extract subs and draw them
const subs = [];
for (const [posKey, posLabel] of formation) {
const players = lineup.get(posKey) || [];
if (players.length > 1) {
for (let i = 1; i < players.length; i++) {
subs.push({ userId: players[i], posLabel });
}
}
}
// Draw subs list
let subY = 70;
for (const sub of subs) {
if (subY > H - 50) break;
const avatarSize = 32;
const avatarRadius = avatarSize / 2;
const x = 20;
const y = subY;
const data = userDataMap.get(sub.userId);
ctx.save();
if (data && data.img) {
ctx.beginPath();
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(data.img, x, y, avatarSize, avatarSize);
} else {
ctx.beginPath();
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
ctx.fillStyle = '#5865F2';
ctx.fill();
}
ctx.restore();
// Border ring
ctx.beginPath();
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius + 1, 0, Math.PI * 2);
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 1.5;
ctx.stroke();
// Username
let displayName = "Player";
if (data && data.user) {
displayName = data.user.globalName || data.user.username;
}
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 14px Arial, sans-serif';
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
const truncated = displayName.length > 13 ? displayName.slice(0, 12) + '…' : displayName;
ctx.fillText(truncated, x + avatarSize + 12, y + avatarRadius);
// Position pill
ctx.font = 'bold 11px Arial, sans-serif';
const posTextW = ctx.measureText(sub.posLabel).width + 10;
const pillX = SIDEBAR_W - 15 - posTextW;
roundRect(ctx, pillX, y + avatarRadius - 8, posTextW, 16, 4);
ctx.fillStyle = 'rgba(88,101,242,0.9)';
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.textAlign = 'center';
ctx.fillText(sub.posLabel, pillX + posTextW / 2, y + avatarRadius);
subY += 45;
}
if (subs.length === 0) {
ctx.fillStyle = 'rgba(255,255,255,0.5)';
ctx.font = 'italic 14px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.fillText('No substitutes yet', SIDEBAR_W / 2, subY + 20);
}
// --- Start Drawing Players ---
const avatarSize = 48;
const avatarRadius = avatarSize / 2;
for (const [posKey, posLabel] of formation) {
const coords = POSITION_COORDS[posKey];
if (!coords) continue;
const x = SIDEBAR_W + (coords[0] / 100) * PITCH_W;
const y = flipY ? ((100 - coords[1]) / 100) * H : (coords[1] / 100) * H;
const players = lineup.get(posKey) || [];
if (players.length > 0) {
ctx.save();
ctx.shadowColor = '#00ff88';
ctx.shadowBlur = 18;
ctx.beginPath();
ctx.arc(x, y, avatarRadius + 4, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(0,255,136,0.3)';
ctx.fill();
ctx.restore();
}
if (players.length > 0 && client) {
const userId = players[0];
const data = userDataMap.get(userId);
if (data && data.img) {
ctx.save();
ctx.beginPath();
ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
ctx.clip();
ctx.drawImage(data.img, x - avatarRadius, y - avatarRadius, avatarSize, avatarSize);
ctx.restore();
ctx.beginPath();
ctx.arc(x, y, avatarRadius + 2, 0, Math.PI * 2);
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2.5;
ctx.stroke();
} else {
ctx.beginPath();
ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
ctx.fillStyle = '#5865F2';
ctx.fill();
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 16px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('?', x, y);
}
if (data && data.user) {
const displayName = data.user.globalName || data.user.username;
const truncated = displayName.length > 10 ? displayName.slice(0, 9) + '...' : displayName;
const labelY = flipY ? y - avatarRadius - 14 : y + avatarRadius + 14;
ctx.font = 'bold 11px Arial, sans-serif';
ctx.textAlign = 'center';
const textWidth = ctx.measureText(truncated).width;
const pillW = textWidth + 12;
const pillH = 16;
roundRect(ctx, x - pillW / 2, labelY - pillH / 2, pillW, pillH, 4);
ctx.fillStyle = 'rgba(0,0,0,0.75)';
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.textBaseline = 'middle';
ctx.fillText(truncated, x, labelY);
}
} else {
ctx.beginPath();
ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(0,0,0,0.35)';
ctx.fill();
ctx.setLineDash([4, 4]);
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.setLineDash([]);
}
const posLabelY = flipY
? (players.length > 0 ? y + avatarRadius + 10 : y + avatarRadius + 8)
: (players.length > 0 ? y - avatarRadius - 10 : y - avatarRadius - 8);
ctx.font = 'bold 12px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const posTextW = ctx.measureText(posLabel).width + 10;
roundRect(ctx, x - posTextW / 2, posLabelY - 8, posTextW, 16, 3);
ctx.fillStyle = players.length > 0 ? 'rgba(88,101,242,0.9)' : 'rgba(100,100,100,0.8)';
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.fillText(posLabel, x, posLabelY);
}
// 4. Footer watermark
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);
// Convert to PNG buffer
const buffer = canvas.toBuffer('image/png');
return buffer;
}
/**
* Create a Discord AttachmentBuilder from the rendered lineup
*/
async function createLineupAttachment(lineup, title, formation, client) {
const buffer = await renderLineupImage(lineup, title, formation, client);
return new AttachmentBuilder(buffer, { name: 'lineup.png' });
}
module.exports = { renderLineupImage, createLineupAttachment }; |