Brozy123 commited on
Commit
61b8779
·
verified ·
1 Parent(s): 39bf661

Update lineupRenderer.js

Browse files
Files changed (1) hide show
  1. lineupRenderer.js +29 -29
lineupRenderer.js CHANGED
@@ -289,7 +289,28 @@ async function renderLineupImage(lineup, formKey, formation, client) {
289
  ctx.fillText('No substitutes yet', SIDEBAR_W / 2, subY + 20);
290
  }
291
 
292
- // 4. Draw players at their positions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  const avatarSize = 48;
294
  const avatarRadius = avatarSize / 2;
295
 
@@ -297,14 +318,11 @@ async function renderLineupImage(lineup, formKey, formation, client) {
297
  const coords = POSITION_COORDS[posKey];
298
  if (!coords) continue;
299
 
300
- // Shift X by SIDEBAR_W
301
  const x = SIDEBAR_W + (coords[0] / 100) * PITCH_W;
302
  const y = (coords[1] / 100) * H;
303
  const players = lineup.get(posKey) || [];
304
 
305
- // Draw position marker (glow ring)
306
  if (players.length > 0) {
307
- // Filled slot glow
308
  ctx.save();
309
  ctx.shadowColor = '#00ff88';
310
  ctx.shadowBlur = 18;
@@ -315,33 +333,24 @@ async function renderLineupImage(lineup, formKey, formation, client) {
315
  ctx.restore();
316
  }
317
 
318
- // Draw avatar or empty slot
319
  if (players.length > 0 && client) {
320
  const userId = players[0];
321
- try {
322
- const user = await client.users.fetch(userId);
323
- const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 128 });
324
- const imgBuf = await downloadImage(avatarUrl);
325
- const img = await loadImage(imgBuf);
326
 
327
- // Clip circle for avatar
328
  ctx.save();
329
  ctx.beginPath();
330
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
331
- ctx.closePath();
332
  ctx.clip();
333
- ctx.drawImage(img, x - avatarRadius, y - avatarRadius, avatarSize, avatarSize);
334
  ctx.restore();
335
 
336
- // Border ring
337
  ctx.beginPath();
338
  ctx.arc(x, y, avatarRadius + 2, 0, Math.PI * 2);
339
  ctx.strokeStyle = '#ffffff';
340
  ctx.lineWidth = 2.5;
341
  ctx.stroke();
342
-
343
- } catch (e) {
344
- // Fallback: draw a filled circle with initials
345
  ctx.beginPath();
346
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
347
  ctx.fillStyle = '#5865F2';
@@ -349,7 +358,6 @@ async function renderLineupImage(lineup, formKey, formation, client) {
349
  ctx.strokeStyle = '#ffffff';
350
  ctx.lineWidth = 2;
351
  ctx.stroke();
352
-
353
  ctx.fillStyle = '#ffffff';
354
  ctx.font = 'bold 16px Arial, sans-serif';
355
  ctx.textAlign = 'center';
@@ -357,13 +365,9 @@ async function renderLineupImage(lineup, formKey, formation, client) {
357
  ctx.fillText('?', x, y);
358
  }
359
 
360
- // Username label below avatar
361
- try {
362
- const user = await client.users.fetch(players[0]);
363
- const displayName = user.globalName || user.username;
364
  const truncated = displayName.length > 10 ? displayName.slice(0, 9) + '…' : displayName;
365
-
366
- // Label background pill
367
  const labelY = y + avatarRadius + 14;
368
  ctx.font = 'bold 11px Arial, sans-serif';
369
  ctx.textAlign = 'center';
@@ -378,10 +382,8 @@ async function renderLineupImage(lineup, formKey, formation, client) {
378
  ctx.fillStyle = '#ffffff';
379
  ctx.textBaseline = 'middle';
380
  ctx.fillText(truncated, x, labelY);
381
- } catch (e) {}
382
-
383
  } else {
384
- // Empty slot: dashed circle
385
  ctx.beginPath();
386
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
387
  ctx.fillStyle = 'rgba(0,0,0,0.35)';
@@ -393,13 +395,11 @@ async function renderLineupImage(lineup, formKey, formation, client) {
393
  ctx.setLineDash([]);
394
  }
395
 
396
- // Position label (always shown)
397
  const posLabelY = players.length > 0 ? y - avatarRadius - 10 : y - avatarRadius - 8;
398
  ctx.font = 'bold 12px Arial, sans-serif';
399
  ctx.textAlign = 'center';
400
  ctx.textBaseline = 'middle';
401
 
402
- // Position tag background
403
  const posTextW = ctx.measureText(posLabel).width + 10;
404
  roundRect(ctx, x - posTextW / 2, posLabelY - 8, posTextW, 16, 3);
405
  ctx.fillStyle = players.length > 0 ? 'rgba(88,101,242,0.9)' : 'rgba(100,100,100,0.8)';
 
289
  ctx.fillText('No substitutes yet', SIDEBAR_W / 2, subY + 20);
290
  }
291
 
292
+ // 4. Pre-fetch all user data and avatars in parallel to speed up rendering
293
+ const allUserIds = new Set();
294
+ for (const players of lineup.values()) {
295
+ players.forEach(id => allUserIds.add(id));
296
+ }
297
+
298
+ const userDataMap = new Map();
299
+ if (client) {
300
+ await Promise.all(Array.from(allUserIds).map(async (uid) => {
301
+ try {
302
+ const user = await client.users.fetch(uid);
303
+ const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 128 });
304
+ const imgBuf = await downloadImage(avatarUrl);
305
+ const img = await loadImage(imgBuf);
306
+ userDataMap.set(uid, { user, img });
307
+ } catch (e) {
308
+ userDataMap.set(uid, { user: null, img: null });
309
+ }
310
+ }));
311
+ }
312
+
313
+ // --- Start Drawing Players ---
314
  const avatarSize = 48;
315
  const avatarRadius = avatarSize / 2;
316
 
 
318
  const coords = POSITION_COORDS[posKey];
319
  if (!coords) continue;
320
 
 
321
  const x = SIDEBAR_W + (coords[0] / 100) * PITCH_W;
322
  const y = (coords[1] / 100) * H;
323
  const players = lineup.get(posKey) || [];
324
 
 
325
  if (players.length > 0) {
 
326
  ctx.save();
327
  ctx.shadowColor = '#00ff88';
328
  ctx.shadowBlur = 18;
 
333
  ctx.restore();
334
  }
335
 
 
336
  if (players.length > 0 && client) {
337
  const userId = players[0];
338
+ const data = userDataMap.get(userId);
 
 
 
 
339
 
340
+ if (data && data.img) {
341
  ctx.save();
342
  ctx.beginPath();
343
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
 
344
  ctx.clip();
345
+ ctx.drawImage(data.img, x - avatarRadius, y - avatarRadius, avatarSize, avatarSize);
346
  ctx.restore();
347
 
 
348
  ctx.beginPath();
349
  ctx.arc(x, y, avatarRadius + 2, 0, Math.PI * 2);
350
  ctx.strokeStyle = '#ffffff';
351
  ctx.lineWidth = 2.5;
352
  ctx.stroke();
353
+ } else {
 
 
354
  ctx.beginPath();
355
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
356
  ctx.fillStyle = '#5865F2';
 
358
  ctx.strokeStyle = '#ffffff';
359
  ctx.lineWidth = 2;
360
  ctx.stroke();
 
361
  ctx.fillStyle = '#ffffff';
362
  ctx.font = 'bold 16px Arial, sans-serif';
363
  ctx.textAlign = 'center';
 
365
  ctx.fillText('?', x, y);
366
  }
367
 
368
+ if (data && data.user) {
369
+ const displayName = data.user.globalName || data.user.username;
 
 
370
  const truncated = displayName.length > 10 ? displayName.slice(0, 9) + '…' : displayName;
 
 
371
  const labelY = y + avatarRadius + 14;
372
  ctx.font = 'bold 11px Arial, sans-serif';
373
  ctx.textAlign = 'center';
 
382
  ctx.fillStyle = '#ffffff';
383
  ctx.textBaseline = 'middle';
384
  ctx.fillText(truncated, x, labelY);
385
+ }
 
386
  } else {
 
387
  ctx.beginPath();
388
  ctx.arc(x, y, avatarRadius, 0, Math.PI * 2);
389
  ctx.fillStyle = 'rgba(0,0,0,0.35)';
 
395
  ctx.setLineDash([]);
396
  }
397
 
 
398
  const posLabelY = players.length > 0 ? y - avatarRadius - 10 : y - avatarRadius - 8;
399
  ctx.font = 'bold 12px Arial, sans-serif';
400
  ctx.textAlign = 'center';
401
  ctx.textBaseline = 'middle';
402
 
 
403
  const posTextW = ctx.measureText(posLabel).width + 10;
404
  roundRect(ctx, x - posTextW / 2, posLabelY - 8, posTextW, 16, 3);
405
  ctx.fillStyle = players.length > 0 ? 'rgba(88,101,242,0.9)' : 'rgba(100,100,100,0.8)';