LineUp-Bot / tacticRenderer.js
Brozy123's picture
Update tacticRenderer.js
5536ff4 verified
Raw History Blame Contribute Delete
14.2 kB
/**
* tacticRenderer.js - Tactic Board Image Generator
* Renders tactical formations with numbered positions and player info
*/
const { createCanvas, loadImage } = require('@napi-rs/canvas');
const { AttachmentBuilder } = require('discord.js');
const https = require('https');
const http = require('http');
// Tactic data for each 7v7 formation
const TACTICS = {
"default": {
name: "4-2-1 Formation",
summary: "Balanced formation with strong wing play and solid defense",
attack: "• Wingers stay wide to stretch defense\n• CM pushes forward to support striker\n• Fullbacks provide width in attack",
defense: "• Four defenders maintain compact shape\n• CM drops deep to protect back line\n• Press high when opportunity arises",
keyPoints: ["• Use width with RW/LW", "• CM timed runs into box", "• Fullbacks stay disciplined"]
},
"3-1-2": {
name: "3-1-2 Formation",
summary: "Compact midfield with dual strikers creating goal threats",
attack: "• Wingers cut inside to create overloads\n• CM acts as primary playmaker\n• Two strikers rotate positions constantly",
defense: "• Three CBs provide excellent cover\n• CM shields the back three\n• Force opponents wide",
keyPoints: ["• CBs play tight triangle", "• CM controls tempo", "• Strikers interchange positions"]
},
"3-0-3": {
name: "3-0-3 Formation",
summary: "Attacking formation with three forwards and no defensive mid",
attack: "• Three forwards press high constantly\n• Quick transitions through wings\n• Overload opponent's defensive flanks",
defense: "• Three CBs must stay disciplined\n• Wingers track back to help defense\n• High defensive line with offside trap",
keyPoints: ["• Very attacking mindset", "• Risk-reward strategy", "• Requires fit forwards"]
},
"3-2-1": {
name: "3-2-1 Formation",
summary: "Balanced with strong midfield duo supporting solo striker",
attack: "• Two CMs control possession\n• ST holds up play for midfield runners\n• Wing play supported by overlapping fullbacks",
defense: "• Three CBs with midfield screen\n• CMs drop to form 5-man defense\n• Compact central shape",
keyPoints: ["• Solid midfield control", "• ST as focal point", "• Flexible formation"]
},
"2-3-1": {
name: "2-3-1 Formation",
summary: "Midfield-heavy formation with diamond shape",
attack: "• Three CMs dominate possession\n• ST supported by attacking CM\n• Quick passing through center",
defense: "• Two CBs with midfield protection\n• CMs track back to form block\n• Pressure opponents in midfield",
keyPoints: ["• Midfield dominance", "• Short passing game", "• Compact structure"]
},
"2-2-2": {
name: "2-2-2 Formation",
summary: "Balanced formation with two banks of two",
attack: "• Two strikers press defenders\n• CMs support attacks with late runs\n• Width from fullbacks",
defense: "• Two CBs with two CM shield\n• Compact shape hard to break down\n• Counter-attack quickly",
keyPoints: ["• Very balanced approach", "• Hard to break down", "• Good for counter-attacks"]
},
"4-1-1": {
name: "4-1-1 Formation",
summary: "Defensive solid with CAM supporting striker",
attack: "• CAM creates chances for ST\n• Wingers provide crosses\n• Patient build-up play",
defense: "• Four defenders with CDM screen\n• Very hard to score against\n• Low block with quick transitions",
keyPoints: ["• Defensive stability", "• CAM key playmaker", "• Low block strategy"]
},
"3-3-0": {
name: "3-3-0 Formation",
summary: "No striker formation with three attacking midfielders",
attack: "• Three CAMs rotate positions\n• False nine approach\n• Fluid attacking movement",
defense: "• Three CBs with midfield triangle\n• Press high as unit\n• Win ball back quickly",
keyPoints: ["• False nine system", "• Fluid movement", "• High pressing"]
},
"2-1-3": {
name: "2-1-3 Formation",
summary: "Ultra-attacking with three forwards",
attack: "• Three forwards create chaos\n• Single CM distributes play\n• All-out attack mentality",
defense: "• Two CBs with limited protection\n• High risk defensive approach\n• Score more than you concede",
keyPoints: ["• Very attacking", "• High risk-high reward", "• Needs fit forwards"]
},
"1-4-1": {
name: "1-4-1 Formation",
summary: "Unconventional with single CB and packed midfield",
attack: "• Four midfielders control game\n• ST leads the line alone\n• Possession-based football",
defense: "• One CB with midfield cover\n• Packed central area\n• Frustrate opponents",
keyPoints: ["• Unconventional approach", "• Midfield overload", "• Possession game"]
},
"4-2-0": {
name: "4-2-0 Formation",
summary: "Defensive with no strikers, focus on clean sheets",
attack: "• CMs make late runs into box\n• Wingers cut inside\n• Goal from midfield",
defense: "• Four CBs with two shields\n• Extremely hard to break down\n• Frustrate opponents completely",
keyPoints: ["• Ultra-defensive", "• Clean sheet focus", "• Goals from midfield"]
},
"3-1-3": {
name: "3-1-3 Formation",
summary: "Attacking with three forwards and playmaker",
attack: "• Three forwards press high\n• CAM creates all chances\n• Quick combination play",
defense: "• Three CBs with CDM cover\n• CAM tracks back when needed\n• Compact when out of possession",
keyPoints: ["• Attack-minded", "• CAM key creator", "• High press"]
},
"5-0-1": {
name: "5-0-1 Formation",
summary: "Ultra-defensive with five at the back",
attack: "• Lone ST holds up play\n• Wingbacks provide width in attack\n• Direct balls to striker",
defense: "• Five defenders impossible to break\n• Very low block\n• Defensive stability priority",
keyPoints: ["• Ultra-defensive", "• Five at back", "• Direct attacking"]
},
"3-1-1-1": {
name: "3-1-1-1 Formation",
summary: "Christmas tree formation with three lines",
attack: "• CAM links midfield to attack\n• ST leads line with support\n• Patient build-up",
defense: "• Three CBs with double screen\n• CDM and CAM track back\n• Structured defensive shape",
keyPoints: ["• Christmas tree shape", "• Structured approach", "• Patient buildup"]
},
"2-2-1-1": {
name: "2-2-1-1 Formation",
summary: "Midfield diamond with two strikers",
attack: "• CAM feeds two strikers\n• Two CMs control tempo\n• Creative attacking play",
defense: "• Two CBs with diamond protection\n• Compact center\n• Press as unit",
keyPoints: ["• Diamond midfield", "• Two strikers", "• Creative play"]
},
"4-1-2": {
name: "4-1-2 Formation",
summary: "Defensive with two attacking midfielders",
attack: "• Two CAMs support ST\n• Wingers stay wide\n• Controlled attacking play",
defense: "• Four defenders with CDM shield\n• Very organized defense\n• Hard to break down centrally",
keyPoints: ["• Organized defense", "• Two creators", "• Controlled attack"]
},
"2-3-2": {
name: "2-3-2 Formation",
summary: "Midfield control with two strikers",
attack: "• Three CMs dominate center\n• Two strikers press high\n• Quick passing combinations",
defense: "• Two CBs with midfield cover\n• Press high together\n• Win ball back quickly",
keyPoints: ["• Midfield dominance", "• High press", "• Quick combinations"]
}
};
// Position coordinates on pitch (x, y as percentage)
const POSITION_COORDS = {
'GK': [50, 90],
'CB': [50, 72], 'CB1': [38, 72], 'CB2': [62, 72],
'LB': [15, 65], 'RB': [85, 65],
'LCB': [35, 72], 'RCB': [65, 72],
'CM': [50, 50], 'CM1': [35, 50], 'CM2': [65, 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],
'LW': [18, 30], 'RW': [82, 30],
'LF': [30, 30], 'RF': [70, 30],
'ST': [50, 20], 'ST1': [38, 20], 'ST2': [62, 20],
'CF': [50, 32]
};
function downloadImage(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
client.get(url, { timeout: 5000 }, (res) => {
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);
});
}
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, 80, 0, Math.PI * 2);
ctx.stroke();
// Center dot
ctx.beginPath();
ctx.arc(W / 2, H / 2, 4, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255,255,255,0.85)';
ctx.fill();
// Penalty areas
ctx.strokeRect(W / 2 - 90, pad, 180, 120);
ctx.strokeRect(W / 2 - 90, H - pad - 120, 180, 120);
// Goal areas
ctx.strokeRect(W / 2 - 40, pad, 80, 50);
ctx.strokeRect(W / 2 - 40, H - pad - 50, 80, 50);
// Penalty dots
ctx.beginPath();
ctx.arc(W / 2, pad + 80, 3, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(W / 2, H - pad - 80, 3, 0, Math.PI * 2);
ctx.fill();
}
function drawPosition(ctx, x, y, number, label, hasPlayer, avatarBuffer) {
const radius = 25;
const canvasX = (x / 100) * 900;
const canvasY = (y / 100) * 600;
// Draw circle background
ctx.beginPath();
ctx.arc(canvasX, canvasY, radius, 0, Math.PI * 2);
ctx.fillStyle = hasPlayer ? 'rgba(46, 204, 113, 0.9)' : 'rgba(149, 165, 166, 0.7)';
ctx.fill();
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.stroke();
// Draw number
ctx.fillStyle = '#fff';
ctx.font = 'bold 16px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(number.toString(), canvasX, canvasY);
// Draw label below
ctx.font = '11px Arial';
ctx.fillStyle = '#fff';
ctx.fillText(label, canvasX, canvasY + radius + 15);
// Draw avatar if available
if (hasPlayer && avatarBuffer) {
try {
const img = new (require('@napi-rs/canvas').Image)();
img.src = avatarBuffer;
ctx.save();
ctx.beginPath();
ctx.arc(canvasX, canvasY, radius - 2, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(img, canvasX - radius + 2, canvasY - radius + 2, (radius - 2) * 2, (radius - 2) * 2);
ctx.restore();
} catch (e) {
// Ignore avatar errors
}
}
}
async function createTacticAttachment(formationName, formation, lineup, tacticData, client) {
const W = 900, H = 600;
const canvas = createCanvas(W, H);
const ctx = canvas.getContext('2d');
// Draw pitch
drawPitch(ctx, W, H);
// Create position to number mapping (1-7)
const positionNumbers = {};
formation.forEach(([posKey, posLabel], index) => {
positionNumbers[posKey] = index + 1;
});
// Draw each position
for (const [posKey, posLabel] of formation) {
const coords = POSITION_COORDS[posKey];
if (!coords) continue;
const number = positionNumbers[posKey];
const players = lineup.get(posKey) || [];
const hasPlayer = players.length > 0;
let avatarBuffer = null;
// Try to load player avatar
if (hasPlayer && client) {
try {
const userId = players[0];
const user = await client.users.fetch(userId);
if (user.avatarURL()) {
avatarBuffer = await downloadImage(user.avatarURL({ format: 'png', size: 64 }));
}
} catch (e) {
// Ignore errors
}
}
drawPosition(ctx, coords[0], coords[1], number, posLabel, hasPlayer, avatarBuffer);
}
// Add tactic info overlay at bottom
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, H - 80, W, 80);
ctx.fillStyle = '#fff';
ctx.font = 'bold 18px Arial';
ctx.textAlign = 'left';
ctx.fillText(`TACTIC: ${tacticData.name}`, 20, H - 50);
ctx.font = '12px Arial';
const summaryText = tacticData.summary.length > 80 ? tacticData.summary.substring(0, 80) + '...' : tacticData.summary;
ctx.fillText(summaryText, 20, H - 25);
return new AttachmentBuilder(canvas.toBuffer('image/png'), { name: 'tactic.png' });
}
module.exports = { TACTICS, createTacticAttachment };