Spaces:
Runtime error
Runtime error
Update lineupRenderer.js
Browse files- lineupRenderer.js +125 -9
lineupRenderer.js
CHANGED
|
@@ -153,28 +153,143 @@ function drawPitch(ctx, W, H) {
|
|
| 153 |
* @returns {Promise<Buffer>} PNG image buffer
|
| 154 |
*/
|
| 155 |
async function renderLineupImage(lineup, formKey, formation, client) {
|
| 156 |
-
const
|
|
|
|
|
|
|
| 157 |
const H = 620;
|
| 158 |
const canvas = createCanvas(W, H);
|
| 159 |
const ctx = canvas.getContext('2d');
|
| 160 |
|
| 161 |
-
// 1. Draw the
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
//
|
| 165 |
-
|
|
|
|
|
|
|
| 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,
|
| 171 |
|
| 172 |
ctx.fillStyle = '#ffffff';
|
| 173 |
ctx.font = 'bold 16px Arial, sans-serif';
|
| 174 |
ctx.textAlign = 'center';
|
| 175 |
-
ctx.fillText(`⚽ LINEUP — ${formKey.toUpperCase()}`,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
//
|
| 178 |
const avatarSize = 48;
|
| 179 |
const avatarRadius = avatarSize / 2;
|
| 180 |
|
|
@@ -182,7 +297,8 @@ async function renderLineupImage(lineup, formKey, formation, client) {
|
|
| 182 |
const coords = POSITION_COORDS[posKey];
|
| 183 |
if (!coords) continue;
|
| 184 |
|
| 185 |
-
|
|
|
|
| 186 |
const y = (coords[1] / 100) * H;
|
| 187 |
const players = lineup.get(posKey) || [];
|
| 188 |
|
|
|
|
| 153 |
* @returns {Promise<Buffer>} PNG image buffer
|
| 154 |
*/
|
| 155 |
async function renderLineupImage(lineup, formKey, formation, client) {
|
| 156 |
+
const PITCH_W = 900;
|
| 157 |
+
const SIDEBAR_W = 280;
|
| 158 |
+
const W = PITCH_W + SIDEBAR_W;
|
| 159 |
const H = 620;
|
| 160 |
const canvas = createCanvas(W, H);
|
| 161 |
const ctx = canvas.getContext('2d');
|
| 162 |
|
| 163 |
+
// 1. Draw the sidebar background
|
| 164 |
+
ctx.fillStyle = '#2B2D31'; // Discord dark gray
|
| 165 |
+
ctx.fillRect(0, 0, SIDEBAR_W, H);
|
| 166 |
+
|
| 167 |
+
// Draw sidebar header
|
| 168 |
+
ctx.fillStyle = '#ffffff';
|
| 169 |
+
ctx.font = 'bold 20px Arial, sans-serif';
|
| 170 |
+
ctx.textAlign = 'center';
|
| 171 |
+
ctx.fillText('🔄 SUBSTITUTES', SIDEBAR_W / 2, 30);
|
| 172 |
+
|
| 173 |
+
ctx.strokeStyle = '#5865F2';
|
| 174 |
+
ctx.lineWidth = 2;
|
| 175 |
+
ctx.beginPath();
|
| 176 |
+
ctx.moveTo(20, 45);
|
| 177 |
+
ctx.lineTo(SIDEBAR_W - 20, 45);
|
| 178 |
+
ctx.stroke();
|
| 179 |
+
|
| 180 |
+
// 2. Draw the pitch on the right
|
| 181 |
+
ctx.save();
|
| 182 |
+
ctx.translate(SIDEBAR_W, 0);
|
| 183 |
+
drawPitch(ctx, PITCH_W, H);
|
| 184 |
+
ctx.restore();
|
| 185 |
|
| 186 |
+
// 3. Draw title bar over the pitch
|
| 187 |
+
ctx.save();
|
| 188 |
+
ctx.translate(SIDEBAR_W, 0);
|
| 189 |
+
const titleGrad = ctx.createLinearGradient(0, 0, PITCH_W, 0);
|
| 190 |
titleGrad.addColorStop(0, 'rgba(0,0,0,0.7)');
|
| 191 |
titleGrad.addColorStop(0.5, 'rgba(0,0,0,0.85)');
|
| 192 |
titleGrad.addColorStop(1, 'rgba(0,0,0,0.7)');
|
| 193 |
ctx.fillStyle = titleGrad;
|
| 194 |
+
ctx.fillRect(0, 0, PITCH_W, 36);
|
| 195 |
|
| 196 |
ctx.fillStyle = '#ffffff';
|
| 197 |
ctx.font = 'bold 16px Arial, sans-serif';
|
| 198 |
ctx.textAlign = 'center';
|
| 199 |
+
ctx.fillText(`⚽ LINEUP — ${formKey.toUpperCase()}`, PITCH_W / 2, 24);
|
| 200 |
+
ctx.restore();
|
| 201 |
+
|
| 202 |
+
// Extract subs and draw them
|
| 203 |
+
const subs = [];
|
| 204 |
+
for (const [posKey, posLabel] of formation) {
|
| 205 |
+
const players = lineup.get(posKey) || [];
|
| 206 |
+
if (players.length > 1) {
|
| 207 |
+
for (let i = 1; i < players.length; i++) {
|
| 208 |
+
subs.push({ userId: players[i], posLabel });
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// Draw subs list
|
| 214 |
+
let subY = 70;
|
| 215 |
+
for (const sub of subs) {
|
| 216 |
+
if (subY > H - 50) break; // prevent overflowing
|
| 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 (client) {
|
| 226 |
+
try {
|
| 227 |
+
const user = await client.users.fetch(sub.userId);
|
| 228 |
+
const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 64 });
|
| 229 |
+
const imgBuf = await downloadImage(avatarUrl);
|
| 230 |
+
const img = await loadImage(imgBuf);
|
| 231 |
+
|
| 232 |
+
ctx.beginPath();
|
| 233 |
+
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius, 0, Math.PI * 2);
|
| 234 |
+
ctx.closePath();
|
| 235 |
+
ctx.clip();
|
| 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 |
+
|
| 246 |
+
// Border ring
|
| 247 |
+
ctx.beginPath();
|
| 248 |
+
ctx.arc(x + avatarRadius, y + avatarRadius, avatarRadius + 1, 0, Math.PI * 2);
|
| 249 |
+
ctx.strokeStyle = '#ffffff';
|
| 250 |
+
ctx.lineWidth = 1.5;
|
| 251 |
+
ctx.stroke();
|
| 252 |
+
|
| 253 |
+
// Username
|
| 254 |
+
let displayName = "Player";
|
| 255 |
+
if (client) {
|
| 256 |
+
try {
|
| 257 |
+
const user = await client.users.fetch(sub.userId);
|
| 258 |
+
displayName = user.globalName || user.username;
|
| 259 |
+
} catch(e) {}
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
ctx.fillStyle = '#ffffff';
|
| 263 |
+
ctx.font = 'bold 14px Arial, sans-serif';
|
| 264 |
+
ctx.textAlign = 'left';
|
| 265 |
+
ctx.textBaseline = 'middle';
|
| 266 |
+
|
| 267 |
+
const truncated = displayName.length > 13 ? displayName.slice(0, 12) + '…' : displayName;
|
| 268 |
+
ctx.fillText(truncated, x + avatarSize + 12, y + avatarRadius);
|
| 269 |
+
|
| 270 |
+
// Position pill
|
| 271 |
+
ctx.font = 'bold 11px Arial, sans-serif';
|
| 272 |
+
const posTextW = ctx.measureText(sub.posLabel).width + 10;
|
| 273 |
+
const pillX = SIDEBAR_W - 15 - posTextW;
|
| 274 |
+
|
| 275 |
+
roundRect(ctx, pillX, y + avatarRadius - 8, posTextW, 16, 4);
|
| 276 |
+
ctx.fillStyle = 'rgba(88,101,242,0.9)';
|
| 277 |
+
ctx.fill();
|
| 278 |
+
|
| 279 |
+
ctx.fillStyle = '#ffffff';
|
| 280 |
+
ctx.textAlign = 'center';
|
| 281 |
+
ctx.fillText(sub.posLabel, pillX + posTextW / 2, y + avatarRadius);
|
| 282 |
+
|
| 283 |
+
subY += 45; // Space for next sub
|
| 284 |
+
}
|
| 285 |
+
if (subs.length === 0) {
|
| 286 |
+
ctx.fillStyle = 'rgba(255,255,255,0.5)';
|
| 287 |
+
ctx.font = 'italic 14px Arial, sans-serif';
|
| 288 |
+
ctx.textAlign = 'center';
|
| 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 |
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 |
|