Spaces:
Runtime error
Runtime error
Update lineupRenderer.js
Browse files- lineupRenderer.js +38 -30
lineupRenderer.js
CHANGED
|
@@ -153,10 +153,28 @@ function drawPitch(ctx, W, H) {
|
|
| 153 |
* @returns {Promise<Buffer>} PNG image buffer
|
| 154 |
*/
|
| 155 |
async function renderLineupImage(lineup, formKey, formation, client) {
|
| 156 |
-
|
| 157 |
-
const
|
| 158 |
-
const
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
const canvas = createCanvas(W, H);
|
| 161 |
const ctx = canvas.getContext('2d');
|
| 162 |
|
|
@@ -213,33 +231,26 @@ async function renderLineupImage(lineup, formKey, formation, client) {
|
|
| 213 |
// Draw subs list
|
| 214 |
let subY = 70;
|
| 215 |
for (const sub of subs) {
|
| 216 |
-
if (subY > H - 50) break;
|
| 217 |
|
| 218 |
-
// Avatar
|
| 219 |
const avatarSize = 32;
|
| 220 |
const avatarRadius = avatarSize / 2;
|
| 221 |
const x = 20;
|
| 222 |
const y = subY;
|
| 223 |
|
|
|
|
| 224 |
ctx.save();
|
| 225 |
-
if (
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
ctx.drawImage(img, x, y, avatarSize, avatarSize);
|
| 237 |
-
} catch (e) {
|
| 238 |
-
ctx.beginPath();
|
| 239 |
-
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
|
| 240 |
-
ctx.fillStyle = '#5865F2';
|
| 241 |
-
ctx.fill();
|
| 242 |
-
}
|
| 243 |
}
|
| 244 |
ctx.restore();
|
| 245 |
|
|
@@ -252,11 +263,8 @@ async function renderLineupImage(lineup, formKey, formation, client) {
|
|
| 252 |
|
| 253 |
// Username
|
| 254 |
let displayName = "Player";
|
| 255 |
-
if (
|
| 256 |
-
|
| 257 |
-
const user = await client.users.fetch(sub.userId);
|
| 258 |
-
displayName = user.globalName || user.username;
|
| 259 |
-
} catch(e) {}
|
| 260 |
}
|
| 261 |
|
| 262 |
ctx.fillStyle = '#ffffff';
|
|
@@ -280,7 +288,7 @@ async function renderLineupImage(lineup, formKey, formation, client) {
|
|
| 280 |
ctx.textAlign = 'center';
|
| 281 |
ctx.fillText(sub.posLabel, pillX + posTextW / 2, y + avatarRadius);
|
| 282 |
|
| 283 |
-
subY += 45;
|
| 284 |
}
|
| 285 |
if (subs.length === 0) {
|
| 286 |
ctx.fillStyle = 'rgba(255,255,255,0.5)';
|
|
|
|
| 153 |
* @returns {Promise<Buffer>} PNG image buffer
|
| 154 |
*/
|
| 155 |
async function renderLineupImage(lineup, formKey, formation, client) {
|
| 156 |
+
// 1. Pre-fetch all user data and avatars in parallel to speed up rendering
|
| 157 |
+
const allUserIds = new Set();
|
| 158 |
+
for (const players of lineup.values()) {
|
| 159 |
+
players.forEach(id => allUserIds.add(id));
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
const userDataMap = new Map();
|
| 163 |
+
if (client) {
|
| 164 |
+
await Promise.all(Array.from(allUserIds).map(async (uid) => {
|
| 165 |
+
try {
|
| 166 |
+
const user = await client.users.fetch(uid);
|
| 167 |
+
const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 128 });
|
| 168 |
+
const imgBuf = await downloadImage(avatarUrl);
|
| 169 |
+
const img = await loadImage(imgBuf);
|
| 170 |
+
userDataMap.set(uid, { user, img });
|
| 171 |
+
} catch (e) {
|
| 172 |
+
userDataMap.set(uid, { user: null, img: null });
|
| 173 |
+
}
|
| 174 |
+
}));
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// --- Draw the content ---
|
| 178 |
const canvas = createCanvas(W, H);
|
| 179 |
const ctx = canvas.getContext('2d');
|
| 180 |
|
|
|
|
| 231 |
// Draw subs list
|
| 232 |
let subY = 70;
|
| 233 |
for (const sub of subs) {
|
| 234 |
+
if (subY > H - 50) break;
|
| 235 |
|
|
|
|
| 236 |
const avatarSize = 32;
|
| 237 |
const avatarRadius = avatarSize / 2;
|
| 238 |
const x = 20;
|
| 239 |
const y = subY;
|
| 240 |
|
| 241 |
+
const data = userDataMap.get(sub.userId);
|
| 242 |
ctx.save();
|
| 243 |
+
if (data && data.img) {
|
| 244 |
+
ctx.beginPath();
|
| 245 |
+
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
|
| 246 |
+
ctx.closePath();
|
| 247 |
+
ctx.clip();
|
| 248 |
+
ctx.drawImage(data.img, x, y, avatarSize, avatarSize);
|
| 249 |
+
} else {
|
| 250 |
+
ctx.beginPath();
|
| 251 |
+
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
|
| 252 |
+
ctx.fillStyle = '#5865F2';
|
| 253 |
+
ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
}
|
| 255 |
ctx.restore();
|
| 256 |
|
|
|
|
| 263 |
|
| 264 |
// Username
|
| 265 |
let displayName = "Player";
|
| 266 |
+
if (data && data.user) {
|
| 267 |
+
displayName = data.user.globalName || data.user.username;
|
|
|
|
|
|
|
|
|
|
| 268 |
}
|
| 269 |
|
| 270 |
ctx.fillStyle = '#ffffff';
|
|
|
|
| 288 |
ctx.textAlign = 'center';
|
| 289 |
ctx.fillText(sub.posLabel, pillX + posTextW / 2, y + avatarRadius);
|
| 290 |
|
| 291 |
+
subY += 45;
|
| 292 |
}
|
| 293 |
if (subs.length === 0) {
|
| 294 |
ctx.fillStyle = 'rgba(255,255,255,0.5)';
|