Brozy123 commited on
Commit
506eba9
·
verified ·
1 Parent(s): 356fcf2

Create lineupRenderer.js

Browse files
Files changed (1) hide show
  1. lineupRenderer.js +318 -0
lineupRenderer.js ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * lineupRenderer.js - Football Pitch Lineup Image Generator
3
+ * Uses @napi-rs/canvas to render a professional football pitch
4
+ * with player Discord avatars and position labels.
5
+ */
6
+ const { createCanvas, loadImage } = require('@napi-rs/canvas');
7
+ const { AttachmentBuilder } = require('discord.js');
8
+ const https = require('https');
9
+ const http = require('http');
10
+
11
+ // Position coordinates on a 900x600 pitch for each formation
12
+ // Coordinates are [x, y] as percentage of canvas width/height
13
+ const POSITION_COORDS = {
14
+ // Goalkeeper
15
+ 'GK': [50, 90],
16
+ // Defenders
17
+ 'CB': [50, 72], 'CB1': [38, 72], 'CB2': [62, 72],
18
+ 'LCB': [35, 72], 'RCB': [65, 72],
19
+ 'LB': [15, 65], 'RB': [85, 65],
20
+ // Midfielders
21
+ 'CM': [50, 50], 'CM1': [35, 50], 'CM2': [65, 50],
22
+ 'LCM': [30, 50], 'RCM': [70, 50],
23
+ 'CDM': [50, 58], 'CAM': [50, 38],
24
+ 'LM': [15, 50], 'RM': [85, 50],
25
+ // Forwards
26
+ 'LW': [18, 30], 'RW': [82, 30],
27
+ 'ST': [50, 20], 'ST1': [38, 20], 'ST2': [62, 20],
28
+ };
29
+
30
+ /**
31
+ * Download an image from a URL and return as a Buffer
32
+ */
33
+ function downloadImage(url) {
34
+ return new Promise((resolve, reject) => {
35
+ const client = url.startsWith('https') ? https : http;
36
+ client.get(url, { timeout: 5000 }, (res) => {
37
+ // Follow redirects
38
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
39
+ return downloadImage(res.headers.location).then(resolve).catch(reject);
40
+ }
41
+ const chunks = [];
42
+ res.on('data', (chunk) => chunks.push(chunk));
43
+ res.on('end', () => resolve(Buffer.concat(chunks)));
44
+ res.on('error', reject);
45
+ }).on('error', reject);
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Draw a rounded rectangle
51
+ */
52
+ function roundRect(ctx, x, y, w, h, r) {
53
+ ctx.beginPath();
54
+ ctx.moveTo(x + r, y);
55
+ ctx.lineTo(x + w - r, y);
56
+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);
57
+ ctx.lineTo(x + w, y + h - r);
58
+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
59
+ ctx.lineTo(x + r, y + h);
60
+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);
61
+ ctx.lineTo(x, y + r);
62
+ ctx.quadraticCurveTo(x, y, x + r, y);
63
+ ctx.closePath();
64
+ }
65
+
66
+ /**
67
+ * Draw the football pitch background
68
+ */
69
+ function drawPitch(ctx, W, H) {
70
+ // Main grass gradient
71
+ const grassGrad = ctx.createLinearGradient(0, 0, 0, H);
72
+ grassGrad.addColorStop(0, '#1a7a3a');
73
+ grassGrad.addColorStop(0.5, '#1e8c42');
74
+ grassGrad.addColorStop(1, '#1a7a3a');
75
+ ctx.fillStyle = grassGrad;
76
+ ctx.fillRect(0, 0, W, H);
77
+
78
+ // Grass stripe pattern
79
+ const stripeCount = 12;
80
+ const stripeH = H / stripeCount;
81
+ for (let i = 0; i < stripeCount; i++) {
82
+ if (i % 2 === 0) {
83
+ ctx.fillStyle = 'rgba(255,255,255,0.04)';
84
+ ctx.fillRect(0, i * stripeH, W, stripeH);
85
+ }
86
+ }
87
+
88
+ // Pitch outline
89
+ const pad = 30;
90
+ ctx.strokeStyle = 'rgba(255,255,255,0.85)';
91
+ ctx.lineWidth = 2.5;
92
+ ctx.strokeRect(pad, pad, W - pad * 2, H - pad * 2);
93
+
94
+ // Center line
95
+ ctx.beginPath();
96
+ ctx.moveTo(pad, H / 2);
97
+ ctx.lineTo(W - pad, H / 2);
98
+ ctx.stroke();
99
+
100
+ // Center circle
101
+ ctx.beginPath();
102
+ ctx.arc(W / 2, H / 2, 60, 0, Math.PI * 2);
103
+ ctx.stroke();
104
+
105
+ // Center dot
106
+ ctx.fillStyle = 'rgba(255,255,255,0.85)';
107
+ ctx.beginPath();
108
+ ctx.arc(W / 2, H / 2, 4, 0, Math.PI * 2);
109
+ ctx.fill();
110
+
111
+ // Top penalty box (attacking end)
112
+ const boxW = 220;
113
+ const boxH = 80;
114
+ ctx.strokeRect((W - boxW) / 2, pad, boxW, boxH);
115
+
116
+ // Top small box
117
+ const sBoxW = 120;
118
+ const sBoxH = 35;
119
+ ctx.strokeRect((W - sBoxW) / 2, pad, sBoxW, sBoxH);
120
+
121
+ // Top penalty arc
122
+ ctx.beginPath();
123
+ ctx.arc(W / 2, pad + boxH, 35, 0, Math.PI);
124
+ ctx.stroke();
125
+
126
+ // Bottom penalty box (GK end)
127
+ ctx.strokeRect((W - boxW) / 2, H - pad - boxH, boxW, boxH);
128
+
129
+ // Bottom small box
130
+ ctx.strokeRect((W - sBoxW) / 2, H - pad - sBoxH, sBoxW, sBoxH);
131
+
132
+ // Bottom penalty arc
133
+ ctx.beginPath();
134
+ ctx.arc(W / 2, H - pad - boxH, 35, Math.PI, Math.PI * 2);
135
+ ctx.stroke();
136
+
137
+ // Corner arcs
138
+ const corners = [[pad, pad], [W - pad, pad], [pad, H - pad], [W - pad, H - pad]];
139
+ const cornerAngles = [[0, Math.PI / 2], [Math.PI / 2, Math.PI], [3 * Math.PI / 2, 2 * Math.PI], [Math.PI, 3 * Math.PI / 2]];
140
+ corners.forEach(([cx, cy], i) => {
141
+ ctx.beginPath();
142
+ ctx.arc(cx, cy, 12, cornerAngles[i][0], cornerAngles[i][1]);
143
+ ctx.stroke();
144
+ });
145
+ }
146
+
147
+ /**
148
+ * Render the lineup as a PNG image buffer
149
+ * @param {Map} lineup - Map of posKey -> [userId, ...]
150
+ * @param {string} formKey - Formation key (e.g. "3-1-2")
151
+ * @param {Array} formation - Array of [posKey, posLabel]
152
+ * @param {object} client - Discord client for fetching user data
153
+ * @returns {Promise<Buffer>} PNG image buffer
154
+ */
155
+ async function renderLineupImage(lineup, formKey, formation, client) {
156
+ const W = 900;
157
+ const H = 620;
158
+ const canvas = createCanvas(W, H);
159
+ const ctx = canvas.getContext('2d');
160
+
161
+ // 1. Draw the pitch
162
+ drawPitch(ctx, W, H);
163
+
164
+ // 2. Draw title bar
165
+ const titleGrad = ctx.createLinearGradient(0, 0, W, 0);
166
+ titleGrad.addColorStop(0, 'rgba(0,0,0,0.7)');
167
+ titleGrad.addColorStop(0.5, 'rgba(0,0,0,0.85)');
168
+ titleGrad.addColorStop(1, 'rgba(0,0,0,0.7)');
169
+ ctx.fillStyle = titleGrad;
170
+ ctx.fillRect(0, 0, W, 36);
171
+
172
+ ctx.fillStyle = '#ffffff';
173
+ ctx.font = 'bold 16px Arial, sans-serif';
174
+ ctx.textAlign = 'center';
175
+ ctx.fillText(`⚽ LINEUP — ${formKey.toUpperCase()}`, W / 2, 24);
176
+
177
+ // 3. Draw players at their positions
178
+ const avatarSize = 48;
179
+ const avatarRadius = avatarSize / 2;
180
+
181
+ for (const [posKey, posLabel] of formation) {
182
+ const coords = POSITION_COORDS[posKey];
183
+ if (!coords) continue;
184
+
185
+ const x = (coords[0] / 100) * W;
186
+ const y = (coords[1] / 100) * H;
187
+ const players = lineup.get(posKey) || [];
188
+
189
+ // Draw position marker (glow ring)
190
+ if (players.length > 0) {
191
+ // Filled slot glow
192
+ ctx.save();
193
+ ctx.shadowColor = '#00ff88';
194
+ ctx.shadowBlur = 18;
195
+ ctx.beginPath();
196
+ ctx.arc(x, y, avatarRadius + 4, 0, Math.PI * 2);
197
+ ctx.fillStyle = 'rgba(0,255,136,0.3)';
198
+ ctx.fill();
199
+ ctx.restore();
200
+ }
201
+
202
+ // Draw avatar or empty slot
203
+ if (players.length > 0 && client) {
204
+ const userId = players[0];
205
+ try {
206
+ const user = await client.users.fetch(userId);
207
+ const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 128 });
208
+ const imgBuf = await downloadImage(avatarUrl);
209
+ const img = await loadImage(imgBuf);
210
+
211
+ // Clip circle for avatar
212
+ ctx.save();
213
+ ctx.beginPath();
214
+ ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
215
+ ctx.closePath();
216
+ ctx.clip();
217
+ ctx.drawImage(img, x - avatarRadius, y - avatarRadius, avatarSize, avatarSize);
218
+ ctx.restore();
219
+
220
+ // Border ring
221
+ ctx.beginPath();
222
+ ctx.arc(x, y, avatarRadius + 2, 0, Math.PI * 2);
223
+ ctx.strokeStyle = '#ffffff';
224
+ ctx.lineWidth = 2.5;
225
+ ctx.stroke();
226
+
227
+ } catch (e) {
228
+ // Fallback: draw a filled circle with initials
229
+ ctx.beginPath();
230
+ ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
231
+ ctx.fillStyle = '#5865F2';
232
+ ctx.fill();
233
+ ctx.strokeStyle = '#ffffff';
234
+ ctx.lineWidth = 2;
235
+ ctx.stroke();
236
+
237
+ ctx.fillStyle = '#ffffff';
238
+ ctx.font = 'bold 16px Arial, sans-serif';
239
+ ctx.textAlign = 'center';
240
+ ctx.textBaseline = 'middle';
241
+ ctx.fillText('?', x, y);
242
+ }
243
+
244
+ // Username label below avatar
245
+ try {
246
+ const user = await client.users.fetch(players[0]);
247
+ const displayName = user.globalName || user.username;
248
+ const truncated = displayName.length > 10 ? displayName.slice(0, 9) + '…' : displayName;
249
+
250
+ // Label background pill
251
+ const labelY = y + avatarRadius + 14;
252
+ ctx.font = 'bold 11px Arial, sans-serif';
253
+ ctx.textAlign = 'center';
254
+ const textWidth = ctx.measureText(truncated).width;
255
+ const pillW = textWidth + 12;
256
+ const pillH = 16;
257
+
258
+ roundRect(ctx, x - pillW / 2, labelY - pillH / 2, pillW, pillH, 4);
259
+ ctx.fillStyle = 'rgba(0,0,0,0.75)';
260
+ ctx.fill();
261
+
262
+ ctx.fillStyle = '#ffffff';
263
+ ctx.textBaseline = 'middle';
264
+ ctx.fillText(truncated, x, labelY);
265
+ } catch (e) {}
266
+
267
+ } else {
268
+ // Empty slot: dashed circle
269
+ ctx.beginPath();
270
+ ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
271
+ ctx.fillStyle = 'rgba(0,0,0,0.35)';
272
+ ctx.fill();
273
+ ctx.setLineDash([4, 4]);
274
+ ctx.strokeStyle = 'rgba(255,255,255,0.5)';
275
+ ctx.lineWidth = 1.5;
276
+ ctx.stroke();
277
+ ctx.setLineDash([]);
278
+ }
279
+
280
+ // Position label (always shown)
281
+ const posLabelY = players.length > 0 ? y - avatarRadius - 10 : y - avatarRadius - 8;
282
+ ctx.font = 'bold 12px Arial, sans-serif';
283
+ ctx.textAlign = 'center';
284
+ ctx.textBaseline = 'middle';
285
+
286
+ // Position tag background
287
+ const posTextW = ctx.measureText(posLabel).width + 10;
288
+ roundRect(ctx, x - posTextW / 2, posLabelY - 8, posTextW, 16, 3);
289
+ ctx.fillStyle = players.length > 0 ? 'rgba(88,101,242,0.9)' : 'rgba(100,100,100,0.8)';
290
+ ctx.fill();
291
+
292
+ ctx.fillStyle = '#ffffff';
293
+ ctx.fillText(posLabel, x, posLabelY);
294
+ }
295
+
296
+ // 4. Footer watermark
297
+ ctx.fillStyle = 'rgba(0,0,0,0.5)';
298
+ ctx.fillRect(0, H - 22, W, 22);
299
+ ctx.fillStyle = 'rgba(255,255,255,0.6)';
300
+ ctx.font = '10px Arial, sans-serif';
301
+ ctx.textAlign = 'center';
302
+ ctx.textBaseline = 'middle';
303
+ ctx.fillText('Generated by FriendlyBot', W / 2, H - 11);
304
+
305
+ // Convert to PNG buffer
306
+ const buffer = canvas.toBuffer('image/png');
307
+ return buffer;
308
+ }
309
+
310
+ /**
311
+ * Create a Discord AttachmentBuilder from the rendered lineup
312
+ */
313
+ async function createLineupAttachment(lineup, formKey, formation, client) {
314
+ const buffer = await renderLineupImage(lineup, formKey, formation, client);
315
+ return new AttachmentBuilder(buffer, { name: 'lineup.png' });
316
+ }
317
+
318
+ module.exports = { renderLineupImage, createLineupAttachment };