Spaces:
Runtime error
Runtime error
File size: 14,175 Bytes
5536ff4 | 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 | /**
* 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 }; |